WxOpenController.php 25 KB

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