WxOpenController.php 25 KB

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