WxOpenController.php 27 KB

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