| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946 |
- <?php
- /***
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\utils\Douyin;
- use app\models\StoreDouyinMini;
- use app\models\Option;
- use app\constants\OptionSetting;
- class Douyin {
-
- public static $baseUrl = 'https://open.microapp.bytedance.com/';
- public static $component_conf = [];
- public static $mini_conf = [];
- public static $authorizer_access_token_refresh = [];
- public static $webviewHostName = 'ws2.cyyvip.com';
- public static $webviewHostInfo = 'https://ws2.cyyvip.com';
- public static function mini_conf($mini_id, $setConf = [], $refresh = 0) {
- $setConf['expires_in'] && $setConf['expires_in'] += time();
- $setConf['refresh_expires_in'] && $setConf['refresh_expires_in'] += time();
- $confDef = [
- 'authorizer_appid' => '',
- 'authorizer_access_token' => '',
- 'expires_in' => '',
- 'authorizer_refresh_token' => '',
- 'refresh_expires_in' => '',
- 'authorize_permission' => [],
- ];
- if($mini_id && ($refresh || !self::$mini_conf[$mini_id])){
- $mini = StoreDouyinMini::findOne($mini_id);
- self::$mini_conf[$mini_id] = json_decode($mini['auth'], true);
- }
- if($mini_id && self::$mini_conf[$mini_id]){
- $confDef = array_merge($confDef, self::$mini_conf[$mini_id]);
- if(!self::$authorizer_access_token_refresh[$mini_id] && $confDef['expires_in'] < (time() + 30 * 60)){
- $token_refresh = self::authorizer_access_token_refresh($mini_id, $confDef['authorizer_appid']);
- if($token_refresh['code'] == 0){
- $confDef = array_merge($confDef, $token_refresh['data']);
- }
- }
- }
- if($setConf){
- $confDef = array_merge($confDef, $setConf);
- }
- $mini_app_init = 0;
- if(!$mini_id && $confDef['authorizer_appid']){
- $mini_app_init = 1;
- $mini = StoreDouyinMini::authUpdate($mini_id, $confDef);
- if($mini['code'] != 0){
- throw new \Exception($mini['msg']);
- }
- $mini_id = $mini['data']['id'];
- }
- //进程内需要先更新self::$mini_conf[$mini_id],才能进行下一次调用接口
- $mini_id && self::$mini_conf[$mini_id] = $confDef;
- if($mini_app_init){
- debug_log([$mini_id, self::$mini_conf[$mini_id]], __CLASS__ . '.log');
- self::mini_app_init($mini_id);
- }
-
- return $confDef;
- }
- public static function component_conf($setConf = [], $refresh = 0) {
- $confDef = [
- 'component_appid' => '',
- 'component_appsecret' => '',
- 'aesKey' => '',
- 'tpToken' => '',
- 'template_id' => '',
- 'component_access_token' => '',
- 'webview_file' => 0,
- 'service_ip' => gethostbyname(\Yii::$app->request->hostName),
- 'hostName' => \Yii::$app->request->hostName,
- 'webviewHostName' => self::$webviewHostName,
- 'callback_component_ticket_url' => \Yii::$app->request->hostInfo . '/index.php/douyin/ticket',
- 'callback_component_event_url' => \Yii::$app->request->hostInfo . '/index.php/douyin/callback/$APPID$',
- 'callback_component_code_url' => \Yii::$app->request->hostInfo . '/index.php/douyin/store-mini-code/$STORE_ID$',
- ];
- if($refresh || !self::$component_conf){
- self::$component_conf = json_decode(Option::get(OptionSetting::DOUYIN_COMPONENT, 0, 'saas', '{}')['value'], true);
- }
- if(self::$component_conf){
- $confDef = array_merge($confDef, self::$component_conf);
- }
- if($setConf){
- $confDef = array_merge($confDef, $setConf);
- Option::set(OptionSetting::DOUYIN_COMPONENT, json_encode($confDef), 0, 'saas');
- }
- self::$component_conf = $confDef;
-
- return self::$component_conf;
- }
-
- public static function component_event_url($mini_id = 0) {
- $conf = self::component_conf();
- return str_replace('$APPID$', $mini_id, $conf['callback_component_event_url']);
- }
-
- public static function component_code_url($store_id = 0) {
- $conf = self::component_conf();
- return str_replace('$STORE_ID$', $store_id, $conf['callback_component_code_url']);
- }
- /***
- * 验证消息签名
- * @param $timestamp, $nonce, $encrypt, $msgSignature
- */
- public static function _component_verify($msg) {
- $conf = self::component_conf();
- $values = array($conf['tpToken'], $msg['TimeStamp'], $msg['Nonce'], $msg['Encrypt']);
- sort($values, SORT_STRING);
- $newMsgSignature = sha1(join("", $values));
- if ($newMsgSignature == $msg['MsgSignature']) {
- return [
- 'code' => 0,
- 'msg' => 'ok',
- ];
- } else {
- \Yii::error('签名验证失败');
- return [
- 'code' => 1,
- 'msg' => '签名验证失败',
- ];
- }
- }
- //三方消息解密
- public static function _component_decrypt($encrypted) {
- $conf = self::component_conf();
- $aesKey = base64_decode($conf['aesKey'] . "=");
- try {
- $blockSize = 32;
- // 使用BASE64对需要解密的字符串进行解码
- $ciphertextDec = base64_decode($encrypted);
- $iv = substr($aesKey, 0, 16);
- // 解密
- $decrypted = openssl_decrypt($ciphertextDec, 'aes-256-cbc', $aesKey, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => 'AesEncryptUtil AES解密串非法,小于16位;' . $e->getMessage(),
- ];
- }
- try {
- // 去除补位字符
- $pad = ord(substr($decrypted, -1));
- if ($pad < 1 || $pad > $blockSize) {
- $pad = 0;
- }
- $result = substr($decrypted, 0, (strlen($decrypted) - $pad));
- // $result = $this->decode($decrypted);
- // 去除16位随机字符串,网络字节序和 tp appid
- if (strlen($result) < 16) {
- throw new Exception('AesEncryptUtil AES解密串非法,小于16位;');
- }
- // 去除16位随机字符串
- $content = substr($result, 16, strlen($result));
- // 获取消息体长度
- $lenList = unpack("N", substr($content, 16, 4));
- $postBodyLen = $lenList[1];
- // 获取消息体
- $postBodyMsg = substr($content, 20, $postBodyLen);
- // 获取消息体的第三方平台 appid
- $fromTpAppId = substr($content, 20 + $postBodyLen);
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => 'AesEncryptUtil AES解密串非法,小于16位;' . $e->getMessage(),
- ];
- }
- return [
- 'code' => 0,
- 'data' => json_decode($postBodyMsg, true),
- 'msg' => '操作成功',
- ];
- }
- public static function msg_decode($msg) {
- $verify = self::_component_verify($msg);
- if ($verify['code'] != 0) {
- throw new \Exception('接口消息验签失败');
- // return $verify;
- }
- $encrypted = $msg['Encrypt'];
- $data = self::_component_decrypt($encrypted);
- if ($data['code'] != 0) {
- throw new \Exception('接口消息解密失败');
- // return $data;
- }
- $decode = $data['data'];
- unset($msg['Nonce']);
- unset($msg['TimeStamp']);
- unset($msg['Encrypt']);
- unset($msg['MsgSignature']);
- $res = array_merge($msg, $decode);
- // debug_log($res, __CLASS__ . '.log');
- return $res;
- }
- //三方ticket
- public static function component_ticket($component_ticket) {
- $time = time();
- // debug_log($component_ticket, __CLASS__ . '.log');
- $ticket = $component_ticket['Ticket'];
- $access_token = self::component_access_token($ticket);
- if ($access_token['code'] != 0) {
- return $access_token;
- }
- self::component_conf([
- 'component_access_token' => $access_token['data']['component_access_token'],
- 'expires_in' => $access_token['data']['expires_in'] + $time,
- ]);
-
- return $access_token;
- }
- //三方url
- public static function component_api_url($uri) {
- $url = self::$baseUrl . $uri;
- return $url;
- }
- //三方get
- public static function component_http_get($uri, $options) {
- $res = self::http_res(http_get(self::component_api_url($uri), $options));
- if($res['code'] != 0){
- debug_log([$uri, $options, $res], __CLASS__ . '.log');
- }
- return $res;
- }
- //三方post
- public static function component_http_post($uri, $options) {
- $res = self::http_res(http_post(self::component_api_url($uri), $options));
- if($res['code'] != 0){
- debug_log([$uri, $options, $res], __CLASS__ . '.log');
- }
- return $res;
- }
-
- public static function http_res($http_res) {
- if ($http_res->getStatusCode() != 200) {
- return [
- 'code' => 1,
- 'msg' => '接口请求http出错!',
- ];
- }
- $ContentType = implode(';', $http_res->getHeader('Content-Type'));
- if(strstr($ContentType, 'json')){
- $content = json_decode((string)$http_res->getBody(), true);
- }elseif(strstr($ContentType, 'image')){
- $content = 'data:' . $ContentType . ';base64,' . base64_encode((string)$http_res->getBody());
- }elseif(strstr($ContentType, 'text/plain')){
- $fileName = '';
- $fileHeader = $http_res->getHeader('Content-Disposition');
- if($fileHeader){
- foreach(explode(';', $fileHeader[0]) as $item){
- if(strstr($item, 'filename')){
- $fileName = trim(str_replace('filename=', '', $item));
- }
- }
- }
- if(empty($fileName)){
- return [
- 'code' => 1,
- 'msg' => '接口返回文件错误!',
- ];
- }
- $content = [
- 'fileName' => $fileName,
- 'fileContent' => (string)$http_res->getBody(),
- ];
- }else{
- $content = (string)$http_res->getBody();
- }
- $res = [
- 'code' => 0,
- 'data' => $content,
- 'msg' => '操作成功',
- ];
- if(isset($content['errno']) && $content['errno'] != 0){
- $res['code'] = $content['errno'];
- $res['msg'] = $content['message'];
- }
- if(isset($content['err_no']) && $content['err_no'] != 0){
- $res['code'] = $content['err_no'];
- $res['msg'] = $content['err_msg'];
- }
- return $res;
- }
- //三方调用时的query
- public static function http_component_query($query = []) {
- $conf = self::component_conf();
- $ret = array_merge([
- 'component_appid' => $conf['component_appid'],
- 'component_access_token' => $conf['component_access_token'],
- ], $query);
- return $ret;
- }
- //三方调用管理小程序时的query
- public static function http_component_app_query($mini_id = 0, $query = []) {
- $conf = self::component_conf();
- $mini_conf = self::mini_conf($mini_id);
- $ret = array_merge([
- 'component_appid' => $conf['component_appid'],
- 'authorizer_access_token' => $mini_conf['authorizer_access_token'],
- ], $query);
- return $ret;
- }
- //三方access_token
- public static function component_access_token($component_ticket) {
- $conf = self::component_conf();
- $data = [
- 'component_appid' => $conf['component_appid'],
- 'component_appsecret' => $conf['component_appsecret'],
- 'component_ticket' => $component_ticket,
- ];
- $res = self::component_http_get('openapi/v1/auth/tp/token', ['query' => $data]);
- return $res;
- }
- //三方小程序应用上传资源
- public static function tp_upload_material($material_type, $material_file) {
- $data = self::httpUpload([
- 'material_file' => self::saveTempImage($material_file),
- ], [
- 'material_type' => $material_type,
- ]);
- $res = self::component_http_post('openapi/v1/tp/upload_pic_material', ['query' => self::http_component_query(), 'multipart' => $data]);
- if($res['code'] != 0){
- return $res;
- }
- if($res['data']['errno'] == 0){
- $res['data'] = $res['data']['data']['path'];
- }
- return $res;
- }
- //代授权小程序上传资源
- public static function mini_upload_material($mini_id, $material_type, $material_file) {
- $data = self::httpUpload([
- 'material_file' => self::saveTempImage($material_file),
- ], [
- 'material_type' => $material_type,
- ]);
- $res = self::component_http_post('openapi/v1/microapp/upload_material', ['query' => self::http_component_app_query($mini_id), 'multipart' => $data]);
- if($res['code'] != 0){
- return $res;
- }
- if($res['data']['errno'] == 0){
- $res['data'] = $res['data']['data']['path'];
- }
- return $res;
- }
- //获取网络图片到临时目录
- private static function saveTempImage($url)
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- \app\utils\CurlHelper::download($url, $save_path);
- return $save_path;
- }
- public static function httpUpload(array $files = [], array $form = [])
- {
- $multipart = [];
- $headers = [];
- foreach ($files as $name => $path) {
- $multipart[] = [
- 'name' => $name,
- 'contents' => fopen($path, 'r'),
- 'headers' => $headers
- ];
- }
- foreach ($form as $name => $contents) {
- $multipart[] = compact('name', 'contents');
- }
- return $multipart;
- }
- //三方下载webview域名校验文件
- public static function webview_file() {
- $res = self::component_http_get('openapi/v1/tp/download/webview_file', ['query' => self::http_component_query()]);
- if($res['code'] != 0){
- return $res;
- }
- $addConfirmFile = self::addConfirmFile($res['data']);
- if($addConfirmFile['code'] != 0){
- return $addConfirmFile;
- }
- self::component_conf([
- 'webview_file' => 1,
- ]);
-
- return $res;
- }
- //校验文件提交至客服系统
- public static function addConfirmFile($file){
- $res = http_post(self::$webviewHostInfo . '/web/index.php?r=common/default/add-confirm-file', [
- 'form_params' => [
- 'token' => 'tianxin100',
- 'file_name' => $file['fileName'],
- 'file_content' => $file['fileContent'],
- ]
- ]);
- if ($res->getStatusCode() != 200) {
- return [
- 'code' => 1,
- 'msg' => '添加校验文件出错',
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- ];
- }
-
-
-
-
- //code2session
- public static function code2session($mini_id = 0, $code = '') {
- $data = [
- 'code' => $code,
- ];
- $res = self::component_http_get('openapi/v1/microapp/code2session', ['query' => self::http_component_app_query($mini_id, $data)]);
- return $res;
- }
- //三方获取授权小程序列表
- public static function get_auth_app_list($fresh = 0,$page = null) {
- if($page === null){
- $cacheK = 'douyin_component_get_auth_app_list';
- $cacheV = cache()->get($cacheK);
- if(!$fresh && $cacheV){
- return [
- 'code' => 0,
- 'data' => $cacheV,
- ];
- }
- }
- $size = 50;
- $data = [
- 'page' => $page ?: 1,
- 'size' => $size,
- ];
- $res = self::component_http_get('openapi/v1/tp/auth_app_list', ['query' => self::http_component_query($data)]);
- if($res['code'] != 0){
- return $res;
- }
- $list = $res['data']['data'];
- $total = $list['total'];
- $ret = array_column($list['authAppList'], 'authAppId');
- if($page === null){
- $pageCount = ceil($total/$size);
- if($pageCount > 1){
- for($i = 2; $i <= $pageCount; $i++){
- $res = self::get_auth_app_list($page);
- $ret = array_merge($ret, $res['data']);
- }
- }
- cache()->set($cacheK, $ret);
- }
- return [
- 'code' => 0,
- 'data' => $ret,
- ];
- }
- //三方获取模板小程序列表(模板草稿列表)
- public static function get_tpl_app_list() {
- $res = self::component_http_get('openapi/v1/tp/template/get_tpl_app_list', ['query' => self::http_component_query()]);
- return $res;
- }
- //三方获取模板列表
- public static function get_tpl_list() {
- $res = self::component_http_get('openapi/v1/tp/template/get_tpl_list', ['query' => self::http_component_query()]);
- return $res;
- }
- //三方添加模板
- public static function add_tpl($draft_id = 0) {
- $data = [
- 'draft_id' => $draft_id,
- ];
- $res = self::component_http_post('openapi/v1/tp/template/add_tpl', ['query' => self::http_component_query(), 'json' => $data]);
- $res['msg'] .= ';或检查是否已经添加过。';
- return $res;
- }
- //三方删除模板
- public static function del_tpl($template_id = 0) {
- $data = [
- 'template_id' => $template_id,
- ];
- $res = self::component_http_post('openapi/v1/tp/template/del_tpl', ['query' => self::http_component_query(), 'json' => $data]);
- return $res;
- }
- //三方直接获取授权链接
- public static function component_pre_auth_code($store_id = 0, $link_type = 1, $app_name = '', $app_icon = '') {
- $limit = 5;
- $cacheKey = 'component_pre_auth_code_' . date('Ymd') . '_' . $store_id;
- $cacheV = cache()->get($cacheKey);
- if($link_type == 2){
- if($cacheV >= $limit){
- return [
- 'code' => 1,
- 'msg' => '操作失败,代创建操作次数超过店铺每天调用限制次数' . $cacheV . '/' . $limit,
- ];
- }
- }
- if($app_icon){
- $material = self::tp_upload_material(2, $app_icon);
- if($material['code'] != 0){
- return $material;
- }
- $app_icon = $material['data'];
- }
- $data = [
- 'link_type' => $link_type,
- 'redirect_uri' => self::component_code_url($store_id),
- ];
- $app_name && $data['app_name'] = $app_name;
- $app_name && $data['app_icon'] = $app_icon;
- $res = self::component_http_post('openapi/v2/auth/gen_link', ['query' => self::http_component_query(), 'json' => $data]);
- if($res['code'] == 0 && $link_type == 2){
- cache()->set($cacheKey, $cacheV + 1, 86400);
- $res['api_times'] = $cacheV + 1;
- }else{
- $res['api_times'] = $cacheV;
- }
- return $res;
- }
- //三方获取授权小程序接口调用凭据
- public static function authorizer_access_token($store_id, $mini_id, $authorization_code) {
- $data = [
- 'authorization_code' => $authorization_code,
- 'grant_type' => 'app_to_tp_authorization_code',
- ];
- $res = self::component_http_get('openapi/v1/oauth/token', ['query' => self::http_component_query($data)]);
- if ($res['code'] == 0) {
- self::mini_conf($mini_id, array_merge($res['data'], ['store_id' => $store_id]));
- }
- return $res;
- }
- //三方找回小程序授权码
- public static function authorization_code($appid = '') {
- $data = [
- 'authorization_appid' => $appid,
- ];
- $res = self::component_http_post('openapi/v1/auth/retrieve', ['query' => self::http_component_query($data)]);
- return $res;
- }
- //授权小程序access_token刷新
- public static function authorizer_access_token_refresh($mini_id = 0, $appid = '') {
- self::$authorizer_access_token_refresh[$mini_id] = 1;
- $authorization_code = self::authorization_code($appid);
- if ($authorization_code['code'] != 0) {
- return $authorization_code;
- }
- $mini = StoreDouyinMini::findOne($mini_id);
- $authorizer_access_token = self::authorizer_access_token($mini['store_id'], $mini_id, $authorization_code['data']['authorization_code']);
- self::$authorizer_access_token_refresh[$mini_id] = 0;
- return $authorizer_access_token;
- }
- //小程序初始化
- public static function mini_app_init($mini_id = 0) {
- try {
- $cacheK = 'Douyin_mini_app_init_' . $mini_id;
- $cacheV = cache()->get($cacheK);
- if($cacheV){
- return;
- }
- cache()->set($cacheK, 1, 10);
- $site = [\Yii::$app->request->serverName];
- self::modify_server_domain($mini_id, 'add', $site, $site, $site, $site);
- self::modify_webview_domain($mini_id, 'add', [self::$webviewHostName]);
- $cate = self::categories($mini_id);
- //如果没有设置过类目
- if($cate['code'] == 0 && ($cate['data']['data']['remainingTimes'] == $cate['data']['data']['categoryChangeLimit'])){
- //工具 - 实用工具-记账
- // $categoryInfoInit = [
- // [
- // "category" => "188,189,190",
- // ]
- // ];
- // self::add_categories($mini_id, $categoryInfoInit);
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- }
- }
- //三方获取是否授权小程序
- public static function has_auth_app($appid = '') {
- $list = self::get_auth_app_list();
- if($list['code'] != 0){
- return $list;
- }
- $in_array = in_array($appid, $list['data']);
- if(!$in_array){
- return [
- 'code' => 1,
- 'msg' => '未授权',
- ];
- }
- return [
- 'code' => 0,
- 'msg' => '已授权',
- ];
- }
- /**
- * 获取店铺小程序列表
- * $simple 是否只获取简单数据
- */
- public static function mini_app_list($store_id = 0, $simple = 0, $search = []) {
- $query = StoreDouyinMini::find()->where(['store_id' => $store_id])->orderBy('id DESC');
- if($search['app_name']){
- $query->andWhere(['like', 'app_name', $search['app_name']]);
- }
- $data = pagination_make($query, false);
- $list = [];
- foreach($data['list'] as $item){
- $_list = $item->attributes;
- if(!$simple){
- $_has_auth_app = !self::has_auth_app($item['auth_app_id'])['code'];
- if($_has_auth_app){
- $appInfo = self::mini_app_info($item['id']);
- try {
- if($appInfo['code'] == 0){
- $minfo = $appInfo['data']['data'];
- $item->app_name = $minfo['app_name'];
- $item->app_logo = $minfo['app_icon'];
- $item->app_desc = $minfo['app_intro'];
- $item->save();
- }
- } catch (\Exception $e) {
- \Yii::error($e);
- }
- $_list = $item->attributes;
- $_list['_mini_app_info'] = $appInfo;
- $_list['_mini_versions'] = self::mini_versions($item['id']);
- }
- $_list['_has_auth_app'] = $_has_auth_app;
- }
- $list[] = $_list;
- }
- return [
- 'code' => 0,
- 'data' => $list,
- ];
- }
- //获取小程序基本信息
- public static function mini_app_info($mini_id = 0) {
- $res = self::component_http_get('openapi/v1/microapp/app/info', ['query' => self::http_component_app_query($mini_id)]);
- if($res['code'] == 0){
- $data = $res['data']['data'];
- $baseStatus = [];
- $baseStatus[] = $data['new_name_audit_info']['new_name_audit_state'];
- $baseStatus[] = $data['new_icon_audit_info']['new_icon_audit_state'];
- $baseStatus[] = $data['new_intro_audit_info']['new_intro_audit_state'];
-
- $res['data']['data']['baseStatus'] = 2;
- if(in_array(3, $baseStatus)){
- $res['data']['data']['baseStatus'] = 3;
- }
- if(in_array(1, $baseStatus)){
- $res['data']['data']['baseStatus'] = 1;
- }
- }
- return $res;
- }
- //获取二维码
- public static function mini_app_qrcode($mini_id = 0, $version = 'current', $path = '') {
- $data = [
- 'version' => $version,
- 'path' => $path,
- ];
- $res = self::component_http_post('openapi/v1/microapp/app/qrcode', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- //小程序名称检测
- public static function check_app_name($mini_id = 0, $app_name = '') {
- $data = [
- 'app_name' => $app_name,
- ];
- $res = self::component_http_get('openapi/v1/microapp/app/check_app_name', ['query' => self::http_component_app_query($mini_id, $data)]);
- return $res;
- }
- /***
- * 修改小程序名称
- * $material_file_path 小程序名称审核材料路径
- 注意:
- 1. 如果调用小程序名称检测接口,返回 21006 错误,则必须先调用代授权小程序上传资源接口上传证明。
- 2. 需要使用代授权小程序上传资源接口返回的路径才可以,接口调用时入参 material_type 字段为 3,否则报错。
- */
- public static function modify_app_name($mini_id = 0, $app_name = '', $material_file_path = '') {
- $check_app_name = self::check_app_name($mini_id, $app_name);
- if($check_app_name['code'] == 21006 && empty($material_file_path)){
- return $check_app_name;
- }
- if($material_file_path){
- $material = self::mini_upload_material($mini_id, 3, $material_file_path);
- if($material['code'] != 0){
- return $material;
- }
- $material_file_path = $material['data'];
- }
- $data = [
- 'new_name' => $app_name,
- ];
- $material_file_path && $data['material_file_path'] = $material_file_path;
- $res = self::component_http_post('openapi/v1/microapp/app/modify_app_name', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- //修改小程序简介
- public static function modify_app_intro($mini_id = 0, $new_intro = '') {
- $data = [
- 'new_intro' => $new_intro,
- ];
- $res = self::component_http_post('openapi/v1/microapp/app/modify_app_intro', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- /**
- * 修改小程序图标
- * $new_icon_path 授权小程序准备修改的图标路径
- 需要使用代授权小程序上传资源接口返回的路径才可以,接口调用时入参 material_type 字段为 1,否则报错。
- */
- public static function modify_app_icon($mini_id = 0, $new_icon_path = '') {
- if($new_icon_path){
- $material = self::mini_upload_material($mini_id, 1, $new_icon_path);
- if($material['code'] != 0){
- return $material;
- }
- $new_icon_path = $material['data'];
- }
- $data = [
- 'new_icon_path' => $new_icon_path,
- ];
- $res = self::component_http_post('openapi/v1/microapp/app/modify_app_icon', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- //修改小程序服务器域名
- public static function modify_server_domain($mini_id = 0, $action = 'get', $request = [], $socket = [], $upload = [], $download = []) {
- $data = [
- 'action' => $action,
- 'request' => $request,
- 'socket' => $socket,
- 'upload' => $upload,
- 'download' => $download,
- ];
- $res = self::component_http_post('openapi/v1/microapp/app/modify_server_domain', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- //修改小程序webview域名
- public static function modify_webview_domain($mini_id = 0, $action = 'get', $webview = []) {
- $data = [
- 'action' => $action,
- 'webview' => $webview,
- ];
- $res = self::component_http_post('openapi/v1/microapp/app/modify_webview_domain', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- /**
- * 获取可以设置的服务类目列表
- */
- public static function all_categories($mini_id = 0) {
- $res = self::component_http_get('openapi/v1/microapp/app/all_categories', ['query' => self::http_component_app_query($mini_id)]);
- return $res;
- }
- /**
- * 获取已设置的服务类目
- */
- public static function categories($mini_id = 0) {
- $res = self::component_http_get('openapi/v1/microapp/app/categories', ['query' => self::http_component_app_query($mini_id)]);
- return $res;
- }
- /**
- * 增加服务类目
- */
- public static function add_categories($mini_id = 0, $categoryInfo = []) {
- foreach($categoryInfo as &$item){
- if(isset($item['categoryCert']) && empty($item['categoryCert'])){
- unset($item['categoryCert']);
- }
- if($item['categoryCert']){
- foreach($item['categoryCert'] as $i => $cert){
- if(!empty($cert['path'])){
- $material = self::mini_upload_material($mini_id, 2, $cert['path']);
- if($material['code'] != 0){
- return $material;
- }
- $item['categoryCert'][$i]['path'] = $material['data'];
- }
- if(!empty($cert['pathList'])){
- foreach($cert['pathList'] as $k => $path){
- $material = self::mini_upload_material($mini_id, 2, $path);
- if($material['code'] != 0){
- return $material;
- }
- $item['categoryCert'][$i]['pathList'][$k] = $material['data'];
- }
- }
- }
- }
- }
- $data = [
- 'categoryInfo' => $categoryInfo,
- ];
- // var_dump($data);die;
- $res = self::component_http_post('openapi/v1/microapp/app/add_categories', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- /**
- * 删除服务类目
- */
- public static function del_categories($mini_id = 0, $categories = []) {
- $data = [
- 'categories' => $categories,
- ];
- $res = self::component_http_post('openapi/v1/microapp/app/del_categories', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- /**
- * 提交代码
- */
- public static function mini_upload($mini_id = 0, $template_id = '', $user_desc = '', $user_version = '') {
- $user_version || $user_version = date('y.md.Hi');
- $user_desc || $user_desc = 'desc';
- $conf = self::component_conf();
- $mini_conf = self::mini_conf($mini_id);
- $mini = StoreDouyinMini::findOne($mini_id);
- if(empty($template_id)){
- $template_id = $conf['template_id'];
- }
- $ext_json = [
- 'extEnable' => true,
- 'extAppid' => $mini_conf['authorizer_appid'],
- 'ext' => [
- 'store_id' => $mini['store_id'],
- 'mini_id' => $mini_id,
- "apiurl" => "https://" . \Yii::$app->request->hostName,
- ],
- ];
- $data = [
- 'template_id' => (int)$template_id,
- 'user_desc' => $user_desc,
- 'user_version' => $user_version,
- 'ext_json' => json_encode($ext_json),
- ];
- $res = self::component_http_post('openapi/v1/microapp/package/upload', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- /**
- * 提审代码
- */
- public static function mini_audit($mini_id = 0, $auditNote = '', $auditWay = 0) {
- $data = [
- 'hostNames' => ['douyin'],
- 'auditWay' => $auditWay,
- ];
- $auditNote && $data['auditNote'] = $auditNote;
- $res = self::component_http_post('openapi/v2/microapp/package/audit', ['query' => self::http_component_app_query($mini_id), 'json' => $data]);
- return $res;
- }
- /**
- * 撤回审核
- */
- public static function mini_revoke_audit($mini_id = 0) {
- $res = self::component_http_post('openapi/v1/microapp/package/revoke_audit', ['query' => self::http_component_app_query($mini_id)]);
- return $res;
- }
- /**
- * 发布代码
- */
- public static function mini_release($mini_id = 0) {
- $res = self::component_http_post('openapi/v1/microapp/package/release', ['query' => self::http_component_app_query($mini_id)]);
- return $res;
- }
- /**
- * 回退代码版本
- */
- public static function mini_rollback($mini_id = 0) {
- $res = self::component_http_post('openapi/v1/microapp/package/release', ['query' => self::http_component_app_query($mini_id)]);
- return $res;
- }
- /**
- * 获取小程序版本列表信息
- */
- public static function mini_versions($mini_id = 0) {
- $res = self::component_http_get('openapi/v1/microapp/package/versions', ['query' => self::http_component_app_query($mini_id)]);
- return $res;
- }
- }
|