WxOpenController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. namespace www\controllers;
  3. use common\services\xhMerchantExtendService;
  4. use Yii;
  5. use common\services\xhApplyOrderService;
  6. use common\services\xhWxMenuService;
  7. use common\services\xhMerchantService;
  8. use common\services\xhInviteService;
  9. use common\components\util;
  10. use common\components\configDict;
  11. use common\services\xhWxOpenService;
  12. use common\components\stringUtil;
  13. use common\components\wxUtil;
  14. use common\services\xhSetMealService;
  15. use yii\helpers\Json;
  16. use common\models\xhWxOpen;
  17. use linslin\yii2\curl;
  18. /**
  19. * 微信开放平台相关
  20. */
  21. class WxOpenController extends PublicController
  22. {
  23. public $withoutLogin = ['callback-create-first-merchant'];
  24. //开放平台开始授权 shish 2020.1.16
  25. public function actionBeginAuth()
  26. {
  27. $id = 1;
  28. $open = xhWxOpenService::getById($id);
  29. $pre = wxUtil::getPreAuthCode($open);
  30. $appId = $open['appId'];
  31. $oData = [];
  32. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s");
  33. //设置预授码过期,让下个用户可以获取新的预授码
  34. xhWxOpenService::updateById($id, $oData);
  35. $url = Yii::$app->params['wHost'] . '/wx-open/callback';
  36. $merchantId = $open['merchantId'];
  37. //如果平台没有对应的商家,先去创建第一个商家并做关联
  38. if (empty($merchantId)) {
  39. $url = Yii::$app->params['wHost'] . '/wx-open/callback-create-first-merchant';
  40. }
  41. echo "<a href='https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$appId}&pre_auth_code={$pre}&redirect_uri={$url}'>授权</a>";
  42. }
  43. public function actionCheckMerchantName()
  44. {
  45. $get = Yii::$app->request->get();
  46. $merchantName = isset($get['merchantName']) ? $get['merchantName'] : '';
  47. $m = xhMerchantService::getByMerchantName($merchantName);
  48. if (!empty($m)) {
  49. util::fail('公众号名称已经使用');
  50. }
  51. util::ok();
  52. }
  53. /**
  54. * 下载微信里的图片到服务器
  55. */
  56. public function actionDownloadWxImg()
  57. {
  58. $get = Yii::$app->request->get();
  59. $mediaId = isset($get['mediaId']) ? $get['mediaId'] : '';
  60. $imgList = wxUtil::downloadmediaIdImg($this->merchant, $mediaId);
  61. if (!empty($imgList)) {
  62. util::success($imgList);
  63. }
  64. util::fail();
  65. }
  66. //创建平台使用的公众号
  67. public function actionCallbackCreateFirstMerchant()
  68. {
  69. $get = Yii::$app->request->get();
  70. if (isset($get['auth_code']) == false) {
  71. util::stop('公众号授权回调未成功');
  72. }
  73. $code = $get['auth_code'];
  74. //使用授权码换取公众号的接口调用凭据和授权信息
  75. $authorize = wxUtil::getAuthorizer($code);
  76. if (isset($authorize['authorization_info']) == false) {
  77. util::stop('未换取公众号的接口调用凭据和授权信息');
  78. }
  79. $info = $authorize['authorization_info'];
  80. $authorizeAppId = $info['authorizer_appid'];
  81. $authorize_access_token = $info['authorizer_access_token'];
  82. $expires_in = $info['expires_in'];//7200
  83. $authorize_refresh_token = $info['authorizer_refresh_token'];
  84. //获取授权方的公众号帐号基本信息
  85. $authorizeDetailInfo = wxUtil::getAuthorizerInfo($authorizeAppId);
  86. if (isset($authorizeDetailInfo['authorizer_info']) == false) {
  87. util::stop('没有获取到公众号帐号基本信息');
  88. }
  89. $authorize_info = $authorizeDetailInfo['authorizer_info'];
  90. $nick_name = $authorize_info['nick_name'];
  91. $head_img = $authorize_info['head_img'];
  92. $service_type_info = $authorize_info['service_type_info'];
  93. if ($service_type_info['id'] != 2) {
  94. util::stop('公众号不是服务号');
  95. }
  96. $verify_type_info = $authorize_info['verify_type_info'];
  97. if ($verify_type_info['id'] != 0) {
  98. util::stop('公众号还没有认证哦');
  99. }
  100. $user_name = $authorize_info['user_name'];
  101. $alias = $authorize_info['alias'];
  102. $qrCodeUrl = $authorize_info['qrcode_url'];
  103. $business_info = $authorize_info['business_info'];
  104. $open_pay = $business_info['open_pay'];
  105. $merchant = xhMerchantService::getByAppId($authorizeAppId);
  106. $wxAccessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒
  107. if (!empty($merchant)) {
  108. util::stop('此公众号已经授权过了');
  109. }
  110. //引导生成商家
  111. $date = date("Y-m-d H:i:s");
  112. $trainDemo = configDict::getConfig('trainDemo');
  113. //$logo = xhMerchantService::logoSave($head_img, $authorizeAppId);
  114. $logo = '';
  115. $appData['logo'] = $logo;
  116. //保存公众号二维码
  117. //$wxQrCodeUrl = xhMerchantService::qrCodeSave($qrCodeUrl, $authorizeAppId);
  118. $wxQrCodeUrl = '';
  119. $appData['wxQrcodeUrl'] = $wxQrCodeUrl;
  120. $appData['wxAppId'] = $authorizeAppId;
  121. $appData['wxUserName'] = $user_name;
  122. $appData['wxAliasName'] = $alias;
  123. $appData['wxAccessToken'] = $authorize_access_token;
  124. $appData['wxAccessTokenTime'] = $wxAccessTokenTime;
  125. $appData['wxRefreshToken'] = $authorize_refresh_token;
  126. $appData['accountStyle'] = 0;//认证服务号
  127. $appData['status'] = 0;//待审核
  128. $appData['createTime'] = $date;
  129. $appData['updateTime'] = $date;
  130. $appData['shopLat'] = (string)0;
  131. $appData['shopLong'] = (string)0;
  132. $appData['shopPrecision'] = (string)0;
  133. $appData['account'] = $trainDemo['platform']['account'];
  134. $appData['adminId'] = 0;
  135. $appData['wxToken'] = stringUtil::buildWxToken(0);
  136. $appData['shopPhotoList'] = '';
  137. $appData['merchantName'] = $nick_name;
  138. $appData['shopProvince'] = '福建';
  139. $appData['shopCity'] = '厦门';
  140. $appData['shopDist'] = '思明区';
  141. $appData['alipayAccount'] = 'shish@zhhinc.com';
  142. $appData['alipayAccountName'] = '石少华';
  143. $appData['agentLevel'] = 1;//默认设置为代理商
  144. $merchant = xhMerchantService::applyCreateMerchant($appData);
  145. $wxOpenId = configDict::getConfig('openId');
  146. $merchantId = $merchant['id'];
  147. xhWxOpenService::updateById($wxOpenId, ['merchantId' => $merchantId]);
  148. $data = ['merchantId'=>$merchantId,'platform'=>1];
  149. $producer = Yii::$app->rabbitmq->getProducer('openShopInitProducer');
  150. $msg = serialize($data);
  151. $producer->publish($msg, 'openShopInitExchange', 'openShopInitRoute');
  152. echo 'ok';
  153. }
  154. public function actionCallback()
  155. {
  156. $get = Yii::$app->request->get();
  157. if (isset($get['auth_code']) == false) {
  158. util::stop('公众号授权回调未成功');
  159. }
  160. $code = $get['auth_code'];
  161. //使用授权码换取公众号的接口调用凭据和授权信息
  162. $authorize = wxUtil::getAuthorizer($code);
  163. if (isset($authorize['authorization_info']) == false) {
  164. util::stop('未换取公众号的接口调用凭据和授权信息');
  165. }
  166. $info = $authorize['authorization_info'];
  167. $authorizeAppId = $info['authorizer_appid'];
  168. $authorize_access_token = $info['authorizer_access_token'];
  169. $expires_in = $info['expires_in'];//7200
  170. $authorize_refresh_token = $info['authorizer_refresh_token'];
  171. //获取授权方的基本信息
  172. $authorizeDetailInfo = wxUtil::getAuthorizerInfo($authorizeAppId);
  173. if (isset($authorizeDetailInfo['authorizer_info']) == false) {
  174. util::stop('没有获取到公众号帐号基本信息');
  175. }
  176. $authorize_info = $authorizeDetailInfo['authorizer_info'];
  177. $nick_name = $authorize_info['nick_name'];
  178. $head_img = $authorize_info['head_img'];
  179. $service_type_info = $authorize_info['service_type_info'];
  180. if (isset($authorize_info['MiniProgramInfo']) == false || empty($authorize_info['MiniProgramInfo'])) {
  181. if ($service_type_info['id'] != 2) {
  182. util::stop('公众号不是服务号,appId:' . $authorizeAppId . ' refresh:' . $authorize_refresh_token);
  183. }
  184. }
  185. $verify_type_info = $authorize_info['verify_type_info'];
  186. if ($verify_type_info['id'] != 0) {
  187. util::stop('公众号还没有认证哦');
  188. }
  189. $user_name = $authorize_info['user_name'];
  190. $alias = $authorize_info['alias'];
  191. $qrCodeUrl = $authorize_info['qrcode_url'];
  192. $business_info = $authorize_info['business_info'];
  193. $open_pay = $business_info['open_pay'];
  194. $accessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒
  195. //小程序
  196. if (isset($authorize_info['MiniProgramInfo']) && !empty($authorize_info['MiniProgramInfo'])) {
  197. $merchant = xhMerchantService::getByCondition(['miniAppId' => $authorizeAppId]);
  198. if (empty($merchant)) {
  199. Yii::info('没有小程序信息 appId:' . $authorizeAppId);
  200. util::stop('没有小程序信息 appId:' . $authorizeAppId);
  201. }
  202. $merchantId = $merchant['id'];
  203. xhMerchantExtendService::updateByMerchantId($merchantId, ['miniAccessToken' => $authorize_access_token, 'miniAccessTokenTime' => $accessTokenTime, 'miniRefreshToken' => $authorize_refresh_token]);
  204. Yii::info('小程序信息更新成功:' . json_encode($authorize_info));
  205. util::stop("小程序信息更新成功");
  206. } else {
  207. $merchant = xhMerchantService::getByAppId($authorizeAppId);
  208. Yii::info('appId:' . $authorizeAppId);
  209. $set = xhSetMealService::getRandOne();
  210. if (empty($set)) {
  211. util::stop('没有套餐信息');
  212. }
  213. if (empty($merchant)) {
  214. util::stop('这个商家没有生成过基本信息。appid:' . $authorizeAppId);
  215. }
  216. if (empty($merchant)) {
  217. echo '新建商家能力暂时关闭';
  218. Yii::$app->end();
  219. //没有生成商家,引导进去设置
  220. $session = Yii::$app->session;
  221. $session['wxAppId'] = $authorizeAppId;
  222. $session['wxUserName'] = $user_name;
  223. $session['wxAliasName'] = $alias;
  224. $session['wxAccessToken'] = $authorize_access_token;
  225. $session['wxAccessTokenTime'] = $accessTokenTime;
  226. $session['wxRefreshToken'] = $authorize_refresh_token;
  227. $session['logo'] = $head_img;
  228. $session['wxQrcodeUrl'] = $qrCodeUrl;
  229. $session['allowGenerateMerchant'] = 'ok';//经过授权流程,允许允许生成商家
  230. $trainDemo = configDict::getConfig('trainDemo');
  231. $demoName = $trainDemo['merchant']['name'];//培训演示的公众号
  232. return $this->renderPartial('callback', ['merchantName' => $nick_name, 'set' => $set, 'demoName' => $demoName]);
  233. }
  234. $cData = [];
  235. $cData['wxAccessToken'] = $authorize_access_token;
  236. $cData['wxAccessTokenTime'] = $accessTokenTime;
  237. $cData['wxRefreshToken'] = $authorize_refresh_token;
  238. //审核通过
  239. $cData['status'] = 1;
  240. $merchantId = $merchant['id'];
  241. xhMerchantService::updateById($merchantId, $cData);
  242. }
  243. echo $authorizeAppId . ' ok';
  244. }
  245. public function actionResult()
  246. {
  247. $get = Yii::$app->request->get();
  248. $id = isset($get['id']) ? $get['id'] : 0;
  249. $merchant = xhMerchantService::getById($id);
  250. $status = configDict::getConfig('merchantStatusName');
  251. $orMechantId = configDict::getConfig('merchantId');
  252. $originalMerchant = xhMerchantService::getById($orMechantId);
  253. return $this->renderPartial('result', ['originalMerchant' => $originalMerchant, 'merchant' => $merchant, 'status' => $status]);
  254. }
  255. /**
  256. * 商家申请入驻
  257. */
  258. public function actionAddMerchant()
  259. {
  260. $post = Yii::$app->request->post();
  261. $session = Yii::$app->session;
  262. if (isset($session['allowGenerateMerchant']) == false) {
  263. //util::fail('没有经过授权的操作');
  264. }
  265. unset($post['_csrf']);
  266. if (isset($session['wxAppId']) == false || empty($session['wxAppId'])) {
  267. util::fail('公众号appId不存在');
  268. }
  269. $wxAppId = $session['wxAppId'];
  270. if (isset($session['logo'])) {
  271. $logo = xhMerchantService::logoSave($session['logo'], $wxAppId);
  272. $post['logo'] = $logo;
  273. }
  274. if (isset($session['wxQrcodeUrl'])) {
  275. $wxQrCodeUrl = xhMerchantService::qrCodeSave($session['wxQrcodeUrl'], $wxAppId);
  276. $post['wxQrcodeUrl'] = $wxQrCodeUrl;
  277. }
  278. $date = date("Y-m-d H:i:s");
  279. $inviteCode = isset($post['inviteCode']) ? $post['inviteCode'] : '';
  280. $accountStyle = configDict::getConfig('accountStyle');
  281. $merchantStatus = configDict::getConfig('merchantStatus');
  282. $serviceAccount = $accountStyle['serviceAccount'];//认证服务号
  283. $checking = $merchantStatus['checking'];//审核中
  284. $merchantName = !empty($post['merchantName']) ? $post['merchantName'] : '';
  285. $exists = xhMerchantService::getByMerchantName($merchantName);
  286. if (!empty($exists)) {
  287. util::fail('花店名称已经存在了');
  288. }
  289. $post['wxAppId'] = $wxAppId;
  290. $post['wxUserName'] = $session['wxUserName'];
  291. $post['wxAliasName'] = $session['wxAliasName'];
  292. $post['wxAccessToken'] = $session['wxAccessToken'];
  293. $post['wxAccessTokenTime'] = $session['wxAccessTokenTime'];
  294. $post['wxRefreshToken'] = $session['wxRefreshToken'];
  295. $post['accountStyle'] = $serviceAccount;//认证服务号
  296. $post['status'] = $checking;//待审
  297. $post['createTime'] = $date;
  298. $post['updateTime'] = $date;
  299. $post['shopLat'] = isset($post['shopLat']) ? (string)$post['shopLat'] : (string)0;
  300. $post['shopLong'] = isset($post['shopLong']) ? (string)$post['shopLong'] : (string)0;
  301. $post['shopPrecision'] = isset($post['shopPrecision']) ? (string)$post['shopPrecision'] : (string)0;
  302. $account = stringUtil::buildOrderNo(0);
  303. $trainDemo = configDict::getConfig('trainDemo');
  304. $demoName = $trainDemo['merchant']['name'];//培训演示的公众号
  305. if (isset($post['merchantName']) && $post['merchantName'] == $demoName) {
  306. $account = $trainDemo['merchant']['account'];
  307. }
  308. $post['account'] = $account;
  309. $post['adminId'] = 0;
  310. $post['wxToken'] = stringUtil::buildWxToken(0);
  311. $shopPhotoListString = json_encode(["outdoor" => $post['shopImg'], 'indoor' => $post['shopImg2']]);//将门店照片组合成json
  312. $post['shopPhotoList'] = $shopPhotoListString;
  313. $merchant = xhMerchantService::applyCreateMerchant($post);
  314. $id = $merchant['id'];
  315. $account = $merchant['id'];
  316. unset($session['allowGenerateMerchant']);
  317. xhWxMenuService::applyInit($merchant);
  318. $set = xhSetMealService::getRandOne();
  319. $price = $set['price'];
  320. $actPrice = $price;
  321. $mainMerchantId = 0;
  322. $now = time();
  323. $orderDeadline = 0;
  324. if (!empty($inviteCode)) {
  325. $invite = xhInviteService::getByCode($inviteCode);
  326. if (empty($invite)) {
  327. util::fail('邀请码不存在');
  328. }
  329. if ($invite['takeStatus'] == 1) {//邀请码未使用,并且没有过期
  330. util::fail('邀请码已经使用');
  331. }
  332. $inviteDeadline = $invite['deadline'];
  333. if ($now > $inviteDeadline) {
  334. util::fail('邀请码已经过期');
  335. }
  336. $mainMerchantId = isset($invite['merchantId']) ? $invite['merchantId'] : 0;
  337. $actPrice = stringUtil::calcSub($set['price'], $invite['save']);//扣除邀请码里的钱
  338. }
  339. $toPay = $actPrice <= 0 ? 'no' : 'yes';//是否需要付款
  340. $inviteCodeStatus = $actPrice <= 0 ? 1 : 0;//邀请码是否已使用
  341. $payStatus = $actPrice <= 0 ? 1 : 0;//付款是否成功
  342. $createTime = date("Y-m-d H:i:s");
  343. $orderId = 'A' . stringUtil::buildOrderNo($id);
  344. $orderData = ['id' => $orderId, 'userId' => $this->userId, 'applyName' => $merchantName, 'email' => '',
  345. 'createTime' => $createTime, 'accountId' => 0, 'inviteCode' => $inviteCode, 'merchantId' => $mainMerchantId, 'invitedMerchantId' => $id,
  346. 'prePrice' => $price, 'actPrice' => $actPrice, 'deadline' => $orderDeadline, 'inviteCodeStatus' => (int)$inviteCodeStatus, 'payStatus' => $payStatus,
  347. ];
  348. xhApplyOrderService::add($orderData);//保存订单信息
  349. if ($actPrice <= 0 && !empty($inviteCode)) {
  350. xhInviteService::updateByCode($inviteCode, ['targetId' => $orderId, 'takeStatus' => 1, 'invitedMerchantId' => $id, 'inviteUseTo' => 0]);
  351. }
  352. $openMerchant = xhWxOpenService::getMerchant();
  353. $openMerchantAccount = $openMerchant['id'];
  354. $newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300));
  355. util::success(['account' => $openMerchantAccount, 'newAccount' => $newAccount, 'toPay' => $toPay, 'orderId' => $orderId, 'actPrice' => $actPrice]);
  356. }
  357. public function actionCheckInviteCode()
  358. {
  359. $get = Yii::$app->request->get();
  360. $code = isset($get['code']) ? $get['code'] : '';
  361. $invite = xhInviteService::getByCode($code);
  362. if (empty($invite)) {
  363. util::fail('邀请码错误');
  364. }
  365. $now = time();
  366. if ($now > $invite['deadline']) {
  367. util::fail('邀请码已经失效');
  368. }
  369. if (!empty($invite['takeStatus'])) {
  370. util::fail('邀请码已经使用');
  371. }
  372. util::success($invite);
  373. }
  374. public function actionSelect()
  375. {
  376. $isWx = util::isWx();
  377. if ($isWx == false) {
  378. return $this->renderPartial('notWeixin');
  379. }
  380. return $this->renderPartial('select');
  381. }
  382. public function actionApi()
  383. {
  384. $openData = file_get_contents('php://input');
  385. if (isset ($openData) == false || empty($openData)) {
  386. util::stop('has no post data');
  387. }
  388. $weixinSecret = Yii::getAlias("@vendor/weixinSecret");
  389. require_once($weixinSecret . '/wxBizMsgCrypt.php');
  390. $get = Yii::$app->request->get();
  391. $encryptMsg = $openData;
  392. $openId = configDict::getConfig('openId');
  393. $open = xhWxOpenService::getById($openId);
  394. $encodingAesKey = $open['aesKey'];
  395. $token = $open['token'];
  396. $appId = $open['appId'];
  397. $signature = isset($get['signature']) ? $get['signature'] : '';
  398. $timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
  399. $nonce = isset($get['nonce']) ? $get['nonce'] : '';
  400. $encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
  401. $msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
  402. $xml_tree = new \DOMDocument();
  403. $xml_tree->loadXML($encryptMsg);
  404. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  405. $encrypt = $array_e->item(0)->nodeValue;
  406. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  407. $from_xml = sprintf($format, $encrypt);
  408. $formString = '1:' . $token . ' 2:' . $encodingAesKey . ' 3:' . $appId . ' 4:' . $msg_signature . ' 5:' . $timestamp . ' 6:' . $nonce . ' 7:' . $from_xml;
  409. Yii::warning("formString:" . $formString);
  410. //第三方收到公众号平台发送的消息
  411. $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
  412. $msg = '';//解密后的消息
  413. $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
  414. if ($errCode != 0) {
  415. Yii::warning("解密未成功,错误代码:" . $errCode);
  416. Yii::$app->end();
  417. }
  418. $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
  419. $infoType = $postObj->InfoType;
  420. switch ($infoType) {
  421. //推送 componentVerifyTicket
  422. case 'component_verify_ticket':
  423. $componentVerifyTicket = $postObj->ComponentVerifyTicket;
  424. if (!empty($componentVerifyTicket)) {
  425. $oData = [];
  426. $oData['verifyTicket'] = (string)$componentVerifyTicket;
  427. $oData['verifyTicketTime'] = date("Y-m-d H:i:s", $timestamp + 600);//每10分钟(600秒)推一次
  428. xhWxOpenService::updateById($openId, $oData);
  429. Yii::warning("component_verify_ticket:" . (string)$componentVerifyTicket);
  430. util::stop('success');
  431. }
  432. break;
  433. //取消授权通知
  434. case 'unauthorized':
  435. $AppId = $postObj->AppId;
  436. $authorizeAppid = $postObj->AuthorizerAppid;
  437. $merchant = xhMerchantService::getByAppId($authorizeAppid);
  438. if (!empty($merchant)) {
  439. $merchantId = $merchant['id'];
  440. xhMerchantService::updateById($merchantId, ['status' => 5]);//自主冻结
  441. }
  442. break;
  443. //授权成功通知
  444. case 'authorized':
  445. $AppId = $postObj->AppId;//第三方平台appid
  446. $CreateTime = $postObj->CreateTime;//公众号
  447. $authorizeAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
  448. $AuthorizationCode = $postObj->AuthorizationCode;
  449. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
  450. break;
  451. //授权更新通知
  452. case 'updateauthorized':
  453. $AppId = $postObj->AppId;
  454. $CreateTime = $postObj->CreateTime;
  455. $authorizeAppid = $postObj->AuthorizerAppid;
  456. $AuthorizationCode = $postObj->AuthorizationCode;
  457. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
  458. $authorize = wxUtil::getAuthorizer($AuthorizationCode);
  459. if (isset($authorize['authorization_info']) == false) {
  460. $msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
  461. Yii::warning($msg, __METHOD__);
  462. Yii::$app->end();
  463. }
  464. $info = $authorize['authorization_info'];
  465. $authorizeAppId = $info['authorizer_appid'];
  466. $authorize_access_token = $info['authorizer_access_token'];
  467. $expires_in = $info['expires_in'];//7200
  468. $authorize_refresh_token = $info['authorizer_refresh_token'];
  469. $merchant = xhMerchantService::getByAppId($authorizeAppId);
  470. if (empty($merchant)) {
  471. return false;
  472. }
  473. $merchantId = $merchant['id'];
  474. $wxData = [];
  475. $wxData['wxAccessToken'] = $authorize_access_token;
  476. $wxData['wxRefreshToken'] = $authorize_refresh_token;
  477. $wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  478. $wxData['status'] = 1;
  479. xhMerchantService::updateById($merchantId, $wxData);
  480. break;
  481. default:
  482. }
  483. Yii::warning("---wx open data end--- \n");
  484. }
  485. }