| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\AggregateQrcode;
- use app\models\MerchantInfo;
- use app\models\Option;
- use app\models\StoreMedia;
- use app\models\StoreMini;
- use app\models\StoreSchedule;
- use app\models\WechatConfig;
- use app\modules\admin\models\file\FileForm;
- use app\utils\CurlHelper;
- use app\utils\WechatMerchant\Merchant;
- use EasyWeChat\Factory;
- use EasyWeChat\Kernel\BaseClient;
- use EasyWeChat\BasicService\Media\Client;
- use yii\base\Model;
- use yii\log\Logger;
- use app\modules\alliance\models\WechatThirdErrorMsgForm as ErrorMsg;
- class WechatThirdForm extends Model
- {
- /**
- * @var mixed
- */
- public $openPlatform;
- public $config;
- public $appid;
- public $data = [];
- public $base_url = "https://api.weixin.qq.com/cgi-bin/";
- public $name; // 企业名
- public $code; // 企业代码
- public $code_type; // 企业代码类型(1:统一社会信用代码, 2:组织机构代码,3:营业执照注册号)
- public $legal_persona_wechat; // 法人微信
- public $legal_persona_name; // 法人姓名
- public $component_phone; //第三方联系电话
- public $authorization_code;
- public $action; // 0获取 1添加 2删除 3覆盖
- public $requestdomain;
- public $wsrequestdomain;
- public $uploaddomain;
- public $downloaddomain;
- public $udpdomain;
- public $tcpdomai;
- public $webviewdomain;
- public $media_image;
- public $mini_nickname;
- public $id_card;
- public $license;
- public $naming_other_stuff_1;
- public $naming_other_stuff_2;
- public $naming_other_stuff_3;
- public $naming_other_stuff_4;
- public $naming_other_stuff_5;
- public $head_img_media_id; //上传头像 头像素材 media_id
- public $x1 = 0; //上传头像 裁剪框左上角 x 坐标(取值范围:[0, 1])
- public $y1 = 0; //上传头像 裁剪框左上角 y 坐标(取值范围:[0, 1])
- public $x2 = 1; //上传头像 裁剪框右下角 x 坐标(取值范围:[0, 1])
- public $y2 = 1; //上传头像 裁剪框右下角 y 坐标(取值范围:[0, 1])
- public $signature; //简介
- public $categories;
- public $cate_first;
- public $cate_second;
- public $user_version; //代码版本号
- public $user_desc = "小程序新版本上传"; //代码描述
- public $is_fast_audit = 0;
- public $privacy_ver = 2; //用户隐私保护指引的版本,1表示现网版本;2表示开发版。默认是2开发版。
- public $owner_setting;
- public $setting_list;
- public $file_url;
- public $file;
- public $mini_id;
- public $mini_path;
- public $item_list;
- public $feedback_info;
- public $feedback_stuff;
- public $is_use;
- public $license_pic;
- public $wechatId;
- public $status;
- public $merchant_id = 0;
- public $store_id;
- public $bind_store_id;
- public function rules()
- {
- return [
- [[
- 'name', 'code', 'legal_persona_wechat', 'legal_persona_name', 'component_phone', 'authorization_code', "action", "requestdomain", "wsrequestdomain", "uploaddomain", "downloaddomain", "udpdomain", "tcpdomai", "webviewdomai", "head_img_media_id", "x1", "y1", "x2", "y2", 'signature', 'certicates', 'media_image', 'mini_path', "feedback_info", 'is_use', 'wechatId', 'userstr', 'status', 'license_pic'
- ], 'string'],
- [['code_type', 'mini_id', 'merchant_id', 'store_id', 'bind_store_id'], 'integer'],
- [['file'], 'file'],
- [['item_list', 'feedback_stuff'], 'array']
- ];
- }
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->config = [
- 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
- 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
- 'token' => Option::get("platform_token", 0, 'saas')['value'],
- 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
- ];
- $this->store_id = !empty($this->store_id) ? $this->store_id : get_store_id();
- $this->openPlatform = Factory::openPlatform($this->config);
- }
- /**
- * mini业务
- */
- public function miniProgram($appid = "")
- {
- $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : $this->store_id;
- $store = StoreMini::find()->where(['appid' => $appid, 'store_id' => $store_id ?: get_store_id()])->one();
- if (empty($store)) {
- $store = StoreMini::find()->where(['or', ['id' => $this->mini_id], ['store_id' => $store_id ?: get_store_id()]])->orderBy("id desc")->one();
- }
-
-
- return $this->openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token);
- }
- /**
- * 获取授权方的帐号基本信息
- */
- public function api_get_authorizer_info()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store = StoreMini::findOne($this->mini_id);
- if (empty($store->appid)) {
- throw new \Exception("当前没有绑定小程序信息");
- }
- $res = $this->openPlatform->getAuthorizer($store->appid);
- if (empty($res->errcode) && !empty($res)) {
- $store = StoreMini::find()->where(['store_id' => $this->store_id])->one();
- $store->mini_url = $res->authorizer_info->qrcode_url;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($res->errmsg);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 快速注册小程序
- */
- public function fastRegisterMini()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $data = [
- 'name' => $this->name,
- 'code' => $this->code,
- 'code_type' => $this->code_type,
- 'legal_persona_wechat' => $this->legal_persona_wechat,
- 'legal_persona_name' => $this->legal_persona_name,
- 'component_phone' => Option::get("platform_phone", 0, 'saas')['value']
- ];
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- } else {
- $store_id = $this->store_id;
- }
- $store = StoreMini::findOne(['store_id' => $store_id]);
- if (empty($store)) {
- $store = new StoreMini();
- $store->store_id = $store_id;
- }
- $store->name = $this->name;
- $store->code = $this->code;
- $store->code_type = $this->code_type * 1;
- $store->legal_persona_wechat = $this->legal_persona_wechat;
- $store->legal_persona_name = $this->legal_persona_name;
- $store->license_pic = $this->license_pic;
- $store->merchant_id = $this->merchant_id;
- $res = $this->openPlatform->component->registerMiniProgram($data);
- if ($res['errcode'] == '89249') {
- $res = $this->openPlatform->component->getRegistrationStatus($this->name, $this->legal_persona_wechat, $this->legal_persona_name);
- if ($res['errcode'] == 89251) {
- $store->status = -1;
- $store->save();
- }
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- ];
- }
- if (empty($res['errcode']) && !empty($res)) {
- $store->status = -1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "申请成功,请等待审核",
- 'data' => $store->id
- ];
- } elseif (empty($res)) {
- $store->save();
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $store->save();
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- // $weChatUrl = $this->base_url."component/fastregisterweapp?action=create&component_access_token=".$this->data['component_access_token'];
- }
- /**
- * 手动查询快速创建任务状态
- */
- public function getRegisterStatus()
- {
- if (!empty($this->bind_store_id)) {
- $store_id = $this->bind_store_id;
- } else {
- $store_id = $this->store_id;
- }
- $store = StoreMini::findOne(['store_id' => $store_id]);
- if (!empty($store->name) && !empty($store->legal_persona_wechat) && !empty($store->legal_persona_name)) {
- $res = $this->openPlatform->component->getRegistrationStatus($store->name, $store->legal_persona_wechat, $store->legal_persona_name);
- \Yii::error($res);
- }
- }
- /**
- * 返回授权需要的参数
- */
- public function backAuthInfo()
- {
- $res = $this->openPlatform->getPreAuthorizationUrl(\Yii::$app->request->hostInfo . '/admin/#/saasChannel/applet', ['auth_type' => 2]);
- return [
- 'code' => 0,
- 'msg' => "请访问链接依次来授权",
- 'url' => $res
- ];
- }
- /**
- * 存储授权码
- */
- // public function setAuthorization_code(){
- // try {
- // $store = new StoreMini();
- // $store->store_id = get_store_id();
- // $store->auth_code = json_encode([
- // 'authorization_code'=>$this->authorization_code,
- // 'end_time'=>time()*1 + 3600*1
- // ]);
- // $store->save();
- // return $this->getAuthorization_info($this->authorization_code);
- // }catch (\Exception $e){
- // return [
- // 'code'=>1,
- // 'msg'=>$e->getMessage()
- // ];
- // }
- //
- //
- // }
- /**
- * 使用授权码获取授权信息
- */
- public function getAuthorization_info($authorization_code)
- {
- try {
- $res = $this->openPlatform->handleAuthorize($authorization_code);
- \Yii::error($res);
- if (empty($res['errcode']) && !empty($res)) {
- $store_id = $this->store_id;
- if ($store_id * 1 > 0 || !empty($this->mini_id)) {
- $store = StoreMini::find()->where(['appid' => $res['authorization_info']['authorizer_appid'], 'store_id' => $store_id])->one();
- if (empty($store)) {
- $store = new StoreMini();
- $store->appid = $res['authorization_info']['authorizer_appid'];
- $store->store_id = $store_id;
- }
- $store->is_cancle = 0;
- $store->auth_code = json_encode([
- 'authorization_code' => $authorization_code,
- 'end_time' => time() * 1 + 3600 * 1
- ]);
- $store->authorizer_access_token = json_encode([
- 'authorizer_access_token' => $res['authorization_info']['authorizer_access_token'],
- 'end_time' => $res['authorization_info']['expires_in'] * 1 + time()
- ]);
- $store->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
- $result = $store->save();
- \Yii::error($result);
- if ($result) {
- return $this->getMiniInfo($res['authorization_info']['authorizer_access_token'], $res['authorization_info']['authorizer_appid']);
- }
- }
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取小程序基础信息
- */
- public function getMiniInfo($authorizer_access_token = "", $appid = "")
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $store_id = $this->store_id;
- $store = StoreMini::findOne(['appid' => $appid, 'store_id' => $store_id]);
- if (empty($authorizer_access_token) && empty($store)) {
- $authorizer_access_token = $store->authorizer_access_token;
- if (empty($authorizer_access_token)) {
- throw new \Exception("暂未授权");
- }
- }
- $miniProgram = $this->miniProgram($appid);
- $res = $miniProgram->account->getBasicInfo();
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_info = json_encode($res);
- $store->mini_nickname = $res['nickname'];
- $store->signature = $res['signature_info']['signature'];
- $store->mini_url = $res['head_image_info']['head_image_url'];
- $res = $store->save();
- if ($res) {
- switch ($res['account_type']) {
- case 1:
- $res['account_type'] = "订阅号";
- break;
- case 2:
- $res['account_type'] = "服务号";
- break;
- case 3:
- $res['account_type'] = "小程序";
- break;
- }
- $t->commit();
- //将Appid填充系统设置
- $WechatConfig = WechatConfig::find()->where(['store_id' => $store_id])->one();
- if (empty($WechatConfig)) {
- $WechatConfig = new WechatConfig();
- $WechatConfig->store_id = $store_id;
- }
- $WechatConfig->app_id = $store->appid;
- $WechatConfig->app_secret = "0";
- $WechatConfig->name = $store->name ?: "";
- $WechatConfig->updated_at = time();
- if ($WechatConfig->save()) {
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $res,
- 'appid' => $appid
- ];
- } else {
- throw new \Exception(json_encode($WechatConfig->errors));
- }
- }
- throw new \Exception(json_encode($store->errors));
- } else {
- $t->rollBack();
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 设置获取修改服务器域名
- */
- public function setDomainName()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- $DomainData = "";
- $res = $this->setWebviewDomainName();
- if ($res['code'] == 1) {
- throw new \Exception($res['msg']);
- }
- $DomainData = $res;
- $miniProgram = $this->miniProgram($store->appid);
- $data = [
- 'action' => 'set',
- 'requestdomain' => ["https://" . \Yii::$app->request->hostName],
- "wsrequestdomain" => ["wss://" . \Yii::$app->request->hostName],
- "uploaddomain" => ["https://" . \Yii::$app->request->hostName],
- "downloaddomain" => ["https://" . \Yii::$app->request->hostName],
- "udpdomain" => ["udp://" . \Yii::$app->request->hostName],
- "tcpdomain" => ["tcp://" . \Yii::$app->request->hostName]
- ];
- $res = $miniProgram->domain->modify($data);
- if (empty($res['errcode']) && !empty($res)) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res,
- 'DomainData' => $DomainData
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '服务器域名错误' . $e->getMessage(),
- ];
- }
- }
- /**
- * @return array
- * 设置业务域名
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function setWebviewDomainName()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->domain->setWebviewDomain([
- 'https://' . \Yii::$app->request->hostName,
- 'https://' . \Yii::$app->params['ws_url'],
- ], 'set');
- if (empty($res['errcode']) && !empty($res)) {
- $store->is_set_web = 1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '业务域名错误' . $e->getMessage()
- ];
- }
- }
- /**
- * 暂停/开始使用
- */
- public function unbind()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- if ($this->is_use == 1 || empty($this->is_use)) {
- StoreMini::updateAll(['is_use' => 2], ['store_id' => $this->store_id]);
- $store->is_use = 1;
- } elseif ($this->is_use == 2) {
- $store->is_use = 2;
- }
- $res = $store->save();
- if ($res) {
- $store->save();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } else {
- throw new \Exception("数据库添加失败");
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 注:前端提交微信临时素材接口,获取素材ID
- * 设置小程序名称
- */
- public function setMiniNianname()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $media = new Client($miniProgram);
- $data = [
- 'id_card' => $this->id_card ? $this->id_card : '',
- 'license' => $this->license ? $this->license : $store->license_pic,
- 'naming_other_stuff_1' => $this->naming_other_stuff_1,
- 'naming_other_stuff_2' => $this->naming_other_stuff_2,
- 'naming_other_stuff_3' => $this->naming_other_stuff_3,
- 'naming_other_stuff_4' => $this->naming_other_stuff_4,
- 'naming_other_stuff_5' => $this->naming_other_stuff_5,
- ];
- // $this->addMedia($data);
- foreach ($data as $index => $item) {
- if (!empty($item)) {
- $head_img_media_id = $media->uploadImage($this->saveTempImage($item));
- if (empty($head_img_media_id['media_id'])) {
- throw new \Exception($this->getZnMsg($head_img_media_id));
- }
- $data[$index] = $head_img_media_id['media_id'];
- }
- }
- $id_card = $data['id_card'] ?? "";
- $license = $data['license'] ?? "";
- $res = $miniProgram->setting->setNickname($this->mini_nickname, $id_card, $license, [
- $data['naming_other_stuff_1'],
- $data['naming_other_stuff_2'],
- $data['naming_other_stuff_3'],
- $data['naming_other_stuff_4'],
- $data['naming_other_stuff_5']
- ]);
- $store->mini_nickname = $this->mini_nickname;
- if (empty($res['errcode']) && !empty($res)) {
- $store->nickname_audit_id = $res['audit_id'];
- $store->apply_name_status = 1;
- if (empty($res['audit_id'])) {
- $store->apply_name_status = 0;
- }
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "开始审核",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- $store->save();
- throw new \Exception("数据错误");
- } else {
- if ($res['errcode'] == 91019) {
- $store->apply_name_status = 1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "小程序审核中"
- ];
- }
-
- throw new \Exception($this->getZnMsg($res).'|'.$res['errcode']);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询名称审核状态
- */
- public function getNicknameAuditStatus()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store) || empty($store->nickname_audit_id)) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '数据错误或审核已经通过',
- 'status' => 3
- ];
- }
- $miniProgram = $this->miniProgram($store->appid);
- $result = $miniProgram->setting->getNicknameAuditStatus($store->nickname_audit_id);
- if (empty($result['errcode']) && !empty($result)) {
- $msg = "获取成功";
- switch ($result['audit_stat']) {
- case 0:
- $msg = "名称审核中";
- $store->apply_name_status = 1;
- break;
- case 1:
- $msg = "名称审核完成";
- $store->apply_name_status = 3;
- break;
- case 2:
- $msg = "名称审核失败";
- $store->apply_name_status = 2;
- $store->apply_name_error = $result['fail_reason'];
- break;
- }
- if (!$store->save()) {
- throw new \Exception("保存失败");
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => $msg,
- 'status' => $store->apply_name_status
- ];
- }
- throw new \Exception("查询失败");
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 增加Media事件
- */
- public function addMedia($data)
- {
- foreach ($data as $index => $item) {
- $mediaForm = StoreMedia::find()->where(['media_id' => $item])->one();
- if (empty($mediaForm)) {
- $mediaForm = new StoreMedia();
- $mediaForm->store_id = $this->store_id;
- $mediaForm->media_id = $item;
- $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
- }
- $mediaForm->event = $index;
- $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
- switch ($index) {
- case 'id_card':
- $mediaForm->desc = "身份证照片";
- break;
- case 'license':
- $mediaForm->desc = "组织机构代码证或营业执照";
- break;
- case 'naming_other_stuff_1':
- $mediaForm->desc = "其他证明材料1";
- break;
- case 'naming_other_stuff_2':
- $mediaForm->desc = "其他证明材料2";
- break;
- case 'naming_other_stuff_3':
- $mediaForm->desc = "其他证明材料3";
- break;
- case 'naming_other_stuff_4':
- $mediaForm->desc = "其他证明材料4";
- break;
- case 'naming_other_stuff_5':
- $mediaForm->desc = "其他证明材料5";
- break;
- case 'head_img_media_id':
- $mediaForm->desc = "头像素材";
- break;
- case "ext_file_media_id":
- $mediaForm->desc = "用户隐私指引";
- }
- $result = $mediaForm->save();
- }
- }
- /**
- * 微信认证名称检测
- */
- public function checkWxVerifyNickname()
- {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->isAvailableNickname($this->mini_nickname);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- /**
- * 修改头像
- */
- public function setHeadImage()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息失败");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $media = new Client($miniProgram);
- $head_img_media_id = $media->uploadThumb($this->saveTempImage($this->head_img_media_id));
- if (empty($head_img_media_id['thumb_media_id'])) {
- throw new \Exception($this->getZnMsg($head_img_media_id));
- }
- \Yii::error($head_img_media_id['thumb_media_id']);
- $res = $miniProgram->account->updateAvatar($head_img_media_id['thumb_media_id'], $this->x1, $this->y1, $this->x2, $this->y2);
- if ($res['errcode'] == '53202') {
- $store->mini_url = $this->head_img_media_id;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- ];
- }
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_url = $this->head_img_media_id;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 修改简介
- */
- public function setSignature()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取小程序信息错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->account->updateSignature($this->signature);
- if ($res['errcode'] == 53200) {
- $store->signature = $this->signature;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- }
- if (empty($res['errcode']) && !empty($res)) {
- $store->signature = $this->signature;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getdata($data, $id = [0])
- {
- foreach ($data as $k => $v) {
- if (in_array($v['id'], $id)) {
- $v['children'] = $this->getdata($data, $v['children']);
- $arr[] = $v;
- }
- }
- return $arr;
- }
- /**
- * @return array
- * 获取全部可选分类
- */
- public function getAllCategories()
- {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store->authorizer_access_token)) {
- return [
- 'code' => 1,
- 'msg' => "没有授权数据",
- 'data' => []
- ];
- }
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->getAllCategories();
- if (empty($res['errcode']) && !empty($res)) {
- $data = $res['categories_list']['categories'];
- $data = $this->getdata($data);
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $data,
- 'old_data' => $res['categories_list']['categories']
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- /**
- * @return array
- * 获取已有分类
- */
- public function getCategories()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,查询小程序信息失败");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->getCategories();
- if (empty($res['errcode']) && !empty($res)) {
- $store->categories = !empty($res['categories']) ? json_encode($res['categories']) : "";
- $store->save();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $res['categories']
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 添加类目
- */
- public function addCategory()
- {
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,查找不到小程序信息");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $data = $this->categories;
- $media = new Client($miniProgram);
- \Yii::error($data);
- foreach ($data as $index => &$item) {
- if(!empty($item['categories'])){
-
- foreach ($item['categories'] as $cateIndex => &$cate) {
- $url = $this->saveTempImage($cate['value']);
- $cate['value'] = $media->uploadImage($url)['media_id'];
- }
- }
- }
- $res = $miniProgram->setting->addCategories($data);
- if (empty($res['errcode']) && !empty($res)) {
- $cat_res = $this->getCategories();
- return [
- 'code' => 0,
- 'msg' => "设置成功",
- 'data' => $res,
- 'cat_data' => $cat_res['data']
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 0,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 删除类目
- */
- public function delCategory()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->setting->deleteCategories($this->cate_first, $this->cate_second);
- if (empty($res['errcode']) && !empty($res)) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "删除成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 获取代码模版列表
- */
- public function getTemplateList()
- {
- $res = $this->openPlatform->code_template->list(0);
- if (empty($res['errcode']) && !empty($res)) {
- $templateList = $res['template_list'];
- foreach ($templateList as &$item) {
- $item['c_time'] = date('Y-m-d H:i:s', $item['create_time']);
- $item['template_id'] = (string)$item['template_id'];
- }
- $last_names = array_column($templateList, 'create_time');
- array_multisort($last_names, SORT_ASC, $templateList);
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'template_list' => $templateList
- ]
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- /**
- * 上传小程序
- */
- public function upMini()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,获取不到小程序信息");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $ext = [
- 'extEnable' => true,
- 'extAppid' => $store->appid,
- 'ext' => [
- "apiurl" => "https://" . \Yii::$app->request->hostName,
- "store_id" => $store->store_id,
- "mini_id" => $store->id
- ],
- "window" => [
- "navigationBarTitleText" => "0000"
- ]
- ];
- $template_id = Option::get("platform_template_id", 0, 'saas')['value'];
- if ($template_id === "-1") {
- throw new \Exception("未选择模板");
- }
- $res = $this->setDomainName();
- if ($res['code'] == 1) {
- throw new \Exception($res['msg']);
- }
- $DomainData = $res;
- $res = $miniProgram->code->commit($template_id, json_encode($ext), cyy_version(), $this->user_desc);
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_up = -1;
- $store->template_id = $template_id;
- $store->user_version = cyy_version();
- $store->user_desc = $this->user_desc;
- $store->ext = json_encode($ext);
- $res = $store->save();
- if ($res) {
- $res = $this->getMiniQrcode($store->id);
- if ($res['code'] === 0) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res['data'],
- 'datas' => $DomainData
- ];
- } else {
- throw new \Exception("获取体验二维码失败");
- }
- } else {
- throw new \Exception("存储数据失败");
- }
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- /**
- * 获取已上传的代码的页面列表
- */
- public function getPageList()
- {
- $store = StoreMini::findOne($this->mini_id);
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->code->getPage();
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- /**
- * 添加体验者
- * @return array
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function addTester()
- {
- $store = StoreMini::findOne($this->mini_id);
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->tester->bind($this->wechatId);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- /**
- * 获取体验版二维码
- */
- public function getMiniQrcode($id = 0)
- {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $id = $id !== 0 ? $id : $this->mini_id;
- $store = StoreMini::findOne($id);
- }
- if (empty($store)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- }
- $miniProgram = $this->miniProgram($store->appid);
- $qrcode = \Yii::$app->basePath . "/web/temp/" . $this->store_id . "_" . date('YmdHis') . ".jpg";
- $res = $miniProgram->code->getQrCode($this->mini_path);
- if (empty(json_decode($res)->errcode)) {
- file_put_contents($qrcode, $res);
- $url = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $qrcode);
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $url
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res);
- return [
- 'code' => $res->errcode,
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- /**
- * 提交审核
- */
- public function submitAudit()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $result = $this->setPrivacySetting();
- if ($result['code'] === 0) {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,未找到对应的小程序信息");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $data = [
- 'item_list' => $this->item_list ?? [],
- 'order_path' => 'order/order/order',
- ];
- $res = $miniProgram->code->submitAudit($data, '', '');
- if (empty($res['errcode']) && !empty($res)) {
- //是否加急审核
- if ($this->is_fast_audit === 1) {
- $res = $miniProgram->code->speedupAudit($res['auditid']);
- }
- $store->mini_up = 1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } else {
- throw new \Exception($result['msg']);
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询最后一次提交审核的状态
- */
- public function lastAuditStatus()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $result = $miniProgram->code->getLatestAuditStatus();
- $msg = "返回成功";
- if (empty($result['errcode']) && !empty($result)) {
- switch ($result['status']) {
- case 0:
- $msg = "审核通过";
- $store->mini_up = 2;
- break;
- case 1:
- $msg = "审核已拒绝:" . $result['reason'];
- $store->mini_up = 3;
- $store->mini_up_error = $result['reason'];
- break;
- case 2:
- $msg = "审核中";
- $store->mini_up = 1;
- break;
- case 3:
- $msg = "未提交审核或已撤回";
- $store->mini_up = 0;
- break;
- }
- if (!$store->save()) {
- throw new \Exception("保存失败");
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => $msg
- ];
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 审核撤回
- */
- public function unDoCodeAudit()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- if (empty($store)) {
- throw new \Exception("数据错误,未查询到小程序信息");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $res = $miniProgram->code->withdrawAudit();
- if (empty($res['errcode']) && !empty($res)) {
- $store->mini_up = -1;
- $store->save();
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 发布已通过审核的小程序
- */
- public function release()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id();
- if (empty($store)) {
- throw new \Exception("数据错误,获取不到小程序信息");
- }
- $miniProgram = $this->miniProgram($store->appid);
- $this->unbind();
- $res = $miniProgram->code->release();
- if ((empty($res['errcode']) && !empty($res)) || $res['errcode'] == "85052") {
- $store->mini_up = 5;
- $res = $this->getMiniQr($store->id);
- if ($res['code'] === 0) {
- if ($store->save()) {
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res['data']
- ];
- } else {
- throw new \Exception("保存数据错误");
- }
- } else {
- throw new \Exception("获取二维码数据错误");
- }
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 设置小程序用户隐私保护指引
- */
- public function setPrivacySetting()
- {
- $data = [
- "privacy_ver" => $this->privacy_ver,
- "owner_setting" => [
- "contact_phone" => "17090402350",
- "notice_method" => "短信"
- ],
- "setting_list" => [
- [
- "privacy_key" => "UserInfo",
- "privacy_text" => "统计订单"
- ],
- [
- "privacy_key" => "Location",
- "privacy_text" => "显示附近自提点"
- ],
- [
- "privacy_key" => "Address",
- "privacy_text" => "显示附近自提点"
- ],
- [
- "privacy_key" => "RunData",
- "privacy_text" => "消费步数兑换商品"
- ],
- [
- "privacy_key" => "Record",
- "privacy_text" => "售后咨询"
- ],
- [
- "privacy_key" => "Camera",
- "privacy_text" => "提交订单备注资料"
- ],
- [
- "privacy_key" => "AlbumWriteOnly",
- "privacy_text" => "保存海报"
- ],
- [
- "privacy_key" => "PhoneNumber",
- "privacy_text" => "多端口数据统一"
- ],
- [
- "privacy_key" => "Album",
- "privacy_text" => "审核资质"
- ],
- [
- "privacy_key" =>"MessageFile",
- "privacy_text" => "订单评价拍照"
- ]
- ],
- ];
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- $client = new BaseClient($this->miniProgram($store->appid));
- $res = $client->httpPostJson('cgi-bin/component/setprivacysetting', $data);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res)
- ];
- }
- }
- /**
- * 查询小程序用户隐私保护指引
- */
- public function getPrivacySetting()
- {
- $data = [
- "privacy_ver" => $this->privacy_ver,
- ];
- if (!empty($this->bind_store_id)) {
- $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
- } else {
- $store = StoreMini::findOne($this->mini_id);
- }
- $client = new BaseClient($this->miniProgram($store->appid));
- $res = $client->httpPostJson('cgi-bin/component/getprivacysetting', $data);
- if (empty($res['errcode']) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- /**
- * 查询小程序码
- */
- public function getMiniQr($id = 0)
- {
- try {
- if (empty($id)) {
- $id = $this->mini_id;
- }
- $store = StoreMini::findOne($id);
- $miniProgram = $this->miniProgram($store->appid);
- $response = $miniProgram->app_code->getUnlimit("store=" . $this->store_id);
- $filename = md5(time()) . '.jpg';
- $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d');
- if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
- $resfilename = $response->saveAs($filePath, $filename);
- if (!empty($resfilename)) {
- $filePath = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $filePath . '/' . $filename);
- $store->qrcode = $filePath;
- $res = $store->save();
- if ($res) {
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $filePath
- ];
- } else {
- throw new \Exception("添加小程序码失败");
- }
- } else {
- throw new \Exception("数据错误");
- }
- } else {
- throw new \Exception("数据错误");
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 查询小程序
- */
- public function getMiniStatus()
- {
- $mini_info = StoreMini::find()->where(['appid' => $this->appid])->select("mini_url,mini_up,is_cancle,appid,status,msg")->asArray()->one();
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $mini_info
- ];
- }
- /**
- * 获取列表
- */
- public function getMiniList()
- {
- $store_id = $this->store_id;
- $query = StoreMini::find()->where(['store_id' => $store_id, 'is_cancle' => 0])->select("id,mini_url,mini_up,is_cancle,appid,status,msg,mini_info,is_use,mini_up_error,categories,mini_nickname,signature,qrcode");
- if (!empty($this->mini_nickname)) {
- $query->andWhere(['LIKE', 'mini_nickname', $this->mini_nickname]);
- }
- if (!empty($this->status)) {
- switch ($this->status) {
- case 1:
- $query->andWhere(['OR', ['mini_nickname' => ""], ['signature' => ""], ['mini_url' => ""]]);
- break;
- case 2:
- $query->andWhere(['AND', ['<>', 'mini_nickname', ""], ['<>', 'signature', ""], ['<>', 'mini_url', ""]]);
- break;
- case 3:
- $query->andWhere(['status' => 2]);
- break;
- case 4:
- $query->andWhere(['status' => -1]);
- break;
- }
- }
- if (!empty($this->is_use)) {
- $query->andWhere(['is_use' => $this->is_use]);
- }
- if (!empty($this->mini_nickname)) {
- $query->andWhere(['LIKE', 'mini_info', $this->mini_nickname]);
- }
- $pagination = pagination_make($query);
- $admins = $pagination['list'];
- foreach ($admins as $index => &$item) {
- \Yii::error($item);
- $item['principal_name'] = json_decode($item['mini_info'])->principal_name;
- $item['nickname'] = json_decode($item['mini_info'])->nickname;
- $item['is_finish'] = 0;
- if (!empty($item['categories'])) {
- $item['categories'] = json_decode($item['categories']);
- } elseif (empty($item['categories']) && !empty($item['appid'])) {
- $this->mini_id = $item['id'];
- $item['categories'] = $this->getCategories()['data'];
- }
- if (!empty($item['mini_info'])) {
- $item['mini_info'] = json_decode($item['mini_info']);
- }
- $item['qr'] = false;
- if (!empty($item['qrcode'])) {
- $item['qr'] = $item['qrcode'];
- }
- $item['user_version'] = cyy_version();
- $item['qrcode'] = '';
- if (!empty($item['appid'])) {
- $res = $this->getQrcodeRules($item['appid']);
- \Yii::error($res);
- $miniInfo = $this->getMiniInfo("", $item['appid']);
- if ($miniInfo["code"] === 0) {
- \Yii::error($miniInfo);
- if (!empty($item['signature']) && !empty($item['mini_url']) && !empty($item['mini_nickname'])) {
- $item['is_finish'] = 1;
- }
- }
- $res = $this->getMiniQrcode($item['id']);
- if ($res['code'] == 0) {
- $item['qrcode'] = $res['data'];
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $admins,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ];
- }
- /**
- * 获取单个
- */
- public function getMini()
- {
- $query = StoreMini::find()->where(['id' => $this->mini_id])->select("id,name,code,code_type,legal_persona_wechat,legal_persona_name,status,msg,appid,mini_up,mini_url,is_cancle,mini_up_error,mini_info,apply_name_status,apply_name_error,is_use,signature,categories,");
- $data = $query->asArray()->one();
- if (!empty(json_decode($data['mini_info']))) {
- $data['principal_name'] = json_decode($data['mini_info'])->principal_name;
- $data['nickname'] = json_decode($data['mini_info'])->nickname;
- $data['mini_url'] = $data['mini_url'] ?? json_decode($data['mini_info'])->head_image_info->head_image_url;
- $data['categories'] = [];
- if ($this->getCategories()['code'] === 0) {
- $data['categories'] = $this->getCategories()['data'];
- }
- }
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => $data
- ];
- }
- //添加二维码规则
- public function setQrcodeRules($id, $appid)
- {
- try {
- $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->asArray()->one();
- $data = [
- 'prefix' => $qrcode['param_url'],
- 'permit_sub_rule' => 1,
- 'path' => $qrcode['wx_url'],
- 'open_version' => 2,
- 'debug_url' => [
- $qrcode['param_url']
- ],
- 'is_edit' => 0
- ];
- \Yii::error($data);
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
- \Yii::error($res);
- if ((empty($res['errcode']) && !empty($res)) || ($res['errcode'] == "85071")) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- throw new \Exception($this->getZnMsg($res));
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //发布二维码规则
- public function submitQrcodeRules($id, $appid)
- {
- $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->one();
- if (empty($qrcode->param_url)) {
- return [
- 'code' => 1,
- 'msg' => '参数错误'
- ];
- }
- $data = [
- 'prefix' => $qrcode->param_url
- ];
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
- if (empty($res->errcode) && !empty($res)) {
- $qrcode->wx_status = 1;
- $res = $qrcode->save();
- if (!$res) {
- throw new \Exception('保存数据失败,二维码规则发布生成');
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- //获取二维码规则
- public function getQrcodeRules($appid)
- {
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpget');
- if (empty($res->errcode) && !empty($res)) {
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res
- ];
- } elseif (empty($res)) {
- return [
- 'code' => 1,
- 'msg' => "数据错误"
- ];
- } else {
- $res = json_decode($res);
- return [
- 'code' => $res['errcode'],
- 'msg' => $this->getZnMsg($res),
- 'data' => $res
- ];
- }
- }
- //下载文件
- public function downQrcodeRules($id, $appid)
- {
- try {
- $client = new BaseClient($this->miniProgram($appid));
- $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
- if (empty($res['errcode']) && !empty($res)) {
- if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) {
- // dir doesn't exist, make it
- mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true);
- }
- $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $res['file_name'], $res['file_content']);
- if ($file_result) {
- $resRules = $this->setQrcodeRules($id, $appid);
- if ($resRules['code'] === 0) {
- $ress = $this->submitQrcodeRules($id, $appid);
- \Yii::error($ress);
- if ($ress['code'] === 0) {
- $resGet = $this->getQrcodeRules($appid);
- \Yii::error($resGet);
- } else {
- throw new \Exception($ress['msg']);
- }
- } else {
- throw new \Exception($resRules['msg']);
- }
- } else {
- throw new \Exception("创建文件失败");
- }
- return [
- 'code' => 0,
- 'msg' => "成功",
- 'data' => $res,
- 'file' => $file_result
- ];
- } elseif (empty($res)) {
- throw new \Exception("数据错误");
- } else {
- //$res = json_decode($res);
- throw new \Exception($res['errmsg']);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //微信支付进件
- public function mchRegister()
- {
- $mini = StoreMini::find()->where(['id' => $this->mini_id])->select('id,appid')->one();
- if (empty($mini->appid)) {
- return [
- 'code' => 0,
- 'msg' => "未找到对应的小程序信息"
- ];
- }
- $merchant_info = MerchantInfo::find()->where(['bind_store_id' => get_store_id(), 'is_delete' => 0])->one();
- if (empty($merchant_info)) {
- $StoreSchedule = StoreSchedule::find()->where(['store_id' => get_store_id()])->one();
- if (!empty($StoreSchedule->merchant_info)) {
- $merchant_info = json_decode($StoreSchedule->merchant_info, true);
- $contact_info = $merchant_info['contact_info'];
- $subject_info = $merchant_info['subject_info'];
- $business_info = $merchant_info['business_info'];
- $bank_account_info = $merchant_info['bank_account_info'];
- } else {
- return [
- 'code' => 1,
- 'msg' => "未找到对应的进件数据"
- ];
- }
- } else {
- $contact_info = !empty($merchant_info->contact_info) ? json_decode($merchant_info->contact_info, true) : "";
- $subject_info = !empty($merchant_info->subject_info) ? json_decode($merchant_info->subject_info, true) : "";
- $business_info = !empty($merchant_info->business_info) ? json_decode($merchant_info->business_info, true) : "";
- $bank_account_info = !empty($merchant_info->bank_account_info) ? json_decode($merchant_info->bank_account_info, true) : "";
- }
- $Merchant = new Merchant();
- return $Merchant->submit($contact_info, $subject_info, $business_info, $bank_account_info, !empty($merchant_info->id) ? $merchant_info->id : 0, $mini->appid);
- }
- //获取网络图片到临时目录
- private 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';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- //返回中文错误信息
- public function getZnMsg($result)
- {
- $ErrorMsg = new ErrorMsg();
- $arr = $ErrorMsg->getArray();
- $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
- return $msg;
- }
- }
|