WxOpenController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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', 'hd');
  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'] ?? 'hd';
  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 == 'ghs') {
  83. //这是供货商
  84. $sjWxInfo = WxOpenClass::getGhsWxInfo();
  85. } elseif ($id == 'hd') {
  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 (is_numeric($id) == false) {
  109. $wxMiniBaseId = 0;
  110. $msg = '未知状态';
  111. if ($id == 'ghs') {
  112. //供货商
  113. $wxMiniBaseId = $open['wxGhsMiniBaseId'] ?? 0;
  114. $msg = '供货商小程序绑定成功';
  115. } elseif ($id == 'hd') {
  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("授权的公众号与商家已绑定的公众号不一致,授权公众号appId:{$authorizeAppId} 商家公众号:{$sjWxInfo['wxAppId']}");
  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 (is_numeric($id) == false) {
  154. $wxBaseId = 0;
  155. $msg = '未知状态';
  156. if ($id == 'ghs') {
  157. //供货商公众号
  158. $wxBaseId = $open['wxGhsBaseId'] ?? 0;
  159. $msg = '供货商公众号绑定成功';
  160. } elseif ($id == 'hd') {
  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. echo '<pre>';
  307. Yii::info("解绑信息:" . json_encode($merchant));
  308. $r = wxUtil::unbindAccount($merchant, $isOpen);
  309. print_r($r);
  310. $r = wxUtil::unbindMiniProgram($merchant, $isOpen);
  311. print_r($r);
  312. Yii::$app->end();
  313. }
  314. Yii::$app->end();
  315. $r = wxUtil::bindOpenAccount($miniAppId, $openAppId, $merchant, true, $isOpen);
  316. print_r($r);
  317. echo $miniAppId . ' ' . $openAppId . ' ';
  318. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  319. print_r($return);
  320. die;
  321. $return = wxUtil::createOpenAccount($wxAppId, $merchant, $isOpen);
  322. print_r($return);
  323. die;
  324. $r = wxUtil::bindOpenAccount($miniAppId, $openAppId, $merchant, false, $isOpen);
  325. print_r($r);
  326. echo $miniAppId . ' ' . $openAppId . ' ';
  327. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  328. print_r($return);
  329. $return = wxUtil::getOpenAccount($miniAppId, $merchant, false, $isOpen);
  330. print_r($return);
  331. }
  332. public function actionResult()
  333. {
  334. $get = Yii::$app->request->get();
  335. $id = isset($get['id']) ? $get['id'] : 0;
  336. $merchant = xhMerchantService::getById($id);
  337. $status = configDict::getConfig('merchantStatusName');
  338. $orMechantId = configDict::getConfig('merchantId');
  339. $originalMerchant = xhMerchantService::getById($orMechantId);
  340. return $this->renderPartial('result', ['originalMerchant' => $originalMerchant, 'merchant' => $merchant, 'status' => $status]);
  341. }
  342. public function actionCheckInviteCode()
  343. {
  344. $get = Yii::$app->request->get();
  345. $code = isset($get['code']) ? $get['code'] : '';
  346. $invite = xhInviteService::getByCode($code);
  347. if (empty($invite)) {
  348. util::fail('邀请码错误');
  349. }
  350. $now = time();
  351. if ($now > $invite['deadline']) {
  352. util::fail('邀请码已经失效');
  353. }
  354. if (!empty($invite['takeStatus'])) {
  355. util::fail('邀请码已经使用');
  356. }
  357. util::success($invite);
  358. }
  359. public function actionSelect()
  360. {
  361. $isWx = util::isWx();
  362. if ($isWx == false) {
  363. return $this->renderPartial('notWeixin');
  364. }
  365. return $this->renderPartial('select');
  366. }
  367. public function actionApi()
  368. {
  369. $openData = file_get_contents('php://input');
  370. if (isset ($openData) == false || empty($openData)) {
  371. util::stop('has no post data');
  372. }
  373. $weixinSecret = Yii::getAlias("@vendor/weixinSecret");
  374. require_once($weixinSecret . '/wxBizMsgCrypt.php');
  375. $get = Yii::$app->request->get();
  376. $encryptMsg = $openData;
  377. $openId = configDict::getConfig('openId');
  378. $open = xhWxOpenService::getById($openId);
  379. $encodingAesKey = $open['aesKey'];
  380. $token = $open['token'];
  381. $appId = $open['appId'];
  382. $appSecret = $open['appSecret'];
  383. $signature = isset($get['signature']) ? $get['signature'] : '';
  384. $timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
  385. $nonce = isset($get['nonce']) ? $get['nonce'] : '';
  386. $encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
  387. $msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
  388. $xml_tree = new \DOMDocument();
  389. $xml_tree->loadXML($encryptMsg);
  390. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  391. $encrypt = $array_e->item(0)->nodeValue;
  392. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  393. $from_xml = sprintf($format, $encrypt);
  394. $formString = " token:" . $token . "\n encodingAesKey:" . $encodingAesKey . "\n appId:" . $appId . "\n msg_signature:" . $msg_signature . "\n appSecret:" . $appSecret . "\n timestamp:" . $timestamp . "\n nonce:" . $nonce;
  395. Yii::warning($formString);
  396. $formString2 = "from_xml:" . $from_xml;
  397. Yii::warning($formString2);
  398. //第三方收到公众号平台发送的消息
  399. $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
  400. $msg = '';//解密后的消息
  401. $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
  402. if ($errCode != 0) {
  403. Yii::warning("解密未成功,错误代码:" . $errCode . "。(出现过的情况汇总:-40001 有可能token、encodingAesKey、appId等填写不对,请仔细核对每个值)");
  404. util::stop();
  405. }
  406. $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
  407. $infoType = $postObj->InfoType;
  408. switch ($infoType) {
  409. //推送 componentVerifyTicket
  410. case 'component_verify_ticket':
  411. $componentVerifyTicket = $postObj->ComponentVerifyTicket;
  412. if (!empty($componentVerifyTicket)) {
  413. $oData = [];
  414. $oData['verifyTicket'] = (string)$componentVerifyTicket;
  415. $oData['verifyTicketTime'] = date("Y-m-d H:i:s", $timestamp + 600);//每10分钟(600秒)推一次
  416. xhWxOpenService::updateById($openId, $oData);
  417. Yii::warning("component_verify_ticket:" . (string)$componentVerifyTicket);
  418. util::stop('success');
  419. }
  420. break;
  421. //取消授权通知
  422. case 'unauthorized':
  423. $AppId = $postObj->AppId;
  424. $authorizeAppid = $postObj->AuthorizerAppid;
  425. $merchant = xhMerchantService::getByAppId($authorizeAppid);
  426. if (!empty($merchant)) {
  427. $merchantId = $merchant['id'];
  428. //自主冻结
  429. xhMerchantService::updateById($merchantId, ['status' => 5]);
  430. }
  431. break;
  432. //授权成功通知
  433. case 'authorized':
  434. $AppId = $postObj->AppId;//第三方平台appid
  435. $CreateTime = $postObj->CreateTime;//公众号
  436. $authorizeAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
  437. $AuthorizationCode = $postObj->AuthorizationCode;
  438. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
  439. break;
  440. //授权更新通知
  441. case 'updateauthorized':
  442. $AppId = $postObj->AppId;
  443. $CreateTime = $postObj->CreateTime;
  444. $authorizeAppid = $postObj->AuthorizerAppid;
  445. $AuthorizationCode = $postObj->AuthorizationCode;
  446. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
  447. util::stop();
  448. $authorize = wxUtil::getAuthorizer($AuthorizationCode);
  449. if (isset($authorize['authorization_info']) == false) {
  450. $msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
  451. Yii::warning($msg, __METHOD__);
  452. Yii::$app->end();
  453. }
  454. $info = $authorize['authorization_info'];
  455. $authorizeAppId = $info['authorizer_appid'];
  456. $authorize_access_token = $info['authorizer_access_token'];
  457. $expires_in = $info['expires_in'];//7200
  458. $authorize_refresh_token = $info['authorizer_refresh_token'];
  459. //查询是否零售或供货商使用的公众号
  460. $hasChange = false;
  461. $wxBase = WxBaseClass::getAllByCondition(['id>' => 0], null, '*');
  462. if (!empty($wxBase)) {
  463. foreach ($wxBase as $wx) {
  464. if (isset($wx['wxAppId']) && $wx['wxAppId'] == $authorizeAppId) {
  465. $id = $wx['id'];
  466. WxBaseClass::updateById($id, [
  467. 'wxAccessToken' => $authorize_access_token,
  468. 'wxRefreshToken' => $authorize_refresh_token,
  469. 'wxAccessTokenTime' => date("Y-m-d H:i:s", (time() + $expires_in - 100))
  470. ]);
  471. $hasChange = true;
  472. }
  473. }
  474. }
  475. if ($hasChange == false) {
  476. $merchant = xhMerchantService::getByAppId($authorizeAppId);
  477. if (empty($merchant)) {
  478. return false;
  479. }
  480. $merchantId = $merchant['id'];
  481. $wxData = [];
  482. $wxData['wxAccessToken'] = $authorize_access_token;
  483. $wxData['wxRefreshToken'] = $authorize_refresh_token;
  484. $wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  485. $wxData['status'] = 1;
  486. xhMerchantService::updateById($merchantId, $wxData);
  487. }
  488. break;
  489. default:
  490. }
  491. Yii::warning("---wx open data end--- \n");
  492. }
  493. //获取平台信息 shish 2020.4.12
  494. public function actionGetWxOpen()
  495. {
  496. $open = WxOpenService::getOpen();
  497. util::success($open);
  498. }
  499. //平台没有绑定商家公众号时初始化一个商家 shish 2020.2.11
  500. public function actionAddMerchant()
  501. {
  502. AdClass::initAdd(12362);
  503. $return = MerchantService::initOpenMerchant();
  504. dd($return);
  505. }
  506. public function actionCheckMerchantName()
  507. {
  508. $get = Yii::$app->request->get();
  509. $merchantName = isset($get['merchantName']) ? $get['merchantName'] : '';
  510. $m = xhMerchantService::getByMerchantName($merchantName);
  511. if (!empty($m)) {
  512. util::fail('公众号名称已经使用');
  513. }
  514. util::complete();
  515. }
  516. /**
  517. * 下载微信里的图片到服务器
  518. */
  519. public function actionDownloadWxImg()
  520. {
  521. $get = Yii::$app->request->get();
  522. $mediaId = isset($get['mediaId']) ? $get['mediaId'] : '';
  523. $imgList = wxUtil::downloadmediaIdImg($this->merchant, $mediaId);
  524. if (!empty($imgList)) {
  525. util::success($imgList);
  526. }
  527. util::fail();
  528. }
  529. }