218 lines
7.9 KiB
PHP
218 lines
7.9 KiB
PHP
<?php
|
||
namespace addon\saas\model;
|
||
use app\model\BaseModel;
|
||
use think\Exception;
|
||
/**
|
||
* Common: 支付宝小程序相关操作
|
||
* Author: wu-hui
|
||
* Time: 2022/12/29 15:10
|
||
* Class AliPayApplet
|
||
* @package addon\aliapp\model
|
||
*/
|
||
class AliPayApplet extends BaseModel
|
||
{
|
||
|
||
protected $site_id;
|
||
// 小程序模板信息
|
||
private $templateInfo = [];
|
||
// 小程序投放的端:com.alipay.alipaywallet=支付宝端。com.alipay.iot.xpaas=支付宝IoT端。
|
||
private $bundle_id = 'com.alipay.alipaywallet';
|
||
public $version = '';
|
||
public $appid = '';
|
||
public $config = '';
|
||
|
||
public function __construct($siteId, $config)
|
||
{
|
||
$this->site_id = $siteId;
|
||
$this->config = $config;
|
||
$this->templateInfo = config('alipay.managetemplate');
|
||
if (isset($config['old_version'])) {
|
||
$this->version = $config;
|
||
} else {
|
||
$this->version = [
|
||
'old_version' => '0.0.0'
|
||
];
|
||
}
|
||
if (isset($config['appid'])) {
|
||
$this->appid = $config['appid'];
|
||
}
|
||
}
|
||
|
||
/***
|
||
* 判断是否新版本
|
||
* @param $user_version
|
||
* @return bool
|
||
*/
|
||
public function isNewestVersion($user_version)
|
||
{
|
||
return (boolean)version_compare($user_version, $this->version['old_version'], '>');
|
||
}
|
||
|
||
/***
|
||
* 发布版本等
|
||
* @param $type
|
||
* @param $app_version
|
||
* @return array
|
||
*/
|
||
public function publishingInit($type, $app_version = '')
|
||
{
|
||
try {
|
||
// 根据操作类型获取对应信息
|
||
$config = $this->publishingConfig($type);
|
||
[$params, $textParams, $extra] = $this->publishingParams($type, $app_version);
|
||
// 发起请求
|
||
$result = (new MinCode($this->site_id))->requestApi($config['api'], $params, $textParams);
|
||
$result = $result[$config['result_key']] ?? ['code' => 0];
|
||
if ($result['code'] == 10000) {
|
||
return success(0, $result);
|
||
} else {
|
||
return $this->error('', $result['sub_msg']);
|
||
}
|
||
} catch (\Exception $e) {
|
||
return $this->error('', $e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Common: 发布流程处理 —— 根据操作类型获取配置信息
|
||
* Author: wu-hui
|
||
* Time: 2023/01/04 10:09
|
||
* @param $type
|
||
* @return string[]
|
||
* @throws Exception
|
||
*/
|
||
private function publishingConfig($type): array
|
||
{
|
||
$list = [
|
||
'delete' => [
|
||
'api' => 'alipay.open.mini.version.delete', // 请求接口
|
||
'result_key' => 'alipay_open_mini_version_delete_response',// 返回结果字段名称
|
||
],
|
||
'release_experience_version' => [
|
||
'api' => 'alipay.open.mini.experience.create',
|
||
'result_key' => 'alipay_open_mini_experience_create_response',
|
||
],
|
||
'cancel_experience_version' => [
|
||
'api' => 'alipay.open.mini.experience.cancel',
|
||
'result_key' => 'alipay_open_mini_experience_cancel_response',
|
||
],
|
||
'see_experience_version' => [
|
||
'api' => 'alipay.open.mini.experience.query',
|
||
'result_key' => 'alipay_open_mini_experience_query_response',
|
||
],
|
||
'audit_submit' => [
|
||
'api' => 'alipay.open.mini.version.audit.apply',
|
||
'result_key' => 'alipay_open_mini_version_audit_apply_response',
|
||
],
|
||
'audit_cancel' => [
|
||
'api' => 'alipay.open.mini.version.audit.cancel',
|
||
'result_key' => 'alipay_open_mini_version_audit_cancel_response',
|
||
],
|
||
'audited_cancel' => [
|
||
'api' => 'alipay.open.mini.version.audited.cancel',
|
||
'result_key' => 'alipay_open_mini_version_audited_cancel_response',
|
||
],
|
||
'version_online' => [
|
||
'api' => 'alipay.open.mini.version.online',
|
||
'result_key' => 'alipay_open_mini_version_online_response',
|
||
],
|
||
'version_gray_online' => [
|
||
'api' => 'alipay.open.mini.version.gray.online',
|
||
'result_key' => 'alipay_open_mini_version_gray_online_response',
|
||
],
|
||
'version_gray_cancel' => [
|
||
'api' => 'alipay.open.mini.version.gray.cancel',
|
||
'result_key' => 'alipay_open_mini_version_gray_cancel_response',
|
||
],
|
||
'version_offline' => [
|
||
'api' => 'alipay.open.mini.version.offline',
|
||
'result_key' => 'alipay_open_mini_version_offline_response',
|
||
],
|
||
'version_rollback' => [
|
||
'api' => 'alipay.open.mini.version.rollback',
|
||
'result_key' => 'alipay_open_mini_version_rollback_response',
|
||
],
|
||
'version_upload' => [
|
||
'api' => 'alipay.open.mini.version.upload',
|
||
'result_key' => 'alipay_open_mini_version_upload_response',
|
||
],
|
||
];
|
||
if ($list[$type]) return $list[$type];
|
||
else throw new Exception('无效的操作!');
|
||
}
|
||
|
||
/**
|
||
* Common: 发布流程处理 —— 根据操作类型生成接口请求参数
|
||
* Author: wu-hui
|
||
* Time: 2023/01/04 10:32
|
||
* @param $type
|
||
* @param $id
|
||
* @param $app_version
|
||
* @return array[]
|
||
* @throws Exception
|
||
*/
|
||
private function publishingParams($type, $app_version = '')
|
||
{
|
||
$params = $textParams = $extra = [];
|
||
// 根据类型进行对应的操作
|
||
switch ($type) {
|
||
case 'delete':
|
||
case 'release_experience_version':
|
||
case 'cancel_experience_version':
|
||
case 'see_experience_version':
|
||
case 'audit_cancel':
|
||
case 'audited_cancel':
|
||
case 'version_online':
|
||
case 'version_gray_cancel':
|
||
case 'version_offline':
|
||
case 'version_rollback':
|
||
$params = [
|
||
'app_version' => $app_version,
|
||
'bundle_id' => $this->bundle_id,
|
||
];
|
||
break;
|
||
case 'audit_submit':
|
||
$textParams = [
|
||
'region_type' => 'CHINA',
|
||
'version_desc' => "发布{$app_version}版本",
|
||
'app_version' => $app_version, //商家自定义版本
|
||
'bundle_id' => $this->bundle_id,
|
||
'speed_up' => 'false',
|
||
'auto_online' => 'auto_online'
|
||
];
|
||
break;
|
||
case 'version_gray_online':
|
||
$params = [
|
||
'app_version' => $app_version,
|
||
'gray_strategy' => 'p10',// 枚举支持:p10、p30、p50
|
||
'bundle_id' => $this->bundle_id,
|
||
];
|
||
break;
|
||
case 'version_upload':// 获取最新版本
|
||
$isNewestVersion = $this->isNewestVersion($app_version);
|
||
if (!$isNewestVersion) throw new Exception('当前版本为最新版本,请勿重复发布!');
|
||
$extra = ['appid' => $this->appid];
|
||
$ext = [
|
||
'extEnable' => true,
|
||
'ext' => [
|
||
'site_id' => $this->site_id
|
||
],
|
||
'window' => [
|
||
'defaultTitle' => ''
|
||
],
|
||
];
|
||
$params = [
|
||
'template_version' => $this->templateInfo['template_version'],// 小程序模板版本号
|
||
'ext' => json_encode($ext),//自定义参数
|
||
'template_id' => $this->templateInfo['template_id'],// 小程序模板 APPID
|
||
'app_version' => $app_version,//商家小程序版本号
|
||
'bundle_id' => $this->bundle_id,
|
||
];
|
||
break;
|
||
default:
|
||
throw new Exception('无效的操作!');
|
||
}
|
||
return [$params, $textParams, $extra];
|
||
}
|
||
}
|