WxOpenController.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. echo 'ok';
  226. }
  227. }
  228. }
  229. //开放平台设置相关操作 ssh 2020.1.18
  230. //供货商使用 https://api.pt.hzghd.com/wx-open/bind-open?actId=3
  231. //花店使用 https://api.pt.huaml.com/wx-open/bind-open?actId=3
  232. //商城使用 https://api.pt.theflorist.cn/wx-open/bind-open?actId=3
  233. public function actionBindOpen()
  234. {
  235. $host = Yii::$app->request->getHostInfo();
  236. if (strpos($host, 'huaml.com') !== false || strpos($host, 'huahb.cn') !== false) {
  237. //花店
  238. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'hd');
  239. $isOpen = 1;
  240. } elseif (strpos($host, 'theflorist.cn') !== false || strpos($host, 'zhiguanhua.cn') !== false) {
  241. //商城
  242. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'mall');
  243. $isOpen = 3;
  244. } elseif (strpos($host, 'hzghd.com') !== false || strpos($host, 'wixhb.com') !== false) {
  245. //供货商
  246. Yii::$app->params['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  247. $isOpen = 2;
  248. } else {
  249. util::stop('域名无效');
  250. }
  251. $sjId = 0;
  252. if ($isOpen == 0) {
  253. //其它
  254. $sjId = Yii::$app->request->get('account', 0);
  255. $merchant = MerchantService::getById($sjId);
  256. if (empty($merchant)) {
  257. util::fail('没有找到商家');
  258. }
  259. $wxAppId = $merchant['wxAppId'];
  260. $miniAppId = $merchant['miniAppId'];
  261. $openAppId = $merchant['openAppId'];
  262. } elseif ($isOpen == 1) {
  263. //零售
  264. $merchant = WxOpenClass::getWxInfo();
  265. $miniAppId = $merchant['miniAppId'];
  266. $wxAppId = $merchant['wxAppId'];
  267. $openAppId = $merchant['openAppId'];
  268. } elseif ($isOpen == 2) {
  269. //供货商
  270. $merchant = WxOpenClass::getGhsWxInfo();
  271. $miniAppId = $merchant['miniAppId'];
  272. $wxAppId = $merchant['wxAppId'];
  273. $openAppId = $merchant['openAppId'];
  274. } elseif ($isOpen == 3) {
  275. $merchant = WxOpenClass::getMallWxInfo();
  276. $miniAppId = $merchant['miniAppId'];
  277. $wxAppId = $merchant['wxAppId'];
  278. $openAppId = $merchant['openAppId'];
  279. }
  280. echo "<pre>";
  281. $actId = Yii::$app->request->get('actId', 0);
  282. //创建平台,并将公众号绑定到平台
  283. if ($actId == 1) {
  284. echo '这步已经不需要做了,请手动去绑定';
  285. util::stop('这步已经不需要做了');
  286. wxUtil::createOpenAccount($wxAppId, $merchant, $isOpen);
  287. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  288. print_r($return);
  289. if ($return['errcode'] == 0) {
  290. $openAppId = $return['open_appid'];
  291. if ($isOpen == 0) {
  292. xhMerchantService::updateById($sjId, ['openAppId' => $openAppId]);
  293. MerchantExtendService::updateByCondition(['sjId' => $sjId], ['openAppId' => $openAppId, 'openAppBind' => 1, 'wxAuth' => 1]);
  294. } elseif ($isOpen == 1) {
  295. $open = WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  296. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  297. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  298. WxMiniBaseClass::updateById($wxMiniBaseId, ['openAppId' => $openAppId]);
  299. WxBaseClass::updateById($wxBaseId, ['openAppId' => $openAppId]);
  300. } elseif ($isOpen == 2) {
  301. $open = WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  302. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  303. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  304. WxMiniBaseClass::updateById($wxMiniBaseId, ['openAppId' => $openAppId]);
  305. WxBaseClass::updateById($wxBaseId, ['openAppId' => $openAppId]);
  306. } elseif ($isOpen == 3) {
  307. $open = WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  308. $wxMiniBaseId = isset($open['wxMiniBaseId']) ? $open['wxMiniBaseId'] : 0;
  309. $wxBaseId = isset($open['wxBaseId']) ? $open['wxBaseId'] : 0;
  310. WxMiniBaseClass::updateById($wxMiniBaseId, ['openAppId' => $openAppId]);
  311. WxBaseClass::updateById($wxBaseId, ['openAppId' => $openAppId]);
  312. }
  313. }
  314. }
  315. //将小程序绑定到平台
  316. if ($actId == 2) {
  317. echo '这步已经不需要做了,请手动去绑定';
  318. util::stop('这步已经不需要做了');
  319. $openAppId = $merchant['openAppId'];
  320. $r = wxUtil::bindOpenAccount($miniAppId, $openAppId, $merchant, true, $isOpen);
  321. //MerchantExtendService::updateByCondition(['sjId' => $sjId], ['openAppBind' => 3, 'miniAuth' => 1]);
  322. print_r($r);
  323. }
  324. //查询公众号 小程序的绑定情况
  325. if ($actId == 3) {
  326. Yii::info("查询公众号 小程序的绑定情况 wxAppId:{$wxAppId} isOpen:{$isOpen}");
  327. $return = wxUtil::getOpenAccount($wxAppId, $merchant, false, $isOpen);
  328. echo "公众号绑定情况:<br />";
  329. print_r($return);
  330. $return = wxUtil::getOpenAccount($miniAppId, $merchant, true, $isOpen);
  331. echo "小程序绑定情况:<br />";
  332. print_r($return);
  333. }
  334. //设置小程序服务器域名
  335. if ($actId == 4) {
  336. wxUtil::setDomain($merchant, $isOpen);
  337. wxUtil::setWebViewDomain($merchant, $isOpen);
  338. }
  339. //为授权的小程序帐号上传小程序代码
  340. if ($actId == 5) {
  341. //模板id
  342. $tId = Yii::$app->request->get('tId');
  343. wxUtil::miniCommit($merchant, $tId, $isOpen);
  344. }
  345. //获取小程序体验码
  346. if ($actId == 6) {
  347. wxUtil::getExperienceQrCode($merchant, '', $isOpen);
  348. }
  349. //获取授权小程序帐号已设置的类目
  350. if ($actId == 7) {
  351. wxUtil::getMiniCategory($merchant, $isOpen);
  352. }
  353. //获取小程序的第三方提交代码的页面配置
  354. if ($actId == 8) {
  355. wxUtil::getMiniPage($merchant, $isOpen);
  356. }
  357. //将第三方提交的代码包提交审核
  358. if ($actId == 9) {
  359. $returnInfo = wxUtil::miniSubmitAudit($merchant, '', $isOpen);
  360. echo "<pre>";
  361. print_r($returnInfo);
  362. $auditId = $returnInfo['auditid'] ?? '';
  363. if (!empty($auditId)) {
  364. $respond = wxUtil::speedupAudit($merchant, $auditId, $isOpen);
  365. echo "加急审核:{$auditId}<br />";
  366. print_r($respond);
  367. }
  368. }
  369. //查询最新一次提交的审核状态 0 审核成功 1 审核被拒绝 2 审核中 3 已撤回
  370. if ($actId == 10) {
  371. wxUtil::miniGetLatestAuditStatus($merchant, $isOpen);
  372. }
  373. //发布已通过审核的小程序
  374. if ($actId == 11) {
  375. $respond = wxUtil::miniRelease($merchant, $isOpen);
  376. dd($respond);
  377. }
  378. //撤回审核
  379. if ($actId == 12) {
  380. wxUtil::rollbackCheck($merchant, $isOpen);
  381. }
  382. //查询服务商的当月提审限额
  383. if ($actId == 13) {
  384. wxUtil::queryQuota($merchant, $isOpen);
  385. }
  386. //解绑小程序和公众号绑定的平台
  387. if ($actId == 14) {
  388. echo '<pre>';
  389. Yii::info("解绑信息:" . json_encode($merchant));
  390. $r = wxUtil::unbindAccount($merchant, $isOpen);
  391. print_r($r);
  392. $r = wxUtil::unbindMiniProgram($merchant, $isOpen);
  393. print_r($r);
  394. Yii::$app->end();
  395. }
  396. //回退到上一个版本
  397. if ($actId == 15) {
  398. wxUtil::revertCodeRelease($merchant, $isOpen);
  399. }
  400. //申请会员的小程序码
  401. if ($actId == 16) {
  402. wxUtil::generateMemberCode($merchant, $isOpen);
  403. }
  404. //完善隐私协议
  405. if ($actId == 17) {
  406. wxUtil::privacySetting($merchant, $isOpen);
  407. }
  408. //申请设置订单页path信息
  409. if ($actId == 18) {
  410. $open = [];
  411. if ($isOpen == 1) {
  412. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'hd')]);
  413. } elseif ($isOpen == 2) {
  414. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'ghs')]);
  415. } elseif ($isOpen == 3) {
  416. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => dict::getDict('ptStyle', 'mall')]);
  417. } else {
  418. util::stop('没有找到平台信息');
  419. }
  420. wxUtil::applySetOrderPathInfo($merchant, $open);
  421. }
  422. //查询小程序用户隐私保护指引
  423. if ($actId == 19) {
  424. wxUtil::privacySettingSelect($merchant, $isOpen);
  425. }
  426. }
  427. public function actionResult()
  428. {
  429. $get = Yii::$app->request->get();
  430. $id = isset($get['id']) ? $get['id'] : 0;
  431. $merchant = xhMerchantService::getById($id);
  432. $status = dict::getDict('merchantStatusName');
  433. $currentMerchantId = dict::getDict('sjId');
  434. $originalMerchant = xhMerchantService::getById($currentMerchantId);
  435. return $this->renderPartial('result', ['originalMerchant' => $originalMerchant, 'merchant' => $merchant, 'status' => $status]);
  436. }
  437. public function actionCheckInviteCode()
  438. {
  439. $get = Yii::$app->request->get();
  440. $code = isset($get['code']) ? $get['code'] : '';
  441. $invite = xhInviteService::getByCode($code);
  442. if (empty($invite)) {
  443. util::fail('邀请码错误');
  444. }
  445. $now = time();
  446. if ($now > $invite['deadline']) {
  447. util::fail('邀请码已经失效');
  448. }
  449. if (!empty($invite['takeStatus'])) {
  450. util::fail('邀请码已经使用');
  451. }
  452. util::success($invite);
  453. }
  454. public function actionSelect()
  455. {
  456. $isWx = util::isWx();
  457. if ($isWx == false) {
  458. return $this->renderPartial('notWeixin');
  459. }
  460. return $this->renderPartial('select');
  461. }
  462. public function actionApi()
  463. {
  464. $get = Yii::$app->request->get();
  465. $style = $get['style'] ?? dict::getDict('ptStyle', 'hd');
  466. $openData = file_get_contents('php://input');
  467. if (isset ($openData) == false || empty($openData)) {
  468. util::stop('has no post data');
  469. }
  470. $wxSecret = Yii::getAlias("@vendor/weixinSecret");
  471. require_once($wxSecret . '/wxBizMsgCrypt.php');
  472. $encryptMsg = $openData;
  473. $open = \biz\wx\classes\WxOpenClass::getByCondition(['style' => $style]);
  474. $openId = $open['id'];
  475. $encodingAesKey = $open['aesKey'];
  476. $token = $open['token'];
  477. $appId = $open['appId'];
  478. $appSecret = $open['appSecret'];
  479. $signature = isset($get['signature']) ? $get['signature'] : '';
  480. $timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
  481. $nonce = isset($get['nonce']) ? $get['nonce'] : '';
  482. $encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
  483. $msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
  484. $xml_tree = new \DOMDocument();
  485. $xml_tree->loadXML($encryptMsg);
  486. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  487. $encrypt = $array_e->item(0)->nodeValue;
  488. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  489. $from_xml = sprintf($format, $encrypt);
  490. $formString = " token:" . $token . "\n encodingAesKey:" . $encodingAesKey . "\n appId:" . $appId . "\n msg_signature:" . $msg_signature . "\n appSecret:" . $appSecret . "\n timestamp:" . $timestamp . "\n nonce:" . $nonce;
  491. Yii::warning($formString);
  492. $formString2 = "from_xml:" . $from_xml;
  493. Yii::warning($formString2);
  494. //第三方收到公众号平台发送的消息
  495. $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
  496. $msg = '';//解密后的消息
  497. $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
  498. if ($errCode != 0) {
  499. Yii::warning("解密未成功,错误代码:" . $errCode . "。(出现过的情况汇总:-40001 有可能token、encodingAesKey、appId等填写不对,请仔细核对每个值)");
  500. util::stop();
  501. }
  502. $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
  503. $infoType = $postObj->InfoType;
  504. switch ($infoType) {
  505. //推送 componentVerifyTicket
  506. case 'component_verify_ticket':
  507. $componentVerifyTicket = $postObj->ComponentVerifyTicket;
  508. if (!empty($componentVerifyTicket)) {
  509. $oData = [];
  510. $oData['verifyTicket'] = (string)$componentVerifyTicket;
  511. $oData['verifyTicketTime'] = date("Y-m-d H:i:s", $timestamp + 600);//每10分钟(600秒)推一次
  512. xhWxOpenService::updateById($openId, $oData);
  513. Yii::warning("component_verify_ticket:" . (string)$componentVerifyTicket);
  514. util::stop('success');
  515. }
  516. break;
  517. //取消授权通知
  518. case 'unauthorized':
  519. //这里没有往下操作,请看本文件callback()方法
  520. break;
  521. //授权成功通知
  522. case 'authorized':
  523. $AppId = $postObj->AppId;
  524. $authorizeAppId = $postObj->AuthorizerAppid;
  525. $CreateTime = $postObj->CreateTime;//公众号
  526. $authorizeAppId = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
  527. $AuthorizationCode = $postObj->AuthorizationCode;
  528. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
  529. //这里没有往下操作,请看本文件callback()方法
  530. break;
  531. //授权更新通知
  532. case 'updateauthorized':
  533. $AppId = $postObj->AppId;
  534. $authorizeAppId = $postObj->AuthorizerAppid;
  535. $CreateTime = $postObj->CreateTime;
  536. $authorizeAppId = $postObj->AuthorizerAppid;
  537. $AuthorizationCode = $postObj->AuthorizationCode;
  538. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
  539. //这里没有往下操作,请看本文件callback()方法
  540. break;
  541. default:
  542. }
  543. Yii::warning("---wx open data end--- \n");
  544. }
  545. public function actionCheckName()
  546. {
  547. $get = Yii::$app->request->get();
  548. $name = isset($get['name']) ? $get['name'] : '';
  549. $m = xhMerchantService::getByName($name);
  550. if (!empty($m)) {
  551. util::fail('公众号名称已经使用');
  552. }
  553. util::complete();
  554. }
  555. /**
  556. * 下载微信里的图片到服务器
  557. */
  558. public function actionDownloadWxImg()
  559. {
  560. $get = Yii::$app->request->get();
  561. $mediaId = isset($get['mediaId']) ? $get['mediaId'] : '';
  562. $imgList = wxUtil::downloadmediaIdImg($this->merchant, $mediaId);
  563. if (!empty($imgList)) {
  564. util::success($imgList);
  565. }
  566. util::fail();
  567. }
  568. //获取平台信息 ssh 2020.4.12
  569. public function actionGetWxOpen()
  570. {
  571. util::success([]);
  572. }
  573. }