WxOpenController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. namespace saas\controllers;
  3. use biz\merchant\services\MerchantService;
  4. use biz\wx\services\WxBaseService;
  5. use biz\wx\services\WxMiniBaseService;
  6. use common\components\util;
  7. use Yii;
  8. use common\components\wxUtil;
  9. use biz\ad\classes\AdClass;
  10. use biz\merchant\classes\MerchantExtendClass;
  11. use biz\wx\classes\WxBaseClass;
  12. use biz\wx\services\WxOpenService;
  13. use common\services\xhMerchantExtendService;
  14. use common\services\xhMerchantService;
  15. use common\services\xhInviteService;
  16. use common\components\configDict;
  17. use common\services\xhWxOpenService;
  18. use biz\wx\classes\WxMiniBaseClass;
  19. use biz\wx\classes\WxOpenClass;
  20. class WxOpenController extends PublicController
  21. {
  22. public $withoutLoginAction = ['bind-open'];
  23. //开放平台开始授权 shish 2020.1.16
  24. public function actionBeginAuth()
  25. {
  26. $sjId = Yii::$app->request->get('id', 0);
  27. $open = WxOpenService::getOpen();
  28. //1只显示公众号 2只显示小程序 3显示公众号和小程序
  29. $authType = Yii::$app->request->get('authType', 3);
  30. $pre = wxUtil::getPreAuthCode($open);
  31. $appId = $open['appId'];
  32. $oData = [];
  33. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s");
  34. //设置预授码过期,让下个用户可以获取新的预授码
  35. xhWxOpenService::updateById(1, $oData);
  36. $url = Yii::$app->params['saasHost'] . '/wx-open-callback/' . $sjId;
  37. echo "<a href='https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$appId}&pre_auth_code={$pre}&redirect_uri={$url}&auth_type={$authType}'>授权</a>";
  38. }
  39. //创建平台使用的公众号
  40. public function actionCallback()
  41. {
  42. $get = Yii::$app->request->get();
  43. if (isset($get['auth_code']) == false) {
  44. util::stop('公众号授权回调未成功');
  45. }
  46. $id = $get['id'] ?? 0;
  47. $code = $get['auth_code'];
  48. //使用授权码换取公众号的接口调用凭据和授权信息
  49. $authorize = wxUtil::getAuthorizer($code);
  50. if (isset($authorize['authorization_info']) == false) {
  51. util::stop('未换取公众号的接口调用凭据和授权信息');
  52. }
  53. $info = $authorize['authorization_info'];
  54. $authorizeAppId = $info['authorizer_appid'];
  55. $authorize_access_token = $info['authorizer_access_token'];
  56. $expires_in = $info['expires_in'];//7200
  57. $authorize_refresh_token = $info['authorizer_refresh_token'];
  58. //获取授权方的公众号帐号基本信息
  59. $authorizeDetailInfo = wxUtil::getAuthorizerInfo($authorizeAppId);
  60. if (isset($authorizeDetailInfo['authorizer_info']) == false) {
  61. util::stop('没有获取到公众号帐号基本信息');
  62. }
  63. $authorize_info = $authorizeDetailInfo['authorizer_info'];
  64. //$nick_name = $authorize_info['nick_name'];
  65. $head_img = $authorize_info['head_img'];
  66. $service_type_info = $authorize_info['service_type_info'];
  67. if ($service_type_info['id'] != 2) {
  68. if (isset($authorize_info['MiniProgramInfo']) == false || empty($authorize_info['MiniProgramInfo'])) {
  69. util::stop('公众号不是服务号');
  70. }
  71. }
  72. $verify_type_info = $authorize_info['verify_type_info'];
  73. if ($verify_type_info['id'] != 0) {
  74. if (isset($authorize_info['MiniProgramInfo']) == false || empty($authorize_info['MiniProgramInfo'])) {
  75. util::stop('公众号还没有认证哦');
  76. }
  77. }
  78. $user_name = $authorize_info['user_name'];
  79. $alias = $authorize_info['alias'];
  80. $qrCodeUrl = $authorize_info['qrcode_url'];
  81. $accessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒
  82. if ($id == 0) {
  83. //这是供货商
  84. $sjWxInfo = WxOpenClass::getGhsWxInfo();
  85. } elseif ($id == -1) {
  86. //这是零售商
  87. $sjWxInfo = WxOpenClass::getWxInfo();
  88. } else {
  89. $sjWxInfo = MerchantExtendClass::getByMerchantId($id);
  90. }
  91. $open = WxOpenService::getOpen();
  92. //如果授权的是小程序
  93. if (isset($authorize_info['MiniProgramInfo']) && !empty($authorize_info['MiniProgramInfo'])) {
  94. if (isset($sjWxInfo['miniAppId']) && !empty($sjWxInfo['miniAppId'])) {
  95. if ($sjWxInfo['miniAppId'] != $authorizeAppId) {
  96. util::stop('授权的小程序与商家已绑定的小程序不一致');
  97. }
  98. } else {
  99. util::stop('数据库请先填写小程序 miniAppId miniAppSecret');
  100. }
  101. $miniData = [
  102. 'miniAppId' => $authorizeAppId,
  103. 'miniAuth' => 1,
  104. 'miniAccessToken' => $authorize_access_token,
  105. 'miniAccessTokenTime' => $accessTokenTime,
  106. 'miniRefreshToken' => $authorize_refresh_token,
  107. ];
  108. if ($id <= 0) {
  109. $wxMiniBaseId = 0;
  110. if ($id == 0) {
  111. //供货商
  112. $wxMiniBaseId = $open['wxGhsMiniBaseId'] ?? 0;
  113. } elseif ($id == -1) {
  114. //零售店
  115. $wxMiniBaseId = $open['wxMiniBaseId'] ?? 0;
  116. }
  117. if (empty($wxMiniBaseId)) {
  118. util::stop('没有小程序信息...');
  119. }
  120. $miniInfo = WxMiniBaseClass::getById($wxMiniBaseId);
  121. if (empty($miniInfo)) {
  122. util::stop('没有小程序信息,请先配置:xhWxMiniBase表 和 xhWxOpen的相关字段');
  123. }
  124. WxMiniBaseClass::updateById($wxMiniBaseId, $miniData);
  125. util::stop('开放平台成功绑定小程序');
  126. } else {
  127. xhMerchantExtendService::updateByMerchantId($id, $miniData);
  128. MerchantService::updateById($id, ['miniAppId' => $authorizeAppId]);
  129. Yii::info('小程序信息更新成功:' . json_encode($authorize_info));
  130. util::stop("小程序信息更新成功");
  131. }
  132. } else {
  133. //如果授权的公众号
  134. if (isset($sjWxInfo['wxAppId']) && !empty($sjWxInfo['wxAppId'])) {
  135. if ($sjWxInfo['wxAppId'] != $authorizeAppId) {
  136. util::stop('授权的公众号与商家已绑定的公众号不一致');
  137. }
  138. }
  139. $wxData = [];
  140. $appData['wxUserName'] = $user_name;
  141. $appData['wxAliasName'] = $alias;
  142. //公众号logo
  143. $wxData['logo'] = WxBaseClass::saveLogo($head_img, $id);
  144. //公众号二维码
  145. $wxData['wxQrCodeUrl'] = WxBaseClass::saveQrCode($qrCodeUrl, $id);
  146. $wxData['wxAppId'] = $authorizeAppId;
  147. $wxData['wxAccessToken'] = $authorize_access_token;
  148. $wxData['wxAccessTokenTime'] = $accessTokenTime;
  149. $wxData['wxRefreshToken'] = $authorize_refresh_token;
  150. if ($id <= 0) {
  151. $wxBaseId = 0;
  152. $msg = '未知状态';
  153. if ($id == 0) {
  154. //供货商公众号
  155. $wxBaseId = $open['wxGhsBaseId'] ?? 0;
  156. $msg = '供货商公众号绑定成功';
  157. } elseif ($id == -1) {
  158. //零售商公众号
  159. $wxBaseId = $open['wxBaseId'] ?? 0;
  160. $msg = '零售商公众号绑定成功';
  161. } else {
  162. }
  163. WxBaseClass::updateById($wxBaseId, [
  164. 'wxAccessToken' => $authorize_access_token,
  165. 'wxRefreshToken' => $authorize_refresh_token,
  166. 'wxAccessTokenTime' => date("Y-m-d H:i:s", (time() + $expires_in - 100))
  167. ]);
  168. echo $msg;
  169. Yii::$app->end();
  170. } else {
  171. $wxData['status'] = 1;//审核通过
  172. xhMerchantService::updateById($id, $wxData);
  173. MerchantExtendClass::updateByCondition(['merchantId' => $id], ['wxAppId' => $authorizeAppId, 'wxAuth' => 1]);
  174. //还没有完成初始化先进行初始化
  175. if (isset($sjWxInfo['init']) && $sjWxInfo['init'] == 0) {
  176. $data = ['merchantId' => $id];
  177. $producer = Yii::$app->rabbitmq->getProducer('openShopInitProducer');
  178. $msg = serialize($data);
  179. $producer->publish($msg, 'openShopInitExchange', 'openShopInitRoute');
  180. }
  181. echo 'ok';
  182. }
  183. }
  184. }
  185. //开放平台设置相关操作 shish 2020.1.18
  186. public function actionBindOpen()
  187. {
  188. $isOpen = Yii::$app->request->get('isOpen', 1);
  189. $merchantId = 0;
  190. if ($isOpen == 0) {
  191. //其它
  192. $merchantId = Yii::$app->request->get('account', 0);
  193. $merchant = MerchantService::getById($merchantId);
  194. if (empty($merchant)) {
  195. util::fail('没有找到商家');
  196. }
  197. $wxAppId = $merchant['wxAppId'];
  198. $miniAppId = $merchant['miniAppId'];
  199. $openAppId = $merchant['openAppId'];
  200. } elseif ($isOpen == 1) {
  201. //零售
  202. $merchant = WxOpenClass::getWxInfo();
  203. $miniAppId = $merchant['miniAppId'];
  204. $wxAppId = $merchant['wxAppId'];
  205. $openAppId = $merchant['openAppId'];
  206. } elseif ($isOpen == 2) {
  207. //供货商
  208. $merchant = WxOpenClass::getGhsWxInfo();
  209. $miniAppId = $merchant['miniAppId'];
  210. $wxAppId = $merchant['wxAppId'];
  211. $openAppId = $merchant['openAppId'];
  212. }
  213. echo "<pre>";
  214. $id = Yii::$app->request->get('actId', 0);
  215. //创建平台,并将公众号绑定到平台
  216. if ($id == 1) {
  217. wxUtil::createOpenAccount($wxAppId, $merchant, $isOpen);
  218. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  219. print_r($return);
  220. if ($return['errcode'] == 0) {
  221. $openAppId = $return['open_appid'];
  222. if ($isOpen == 0) {
  223. xhMerchantService::updateById($merchantId, ['openAppId' => $openAppId]);
  224. MerchantExtendService::updateByCondition(['merchantId' => $merchantId], ['openAppId' => $openAppId, 'openAppBind' => 1, 'wxAuth' => 1]);
  225. } elseif ($isOpen == 1) {
  226. $open = WxOpenClass::getOpen();
  227. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  228. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  229. WxMiniBaseClass::updateById($wxMiniBaseId, ['openAppId' => $openAppId]);
  230. WxBaseClass::updateById($wxBaseId, ['openAppId' => $openAppId]);
  231. } elseif ($isOpen == 2) {
  232. $open = WxOpenClass::getOpen();
  233. $wxMiniBaseId = isset($open['wxGhsMiniBaseId']) ? $open['wxGhsMiniBaseId'] : 0;
  234. $wxBaseId = isset($open['wxGhsBaseId']) ? $open['wxGhsBaseId'] : 0;
  235. WxMiniBaseClass::updateById($wxMiniBaseId, ['openAppId' => $openAppId]);
  236. WxBaseClass::updateById($wxBaseId, ['openAppId' => $openAppId]);
  237. }
  238. }
  239. }
  240. //将小程序绑定到平台
  241. if ($id == 2) {
  242. $openAppId = $merchant['openAppId'];
  243. $r = wxUtil::bindOpenAccount($miniAppId, $openAppId, $merchant, true, $isOpen);
  244. //MerchantExtendService::updateByCondition(['merchantId' => $merchantId], ['openAppBind' => 3, 'miniAuth' => 1]);
  245. print_r($r);
  246. }
  247. //查询公众号 小程序的绑定情况
  248. if ($id == 3) {
  249. Yii::info("查询公众号 小程序的绑定情况 wxAppId:{$wxAppId} isOpen:{$isOpen}");
  250. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  251. echo "公众号绑定情况:<br />";
  252. print_r($return);
  253. $return = wxUtil::getOpenAccount($miniAppId, $merchant, true, $isOpen);
  254. echo "小程序绑定情况:<br />";
  255. print_r($return);
  256. }
  257. //设置小程序服务器域名
  258. if ($id == 4) {
  259. wxUtil::setDomain($merchant, $isOpen);
  260. wxUtil::setWebViewDomain($merchant, $isOpen);
  261. }
  262. //为授权的小程序帐号上传小程序代码
  263. if ($id == 5) {
  264. //模板id
  265. $tId = Yii::$app->request->get('tId');
  266. wxUtil::miniCommit($merchant, $tId, $isOpen);
  267. }
  268. //获取小程序体验码
  269. if ($id == 6) {
  270. wxUtil::getExperienceQrCode($merchant, '', $isOpen);
  271. }
  272. //获取授权小程序帐号已设置的类目
  273. if ($id == 7) {
  274. wxUtil::getMiniCategory($merchant, $isOpen);
  275. }
  276. //获取小程序的第三方提交代码的页面配置
  277. if ($id == 8) {
  278. wxUtil::getMiniPage($merchant, $isOpen);
  279. }
  280. //将第三方提交的代码包提交审核
  281. if ($id == 9) {
  282. wxUtil::miniSubmitAudit($merchant, '', $isOpen);
  283. }
  284. //查询最新一次提交的审核状态
  285. if ($id == 10) {
  286. wxUtil::miniGetLatestAuditStatus($merchant, $isOpen);
  287. }
  288. //发布已通过审核的小程序
  289. if ($id == 11) {
  290. $respond = wxUtil::miniRelease($merchant, $isOpen);
  291. dd($respond);
  292. }
  293. //撤回审核
  294. if ($id == 12) {
  295. wxUtil::rollbackCheck($merchant, $isOpen);
  296. }
  297. //生成 申请会员页 的小程序码
  298. if ($id == 13) {
  299. wxUtil::generateMemberCode($merchant, $isOpen);
  300. }
  301. //解绑小程序和公众号绑定的平台
  302. if ($id == 14) {
  303. wxUtil::unbindAccount($merchant, $isOpen);
  304. wxUtil::unbindMiniProgram($merchant, $isOpen);
  305. }
  306. Yii::$app->end();
  307. $r = wxUtil::bindOpenAccount($miniAppId, $openAppId, $merchant, true, $isOpen);
  308. print_r($r);
  309. echo $miniAppId . ' ' . $openAppId . ' ';
  310. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  311. print_r($return);
  312. die;
  313. $return = wxUtil::createOpenAccount($wxAppId, $merchant, $isOpen);
  314. print_r($return);
  315. die;
  316. $r = wxUtil::bindOpenAccount($miniAppId, $openAppId, $merchant, false, $isOpen);
  317. print_r($r);
  318. echo $miniAppId . ' ' . $openAppId . ' ';
  319. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  320. print_r($return);
  321. $return = wxUtil::getOpenAccount($miniAppId, $merchant, false, $isOpen);
  322. print_r($return);
  323. }
  324. public function actionResult()
  325. {
  326. $get = Yii::$app->request->get();
  327. $id = isset($get['id']) ? $get['id'] : 0;
  328. $merchant = xhMerchantService::getById($id);
  329. $status = configDict::getConfig('merchantStatusName');
  330. $orMechantId = configDict::getConfig('merchantId');
  331. $originalMerchant = xhMerchantService::getById($orMechantId);
  332. return $this->renderPartial('result', ['originalMerchant' => $originalMerchant, 'merchant' => $merchant, 'status' => $status]);
  333. }
  334. public function actionCheckInviteCode()
  335. {
  336. $get = Yii::$app->request->get();
  337. $code = isset($get['code']) ? $get['code'] : '';
  338. $invite = xhInviteService::getByCode($code);
  339. if (empty($invite)) {
  340. util::fail('邀请码错误');
  341. }
  342. $now = time();
  343. if ($now > $invite['deadline']) {
  344. util::fail('邀请码已经失效');
  345. }
  346. if (!empty($invite['takeStatus'])) {
  347. util::fail('邀请码已经使用');
  348. }
  349. util::success($invite);
  350. }
  351. public function actionSelect()
  352. {
  353. $isWx = util::isWx();
  354. if ($isWx == false) {
  355. return $this->renderPartial('notWeixin');
  356. }
  357. return $this->renderPartial('select');
  358. }
  359. public function actionApi()
  360. {
  361. $openData = file_get_contents('php://input');
  362. if (isset ($openData) == false || empty($openData)) {
  363. util::stop('has no post data');
  364. }
  365. $weixinSecret = Yii::getAlias("@vendor/weixinSecret");
  366. require_once($weixinSecret . '/wxBizMsgCrypt.php');
  367. $get = Yii::$app->request->get();
  368. $encryptMsg = $openData;
  369. $openId = configDict::getConfig('openId');
  370. $open = xhWxOpenService::getById($openId);
  371. $encodingAesKey = $open['aesKey'];
  372. $token = $open['token'];
  373. $appId = $open['appId'];
  374. $appSecret = $open['appSecret'];
  375. $signature = isset($get['signature']) ? $get['signature'] : '';
  376. $timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
  377. $nonce = isset($get['nonce']) ? $get['nonce'] : '';
  378. $encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
  379. $msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
  380. $xml_tree = new \DOMDocument();
  381. $xml_tree->loadXML($encryptMsg);
  382. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  383. $encrypt = $array_e->item(0)->nodeValue;
  384. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  385. $from_xml = sprintf($format, $encrypt);
  386. $formString = " token:" . $token . "\n encodingAesKey:" . $encodingAesKey . "\n appId:" . $appId . "\n msg_signature:" . $msg_signature . "\n appSecret:" . $appSecret . "\n timestamp:" . $timestamp . "\n nonce:" . $nonce;
  387. Yii::warning($formString);
  388. $formString2 = "from_xml:" . $from_xml;
  389. Yii::warning($formString2);
  390. //第三方收到公众号平台发送的消息
  391. $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
  392. $msg = '';//解密后的消息
  393. $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
  394. if ($errCode != 0) {
  395. Yii::warning("解密未成功,错误代码:" . $errCode . "。(出现过的情况汇总:-40001 有可能token、encodingAesKey、appId等填写不对,请仔细核对每个值)");
  396. util::stop();
  397. }
  398. $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
  399. $infoType = $postObj->InfoType;
  400. switch ($infoType) {
  401. //推送 componentVerifyTicket
  402. case 'component_verify_ticket':
  403. $componentVerifyTicket = $postObj->ComponentVerifyTicket;
  404. if (!empty($componentVerifyTicket)) {
  405. $oData = [];
  406. $oData['verifyTicket'] = (string)$componentVerifyTicket;
  407. $oData['verifyTicketTime'] = date("Y-m-d H:i:s", $timestamp + 600);//每10分钟(600秒)推一次
  408. xhWxOpenService::updateById($openId, $oData);
  409. Yii::warning("component_verify_ticket:" . (string)$componentVerifyTicket);
  410. util::stop('success');
  411. }
  412. break;
  413. //取消授权通知
  414. case 'unauthorized':
  415. $AppId = $postObj->AppId;
  416. $authorizeAppid = $postObj->AuthorizerAppid;
  417. $merchant = xhMerchantService::getByAppId($authorizeAppid);
  418. if (!empty($merchant)) {
  419. $merchantId = $merchant['id'];
  420. //自主冻结
  421. xhMerchantService::updateById($merchantId, ['status' => 5]);
  422. }
  423. break;
  424. //授权成功通知
  425. case 'authorized':
  426. $AppId = $postObj->AppId;//第三方平台appid
  427. $CreateTime = $postObj->CreateTime;//公众号
  428. $authorizeAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
  429. $AuthorizationCode = $postObj->AuthorizationCode;
  430. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
  431. break;
  432. //授权更新通知
  433. case 'updateauthorized':
  434. $AppId = $postObj->AppId;
  435. $CreateTime = $postObj->CreateTime;
  436. $authorizeAppid = $postObj->AuthorizerAppid;
  437. $AuthorizationCode = $postObj->AuthorizationCode;
  438. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
  439. util::stop();
  440. $authorize = wxUtil::getAuthorizer($AuthorizationCode);
  441. if (isset($authorize['authorization_info']) == false) {
  442. $msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
  443. Yii::warning($msg, __METHOD__);
  444. Yii::$app->end();
  445. }
  446. $info = $authorize['authorization_info'];
  447. $authorizeAppId = $info['authorizer_appid'];
  448. $authorize_access_token = $info['authorizer_access_token'];
  449. $expires_in = $info['expires_in'];//7200
  450. $authorize_refresh_token = $info['authorizer_refresh_token'];
  451. //查询是否零售或供货商使用的公众号
  452. $hasChange = false;
  453. $wxBase = WxBaseClass::getAllByCondition(['id>' => 0], null, '*');
  454. if (!empty($wxBase)) {
  455. foreach ($wxBase as $wx) {
  456. if (isset($wx['wxAppId']) && $wx['wxAppId'] == $authorizeAppId) {
  457. $id = $wx['id'];
  458. WxBaseClass::updateById($id, [
  459. 'wxAccessToken' => $authorize_access_token,
  460. 'wxRefreshToken' => $authorize_refresh_token,
  461. 'wxAccessTokenTime' => date("Y-m-d H:i:s", (time() + $expires_in - 100))
  462. ]);
  463. $hasChange = true;
  464. }
  465. }
  466. }
  467. if ($hasChange == false) {
  468. $merchant = xhMerchantService::getByAppId($authorizeAppId);
  469. if (empty($merchant)) {
  470. return false;
  471. }
  472. $merchantId = $merchant['id'];
  473. $wxData = [];
  474. $wxData['wxAccessToken'] = $authorize_access_token;
  475. $wxData['wxRefreshToken'] = $authorize_refresh_token;
  476. $wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  477. $wxData['status'] = 1;
  478. xhMerchantService::updateById($merchantId, $wxData);
  479. }
  480. break;
  481. default:
  482. }
  483. Yii::warning("---wx open data end--- \n");
  484. }
  485. //获取平台信息 shish 2020.4.12
  486. public function actionGetWxOpen()
  487. {
  488. $open = WxOpenService::getOpen();
  489. util::success($open);
  490. }
  491. //平台没有绑定商家公众号时初始化一个商家 shish 2020.2.11
  492. public function actionAddMerchant()
  493. {
  494. AdClass::initAdd(12362);
  495. $return = MerchantService::initOpenMerchant();
  496. dd($return);
  497. }
  498. public function actionCheckMerchantName()
  499. {
  500. $get = Yii::$app->request->get();
  501. $merchantName = isset($get['merchantName']) ? $get['merchantName'] : '';
  502. $m = xhMerchantService::getByMerchantName($merchantName);
  503. if (!empty($m)) {
  504. util::fail('公众号名称已经使用');
  505. }
  506. util::complete();
  507. }
  508. /**
  509. * 下载微信里的图片到服务器
  510. */
  511. public function actionDownloadWxImg()
  512. {
  513. $get = Yii::$app->request->get();
  514. $mediaId = isset($get['mediaId']) ? $get['mediaId'] : '';
  515. $imgList = wxUtil::downloadmediaIdImg($this->merchant, $mediaId);
  516. if (!empty($imgList)) {
  517. util::success($imgList);
  518. }
  519. util::fail();
  520. }
  521. }