325 lines
14 KiB
PHP
325 lines
14 KiB
PHP
<?php
|
||
/**
|
||
* SAAS应用系统 --- 十年开发经验汇集巨献!
|
||
* ==========================================================
|
||
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
|
||
* ----------------------------------------------------------
|
||
* 官方网址: https://www.zoomtk.com
|
||
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
|
||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布传播。
|
||
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
|
||
* ==========================================================
|
||
*/
|
||
namespace addon\wxoplatform\model;
|
||
use addon\weapp\model\Config as WeappConfigModel;
|
||
use addon\wxoplatform\model\Oplatform as OplatformModel;
|
||
use app\model\BaseModel;
|
||
use app\model\system\Api;
|
||
use app\model\system\Config as ConfigModel;
|
||
use app\model\system\UniAccount;
|
||
use app\model\upload\Upload;
|
||
|
||
/**
|
||
* 微信公众号配置
|
||
*/
|
||
class Weapp extends BaseModel
|
||
{
|
||
protected $audit_status = [
|
||
0 => '审核中',
|
||
1 => '审核成功',
|
||
2 => '审核被拒绝',
|
||
3 => '审核延后',
|
||
4 => '已撤回',
|
||
5 => '已发布',
|
||
6 => '待提交',
|
||
7 => '历史版本',
|
||
];
|
||
|
||
/**
|
||
* 获取审核记录
|
||
* @param array $condition
|
||
* @param bool $field
|
||
* @param string $order
|
||
* @param int $page
|
||
* @param int $list_rows
|
||
*/
|
||
public function getAuditPageList($site_id, $condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS){
|
||
$list = model('weapp_audit_record')->pageList($condition, $field, $order, $page, $list_rows);
|
||
if (!empty($list['list'])) {
|
||
$platform = new OpenPlatform($site_id);
|
||
$audi=[];
|
||
foreach ($list['list'] as $k => $item) {
|
||
if ($item['auditid']&&($item['status'] == 0 || $item['status'] == 3)) {
|
||
$audit_status = $platform->getAuditstatus( ['auditid' => $item['auditid'] ] );
|
||
if ($audit_status['code'] >= 0 && $audit_status['data']['status'] != 2) {
|
||
$this->updateAuditStatus($item['id'], $audit_status['data']);
|
||
}
|
||
}else if(!in_array($item['status'],[1,5,6])){
|
||
$item['status']=7;
|
||
$list['list'][$k]['status']=7;
|
||
}
|
||
$list['list'][$k]['status_name'] = $this->audit_status[ $item['status'] ] ?? '';
|
||
}
|
||
}
|
||
return $this->success($list);
|
||
}
|
||
|
||
/**
|
||
* 变更审核状态
|
||
* @param $id
|
||
* @param $data
|
||
*/
|
||
private function updateAuditStatus($id, $param){
|
||
$data = [];
|
||
switch ($param['status']) {
|
||
case 0: // 审核通过
|
||
$data = [
|
||
'status' => 1,
|
||
'audit_time' => time()
|
||
];
|
||
break;
|
||
case 1: // 审核未通过
|
||
$data = [
|
||
'status' => 2,
|
||
'audit_time' => time(),
|
||
'reason' => $param['reason']
|
||
];
|
||
break;
|
||
case 4: // 审核延后
|
||
$data = [
|
||
'status' => 3,
|
||
'reason' => $param['reason']
|
||
];
|
||
break;
|
||
}
|
||
if (!empty($data)) {
|
||
model('weapp_audit_record')->update($data, [ ['id', '=', $id] ]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 撤销审核
|
||
* @param $id
|
||
* @param $site_id
|
||
*/
|
||
public function cancelAudit($id, $site_id){
|
||
$audit_info = model('weapp_audit_record')->getInfo([ ['id', '=', $id],['site_id', '=', $site_id], ['status', '=', 0] ]);
|
||
if (empty($audit_info)) return $this->error();
|
||
|
||
$platform = new OpenPlatform($site_id);
|
||
$result = $platform->undocodeaudit([ 'auditid' => $audit_info['auditid'] ]);
|
||
if ($result['code'] < 0) return $result;
|
||
|
||
model('weapp_audit_record')->update([ 'status' => 4 ], [ ['id', '=', $id],['site_id', '=', $site_id] ]);
|
||
return $this->success('', 'CANCEL_SUCCESS');
|
||
}
|
||
|
||
/**
|
||
* 小程序发布
|
||
* @param $id
|
||
* @param $site_id
|
||
* @return array
|
||
*/
|
||
public function release($id, $site_id){
|
||
$audit_info = model('weapp_audit_record')->getInfo([ ['id', '=', $id],['site_id', '=', $site_id], ['status', '=', 1] ]);
|
||
if (empty($audit_info)) return $this->error();
|
||
|
||
$platform = new OpenPlatform($site_id);
|
||
$result = $platform->release();
|
||
if ($result['code'] < 0) return $result;
|
||
|
||
model('weapp_audit_record')->update([ 'status' => 5, 'release_time' => time() ], [ ['id', '=', $id],['site_id', '=', $site_id] ]);
|
||
return $this->success('', 'RELEASE_SUCCESS');
|
||
}
|
||
|
||
/**
|
||
* 获取体验者列表
|
||
* @param array $condition
|
||
* @param bool $field
|
||
* @param string $order
|
||
* @param int $page
|
||
* @param int $list_rows
|
||
*/
|
||
public function getExperiencerPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS){
|
||
$list = model('weapp_experiencer')->pageList($condition, $field, $order, $page, $list_rows);
|
||
return $this->success($list);
|
||
}
|
||
|
||
/**
|
||
* 绑定体验者
|
||
* @param $data
|
||
*/
|
||
public function bindExperiencer($site_id, $wechatid){
|
||
$platform = new OpenPlatform($site_id);
|
||
$result = $platform->bindTester(['wechatid' => $wechatid]);
|
||
if ($result['code'] < 0) return $result;
|
||
|
||
$data = [
|
||
'site_id' => $site_id,
|
||
'wechatid' => $wechatid,
|
||
'userstr' => $result['data']['userstr'],
|
||
'create_time' => time()
|
||
];
|
||
$result = model('weapp_experiencer')->add($data);
|
||
return $this->success($result);
|
||
}
|
||
|
||
/**
|
||
* 体验者取消绑定
|
||
* @param $id
|
||
* @param $site_id
|
||
*/
|
||
public function unbindExperiencer($id, $site_id){
|
||
$experiencer_info = model('weapp_experiencer')->getInfo([ ['id', '=', $id],['site_id', '=', $site_id] ]);
|
||
if (empty($experiencer_info)) return $this->error();
|
||
|
||
$platform = new OpenPlatform($site_id);
|
||
$result = $platform->unbindTester(['userstr' => $experiencer_info['userstr'] ]);
|
||
if ($result['code'] < 0) return $result;
|
||
|
||
model('weapp_experiencer')->delete([ ['id', '=', $id],['site_id', '=', $site_id] ]);
|
||
return $result;
|
||
}
|
||
/**
|
||
* 检测小程序模板是否发生了变更
|
||
*/
|
||
public function checkWeappVersionIsChange($template_info){
|
||
$config = new ConfigModel();
|
||
$weapp_version_info = $config->getConfig([['site_id', '=', 0], ['app_module', '=', 'admin'], ['config_key', '=', 'OPLATFORM_WEAPP_VERSION']]);
|
||
$weapp_version_info = $weapp_version_info['data']['value'];
|
||
if (empty($weapp_version_info) || $weapp_version_info['template_id'] != $template_info['template_id']) {
|
||
// $cron = new Cron();
|
||
// $cron->addCron(1,0,'小程序代码自动上传', 'WeappAutoSubmit', (time() + 30), 0);
|
||
$config->setConfig($template_info, '开放平台最新小程序模板信息', 1, [['site_id', '=', 0], ['app_module', '=', 'admin'], ['config_key', '=', 'OPLATFORM_WEAPP_VERSION']]);
|
||
}
|
||
}
|
||
/**
|
||
* 小程序自动提交审核
|
||
*/
|
||
public function autoSubmitAudit(){
|
||
|
||
return;
|
||
set_time_limit(0);
|
||
// 小程序授权的站点
|
||
$site_list = model('config')->getList([ ['app_module', '=', 'shop'], ['config_key', '=', 'WEAPP_CONFIG' ], ['value', 'like', '%is_authopen%'] ], 'site_id');
|
||
// 获取最新的一条模板
|
||
$platform = new OpenPlatform();
|
||
$template_list = $platform->getTemplateList();
|
||
if ($template_list['code'] == 0 && is_array($template_list['data']) && count($template_list['data']) > 1) array_multisort(array_column($template_list['data'], 'template_id'), SORT_DESC, $template_list['data']);
|
||
$template_info = $template_list['data'][0] ?? [];
|
||
if (!empty($site_list) && $template_info) {
|
||
// api安全配置
|
||
$api_model = new Api();
|
||
$api_config = $api_model->getApiConfig();
|
||
$api_config =$api_config['data'];
|
||
foreach ($site_list as $site_item) {
|
||
$open_platform_model = new OpenPlatform($site_item['site_id']);
|
||
$socket_url = (strstr(ROOT_URL, 'https://') === false ? str_replace('http', 'ws', ROOT_URL) : str_replace('https', 'wss', ROOT_URL)) . '/wss';
|
||
$data = array(
|
||
'template_id' => $template_info['template_id'],
|
||
'ext_json' => [
|
||
'extEnable' => true,
|
||
'extAppid' => $open_platform_model->appid,
|
||
'ext' => [
|
||
'site_id' => $site_item['site_id'],
|
||
'baseUrl' => $open_platform_model->openConfig['api_url'],
|
||
'imgDomain' => $open_platform_model->openConfig['cdn_url'],
|
||
'apiSecurity' => $api_config[ 'is_use' ] ?? 0,
|
||
'publicKey' => $api_config[ 'value' ][ 'public_key' ] ?? '',
|
||
"client_type" => 'shop',
|
||
'platform_id' => 0,
|
||
'webSocket' => $open_platform_model->openConfig['webSocket'],
|
||
]
|
||
],
|
||
'user_version' => $template_info['user_version'],
|
||
'user_desc' => $template_info['user_desc'] . ',上传时间'.date('Y-m-d H:i:s', time()),
|
||
);
|
||
$platform->weappCommit($data);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public function AutoSetConfig($appid, $site_id = 0)
|
||
{
|
||
$oplatform_model = new OplatformModel();
|
||
$config_model = new \addon\wxoplatform\model\Config();
|
||
$oplatform_result = $config_model->getOplatformConfig();
|
||
$oplatform_config = $oplatform_result["data"];
|
||
$auth_info_result = $oplatform_model->getAuthorizer(["authorizer_appid" => $appid]);
|
||
$upload_model = new Upload($site_id);
|
||
if ($auth_info_result["code"] >= 0) {
|
||
$arr = $auth_info_result["data"]['authorization_info'];
|
||
//拉取授权方账号信息
|
||
$data = array(
|
||
'appid' => $arr["authorizer_appid"] ?? '',//授权方 appid
|
||
'authorizer_appid' => $arr["authorizer_appid"] ?? '',//授权方 appid
|
||
'authorizer_access_token' => $arr["authorizer_access_token"] ?? '',//接口调用令牌(在授权的公众号/小程序具备 API 权限时,才有此返回值)
|
||
'authorizer_refresh_token' => $arr["authorizer_refresh_token"] ?? '',//刷新令牌(在授权的公众号具备API权限时,才有此返回值),刷新令牌主要用于第三方平台获取和刷新已授权用户的 authorizer_access_token。一旦丢失,只能让用户重新授权,才能再次拿到新的刷新令牌。用户重新授权后,之前的刷新令牌会失效
|
||
'is_authopen' => 1,//是否是开放平台授权,
|
||
'component_appid' => $oplatform_config['value']['appid']
|
||
);
|
||
$weapp_model = new \addon\weapp\model\Weapp();
|
||
$weapp_config_model = new WeappConfigModel();
|
||
$auth_info = $auth_info_result["data"];
|
||
$authorizer_info = $auth_info["authorizer_info"];//授权信息
|
||
$headimg = '';
|
||
if(isset($authorizer_info["head_img"])){
|
||
//拉取头像和二维码
|
||
$headimg_result = $upload_model->setPath("headimg/weapp/" . date("Ymd") . '/')->remotePull($authorizer_info["head_img"]);
|
||
if ($headimg_result["code"] >= 0) {
|
||
$headimg = $headimg_result["data"]["pic_path"];
|
||
}
|
||
}
|
||
$qrcode_url_result = $upload_model->setPath("qrcode/weapp/" . date("Ymd") . '/')->remotePull($authorizer_info["qrcode_url"]);
|
||
$qrcode_path = '';
|
||
if ($qrcode_url_result["code"] >= 0) {
|
||
$qrcode_path = $qrcode_url_result["data"]["pic_path"];
|
||
}
|
||
$data["appsecret"] = '';//
|
||
$data["service_type_info"] = $weapp_model->service_type[$authorizer_info["service_type_info"]["id"]];//小程序类型
|
||
$data["verify_type_info"] = $weapp_model->verify_type[$authorizer_info["verify_type_info"]["id"]];//小程序认证类型
|
||
$data["weapp_name"] = $authorizer_info["nick_name"];//昵称
|
||
$data["head_img"] = $headimg;//头像
|
||
$data["weapp_original"] = $authorizer_info["user_name"];//原始 ID
|
||
$data["user_name"] = $authorizer_info["user_name"];//原始 ID
|
||
$data["principal_name"] = $authorizer_info["principal_name"];//主体名称
|
||
$data["business_info"] = $authorizer_info["business_info"]; //功能开通情况
|
||
$data["qrcode"] = $qrcode_path;//二维码
|
||
$data["signature"] = $authorizer_info["signature"];//帐号介绍
|
||
$data["miniprograminfo"] = $authorizer_info["MiniProgramInfo"];//小程序配置,根据这个字段判断是否为小程序类型授权
|
||
//配置小程序合法域名
|
||
$domain = $oplatform_config['value'];
|
||
$network = array(
|
||
'RequestDomain' => explode(';', $domain['request_url']),
|
||
'WsRequestDomain' => explode(';', $domain['socket_url']),
|
||
'UploadDomain' => explode(';', $domain['upload_url']),
|
||
'DownloadDomain' => explode(';', $domain['download_url']),
|
||
);
|
||
$data['miniprograminfo']['network'] = $network;
|
||
if($site_id){
|
||
$config = new UniAccount();
|
||
$condition=[
|
||
'appid'=>$data['appid'],
|
||
'site_id'=>$site_id,
|
||
];
|
||
$config->setUniConfig($condition,$data,'weapp','WEAPP_CONFIG',$data["weapp_original"]);
|
||
$weapp_config_model->setWeappConfig($data, 1, $site_id);//设置系统配置//后期移除
|
||
$open_platform_model = new OpenPlatform($site_id);
|
||
$Domain = array(
|
||
'action' => 'set',
|
||
'requestdomain' => $network['RequestDomain'],
|
||
'wsrequestdomain' => $network['WsRequestDomain'],
|
||
'uploaddomain' => $network['UploadDomain'],
|
||
'downloaddomain' => $network['DownloadDomain'],
|
||
);
|
||
$open_platform_model->modifyDomain($Domain);//设置域名
|
||
}
|
||
return $data;
|
||
}else{
|
||
return $auth_info_result;
|
||
}
|
||
}
|
||
|
||
}
|