WxOpenController.php 25 KB

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