jh-admin/addon/weapp/shop/controller/Weapp.php

364 lines
13 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
*/
namespace addon\weapp\shop\controller;
use addon\weapp\model\Config as ConfigModel;
use addon\weapp\model\MiniProgramCi;
use app\model\share\WeappShareBase as ShareModel;
use app\model\upload\Upload;
use app\shop\controller\BaseShop;
use addon\weapp\model\Weapp as WeappModel;
use think\facade\Cache;
use think\facade\Filesystem;
/**
* 微信小程序功能设置
*/
class Weapp extends BaseShop
{
protected $replace = []; //视图输出字符串内容替换 相当于配置文件中的'view_replace_str'
public function __construct()
{
date_default_timezone_set("Asia/chongqing");
parent::__construct();
$this->replace = [
'WEAPP_CSS' => __ROOT__ . '/addon/weapp/shop/view/public/css',
'WEAPP_JS' => __ROOT__ . '/addon/weapp/shop/view/public/js',
'WEAPP_IMG' => __ROOT__ . '/addon/weapp/shop/view/public/img',
'WEAPP_SVG' => __ROOT__ . '/addon/weapp/shop/view/public/svg',
];
}
/**
* 功能设置
*/
public function setting()
{
$config_model = new ConfigModel();
$config_result = $config_model->getWeappConfig($this->site_id);
$config = $config_result['data']["value"];
$is_authopen = 0;
if (addon_is_exit('wxoplatform')) {
$is_authopen = $config['is_authopen'] ?? 0;
}
$this->assign('is_authopen', $is_authopen);
$is_new_version = 0;
// 获取站点小程序版本信息
$version_info = $config_model->getWeappVersion($this->site_id);
$version_info = $version_info['data']['value'];
$currrent_version_info = config('info');
if (!isset($version_info['version']) || (isset($version_info['version']) && $version_info['version'] != $currrent_version_info['version_no']) ) {
$is_new_version = 1;
}
$this->assign('is_new_version', $is_new_version);
$weapp_menu = event('WeappMenu', ['site_id'=>$this->site_id]);
$this->assign('weapp_menu', $weapp_menu);
return $this->fetch('weapp/setting', [], $this->replace);
}
/**
* 中间页
*/
public function configSetting(){
$config_model = new ConfigModel();
$config_result = $config_model->getWeappConfig($this->site_id);
$config = $config_result["data"];
if(!empty($config["value"])){
//是否是开放平台授权
$is_authopen = $config_result['data']['value']['is_authopen'] ?? 0;
if($is_authopen > 0 && addon_is_exit('wxoplatform')){
$this->redirect(addon_url("wxoplatform://shop/oplatform/weapp"));
}else{
$this->redirect(addon_url("weapp://shop/weapp/config"));
}
}else{
if (addon_is_exit('wxoplatform')) {
$this->redirect(addon_url("wxoplatform://shop/oplatform/weappsettled"));
} else {
$this->redirect(addon_url("weapp://shop/weapp/config"));
}
}
}
/**
* 公众号配置
*/
public function config()
{
$weapp_model = new ConfigModel();
if (request()->isAjax()) {
$weapp_name = input('weapp_name', '');
$weapp_original = input('weapp_original', '');
$appid = input('appid', '');
$appsecret = input('appsecret', '');
$token = input('token', 'TOKEN');
$encodingaeskey = input('encodingaeskey', '');
$is_use = input('is_use', 0);
$qrcode = input('qrcode', '');
$data = array (
"appid" => $appid,
"appsecret" => $appsecret,
"token" => $token,
"weapp_name" => $weapp_name,
"weapp_original" => $weapp_original,
"encodingaeskey" => $encodingaeskey,
'qrcode' => $qrcode
);
$res = $weapp_model->setWeAppConfig($data, $is_use, $this->site_id);
return $res;
} else {
$weapp_config_result = $weapp_model->getWeAppConfig($this->site_id);
$config_info = $weapp_config_result[ 'data' ][ "value" ];
$this->assign("config_info", $config_info);
// 获取当前域名
$url = __ROOT__;
// 去除链接的http://头部
$url_top = str_replace("https://", "", $url);
$url_top = str_replace("http://", "", $url_top);
// 去除链接的尾部/?s=
$url_top = str_replace('/?s=', '', $url_top);
$call_back_url = addon_url("weapp://api/auth/relateweixin");
$this->assign("url", $url_top);
$this->assign("call_back_url", $call_back_url);
return $this->fetch('weapp/config', [], $this->replace);
}
}
/**
* 小程序包管理
*
*/
public function package()
{
$config = new ConfigModel();
$weapp_config = $config->getWeappConfig($this->site_id);
$weapp_config = $weapp_config[ 'data' ][ 'value' ];
if (empty($weapp_config) || empty($weapp_config[ 'appid' ])) $this->error('小程序尚未配置,请先配置您的小程序!', addon_url('weapp://shop/weapp/config'));
$this->assign('config', $weapp_config);
$is_new_version = 0;
// 获取站点小程序版本信息
$version_info = $config->getWeappVersion($this->site_id);
$version_info = $version_info[ 'data' ][ 'value' ];
$currrent_version_info = config('info');
if (!isset($version_info[ 'version' ]) || ( isset($version_info[ 'version' ]) && $version_info[ 'version' ] != $currrent_version_info[ 'version_no' ] )) {
$is_new_version = 1;
}
$this->assign('is_new_version', $is_new_version);
return $this->fetch('weapp/package', [], $this->replace);
}
/**
* 小程序包管理
*/
public function deploy()
{
$config = new ConfigModel();
$weapp_config = $config->getWeappConfig($this->site_id);
$weapp_config = $weapp_config[ 'data' ][ 'value' ];
$this->assign('config', $weapp_config);
$is_new_version = 0;
// 获取站点小程序版本信息
$version_info = $config->getWeappVersion($this->site_id);
$version_info = $version_info[ 'data' ][ 'value' ];
$currrent_version_info = config('info');
if (!isset($version_info[ 'version' ]) || ( isset($version_info[ 'version' ]) && $version_info[ 'version' ] != $currrent_version_info[ 'version_no' ] )) {
$is_new_version = 1;
}
$this->assign('is_new_version', $is_new_version);
return $this->fetch('addon/weapp/shop/view/weapp/deploy.html', [], $this->replace);
}
/**
* 小程序包下载
*/
public function download()
{
if (strstr(ROOT_URL, 'small.cn') === false) {
$weapp = new WeappModel();
$weapp->download($this->site_id);
$config = new ConfigModel();
$version_info = config('info');
$versionInfo = $config->getWeappVersion($this->site_id)['data']['value'];
$versionInfo['version'] = $version_info[ 'version_no' ];
$config->setWeappVersion($versionInfo, 1, $this->site_id);
}
}
/**
* 下载uniapp源码
*/
public function downloadUniapp()
{
if (strstr(ROOT_URL, 'small.cn') === false) {
$app_info = config('info');
$upgrade_model = new Upgrade();
$res = $upgrade_model->downloadUniapp($app_info[ 'version_no' ]);
if ($res[ 'code' ] == 0) {
$filename = "upload/{$app_info['version_no']}_uniapp.zip";
$res = file_put_contents($filename, base64_decode($res[ 'data' ]));
header("Content-Type: application/zip");
header("Content-BirthdayGift-Encoding: Binary");
header("Content-Length: " . filesize($filename));
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\"");
readfile($filename);
@unlink($filename);
} else {
$this->error($res[ 'message' ]);
}
}
}
/**
* 分享
*/
public function shareBack()
{
$config_model = new ConfigModel();
if (request()->isAjax()) {
$key = input('key', 'index');
$value = [
'title' => input('title', ''),
'path' => input('path', '')
];
$res = $config_model->setShareConfig($this->site_id, $this->app_module, $key, $value);
return $res;
}
$scene = [
[
'key' => 'index',
'title' => '首页'
]
];
$this->assign('scene', $scene);
$config = $config_model->getShareConfig($this->site_id, $this->app_module);
$this->assign('config', $config[ 'data' ][ 'value' ]);
$this->assign('shop_info', $this->shop_info);
return $this->fetch('weapp/share_back', [], $this->replace);
}
/**
* 分享
*/
public function share()
{
if (request()->isAjax()) {
$data_json = input('data_json', '');
$data = json_decode($data_json, true);
$share_model = new ShareModel();
return $share_model->setShareConfig($this->site_id, $data);
} else {
$share_config = event('WeappShareConfig', [ 'site_id' => $this->site_id ]);
$config_list = [];
foreach ($share_config as $data) {
foreach ($data[ 'data' ] as $val) {
$config_list[] = $val;
}
}
$this->assign('config_list', $config_list);
return $this->fetch('weapp/share', [], $this->replace);
}
}
/**
* Common: 小程序上传 - 进入页面
* Author: wu-hui
* Time: 2024/07/26 11:49
* @return mixed
* @throws \think\Exception
*/
public function releaseNew(){
// 获取小程序配置信息
$config = (new ConfigModel())->getWeappConfig($this->site_id)['data']['value'];
if(empty($config) || empty($config['appid'])) $this->error('小程序尚未配置,请先配置您的小程序!',addon_url('weapp://shop/weapp/config'));
// 小程序版本信息及预览二维码
$miniProgramCi = (new MiniProgramCi($this->site_id));
$versionInfo = $miniProgramCi->getAutoReleaseVersion();// 获取版本信息
$isHaveKeyFile = $miniProgramCi->privateKeyFileIsExist();// 是否存在秘钥文件
// 预览二维码
$previewQrCode = '';
if($isHaveKeyFile) $previewQrCode = $miniProgramCi->getPreviewQrCode(true);// 获取预览二维码
// 是否允许提交
$limitTime = Cache::get('releaseVersionLimitTime');
$surplusTime = $limitTime > 0 ? $limitTime - time() : 0;
return $this->fetch('weapp/release_new',[
'config' => json_encode($config),
'version' => json_encode($versionInfo),
'qr_code' => $previewQrCode,
'is_have_key_file' => $isHaveKeyFile,
'surplus_time' => $surplusTime,
],$this->replace);
}
/**
* Common: 小程序上传 - 获取预览二维码
* Author: wu-hui
* Time: 2024/07/26 16:13
* @return array
*/
public function getPreviewQrCode(){
// 获取预览二维码
$previewQrCode = (new MiniProgramCi($this->site_id))->getPreviewQrCode();
return success(0,'success', $previewQrCode);
}
/**
* Common: 小程序上传 - 点击上传小程序
* Author: wu-hui
* Time: 2024/07/26 16:48
* @return array
*/
public function releaseVersion(){
// 判断:是否在限制时间内 未过限制时间禁止提交
$limitTime = Cache::get('releaseVersionLimitTime');
if(!empty($limitTime)) return error(-1,'5分钟内仅允许提交一次');
// 上传小程序
(new MiniProgramCi($this->site_id))->releaseVersion();
// 记录时间 值为可再次上传时间
$time = strtotime("+5 minutes");
Cache::set('releaseVersionLimitTime', $time,300);
return success(0,'success');
}
/**
* Common: 小程序上传秘钥文件上传
* Author: wu-hui
* Time: 2024/07/29 14:51
* @return array
*/
public function uploadKeyFile(){
try{
// 参数获取
$file = request()->file('file');
$fileName = $file->getOriginalName();
$savePath = '/extend/miniprogram-ci/key';
Filesystem::disk('public')->putFileAs($savePath, $file, $fileName);
return success(0,'success');
}catch(\Exception $e){
return error(-1,$e->getMessage());
}
}
}