WxOpenController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. //商品、分类、首页幻灯片、门店图片
  149. //创建门店
  150. //创建管理员
  151. //客户信息初始化
  152. //初始化微信菜单
  153. xhWxMenuService::applyInit($merchant);
  154. echo 'ok';
  155. }
  156. public function actionCallback()
  157. {
  158. $get = Yii::$app->request->get();
  159. if (isset($get['auth_code']) == false) {
  160. util::stop('公众号授权回调未成功');
  161. }
  162. $code = $get['auth_code'];
  163. //使用授权码换取公众号的接口调用凭据和授权信息
  164. $authorize = wxUtil::getAuthorizer($code);
  165. if (isset($authorize['authorization_info']) == false) {
  166. util::stop('未换取公众号的接口调用凭据和授权信息');
  167. }
  168. $info = $authorize['authorization_info'];
  169. $authorizeAppId = $info['authorizer_appid'];
  170. $authorize_access_token = $info['authorizer_access_token'];
  171. $expires_in = $info['expires_in'];//7200
  172. $authorize_refresh_token = $info['authorizer_refresh_token'];
  173. //获取授权方的基本信息
  174. $authorizeDetailInfo = wxUtil::getAuthorizerInfo($authorizeAppId);
  175. if (isset($authorizeDetailInfo['authorizer_info']) == false) {
  176. util::stop('没有获取到公众号帐号基本信息');
  177. }
  178. $authorize_info = $authorizeDetailInfo['authorizer_info'];
  179. $nick_name = $authorize_info['nick_name'];
  180. $head_img = $authorize_info['head_img'];
  181. $service_type_info = $authorize_info['service_type_info'];
  182. if (isset($authorize_info['MiniProgramInfo']) == false || empty($authorize_info['MiniProgramInfo'])) {
  183. if ($service_type_info['id'] != 2) {
  184. util::stop('公众号不是服务号,appId:' . $authorizeAppId . ' refresh:' . $authorize_refresh_token);
  185. }
  186. }
  187. $verify_type_info = $authorize_info['verify_type_info'];
  188. if ($verify_type_info['id'] != 0) {
  189. util::stop('公众号还没有认证哦');
  190. }
  191. $user_name = $authorize_info['user_name'];
  192. $alias = $authorize_info['alias'];
  193. $qrCodeUrl = $authorize_info['qrcode_url'];
  194. $business_info = $authorize_info['business_info'];
  195. $open_pay = $business_info['open_pay'];
  196. $accessTokenTime = date("Y-m-d H:i:s", (time() + $expires_in - 100));//安全起见,将过期时间设置得比微信里还短100秒
  197. //小程序
  198. if (isset($authorize_info['MiniProgramInfo']) && !empty($authorize_info['MiniProgramInfo'])) {
  199. $merchant = xhMerchantService::getByCondition(['miniAppId' => $authorizeAppId]);
  200. if (empty($merchant)) {
  201. Yii::info('没有小程序信息 appId:' . $authorizeAppId);
  202. util::stop('没有小程序信息 appId:' . $authorizeAppId);
  203. }
  204. $merchantId = $merchant['id'];
  205. xhMerchantExtendService::updateByMerchantId($merchantId, ['miniAccessToken' => $authorize_access_token, 'miniAccessTokenTime' => $accessTokenTime, 'miniRefreshToken' => $authorize_refresh_token]);
  206. Yii::info('小程序信息更新成功:' . json_encode($authorize_info));
  207. util::stop("小程序信息更新成功");
  208. } else {
  209. $merchant = xhMerchantService::getByAppId($authorizeAppId);
  210. Yii::info('appId:' . $authorizeAppId);
  211. $set = xhSetMealService::getRandOne();
  212. if (empty($set)) {
  213. util::stop('没有套餐信息');
  214. }
  215. if (empty($merchant)) {
  216. util::stop('这个商家没有生成过基本信息。appid:' . $authorizeAppId);
  217. }
  218. if (empty($merchant)) {
  219. echo '新建商家能力暂时关闭';
  220. Yii::$app->end();
  221. //没有生成商家,引导进去设置
  222. $session = Yii::$app->session;
  223. $session['wxAppId'] = $authorizeAppId;
  224. $session['wxUserName'] = $user_name;
  225. $session['wxAliasName'] = $alias;
  226. $session['wxAccessToken'] = $authorize_access_token;
  227. $session['wxAccessTokenTime'] = $accessTokenTime;
  228. $session['wxRefreshToken'] = $authorize_refresh_token;
  229. $session['logo'] = $head_img;
  230. $session['wxQrcodeUrl'] = $qrCodeUrl;
  231. $session['allowGenerateMerchant'] = 'ok';//经过授权流程,允许允许生成商家
  232. $trainDemo = configDict::getConfig('trainDemo');
  233. $demoName = $trainDemo['merchant']['name'];//培训演示的公众号
  234. return $this->renderPartial('callback', ['merchantName' => $nick_name, 'set' => $set, 'demoName' => $demoName]);
  235. }
  236. $cData = [];
  237. $cData['wxAccessToken'] = $authorize_access_token;
  238. $cData['wxAccessTokenTime'] = $accessTokenTime;
  239. $cData['wxRefreshToken'] = $authorize_refresh_token;
  240. //审核通过
  241. $cData['status'] = 1;
  242. $merchantId = $merchant['id'];
  243. xhMerchantService::updateById($merchantId, $cData);
  244. }
  245. echo $authorizeAppId . ' ok';
  246. }
  247. public function actionResult()
  248. {
  249. $get = Yii::$app->request->get();
  250. $id = isset($get['id']) ? $get['id'] : 0;
  251. $merchant = xhMerchantService::getById($id);
  252. $status = configDict::getConfig('merchantStatusName');
  253. $orMechantId = configDict::getConfig('merchantId');
  254. $originalMerchant = xhMerchantService::getById($orMechantId);
  255. return $this->renderPartial('result', ['originalMerchant' => $originalMerchant, 'merchant' => $merchant, 'status' => $status]);
  256. }
  257. /**
  258. * 商家申请入驻
  259. */
  260. public function actionAddMerchant()
  261. {
  262. $post = Yii::$app->request->post();
  263. $session = Yii::$app->session;
  264. if (isset($session['allowGenerateMerchant']) == false) {
  265. //util::fail('没有经过授权的操作');
  266. }
  267. unset($post['_csrf']);
  268. if (isset($session['wxAppId']) == false || empty($session['wxAppId'])) {
  269. util::fail('公众号appId不存在');
  270. }
  271. $wxAppId = $session['wxAppId'];
  272. if (isset($session['logo'])) {
  273. $logo = xhMerchantService::logoSave($session['logo'], $wxAppId);
  274. $post['logo'] = $logo;
  275. }
  276. if (isset($session['wxQrcodeUrl'])) {
  277. $wxQrCodeUrl = xhMerchantService::qrCodeSave($session['wxQrcodeUrl'], $wxAppId);
  278. $post['wxQrcodeUrl'] = $wxQrCodeUrl;
  279. }
  280. $date = date("Y-m-d H:i:s");
  281. $inviteCode = isset($post['inviteCode']) ? $post['inviteCode'] : '';
  282. $accountStyle = configDict::getConfig('accountStyle');
  283. $merchantStatus = configDict::getConfig('merchantStatus');
  284. $serviceAccount = $accountStyle['serviceAccount'];//认证服务号
  285. $checking = $merchantStatus['checking'];//审核中
  286. $merchantName = !empty($post['merchantName']) ? $post['merchantName'] : '';
  287. $exists = xhMerchantService::getByMerchantName($merchantName);
  288. if (!empty($exists)) {
  289. util::fail('花店名称已经存在了');
  290. }
  291. $post['wxAppId'] = $wxAppId;
  292. $post['wxUserName'] = $session['wxUserName'];
  293. $post['wxAliasName'] = $session['wxAliasName'];
  294. $post['wxAccessToken'] = $session['wxAccessToken'];
  295. $post['wxAccessTokenTime'] = $session['wxAccessTokenTime'];
  296. $post['wxRefreshToken'] = $session['wxRefreshToken'];
  297. $post['accountStyle'] = $serviceAccount;//认证服务号
  298. $post['status'] = $checking;//待审
  299. $post['createTime'] = $date;
  300. $post['updateTime'] = $date;
  301. $post['shopLat'] = isset($post['shopLat']) ? (string)$post['shopLat'] : (string)0;
  302. $post['shopLong'] = isset($post['shopLong']) ? (string)$post['shopLong'] : (string)0;
  303. $post['shopPrecision'] = isset($post['shopPrecision']) ? (string)$post['shopPrecision'] : (string)0;
  304. $account = stringUtil::buildOrderNo(0);
  305. $trainDemo = configDict::getConfig('trainDemo');
  306. $demoName = $trainDemo['merchant']['name'];//培训演示的公众号
  307. if (isset($post['merchantName']) && $post['merchantName'] == $demoName) {
  308. $account = $trainDemo['merchant']['account'];
  309. }
  310. $post['account'] = $account;
  311. $post['adminId'] = 0;
  312. $post['wxToken'] = stringUtil::buildWxToken(0);
  313. $shopPhotoListString = json_encode(["outdoor" => $post['shopImg'], 'indoor' => $post['shopImg2']]);//将门店照片组合成json
  314. $post['shopPhotoList'] = $shopPhotoListString;
  315. $merchant = xhMerchantService::applyCreateMerchant($post);
  316. $id = $merchant['id'];
  317. $account = $merchant['id'];
  318. unset($session['allowGenerateMerchant']);
  319. xhWxMenuService::applyInit($merchant);
  320. $set = xhSetMealService::getRandOne();
  321. $price = $set['price'];
  322. $actPrice = $price;
  323. $mainMerchantId = 0;
  324. $now = time();
  325. $orderDeadline = 0;
  326. if (!empty($inviteCode)) {
  327. $invite = xhInviteService::getByCode($inviteCode);
  328. if (empty($invite)) {
  329. util::fail('邀请码不存在');
  330. }
  331. if ($invite['takeStatus'] == 1) {//邀请码未使用,并且没有过期
  332. util::fail('邀请码已经使用');
  333. }
  334. $inviteDeadline = $invite['deadline'];
  335. if ($now > $inviteDeadline) {
  336. util::fail('邀请码已经过期');
  337. }
  338. $mainMerchantId = isset($invite['merchantId']) ? $invite['merchantId'] : 0;
  339. $actPrice = stringUtil::calcSub($set['price'], $invite['save']);//扣除邀请码里的钱
  340. }
  341. $toPay = $actPrice <= 0 ? 'no' : 'yes';//是否需要付款
  342. $inviteCodeStatus = $actPrice <= 0 ? 1 : 0;//邀请码是否已使用
  343. $payStatus = $actPrice <= 0 ? 1 : 0;//付款是否成功
  344. $createTime = date("Y-m-d H:i:s");
  345. $orderId = 'A' . stringUtil::buildOrderNo($id);
  346. $orderData = ['id' => $orderId, 'userId' => $this->userId, 'applyName' => $merchantName, 'email' => '',
  347. 'createTime' => $createTime, 'accountId' => 0, 'inviteCode' => $inviteCode, 'merchantId' => $mainMerchantId, 'invitedMerchantId' => $id,
  348. 'prePrice' => $price, 'actPrice' => $actPrice, 'deadline' => $orderDeadline, 'inviteCodeStatus' => (int)$inviteCodeStatus, 'payStatus' => $payStatus,
  349. ];
  350. xhApplyOrderService::add($orderData);//保存订单信息
  351. if ($actPrice <= 0 && !empty($inviteCode)) {
  352. xhInviteService::updateByCode($inviteCode, ['targetId' => $orderId, 'takeStatus' => 1, 'invitedMerchantId' => $id, 'inviteUseTo' => 0]);
  353. }
  354. $openMerchant = xhWxOpenService::getMerchant();
  355. $openMerchantAccount = $openMerchant['id'];
  356. $newAccount = urlencode(stringUtil::authcode($account, 'ENCODE', 300));
  357. util::success(['account' => $openMerchantAccount, 'newAccount' => $newAccount, 'toPay' => $toPay, 'orderId' => $orderId, 'actPrice' => $actPrice]);
  358. }
  359. public function actionCheckInviteCode()
  360. {
  361. $get = Yii::$app->request->get();
  362. $code = isset($get['code']) ? $get['code'] : '';
  363. $invite = xhInviteService::getByCode($code);
  364. if (empty($invite)) {
  365. util::fail('邀请码错误');
  366. }
  367. $now = time();
  368. if ($now > $invite['deadline']) {
  369. util::fail('邀请码已经失效');
  370. }
  371. if (!empty($invite['takeStatus'])) {
  372. util::fail('邀请码已经使用');
  373. }
  374. util::success($invite);
  375. }
  376. public function actionSelect()
  377. {
  378. $isWx = util::isWx();
  379. if ($isWx == false) {
  380. return $this->renderPartial('notWeixin');
  381. }
  382. return $this->renderPartial('select');
  383. }
  384. public function actionApi()
  385. {
  386. $openData = file_get_contents('php://input');
  387. if (isset ($openData) == false || empty($openData)) {
  388. util::stop('has no post data');
  389. }
  390. $weixinSecret = Yii::getAlias("@vendor/weixinSecret");
  391. require_once($weixinSecret . '/wxBizMsgCrypt.php');
  392. $get = Yii::$app->request->get();
  393. $encryptMsg = $openData;
  394. $openId = configDict::getConfig('openId');
  395. $open = xhWxOpenService::getById($openId);
  396. $encodingAesKey = $open['aesKey'];
  397. $token = $open['token'];
  398. $appId = $open['appId'];
  399. $signature = isset($get['signature']) ? $get['signature'] : '';
  400. $timestamp = isset($get['timestamp']) ? $get['timestamp'] : '';
  401. $nonce = isset($get['nonce']) ? $get['nonce'] : '';
  402. $encrypt_type = isset($get['encrypt_type']) ? $get['encrypt_type'] : '';
  403. $msg_signature = isset($get['msg_signature']) ? $get['msg_signature'] : '';
  404. $xml_tree = new \DOMDocument();
  405. $xml_tree->loadXML($encryptMsg);
  406. $array_e = $xml_tree->getElementsByTagName('Encrypt');
  407. $encrypt = $array_e->item(0)->nodeValue;
  408. $format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
  409. $from_xml = sprintf($format, $encrypt);
  410. $formString = '1:' . $token . ' 2:' . $encodingAesKey . ' 3:' . $appId . ' 4:' . $msg_signature . ' 5:' . $timestamp . ' 6:' . $nonce . ' 7:' . $from_xml;
  411. Yii::warning("formString:" . $formString);
  412. //第三方收到公众号平台发送的消息
  413. $pc = new \WXBizMsgCrypt($token, $encodingAesKey, $appId);
  414. $msg = '';//解密后的消息
  415. $errCode = $pc->decryptMsg($msg_signature, $timestamp, $nonce, $from_xml, $msg);
  416. if ($errCode != 0) {
  417. Yii::warning("解密未成功,错误代码:" . $errCode);
  418. Yii::$app->end();
  419. }
  420. $postObj = simplexml_load_string($msg, 'SimpleXMLElement', LIBXML_NOCDATA);
  421. $infoType = $postObj->InfoType;
  422. switch ($infoType) {
  423. //推送 componentVerifyTicket
  424. case 'component_verify_ticket':
  425. $componentVerifyTicket = $postObj->ComponentVerifyTicket;
  426. if (!empty($componentVerifyTicket)) {
  427. $oData = [];
  428. $oData['verifyTicket'] = (string)$componentVerifyTicket;
  429. $oData['verifyTicketTime'] = date("Y-m-d H:i:s", $timestamp + 600);//每10分钟(600秒)推一次
  430. xhWxOpenService::updateById($openId, $oData);
  431. Yii::warning("component_verify_ticket:" . (string)$componentVerifyTicket);
  432. util::stop('success');
  433. }
  434. break;
  435. //取消授权通知
  436. case 'unauthorized':
  437. $AppId = $postObj->AppId;
  438. $authorizeAppid = $postObj->AuthorizerAppid;
  439. $merchant = xhMerchantService::getByAppId($authorizeAppid);
  440. if (!empty($merchant)) {
  441. $merchantId = $merchant['id'];
  442. xhMerchantService::updateById($merchantId, ['status' => 5]);//自主冻结
  443. }
  444. break;
  445. //授权成功通知
  446. case 'authorized':
  447. $AppId = $postObj->AppId;//第三方平台appid
  448. $CreateTime = $postObj->CreateTime;//公众号
  449. $authorizeAppid = $postObj->AuthorizerAppid;//授权码,可用于换取公众号的接口调用凭据,详细见上面的说明
  450. $AuthorizationCode = $postObj->AuthorizationCode;
  451. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;//授权码过期时间
  452. break;
  453. //授权更新通知
  454. case 'updateauthorized':
  455. $AppId = $postObj->AppId;
  456. $CreateTime = $postObj->CreateTime;
  457. $authorizeAppid = $postObj->AuthorizerAppid;
  458. $AuthorizationCode = $postObj->AuthorizationCode;
  459. $AuthorizationCodeExpiredTime = $postObj->AuthorizationCodeExpiredTime;
  460. $authorize = wxUtil::getAuthorizer($AuthorizationCode);
  461. if (isset($authorize['authorization_info']) == false) {
  462. $msg = '授权回调获取到auth_code,但未换取公众号的接口调用凭据和授权信息!!';
  463. Yii::warning($msg, __METHOD__);
  464. Yii::$app->end();
  465. }
  466. $info = $authorize['authorization_info'];
  467. $authorizeAppId = $info['authorizer_appid'];
  468. $authorize_access_token = $info['authorizer_access_token'];
  469. $expires_in = $info['expires_in'];//7200
  470. $authorize_refresh_token = $info['authorizer_refresh_token'];
  471. $merchant = xhMerchantService::getByAppId($authorizeAppId);
  472. if (empty($merchant)) {
  473. return false;
  474. }
  475. $merchantId = $merchant['id'];
  476. $wxData = [];
  477. $wxData['wxAccessToken'] = $authorize_access_token;
  478. $wxData['wxRefreshToken'] = $authorize_refresh_token;
  479. $wxData['wxAccessTokenTime'] = date("Y-m-d H:i:s", (time() + $expires_in - 100));
  480. $wxData['status'] = 1;
  481. xhMerchantService::updateById($merchantId, $wxData);
  482. break;
  483. default:
  484. }
  485. Yii::warning("---wx open data end--- \n");
  486. }
  487. }