WxOpenController.php 24 KB

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