WechatThirdForm.php 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\AggregateQrcode;
  9. use app\models\MerchantInfo;
  10. use app\models\Option;
  11. use app\models\StoreMedia;
  12. use app\models\StoreMini;
  13. use app\models\StoreSchedule;
  14. use app\models\WechatConfig;
  15. use app\modules\admin\models\file\FileForm;
  16. use app\utils\CurlHelper;
  17. use app\utils\WechatMerchant\Merchant;
  18. use EasyWeChat\Factory;
  19. use EasyWeChat\Kernel\BaseClient;
  20. use EasyWeChat\BasicService\Media\Client;
  21. use yii\base\Model;
  22. use yii\log\Logger;
  23. use app\modules\alliance\models\WechatThirdErrorMsgForm as ErrorMsg;
  24. class WechatThirdForm extends Model
  25. {
  26. /**
  27. * @var mixed
  28. */
  29. public $openPlatform;
  30. public $config;
  31. public $appid;
  32. public $data = [];
  33. public $base_url = "https://api.weixin.qq.com/cgi-bin/";
  34. public $name; // 企业名
  35. public $code; // 企业代码
  36. public $code_type; // 企业代码类型(1:统一社会信用代码, 2:组织机构代码,3:营业执照注册号)
  37. public $legal_persona_wechat; // 法人微信
  38. public $legal_persona_name; // 法人姓名
  39. public $component_phone; //第三方联系电话
  40. public $authorization_code;
  41. public $action; // 0获取 1添加 2删除 3覆盖
  42. public $requestdomain;
  43. public $wsrequestdomain;
  44. public $uploaddomain;
  45. public $downloaddomain;
  46. public $udpdomain;
  47. public $tcpdomai;
  48. public $webviewdomain;
  49. public $media_image;
  50. public $mini_nickname;
  51. public $id_card;
  52. public $license;
  53. public $naming_other_stuff_1;
  54. public $naming_other_stuff_2;
  55. public $naming_other_stuff_3;
  56. public $naming_other_stuff_4;
  57. public $naming_other_stuff_5;
  58. public $head_img_media_id; //上传头像 头像素材 media_id
  59. public $x1 = 0; //上传头像 裁剪框左上角 x 坐标(取值范围:[0, 1])
  60. public $y1 = 0; //上传头像 裁剪框左上角 y 坐标(取值范围:[0, 1])
  61. public $x2 = 1; //上传头像 裁剪框右下角 x 坐标(取值范围:[0, 1])
  62. public $y2 = 1; //上传头像 裁剪框右下角 y 坐标(取值范围:[0, 1])
  63. public $signature; //简介
  64. public $categories;
  65. public $cate_first;
  66. public $cate_second;
  67. public $user_version; //代码版本号
  68. public $user_desc = "小程序新版本上传"; //代码描述
  69. public $is_fast_audit = 0;
  70. public $privacy_ver = 2; //用户隐私保护指引的版本,1表示现网版本;2表示开发版。默认是2开发版。
  71. public $owner_setting;
  72. public $setting_list;
  73. public $file_url;
  74. public $file;
  75. public $mini_id;
  76. public $mini_path;
  77. public $item_list;
  78. public $feedback_info;
  79. public $feedback_stuff;
  80. public $is_use;
  81. public $license_pic;
  82. public $wechatId;
  83. public $status;
  84. public $merchant_id = 0;
  85. public $store_id;
  86. public $bind_store_id;
  87. public function rules()
  88. {
  89. return [
  90. [[
  91. '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'
  92. ], 'string'],
  93. [['code_type', 'mini_id', 'merchant_id', 'store_id', 'bind_store_id'], 'integer'],
  94. [['file'], 'file'],
  95. [['item_list', 'feedback_stuff'], 'array']
  96. ];
  97. }
  98. public function __construct($config = [])
  99. {
  100. parent::__construct($config);
  101. $this->config = [
  102. 'app_id' => Option::get("platform_third_appid", 0, 'saas')['value'],
  103. 'secret' => Option::get("platform_third_secret", 0, 'saas')['value'],
  104. 'token' => Option::get("platform_token", 0, 'saas')['value'],
  105. 'aes_key' => Option::get("platform_encodingAesKey", 0, 'saas')['value']
  106. ];
  107. $this->store_id = !empty($this->store_id) ? $this->store_id : get_store_id();
  108. $this->openPlatform = Factory::openPlatform($this->config);
  109. }
  110. /**
  111. * mini业务
  112. */
  113. public function miniProgram($appid = "")
  114. {
  115. $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : $this->store_id;
  116. $store = StoreMini::find()->where(['appid' => $appid, 'store_id' => $store_id ?: get_store_id()])->one();
  117. if (empty($store)) {
  118. $store = StoreMini::find()->where(['or', ['id' => $this->mini_id], ['store_id' => $store_id ?: get_store_id()]])->orderBy("id desc")->one();
  119. }
  120. return $this->openPlatform->miniProgram($store->appid, $store->authorizer_refresh_token);
  121. }
  122. /**
  123. * 获取授权方的帐号基本信息
  124. */
  125. public function api_get_authorizer_info()
  126. {
  127. $t = \Yii::$app->db->beginTransaction();
  128. try {
  129. $store = StoreMini::findOne($this->mini_id);
  130. if (empty($store->appid)) {
  131. throw new \Exception("当前没有绑定小程序信息");
  132. }
  133. $res = $this->openPlatform->getAuthorizer($store->appid);
  134. if (empty($res->errcode) && !empty($res)) {
  135. $store = StoreMini::find()->where(['store_id' => $this->store_id])->one();
  136. $store->mini_url = $res->authorizer_info->qrcode_url;
  137. $store->save();
  138. $t->commit();
  139. return [
  140. 'code' => 0,
  141. 'msg' => "获取成功",
  142. 'data' => $res
  143. ];
  144. } elseif (empty($res)) {
  145. throw new \Exception("数据错误");
  146. } else {
  147. throw new \Exception($res->errmsg);
  148. }
  149. } catch (\Exception $e) {
  150. $t->rollBack();
  151. return [
  152. 'code' => 1,
  153. 'msg' => $e->getMessage()
  154. ];
  155. }
  156. }
  157. /**
  158. * 快速注册小程序
  159. */
  160. public function fastRegisterMini()
  161. {
  162. $t = \Yii::$app->db->beginTransaction();
  163. try {
  164. $data = [
  165. 'name' => $this->name,
  166. 'code' => $this->code,
  167. 'code_type' => $this->code_type,
  168. 'legal_persona_wechat' => $this->legal_persona_wechat,
  169. 'legal_persona_name' => $this->legal_persona_name,
  170. 'component_phone' => Option::get("platform_phone", 0, 'saas')['value']
  171. ];
  172. if (!empty($this->bind_store_id)) {
  173. $store_id = $this->bind_store_id;
  174. } else {
  175. $store_id = $this->store_id;
  176. }
  177. $store = StoreMini::findOne(['store_id' => $store_id]);
  178. if (empty($store)) {
  179. $store = new StoreMini();
  180. $store->store_id = $store_id;
  181. }
  182. $store->name = $this->name;
  183. $store->code = $this->code;
  184. $store->code_type = $this->code_type * 1;
  185. $store->legal_persona_wechat = $this->legal_persona_wechat;
  186. $store->legal_persona_name = $this->legal_persona_name;
  187. $store->license_pic = $this->license_pic;
  188. $store->merchant_id = $this->merchant_id;
  189. $res = $this->openPlatform->component->registerMiniProgram($data);
  190. if ($res['errcode'] == '89249') {
  191. $res = $this->openPlatform->component->getRegistrationStatus($this->name, $this->legal_persona_wechat, $this->legal_persona_name);
  192. if ($res['errcode'] == 89251) {
  193. $store->status = -1;
  194. $store->save();
  195. }
  196. return [
  197. 'code' => $res['errcode'],
  198. 'msg' => $this->getZnMsg($res),
  199. ];
  200. }
  201. if (empty($res['errcode']) && !empty($res)) {
  202. $store->status = -1;
  203. $store->save();
  204. $t->commit();
  205. return [
  206. 'code' => 0,
  207. 'msg' => "申请成功,请等待审核",
  208. 'data' => $store->id
  209. ];
  210. } elseif (empty($res)) {
  211. $store->save();
  212. return [
  213. 'code' => 1,
  214. 'msg' => "数据错误"
  215. ];
  216. } else {
  217. $store->save();
  218. throw new \Exception($this->getZnMsg($res));
  219. }
  220. } catch (\Exception $e) {
  221. $t->rollBack();
  222. return [
  223. 'code' => 1,
  224. 'msg' => $e->getMessage(),
  225. ];
  226. }
  227. // $weChatUrl = $this->base_url."component/fastregisterweapp?action=create&component_access_token=".$this->data['component_access_token'];
  228. }
  229. /**
  230. * 手动查询快速创建任务状态
  231. */
  232. public function getRegisterStatus()
  233. {
  234. if (!empty($this->bind_store_id)) {
  235. $store_id = $this->bind_store_id;
  236. } else {
  237. $store_id = $this->store_id;
  238. }
  239. $store = StoreMini::findOne(['store_id' => $store_id]);
  240. if (!empty($store->name) && !empty($store->legal_persona_wechat) && !empty($store->legal_persona_name)) {
  241. $res = $this->openPlatform->component->getRegistrationStatus($store->name, $store->legal_persona_wechat, $store->legal_persona_name);
  242. \Yii::error($res);
  243. }
  244. }
  245. /**
  246. * 返回授权需要的参数
  247. */
  248. public function backAuthInfo()
  249. {
  250. $res = $this->openPlatform->getPreAuthorizationUrl(\Yii::$app->request->hostInfo . '/admin/#/saasChannel/applet', ['auth_type' => 2]);
  251. return [
  252. 'code' => 0,
  253. 'msg' => "请访问链接依次来授权",
  254. 'url' => $res
  255. ];
  256. }
  257. /**
  258. * 存储授权码
  259. */
  260. // public function setAuthorization_code(){
  261. // try {
  262. // $store = new StoreMini();
  263. // $store->store_id = get_store_id();
  264. // $store->auth_code = json_encode([
  265. // 'authorization_code'=>$this->authorization_code,
  266. // 'end_time'=>time()*1 + 3600*1
  267. // ]);
  268. // $store->save();
  269. // return $this->getAuthorization_info($this->authorization_code);
  270. // }catch (\Exception $e){
  271. // return [
  272. // 'code'=>1,
  273. // 'msg'=>$e->getMessage()
  274. // ];
  275. // }
  276. //
  277. //
  278. // }
  279. /**
  280. * 使用授权码获取授权信息
  281. */
  282. public function getAuthorization_info($authorization_code)
  283. {
  284. try {
  285. $res = $this->openPlatform->handleAuthorize($authorization_code);
  286. \Yii::error($res);
  287. if (empty($res['errcode']) && !empty($res)) {
  288. $store_id = $this->store_id;
  289. if ($store_id * 1 > 0 || !empty($this->mini_id)) {
  290. $store = StoreMini::find()->where(['appid' => $res['authorization_info']['authorizer_appid'], 'store_id' => $store_id])->one();
  291. if (empty($store)) {
  292. $store = new StoreMini();
  293. $store->appid = $res['authorization_info']['authorizer_appid'];
  294. $store->store_id = $store_id;
  295. }
  296. $store->is_cancle = 0;
  297. $store->auth_code = json_encode([
  298. 'authorization_code' => $authorization_code,
  299. 'end_time' => time() * 1 + 3600 * 1
  300. ]);
  301. $store->authorizer_access_token = json_encode([
  302. 'authorizer_access_token' => $res['authorization_info']['authorizer_access_token'],
  303. 'end_time' => $res['authorization_info']['expires_in'] * 1 + time()
  304. ]);
  305. $store->authorizer_refresh_token = $res['authorization_info']['authorizer_refresh_token'];
  306. $result = $store->save();
  307. \Yii::error($result);
  308. if ($result) {
  309. return $this->getMiniInfo($res['authorization_info']['authorizer_access_token'], $res['authorization_info']['authorizer_appid']);
  310. }
  311. }
  312. } else {
  313. throw new \Exception($this->getZnMsg($res));
  314. }
  315. } catch (\Exception $e) {
  316. return [
  317. 'code' => 1,
  318. 'msg' => $e->getMessage(),
  319. ];
  320. }
  321. }
  322. /**
  323. * 获取小程序基础信息
  324. */
  325. public function getMiniInfo($authorizer_access_token = "", $appid = "")
  326. {
  327. $t = \Yii::$app->db->beginTransaction();
  328. try {
  329. $store_id = $this->store_id;
  330. $store = StoreMini::findOne(['appid' => $appid, 'store_id' => $store_id]);
  331. if (empty($authorizer_access_token) && empty($store)) {
  332. $authorizer_access_token = $store->authorizer_access_token;
  333. if (empty($authorizer_access_token)) {
  334. throw new \Exception("暂未授权");
  335. }
  336. }
  337. $miniProgram = $this->miniProgram($appid);
  338. $res = $miniProgram->account->getBasicInfo();
  339. if (empty($res['errcode']) && !empty($res)) {
  340. $store->mini_info = json_encode($res);
  341. $store->mini_nickname = $res['nickname'];
  342. $store->signature = $res['signature_info']['signature'];
  343. $store->mini_url = $res['head_image_info']['head_image_url'];
  344. $res = $store->save();
  345. if ($res) {
  346. switch ($res['account_type']) {
  347. case 1:
  348. $res['account_type'] = "订阅号";
  349. break;
  350. case 2:
  351. $res['account_type'] = "服务号";
  352. break;
  353. case 3:
  354. $res['account_type'] = "小程序";
  355. break;
  356. }
  357. $t->commit();
  358. //将Appid填充系统设置
  359. $WechatConfig = WechatConfig::find()->where(['store_id' => $store_id])->one();
  360. if (empty($WechatConfig)) {
  361. $WechatConfig = new WechatConfig();
  362. $WechatConfig->store_id = $store_id;
  363. }
  364. $WechatConfig->app_id = $store->appid;
  365. $WechatConfig->app_secret = "0";
  366. $WechatConfig->name = $store->name ?: "";
  367. $WechatConfig->updated_at = time();
  368. if ($WechatConfig->save()) {
  369. return [
  370. 'code' => 0,
  371. 'msg' => "获取成功",
  372. 'data' => $res,
  373. 'appid' => $appid
  374. ];
  375. } else {
  376. throw new \Exception(json_encode($WechatConfig->errors));
  377. }
  378. }
  379. throw new \Exception(json_encode($store->errors));
  380. } else {
  381. $t->rollBack();
  382. return [
  383. 'code' => $res['errcode'],
  384. 'msg' => $this->getZnMsg($res),
  385. ];
  386. }
  387. } catch (\Exception $e) {
  388. return [
  389. 'code' => 1,
  390. 'msg' => $e->getMessage(),
  391. ];
  392. }
  393. }
  394. /**
  395. * 设置获取修改服务器域名
  396. */
  397. public function setDomainName()
  398. {
  399. $t = \Yii::$app->db->beginTransaction();
  400. try {
  401. if (!empty($this->bind_store_id)) {
  402. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  403. } else {
  404. $store = StoreMini::findOne($this->mini_id);
  405. }
  406. if (empty($store)) {
  407. throw new \Exception("数据错误,获取小程序信息失败");
  408. }
  409. $DomainData = "";
  410. $res = $this->setWebviewDomainName();
  411. if ($res['code'] == 1) {
  412. throw new \Exception($res['msg']);
  413. }
  414. $DomainData = $res;
  415. $miniProgram = $this->miniProgram($store->appid);
  416. $data = [
  417. 'action' => 'set',
  418. 'requestdomain' => ["https://" . \Yii::$app->request->hostName],
  419. "wsrequestdomain" => ["wss://" . \Yii::$app->request->hostName],
  420. "uploaddomain" => ["https://" . \Yii::$app->request->hostName],
  421. "downloaddomain" => ["https://" . \Yii::$app->request->hostName],
  422. "udpdomain" => ["udp://" . \Yii::$app->request->hostName],
  423. "tcpdomain" => ["tcp://" . \Yii::$app->request->hostName]
  424. ];
  425. $res = $miniProgram->domain->modify($data);
  426. if (empty($res['errcode']) && !empty($res)) {
  427. $t->commit();
  428. return [
  429. 'code' => 0,
  430. 'msg' => "设置成功",
  431. 'data' => $res,
  432. 'DomainData' => $DomainData
  433. ];
  434. } elseif (empty($res)) {
  435. throw new \Exception("数据错误");
  436. } else {
  437. throw new \Exception($this->getZnMsg($res));
  438. }
  439. } catch (\Exception $e) {
  440. $t->rollBack();
  441. return [
  442. 'code' => 1,
  443. 'msg' => '服务器域名错误' . $e->getMessage(),
  444. ];
  445. }
  446. }
  447. /**
  448. * @return array
  449. * 设置业务域名
  450. * @throws \GuzzleHttp\Exception\GuzzleException
  451. */
  452. public function setWebviewDomainName()
  453. {
  454. $t = \Yii::$app->db->beginTransaction();
  455. try {
  456. if (!empty($this->bind_store_id)) {
  457. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  458. } else {
  459. $store = StoreMini::findOne($this->mini_id);
  460. }
  461. if (empty($store)) {
  462. throw new \Exception("数据错误,获取小程序信息失败");
  463. }
  464. $miniProgram = $this->miniProgram($store->appid);
  465. $res = $miniProgram->domain->setWebviewDomain([
  466. 'https://' . \Yii::$app->request->hostName,
  467. 'https://' . \Yii::$app->params['ws_url'],
  468. ], 'set');
  469. if (empty($res['errcode']) && !empty($res)) {
  470. $store->is_set_web = 1;
  471. $store->save();
  472. $t->commit();
  473. return [
  474. 'code' => 0,
  475. 'msg' => "设置成功",
  476. 'data' => $res
  477. ];
  478. } elseif (empty($res)) {
  479. throw new \Exception("数据错误");
  480. } else {
  481. throw new \Exception($this->getZnMsg($res));
  482. }
  483. } catch (\Exception $e) {
  484. $t->rollBack();
  485. return [
  486. 'code' => 1,
  487. 'msg' => '业务域名错误' . $e->getMessage()
  488. ];
  489. }
  490. }
  491. /**
  492. * 暂停/开始使用
  493. */
  494. public function unbind()
  495. {
  496. try {
  497. if (!empty($this->bind_store_id)) {
  498. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  499. } else {
  500. $store = StoreMini::findOne($this->mini_id);
  501. }
  502. if (empty($store)) {
  503. throw new \Exception("数据错误,获取小程序信息失败");
  504. }
  505. if ($this->is_use == 1 || empty($this->is_use)) {
  506. StoreMini::updateAll(['is_use' => 2], ['store_id' => $this->store_id]);
  507. $store->is_use = 1;
  508. } elseif ($this->is_use == 2) {
  509. $store->is_use = 2;
  510. }
  511. $res = $store->save();
  512. if ($res) {
  513. $store->save();
  514. return [
  515. 'code' => 0,
  516. 'msg' => "设置成功",
  517. 'data' => $res
  518. ];
  519. } else {
  520. throw new \Exception("数据库添加失败");
  521. }
  522. } catch (\Exception $e) {
  523. return [
  524. 'code' => 1,
  525. 'msg' => $e->getMessage()
  526. ];
  527. }
  528. }
  529. /**
  530. * 注:前端提交微信临时素材接口,获取素材ID
  531. * 设置小程序名称
  532. */
  533. public function setMiniNianname()
  534. {
  535. $t = \Yii::$app->db->beginTransaction();
  536. try {
  537. if (!empty($this->bind_store_id)) {
  538. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  539. } else {
  540. $store = StoreMini::findOne($this->mini_id);
  541. }
  542. if (empty($store)) {
  543. throw new \Exception("数据错误,获取小程序信息失败");
  544. }
  545. $miniProgram = $this->miniProgram($store->appid);
  546. $media = new Client($miniProgram);
  547. $data = [
  548. 'id_card' => $this->id_card ? $this->id_card : '',
  549. 'license' => $this->license ? $this->license : $store->license_pic,
  550. 'naming_other_stuff_1' => $this->naming_other_stuff_1,
  551. 'naming_other_stuff_2' => $this->naming_other_stuff_2,
  552. 'naming_other_stuff_3' => $this->naming_other_stuff_3,
  553. 'naming_other_stuff_4' => $this->naming_other_stuff_4,
  554. 'naming_other_stuff_5' => $this->naming_other_stuff_5,
  555. ];
  556. // $this->addMedia($data);
  557. foreach ($data as $index => $item) {
  558. if (!empty($item)) {
  559. $head_img_media_id = $media->uploadImage($this->saveTempImage($item));
  560. if (empty($head_img_media_id['media_id'])) {
  561. throw new \Exception($this->getZnMsg($head_img_media_id));
  562. }
  563. $data[$index] = $head_img_media_id['media_id'];
  564. }
  565. }
  566. $id_card = $data['id_card'] ?? "";
  567. $license = $data['license'] ?? "";
  568. $res = $miniProgram->setting->setNickname($this->mini_nickname, $id_card, $license, [
  569. $data['naming_other_stuff_1'],
  570. $data['naming_other_stuff_2'],
  571. $data['naming_other_stuff_3'],
  572. $data['naming_other_stuff_4'],
  573. $data['naming_other_stuff_5']
  574. ]);
  575. $store->mini_nickname = $this->mini_nickname;
  576. if (empty($res['errcode']) && !empty($res)) {
  577. $store->nickname_audit_id = $res['audit_id'];
  578. $store->apply_name_status = 1;
  579. if (empty($res['audit_id'])) {
  580. $store->apply_name_status = 0;
  581. }
  582. $store->save();
  583. $t->commit();
  584. return [
  585. 'code' => 0,
  586. 'msg' => "开始审核",
  587. 'data' => $res
  588. ];
  589. } elseif (empty($res)) {
  590. $store->save();
  591. throw new \Exception("数据错误");
  592. } else {
  593. if ($res['errcode'] == 91019) {
  594. $store->apply_name_status = 1;
  595. $store->save();
  596. $t->commit();
  597. return [
  598. 'code' => 0,
  599. 'msg' => "小程序审核中"
  600. ];
  601. }
  602. throw new \Exception($this->getZnMsg($res).'|'.$res['errcode']);
  603. }
  604. } catch (\Exception $e) {
  605. $t->rollBack();
  606. return [
  607. 'code' => 1,
  608. 'msg' => $e->getMessage()
  609. ];
  610. }
  611. }
  612. /**
  613. * 查询名称审核状态
  614. */
  615. public function getNicknameAuditStatus()
  616. {
  617. $t = \Yii::$app->db->beginTransaction();
  618. try {
  619. if (!empty($this->bind_store_id)) {
  620. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  621. } else {
  622. $store = StoreMini::findOne($this->mini_id);
  623. }
  624. if (empty($store) || empty($store->nickname_audit_id)) {
  625. $t->commit();
  626. return [
  627. 'code' => 0,
  628. 'msg' => '数据错误或审核已经通过',
  629. 'status' => 3
  630. ];
  631. }
  632. $miniProgram = $this->miniProgram($store->appid);
  633. $result = $miniProgram->setting->getNicknameAuditStatus($store->nickname_audit_id);
  634. if (empty($result['errcode']) && !empty($result)) {
  635. $msg = "获取成功";
  636. switch ($result['audit_stat']) {
  637. case 0:
  638. $msg = "名称审核中";
  639. $store->apply_name_status = 1;
  640. break;
  641. case 1:
  642. $msg = "名称审核完成";
  643. $store->apply_name_status = 3;
  644. break;
  645. case 2:
  646. $msg = "名称审核失败";
  647. $store->apply_name_status = 2;
  648. $store->apply_name_error = $result['fail_reason'];
  649. break;
  650. }
  651. if (!$store->save()) {
  652. throw new \Exception("保存失败");
  653. }
  654. $t->commit();
  655. return [
  656. 'code' => 0,
  657. 'msg' => $msg,
  658. 'status' => $store->apply_name_status
  659. ];
  660. }
  661. throw new \Exception("查询失败");
  662. } catch (\Exception $e) {
  663. $t->rollBack();
  664. return [
  665. 'code' => 1,
  666. 'msg' => $e->getMessage()
  667. ];
  668. }
  669. }
  670. /**
  671. * 增加Media事件
  672. */
  673. public function addMedia($data)
  674. {
  675. foreach ($data as $index => $item) {
  676. $mediaForm = StoreMedia::find()->where(['media_id' => $item])->one();
  677. if (empty($mediaForm)) {
  678. $mediaForm = new StoreMedia();
  679. $mediaForm->store_id = $this->store_id;
  680. $mediaForm->media_id = $item;
  681. $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
  682. }
  683. $mediaForm->event = $index;
  684. $mediaForm->end_time = time() * 1 + 60 * 60 * 24 * 3;
  685. switch ($index) {
  686. case 'id_card':
  687. $mediaForm->desc = "身份证照片";
  688. break;
  689. case 'license':
  690. $mediaForm->desc = "组织机构代码证或营业执照";
  691. break;
  692. case 'naming_other_stuff_1':
  693. $mediaForm->desc = "其他证明材料1";
  694. break;
  695. case 'naming_other_stuff_2':
  696. $mediaForm->desc = "其他证明材料2";
  697. break;
  698. case 'naming_other_stuff_3':
  699. $mediaForm->desc = "其他证明材料3";
  700. break;
  701. case 'naming_other_stuff_4':
  702. $mediaForm->desc = "其他证明材料4";
  703. break;
  704. case 'naming_other_stuff_5':
  705. $mediaForm->desc = "其他证明材料5";
  706. break;
  707. case 'head_img_media_id':
  708. $mediaForm->desc = "头像素材";
  709. break;
  710. case "ext_file_media_id":
  711. $mediaForm->desc = "用户隐私指引";
  712. }
  713. $result = $mediaForm->save();
  714. }
  715. }
  716. /**
  717. * 微信认证名称检测
  718. */
  719. public function checkWxVerifyNickname()
  720. {
  721. if (!empty($this->bind_store_id)) {
  722. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  723. } else {
  724. $store = StoreMini::findOne($this->mini_id);
  725. }
  726. if (empty($store)) {
  727. return [
  728. 'code' => 1,
  729. 'msg' => "数据错误"
  730. ];
  731. }
  732. $miniProgram = $this->miniProgram($store->appid);
  733. $res = $miniProgram->setting->isAvailableNickname($this->mini_nickname);
  734. if (empty($res['errcode']) && !empty($res)) {
  735. return [
  736. 'code' => 0,
  737. 'msg' => "成功",
  738. 'data' => $res
  739. ];
  740. } elseif (empty($res)) {
  741. return [
  742. 'code' => 1,
  743. 'msg' => "数据错误"
  744. ];
  745. } else {
  746. return [
  747. 'code' => $res['errcode'],
  748. 'msg' => $this->getZnMsg($res),
  749. 'data' => $res
  750. ];
  751. }
  752. }
  753. /**
  754. * 修改头像
  755. */
  756. public function setHeadImage()
  757. {
  758. $t = \Yii::$app->db->beginTransaction();
  759. try {
  760. if (!empty($this->bind_store_id)) {
  761. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  762. } else {
  763. $store = StoreMini::findOne($this->mini_id);
  764. }
  765. if (empty($store)) {
  766. throw new \Exception("数据错误,获取小程序信息失败");
  767. }
  768. $miniProgram = $this->miniProgram($store->appid);
  769. $media = new Client($miniProgram);
  770. $head_img_media_id = $media->uploadThumb($this->saveTempImage($this->head_img_media_id));
  771. if (empty($head_img_media_id['thumb_media_id'])) {
  772. throw new \Exception($this->getZnMsg($head_img_media_id));
  773. }
  774. \Yii::error($head_img_media_id['thumb_media_id']);
  775. $res = $miniProgram->account->updateAvatar($head_img_media_id['thumb_media_id'], $this->x1, $this->y1, $this->x2, $this->y2);
  776. if ($res['errcode'] == '53202') {
  777. $store->mini_url = $this->head_img_media_id;
  778. $store->save();
  779. $t->commit();
  780. return [
  781. 'code' => 0,
  782. 'msg' => "设置成功",
  783. ];
  784. }
  785. if (empty($res['errcode']) && !empty($res)) {
  786. $store->mini_url = $this->head_img_media_id;
  787. $store->save();
  788. $t->commit();
  789. return [
  790. 'code' => 0,
  791. 'msg' => "设置成功",
  792. 'data' => $res
  793. ];
  794. } elseif (empty($res)) {
  795. throw new \Exception("数据错误");
  796. } else {
  797. throw new \Exception($this->getZnMsg($res));
  798. }
  799. } catch (\Exception $e) {
  800. $t->rollBack();
  801. return [
  802. 'code' => 1,
  803. 'msg' => $e->getMessage()
  804. ];
  805. }
  806. }
  807. /**
  808. * 修改简介
  809. */
  810. public function setSignature()
  811. {
  812. $t = \Yii::$app->db->beginTransaction();
  813. try {
  814. if (!empty($this->bind_store_id)) {
  815. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  816. } else {
  817. $store = StoreMini::findOne($this->mini_id);
  818. }
  819. if (empty($store)) {
  820. throw new \Exception("数据错误,获取小程序信息错误");
  821. }
  822. $miniProgram = $this->miniProgram($store->appid);
  823. $res = $miniProgram->account->updateSignature($this->signature);
  824. if ($res['errcode'] == 53200) {
  825. $store->signature = $this->signature;
  826. $store->save();
  827. $t->commit();
  828. return [
  829. 'code' => 0,
  830. 'msg' => "设置成功",
  831. 'data' => $res
  832. ];
  833. }
  834. if (empty($res['errcode']) && !empty($res)) {
  835. $store->signature = $this->signature;
  836. $store->save();
  837. $t->commit();
  838. return [
  839. 'code' => 0,
  840. 'msg' => "设置成功",
  841. 'data' => $res
  842. ];
  843. } elseif (empty($res)) {
  844. throw new \Exception("数据错误");
  845. } else {
  846. throw new \Exception($this->getZnMsg($res));
  847. }
  848. } catch (\Exception $e) {
  849. $t->rollBack();
  850. return [
  851. 'code' => 1,
  852. 'msg' => $e->getMessage()
  853. ];
  854. }
  855. }
  856. public function getdata($data, $id = [0])
  857. {
  858. foreach ($data as $k => $v) {
  859. if (in_array($v['id'], $id)) {
  860. $v['children'] = $this->getdata($data, $v['children']);
  861. $arr[] = $v;
  862. }
  863. }
  864. return $arr;
  865. }
  866. /**
  867. * @return array
  868. * 获取全部可选分类
  869. */
  870. public function getAllCategories()
  871. {
  872. if (!empty($this->bind_store_id)) {
  873. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  874. } else {
  875. $store = StoreMini::findOne($this->mini_id);
  876. }
  877. if (empty($store->authorizer_access_token)) {
  878. return [
  879. 'code' => 1,
  880. 'msg' => "没有授权数据",
  881. 'data' => []
  882. ];
  883. }
  884. if (empty($store)) {
  885. return [
  886. 'code' => 1,
  887. 'msg' => "数据错误"
  888. ];
  889. }
  890. $miniProgram = $this->miniProgram($store->appid);
  891. $res = $miniProgram->setting->getAllCategories();
  892. if (empty($res['errcode']) && !empty($res)) {
  893. $data = $res['categories_list']['categories'];
  894. $data = $this->getdata($data);
  895. return [
  896. 'code' => 0,
  897. 'msg' => "获取成功",
  898. 'data' => $data,
  899. 'old_data' => $res['categories_list']['categories']
  900. ];
  901. } elseif (empty($res)) {
  902. return [
  903. 'code' => 1,
  904. 'msg' => "数据错误"
  905. ];
  906. } else {
  907. return [
  908. 'code' => $res['errcode'],
  909. 'msg' => $this->getZnMsg($res),
  910. 'data' => $res
  911. ];
  912. }
  913. }
  914. /**
  915. * @return array
  916. * 获取已有分类
  917. */
  918. public function getCategories()
  919. {
  920. try {
  921. if (!empty($this->bind_store_id)) {
  922. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  923. } else {
  924. $store = StoreMini::findOne($this->mini_id);
  925. }
  926. if (empty($store)) {
  927. throw new \Exception("数据错误,查询小程序信息失败");
  928. }
  929. $miniProgram = $this->miniProgram($store->appid);
  930. $res = $miniProgram->setting->getCategories();
  931. if (empty($res['errcode']) && !empty($res)) {
  932. $store->categories = !empty($res['categories']) ? json_encode($res['categories']) : "";
  933. $store->save();
  934. return [
  935. 'code' => 0,
  936. 'msg' => "获取成功",
  937. 'data' => $res['categories']
  938. ];
  939. } elseif (empty($res)) {
  940. throw new \Exception("数据错误");
  941. } else {
  942. throw new \Exception($this->getZnMsg($res));
  943. }
  944. } catch (\Exception $e) {
  945. return [
  946. 'code' => 1,
  947. 'msg' => $e->getMessage()
  948. ];
  949. }
  950. }
  951. /**
  952. * 添加类目
  953. */
  954. public function addCategory()
  955. {
  956. try {
  957. if (!empty($this->bind_store_id)) {
  958. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  959. } else {
  960. $store = StoreMini::findOne($this->mini_id);
  961. }
  962. if (empty($store)) {
  963. throw new \Exception("数据错误,查找不到小程序信息");
  964. }
  965. $miniProgram = $this->miniProgram($store->appid);
  966. $data = $this->categories;
  967. $media = new Client($miniProgram);
  968. \Yii::error($data);
  969. foreach ($data as $index => &$item) {
  970. if(!empty($item['categories'])){
  971. foreach ($item['categories'] as $cateIndex => &$cate) {
  972. $url = $this->saveTempImage($cate['value']);
  973. $cate['value'] = $media->uploadImage($url)['media_id'];
  974. }
  975. }
  976. }
  977. $res = $miniProgram->setting->addCategories($data);
  978. if (empty($res['errcode']) && !empty($res)) {
  979. $cat_res = $this->getCategories();
  980. return [
  981. 'code' => 0,
  982. 'msg' => "设置成功",
  983. 'data' => $res,
  984. 'cat_data' => $cat_res['data']
  985. ];
  986. } elseif (empty($res)) {
  987. throw new \Exception("数据错误");
  988. } else {
  989. throw new \Exception($this->getZnMsg($res));
  990. }
  991. } catch (\Exception $e) {
  992. return [
  993. 'code' => 0,
  994. 'msg' => $e->getMessage()
  995. ];
  996. }
  997. }
  998. /**
  999. * 删除类目
  1000. */
  1001. public function delCategory()
  1002. {
  1003. $t = \Yii::$app->db->beginTransaction();
  1004. try {
  1005. if (!empty($this->bind_store_id)) {
  1006. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1007. } else {
  1008. $store = StoreMini::findOne($this->mini_id);
  1009. }
  1010. if (empty($store)) {
  1011. throw new \Exception("数据错误");
  1012. }
  1013. $miniProgram = $this->miniProgram($store->appid);
  1014. $res = $miniProgram->setting->deleteCategories($this->cate_first, $this->cate_second);
  1015. if (empty($res['errcode']) && !empty($res)) {
  1016. $t->commit();
  1017. return [
  1018. 'code' => 0,
  1019. 'msg' => "删除成功",
  1020. 'data' => $res
  1021. ];
  1022. } elseif (empty($res)) {
  1023. throw new \Exception("数据错误");
  1024. } else {
  1025. throw new \Exception($this->getZnMsg($res));
  1026. }
  1027. } catch (\Exception $e) {
  1028. $t->rollBack();
  1029. return [
  1030. 'code' => 1,
  1031. 'msg' => $e->getMessage()
  1032. ];
  1033. }
  1034. }
  1035. /**
  1036. * 获取代码模版列表
  1037. */
  1038. public function getTemplateList()
  1039. {
  1040. $res = $this->openPlatform->code_template->list(0);
  1041. if (empty($res['errcode']) && !empty($res)) {
  1042. $templateList = $res['template_list'];
  1043. foreach ($templateList as &$item) {
  1044. $item['c_time'] = date('Y-m-d H:i:s', $item['create_time']);
  1045. $item['template_id'] = (string)$item['template_id'];
  1046. }
  1047. $last_names = array_column($templateList, 'create_time');
  1048. array_multisort($last_names, SORT_ASC, $templateList);
  1049. return [
  1050. 'code' => 0,
  1051. 'msg' => "获取成功",
  1052. 'data' => [
  1053. 'template_list' => $templateList
  1054. ]
  1055. ];
  1056. } elseif (empty($res)) {
  1057. return [
  1058. 'code' => 1,
  1059. 'msg' => "数据错误"
  1060. ];
  1061. } else {
  1062. return [
  1063. 'code' => $res['errcode'],
  1064. 'msg' => $this->getZnMsg($res),
  1065. 'data' => $res
  1066. ];
  1067. }
  1068. }
  1069. /**
  1070. * 上传小程序
  1071. */
  1072. public function upMini()
  1073. {
  1074. $t = \Yii::$app->db->beginTransaction();
  1075. try {
  1076. if (!empty($this->bind_store_id)) {
  1077. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1078. } else {
  1079. $store = StoreMini::findOne($this->mini_id);
  1080. }
  1081. if (empty($store)) {
  1082. throw new \Exception("数据错误,获取不到小程序信息");
  1083. }
  1084. $miniProgram = $this->miniProgram($store->appid);
  1085. $ext = [
  1086. 'extEnable' => true,
  1087. 'extAppid' => $store->appid,
  1088. 'ext' => [
  1089. "apiurl" => "https://" . \Yii::$app->request->hostName,
  1090. "store_id" => $store->store_id,
  1091. "mini_id" => $store->id
  1092. ],
  1093. "window" => [
  1094. "navigationBarTitleText" => "0000"
  1095. ]
  1096. ];
  1097. $template_id = Option::get("platform_template_id", 0, 'saas')['value'];
  1098. if ($template_id === "-1") {
  1099. throw new \Exception("未选择模板");
  1100. }
  1101. $res = $this->setDomainName();
  1102. if ($res['code'] == 1) {
  1103. throw new \Exception($res['msg']);
  1104. }
  1105. $DomainData = $res;
  1106. $res = $miniProgram->code->commit($template_id, json_encode($ext), cyy_version(), $this->user_desc);
  1107. if (empty($res['errcode']) && !empty($res)) {
  1108. $store->mini_up = -1;
  1109. $store->template_id = $template_id;
  1110. $store->user_version = cyy_version();
  1111. $store->user_desc = $this->user_desc;
  1112. $store->ext = json_encode($ext);
  1113. $res = $store->save();
  1114. if ($res) {
  1115. $res = $this->getMiniQrcode($store->id);
  1116. if ($res['code'] === 0) {
  1117. $t->commit();
  1118. return [
  1119. 'code' => 0,
  1120. 'msg' => "成功",
  1121. 'data' => $res['data'],
  1122. 'datas' => $DomainData
  1123. ];
  1124. } else {
  1125. throw new \Exception("获取体验二维码失败");
  1126. }
  1127. } else {
  1128. throw new \Exception("存储数据失败");
  1129. }
  1130. } elseif (empty($res)) {
  1131. throw new \Exception("数据错误");
  1132. } else {
  1133. throw new \Exception($this->getZnMsg($res));
  1134. }
  1135. } catch (\Exception $e) {
  1136. $t->rollBack();
  1137. return [
  1138. 'code' => 1,
  1139. 'msg' => $e->getMessage(),
  1140. ];
  1141. }
  1142. }
  1143. /**
  1144. * 获取已上传的代码的页面列表
  1145. */
  1146. public function getPageList()
  1147. {
  1148. $store = StoreMini::findOne($this->mini_id);
  1149. if (empty($store)) {
  1150. return [
  1151. 'code' => 1,
  1152. 'msg' => "数据错误"
  1153. ];
  1154. }
  1155. $miniProgram = $this->miniProgram($store->appid);
  1156. $res = $miniProgram->code->getPage();
  1157. if (empty($res['errcode']) && !empty($res)) {
  1158. return [
  1159. 'code' => 0,
  1160. 'msg' => "成功",
  1161. 'data' => $res
  1162. ];
  1163. } elseif (empty($res)) {
  1164. return [
  1165. 'code' => 1,
  1166. 'msg' => "数据错误"
  1167. ];
  1168. } else {
  1169. return [
  1170. 'code' => $res['errcode'],
  1171. 'msg' => $this->getZnMsg($res),
  1172. 'data' => $res
  1173. ];
  1174. }
  1175. }
  1176. /**
  1177. * 添加体验者
  1178. * @return array
  1179. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  1180. * @throws \GuzzleHttp\Exception\GuzzleException
  1181. */
  1182. public function addTester()
  1183. {
  1184. $store = StoreMini::findOne($this->mini_id);
  1185. if (empty($store)) {
  1186. return [
  1187. 'code' => 1,
  1188. 'msg' => "数据错误"
  1189. ];
  1190. }
  1191. $miniProgram = $this->miniProgram($store->appid);
  1192. $res = $miniProgram->tester->bind($this->wechatId);
  1193. if (empty($res['errcode']) && !empty($res)) {
  1194. return [
  1195. 'code' => 0,
  1196. 'msg' => "成功",
  1197. 'data' => $res
  1198. ];
  1199. } elseif (empty($res)) {
  1200. return [
  1201. 'code' => 1,
  1202. 'msg' => "数据错误"
  1203. ];
  1204. } else {
  1205. return [
  1206. 'code' => $res['errcode'],
  1207. 'msg' => $this->getZnMsg($res),
  1208. 'data' => $res
  1209. ];
  1210. }
  1211. }
  1212. /**
  1213. * 获取体验版二维码
  1214. */
  1215. public function getMiniQrcode($id = 0)
  1216. {
  1217. if (!empty($this->bind_store_id)) {
  1218. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1219. } else {
  1220. $id = $id !== 0 ? $id : $this->mini_id;
  1221. $store = StoreMini::findOne($id);
  1222. }
  1223. if (empty($store)) {
  1224. return [
  1225. 'code' => 1,
  1226. 'msg' => "数据错误"
  1227. ];
  1228. }
  1229. $miniProgram = $this->miniProgram($store->appid);
  1230. $qrcode = \Yii::$app->basePath . "/web/temp/" . $this->store_id . "_" . date('YmdHis') . ".jpg";
  1231. $res = $miniProgram->code->getQrCode($this->mini_path);
  1232. if (empty(json_decode($res)->errcode)) {
  1233. file_put_contents($qrcode, $res);
  1234. $url = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $qrcode);
  1235. return [
  1236. 'code' => 0,
  1237. 'msg' => "成功",
  1238. 'data' => $url
  1239. ];
  1240. } elseif (empty($res)) {
  1241. return [
  1242. 'code' => 1,
  1243. 'msg' => "数据错误"
  1244. ];
  1245. } else {
  1246. $res = json_decode($res);
  1247. return [
  1248. 'code' => $res->errcode,
  1249. 'msg' => $this->getZnMsg($res),
  1250. 'data' => $res
  1251. ];
  1252. }
  1253. }
  1254. /**
  1255. * 提交审核
  1256. */
  1257. public function submitAudit()
  1258. {
  1259. $t = \Yii::$app->db->beginTransaction();
  1260. try {
  1261. $result = $this->setPrivacySetting();
  1262. if ($result['code'] === 0) {
  1263. if (!empty($this->bind_store_id)) {
  1264. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1265. } else {
  1266. $store = StoreMini::findOne($this->mini_id);
  1267. }
  1268. if (empty($store)) {
  1269. throw new \Exception("数据错误,未找到对应的小程序信息");
  1270. }
  1271. $miniProgram = $this->miniProgram($store->appid);
  1272. $data = [
  1273. 'item_list' => $this->item_list ?? [],
  1274. 'order_path' => 'order/order/order',
  1275. ];
  1276. $res = $miniProgram->code->submitAudit($data, '', '');
  1277. if (empty($res['errcode']) && !empty($res)) {
  1278. //是否加急审核
  1279. if ($this->is_fast_audit === 1) {
  1280. $res = $miniProgram->code->speedupAudit($res['auditid']);
  1281. }
  1282. $store->mini_up = 1;
  1283. $store->save();
  1284. $t->commit();
  1285. return [
  1286. 'code' => 0,
  1287. 'msg' => "成功",
  1288. 'data' => $res
  1289. ];
  1290. } elseif (empty($res)) {
  1291. throw new \Exception("数据错误");
  1292. } else {
  1293. throw new \Exception($this->getZnMsg($res));
  1294. }
  1295. } else {
  1296. throw new \Exception($result['msg']);
  1297. }
  1298. } catch (\Exception $e) {
  1299. $t->rollBack();
  1300. return [
  1301. 'code' => 1,
  1302. 'msg' => $e->getMessage()
  1303. ];
  1304. }
  1305. }
  1306. /**
  1307. * 查询最后一次提交审核的状态
  1308. */
  1309. public function lastAuditStatus()
  1310. {
  1311. $t = \Yii::$app->db->beginTransaction();
  1312. try {
  1313. if (!empty($this->bind_store_id)) {
  1314. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1315. } else {
  1316. $store = StoreMini::findOne($this->mini_id);
  1317. }
  1318. if (empty($store)) {
  1319. throw new \Exception("数据错误");
  1320. }
  1321. $miniProgram = $this->miniProgram($store->appid);
  1322. $result = $miniProgram->code->getLatestAuditStatus();
  1323. $msg = "返回成功";
  1324. if (empty($result['errcode']) && !empty($result)) {
  1325. switch ($result['status']) {
  1326. case 0:
  1327. $msg = "审核通过";
  1328. $store->mini_up = 2;
  1329. break;
  1330. case 1:
  1331. $msg = "审核已拒绝:" . $result['reason'];
  1332. $store->mini_up = 3;
  1333. $store->mini_up_error = $result['reason'];
  1334. break;
  1335. case 2:
  1336. $msg = "审核中";
  1337. $store->mini_up = 1;
  1338. break;
  1339. case 3:
  1340. $msg = "未提交审核或已撤回";
  1341. $store->mini_up = 0;
  1342. break;
  1343. }
  1344. if (!$store->save()) {
  1345. throw new \Exception("保存失败");
  1346. }
  1347. $t->commit();
  1348. return [
  1349. 'code' => 0,
  1350. 'msg' => $msg
  1351. ];
  1352. }
  1353. } catch (\Exception $e) {
  1354. $t->rollBack();
  1355. return [
  1356. 'code' => 1,
  1357. 'msg' => $e->getMessage()
  1358. ];
  1359. }
  1360. }
  1361. /**
  1362. * 审核撤回
  1363. */
  1364. public function unDoCodeAudit()
  1365. {
  1366. $t = \Yii::$app->db->beginTransaction();
  1367. try {
  1368. if (!empty($this->bind_store_id)) {
  1369. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1370. } else {
  1371. $store = StoreMini::findOne($this->mini_id);
  1372. }
  1373. if (empty($store)) {
  1374. throw new \Exception("数据错误,未查询到小程序信息");
  1375. }
  1376. $miniProgram = $this->miniProgram($store->appid);
  1377. $res = $miniProgram->code->withdrawAudit();
  1378. if (empty($res['errcode']) && !empty($res)) {
  1379. $store->mini_up = -1;
  1380. $store->save();
  1381. $t->commit();
  1382. return [
  1383. 'code' => 0,
  1384. 'msg' => "成功",
  1385. 'data' => $res
  1386. ];
  1387. } elseif (empty($res)) {
  1388. throw new \Exception("数据错误");
  1389. } else {
  1390. throw new \Exception($this->getZnMsg($res));
  1391. }
  1392. } catch (\Exception $e) {
  1393. $t->rollBack();
  1394. return [
  1395. 'code' => 1,
  1396. 'msg' => $e->getMessage()
  1397. ];
  1398. }
  1399. }
  1400. /**
  1401. * 发布已通过审核的小程序
  1402. */
  1403. public function release()
  1404. {
  1405. $t = \Yii::$app->db->beginTransaction();
  1406. try {
  1407. if (!empty($this->bind_store_id)) {
  1408. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1409. } else {
  1410. $store = StoreMini::findOne($this->mini_id);
  1411. }
  1412. $store_id = !empty($this->bind_store_id) ? $this->bind_store_id : get_store_id();
  1413. if (empty($store)) {
  1414. throw new \Exception("数据错误,获取不到小程序信息");
  1415. }
  1416. $miniProgram = $this->miniProgram($store->appid);
  1417. $this->unbind();
  1418. $res = $miniProgram->code->release();
  1419. if ((empty($res['errcode']) && !empty($res)) || $res['errcode'] == "85052") {
  1420. $store->mini_up = 5;
  1421. $res = $this->getMiniQr($store->id);
  1422. if ($res['code'] === 0) {
  1423. if ($store->save()) {
  1424. $t->commit();
  1425. return [
  1426. 'code' => 0,
  1427. 'msg' => "成功",
  1428. 'data' => $res['data']
  1429. ];
  1430. } else {
  1431. throw new \Exception("保存数据错误");
  1432. }
  1433. } else {
  1434. throw new \Exception("获取二维码数据错误");
  1435. }
  1436. } elseif (empty($res)) {
  1437. throw new \Exception("数据错误");
  1438. } else {
  1439. throw new \Exception($this->getZnMsg($res));
  1440. }
  1441. } catch (\Exception $e) {
  1442. $t->rollBack();
  1443. return [
  1444. 'code' => 1,
  1445. 'msg' => $e->getMessage()
  1446. ];
  1447. }
  1448. }
  1449. /**
  1450. * 设置小程序用户隐私保护指引
  1451. */
  1452. public function setPrivacySetting()
  1453. {
  1454. $data = [
  1455. "privacy_ver" => $this->privacy_ver,
  1456. "owner_setting" => [
  1457. "contact_phone" => "17090402350",
  1458. "notice_method" => "短信"
  1459. ],
  1460. "setting_list" => [
  1461. [
  1462. "privacy_key" => "UserInfo",
  1463. "privacy_text" => "统计订单"
  1464. ],
  1465. [
  1466. "privacy_key" => "Location",
  1467. "privacy_text" => "显示附近自提点"
  1468. ],
  1469. [
  1470. "privacy_key" => "Address",
  1471. "privacy_text" => "显示附近自提点"
  1472. ],
  1473. [
  1474. "privacy_key" => "RunData",
  1475. "privacy_text" => "消费步数兑换商品"
  1476. ],
  1477. [
  1478. "privacy_key" => "Record",
  1479. "privacy_text" => "售后咨询"
  1480. ],
  1481. [
  1482. "privacy_key" => "Camera",
  1483. "privacy_text" => "提交订单备注资料"
  1484. ],
  1485. [
  1486. "privacy_key" => "AlbumWriteOnly",
  1487. "privacy_text" => "保存海报"
  1488. ],
  1489. [
  1490. "privacy_key" => "PhoneNumber",
  1491. "privacy_text" => "多端口数据统一"
  1492. ],
  1493. [
  1494. "privacy_key" => "Album",
  1495. "privacy_text" => "审核资质"
  1496. ],
  1497. [
  1498. "privacy_key" =>"MessageFile",
  1499. "privacy_text" => "订单评价拍照"
  1500. ]
  1501. ],
  1502. ];
  1503. if (!empty($this->bind_store_id)) {
  1504. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1505. } else {
  1506. $store = StoreMini::findOne($this->mini_id);
  1507. }
  1508. $client = new BaseClient($this->miniProgram($store->appid));
  1509. $res = $client->httpPostJson('cgi-bin/component/setprivacysetting', $data);
  1510. if (empty($res['errcode']) && !empty($res)) {
  1511. return [
  1512. 'code' => 0,
  1513. 'msg' => "成功",
  1514. 'data' => $res
  1515. ];
  1516. } elseif (empty($res)) {
  1517. return [
  1518. 'code' => 1,
  1519. 'msg' => "数据错误"
  1520. ];
  1521. } else {
  1522. return [
  1523. 'code' => $res['errcode'],
  1524. 'msg' => $this->getZnMsg($res)
  1525. ];
  1526. }
  1527. }
  1528. /**
  1529. * 查询小程序用户隐私保护指引
  1530. */
  1531. public function getPrivacySetting()
  1532. {
  1533. $data = [
  1534. "privacy_ver" => $this->privacy_ver,
  1535. ];
  1536. if (!empty($this->bind_store_id)) {
  1537. $store = StoreMini::find()->where(['store_id' => $this->bind_store_id])->orderBy('id desc')->one();
  1538. } else {
  1539. $store = StoreMini::findOne($this->mini_id);
  1540. }
  1541. $client = new BaseClient($this->miniProgram($store->appid));
  1542. $res = $client->httpPostJson('cgi-bin/component/getprivacysetting', $data);
  1543. if (empty($res['errcode']) && !empty($res)) {
  1544. return [
  1545. 'code' => 0,
  1546. 'msg' => "成功",
  1547. 'data' => $res
  1548. ];
  1549. } elseif (empty($res)) {
  1550. return [
  1551. 'code' => 1,
  1552. 'msg' => "数据错误"
  1553. ];
  1554. } else {
  1555. return [
  1556. 'code' => $res['errcode'],
  1557. 'msg' => $this->getZnMsg($res),
  1558. 'data' => $res
  1559. ];
  1560. }
  1561. }
  1562. /**
  1563. * 查询小程序码
  1564. */
  1565. public function getMiniQr($id = 0)
  1566. {
  1567. try {
  1568. if (empty($id)) {
  1569. $id = $this->mini_id;
  1570. }
  1571. $store = StoreMini::findOne($id);
  1572. $miniProgram = $this->miniProgram($store->appid);
  1573. $response = $miniProgram->app_code->getUnlimit("store=" . $this->store_id);
  1574. $filename = md5(time()) . '.jpg';
  1575. $filePath = \Yii::$app->basePath . '/web/uploads/images/store_' . $this->store_id . '/' . date('Y-m-d');
  1576. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  1577. $resfilename = $response->saveAs($filePath, $filename);
  1578. if (!empty($resfilename)) {
  1579. $filePath = str_replace(\Yii::$app->basePath, \Yii::$app->request->hostInfo, $filePath . '/' . $filename);
  1580. $store->qrcode = $filePath;
  1581. $res = $store->save();
  1582. if ($res) {
  1583. return [
  1584. 'code' => 0,
  1585. 'msg' => "获取成功",
  1586. 'data' => $filePath
  1587. ];
  1588. } else {
  1589. throw new \Exception("添加小程序码失败");
  1590. }
  1591. } else {
  1592. throw new \Exception("数据错误");
  1593. }
  1594. } else {
  1595. throw new \Exception("数据错误");
  1596. }
  1597. } catch (\Exception $e) {
  1598. return [
  1599. 'code' => 1,
  1600. 'msg' => $e->getMessage()
  1601. ];
  1602. }
  1603. }
  1604. /**
  1605. * 查询小程序
  1606. */
  1607. public function getMiniStatus()
  1608. {
  1609. $mini_info = StoreMini::find()->where(['appid' => $this->appid])->select("mini_url,mini_up,is_cancle,appid,status,msg")->asArray()->one();
  1610. return [
  1611. 'code' => 0,
  1612. 'msg' => "获取成功",
  1613. 'data' => $mini_info
  1614. ];
  1615. }
  1616. /**
  1617. * 获取列表
  1618. */
  1619. public function getMiniList()
  1620. {
  1621. $store_id = $this->store_id;
  1622. $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");
  1623. if (!empty($this->mini_nickname)) {
  1624. $query->andWhere(['LIKE', 'mini_nickname', $this->mini_nickname]);
  1625. }
  1626. if (!empty($this->status)) {
  1627. switch ($this->status) {
  1628. case 1:
  1629. $query->andWhere(['OR', ['mini_nickname' => ""], ['signature' => ""], ['mini_url' => ""]]);
  1630. break;
  1631. case 2:
  1632. $query->andWhere(['AND', ['<>', 'mini_nickname', ""], ['<>', 'signature', ""], ['<>', 'mini_url', ""]]);
  1633. break;
  1634. case 3:
  1635. $query->andWhere(['status' => 2]);
  1636. break;
  1637. case 4:
  1638. $query->andWhere(['status' => -1]);
  1639. break;
  1640. }
  1641. }
  1642. if (!empty($this->is_use)) {
  1643. $query->andWhere(['is_use' => $this->is_use]);
  1644. }
  1645. if (!empty($this->mini_nickname)) {
  1646. $query->andWhere(['LIKE', 'mini_info', $this->mini_nickname]);
  1647. }
  1648. $pagination = pagination_make($query);
  1649. $admins = $pagination['list'];
  1650. foreach ($admins as $index => &$item) {
  1651. \Yii::error($item);
  1652. $item['principal_name'] = json_decode($item['mini_info'])->principal_name;
  1653. $item['nickname'] = json_decode($item['mini_info'])->nickname;
  1654. $item['is_finish'] = 0;
  1655. if (!empty($item['categories'])) {
  1656. $item['categories'] = json_decode($item['categories']);
  1657. } elseif (empty($item['categories']) && !empty($item['appid'])) {
  1658. $this->mini_id = $item['id'];
  1659. $item['categories'] = $this->getCategories()['data'];
  1660. }
  1661. if (!empty($item['mini_info'])) {
  1662. $item['mini_info'] = json_decode($item['mini_info']);
  1663. }
  1664. $item['qr'] = false;
  1665. if (!empty($item['qrcode'])) {
  1666. $item['qr'] = $item['qrcode'];
  1667. }
  1668. $item['user_version'] = cyy_version();
  1669. $item['qrcode'] = '';
  1670. if (!empty($item['appid'])) {
  1671. $res = $this->getQrcodeRules($item['appid']);
  1672. \Yii::error($res);
  1673. $miniInfo = $this->getMiniInfo("", $item['appid']);
  1674. if ($miniInfo["code"] === 0) {
  1675. \Yii::error($miniInfo);
  1676. if (!empty($item['signature']) && !empty($item['mini_url']) && !empty($item['mini_nickname'])) {
  1677. $item['is_finish'] = 1;
  1678. }
  1679. }
  1680. $res = $this->getMiniQrcode($item['id']);
  1681. if ($res['code'] == 0) {
  1682. $item['qrcode'] = $res['data'];
  1683. }
  1684. }
  1685. }
  1686. return [
  1687. 'code' => 0,
  1688. 'msg' => "获取成功",
  1689. 'data' => $admins,
  1690. 'pageNo' => $pagination['pageNo'],
  1691. 'totalCount' => $pagination['totalCount'],
  1692. ];
  1693. }
  1694. /**
  1695. * 获取单个
  1696. */
  1697. public function getMini()
  1698. {
  1699. $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,");
  1700. $data = $query->asArray()->one();
  1701. if (!empty(json_decode($data['mini_info']))) {
  1702. $data['principal_name'] = json_decode($data['mini_info'])->principal_name;
  1703. $data['nickname'] = json_decode($data['mini_info'])->nickname;
  1704. $data['mini_url'] = $data['mini_url'] ?? json_decode($data['mini_info'])->head_image_info->head_image_url;
  1705. $data['categories'] = [];
  1706. if ($this->getCategories()['code'] === 0) {
  1707. $data['categories'] = $this->getCategories()['data'];
  1708. }
  1709. }
  1710. return [
  1711. 'code' => 0,
  1712. 'msg' => "获取成功",
  1713. 'data' => $data
  1714. ];
  1715. }
  1716. //添加二维码规则
  1717. public function setQrcodeRules($id, $appid)
  1718. {
  1719. try {
  1720. $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->asArray()->one();
  1721. $data = [
  1722. 'prefix' => $qrcode['param_url'],
  1723. 'permit_sub_rule' => 1,
  1724. 'path' => $qrcode['wx_url'],
  1725. 'open_version' => 2,
  1726. 'debug_url' => [
  1727. $qrcode['param_url']
  1728. ],
  1729. 'is_edit' => 0
  1730. ];
  1731. \Yii::error($data);
  1732. $client = new BaseClient($this->miniProgram($appid));
  1733. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpadd', $data);
  1734. \Yii::error($res);
  1735. if ((empty($res['errcode']) && !empty($res)) || ($res['errcode'] == "85071")) {
  1736. return [
  1737. 'code' => 0,
  1738. 'msg' => "成功",
  1739. 'data' => $res
  1740. ];
  1741. } elseif (empty($res)) {
  1742. throw new \Exception("数据错误");
  1743. } else {
  1744. throw new \Exception($this->getZnMsg($res));
  1745. }
  1746. } catch (\Exception $e) {
  1747. return [
  1748. 'code' => 1,
  1749. 'msg' => $e->getMessage()
  1750. ];
  1751. }
  1752. }
  1753. //发布二维码规则
  1754. public function submitQrcodeRules($id, $appid)
  1755. {
  1756. $qrcode = AggregateQrcode::find()->where(['wx_mini_id' => $id])->one();
  1757. if (empty($qrcode->param_url)) {
  1758. return [
  1759. 'code' => 1,
  1760. 'msg' => '参数错误'
  1761. ];
  1762. }
  1763. $data = [
  1764. 'prefix' => $qrcode->param_url
  1765. ];
  1766. $client = new BaseClient($this->miniProgram($appid));
  1767. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumppublish', $data);
  1768. if (empty($res->errcode) && !empty($res)) {
  1769. $qrcode->wx_status = 1;
  1770. $res = $qrcode->save();
  1771. if (!$res) {
  1772. throw new \Exception('保存数据失败,二维码规则发布生成');
  1773. }
  1774. return [
  1775. 'code' => 0,
  1776. 'msg' => "成功",
  1777. 'data' => $res
  1778. ];
  1779. } elseif (empty($res)) {
  1780. return [
  1781. 'code' => 1,
  1782. 'msg' => "数据错误"
  1783. ];
  1784. } else {
  1785. $res = json_decode($res);
  1786. return [
  1787. 'code' => $res['errcode'],
  1788. 'msg' => $this->getZnMsg($res),
  1789. 'data' => $res
  1790. ];
  1791. }
  1792. }
  1793. //获取二维码规则
  1794. public function getQrcodeRules($appid)
  1795. {
  1796. $client = new BaseClient($this->miniProgram($appid));
  1797. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpget');
  1798. if (empty($res->errcode) && !empty($res)) {
  1799. return [
  1800. 'code' => 0,
  1801. 'msg' => "成功",
  1802. 'data' => $res
  1803. ];
  1804. } elseif (empty($res)) {
  1805. return [
  1806. 'code' => 1,
  1807. 'msg' => "数据错误"
  1808. ];
  1809. } else {
  1810. $res = json_decode($res);
  1811. return [
  1812. 'code' => $res['errcode'],
  1813. 'msg' => $this->getZnMsg($res),
  1814. 'data' => $res
  1815. ];
  1816. }
  1817. }
  1818. //下载文件
  1819. public function downQrcodeRules($id, $appid)
  1820. {
  1821. try {
  1822. $client = new BaseClient($this->miniProgram($appid));
  1823. $res = $client->httpPostJson('cgi-bin/wxopen/qrcodejumpdownload');
  1824. if (empty($res['errcode']) && !empty($res)) {
  1825. if (!is_dir(\Yii::$app->basePath . '/web/face/pay')) {
  1826. // dir doesn't exist, make it
  1827. mkdir(\Yii::$app->basePath . '/web/face/pay', 0777, true);
  1828. }
  1829. $file_result = file_put_contents(\Yii::$app->basePath . '/web/face/pay/' . $res['file_name'], $res['file_content']);
  1830. if ($file_result) {
  1831. $resRules = $this->setQrcodeRules($id, $appid);
  1832. if ($resRules['code'] === 0) {
  1833. $ress = $this->submitQrcodeRules($id, $appid);
  1834. \Yii::error($ress);
  1835. if ($ress['code'] === 0) {
  1836. $resGet = $this->getQrcodeRules($appid);
  1837. \Yii::error($resGet);
  1838. } else {
  1839. throw new \Exception($ress['msg']);
  1840. }
  1841. } else {
  1842. throw new \Exception($resRules['msg']);
  1843. }
  1844. } else {
  1845. throw new \Exception("创建文件失败");
  1846. }
  1847. return [
  1848. 'code' => 0,
  1849. 'msg' => "成功",
  1850. 'data' => $res,
  1851. 'file' => $file_result
  1852. ];
  1853. } elseif (empty($res)) {
  1854. throw new \Exception("数据错误");
  1855. } else {
  1856. //$res = json_decode($res);
  1857. throw new \Exception($res['errmsg']);
  1858. }
  1859. } catch (\Exception $e) {
  1860. return [
  1861. 'code' => 1,
  1862. 'msg' => $e->getMessage()
  1863. ];
  1864. }
  1865. }
  1866. //微信支付进件
  1867. public function mchRegister()
  1868. {
  1869. $mini = StoreMini::find()->where(['id' => $this->mini_id])->select('id,appid')->one();
  1870. if (empty($mini->appid)) {
  1871. return [
  1872. 'code' => 0,
  1873. 'msg' => "未找到对应的小程序信息"
  1874. ];
  1875. }
  1876. $merchant_info = MerchantInfo::find()->where(['bind_store_id' => get_store_id(), 'is_delete' => 0])->one();
  1877. if (empty($merchant_info)) {
  1878. $StoreSchedule = StoreSchedule::find()->where(['store_id' => get_store_id()])->one();
  1879. if (!empty($StoreSchedule->merchant_info)) {
  1880. $merchant_info = json_decode($StoreSchedule->merchant_info, true);
  1881. $contact_info = $merchant_info['contact_info'];
  1882. $subject_info = $merchant_info['subject_info'];
  1883. $business_info = $merchant_info['business_info'];
  1884. $bank_account_info = $merchant_info['bank_account_info'];
  1885. } else {
  1886. return [
  1887. 'code' => 1,
  1888. 'msg' => "未找到对应的进件数据"
  1889. ];
  1890. }
  1891. } else {
  1892. $contact_info = !empty($merchant_info->contact_info) ? json_decode($merchant_info->contact_info, true) : "";
  1893. $subject_info = !empty($merchant_info->subject_info) ? json_decode($merchant_info->subject_info, true) : "";
  1894. $business_info = !empty($merchant_info->business_info) ? json_decode($merchant_info->business_info, true) : "";
  1895. $bank_account_info = !empty($merchant_info->bank_account_info) ? json_decode($merchant_info->bank_account_info, true) : "";
  1896. }
  1897. $Merchant = new Merchant();
  1898. return $Merchant->submit($contact_info, $subject_info, $business_info, $bank_account_info, !empty($merchant_info->id) ? $merchant_info->id : 0, $mini->appid);
  1899. }
  1900. //获取网络图片到临时目录
  1901. private function saveTempImage($url)
  1902. {
  1903. if (strpos($url, 'http') === false) {
  1904. $url = 'http:' . trim($url);
  1905. }
  1906. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  1907. mkdir(\Yii::$app->runtimePath . '/image');
  1908. }
  1909. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  1910. CurlHelper::download($url, $save_path);
  1911. return $save_path;
  1912. }
  1913. //返回中文错误信息
  1914. public function getZnMsg($result)
  1915. {
  1916. $ErrorMsg = new ErrorMsg();
  1917. $arr = $ErrorMsg->getArray();
  1918. $msg = !empty($arr[$result['errcode']]) ? $arr[$result['errcode']] : $result['errmsg'];
  1919. return $msg;
  1920. }
  1921. }