WxOpenController.php 24 KB

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