WxOpenController.php 25 KB

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