JoinController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace openend\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  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. class JoinController extends OpenendController{
  17. public $withoutLogin = ['callback-create-first-merchant'];
  18. public function actionApply()
  19. {
  20. $id = configDict::getConfig('openId');
  21. $open = xhWxOpenService::getById($id);
  22. $pre = weixinOpenUtil::getPreAuthCode($open);
  23. $appId = $open['appId'];
  24. $oData = [];
  25. $oData['preAuthCodeTime'] = date("Y-m-d H:i:s");//设置预授码过期,让下个用户可以获取新的预授码
  26. xhWxOpenService::updateById($id,$oData);
  27. $url = $this->openUrl.'/join/callback';
  28. $merchantId = $open['merchantId'];//如果平台没有绑定商家,先去创建第一个商家
  29. if(empty($merchantId)){
  30. $url = $this->openUrl.'/join/callback-create-first-merchant';
  31. }
  32. $redirect = "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid={$appId}&pre_auth_code={$pre}&redirect_uri={$url}";
  33. return $this->renderPartial('apply',['url' => $redirect]);
  34. }
  35. public function actionCheckMerchantName()
  36. {
  37. $get = Yii::$app->request->get();
  38. $merchantName = isset($get['merchantName']) ? $get['merchantName'] : '';
  39. $m = xhMerchantService::getByMerchantName($merchantName);
  40. if(!empty($m)){
  41. util::failInfo('公众号名称已经使用');
  42. }
  43. util::successInfo();
  44. }
  45. /**
  46. * 下载微信里的图片到服务器
  47. */
  48. public function actionDownloadWxImg()
  49. {
  50. $get = Yii::$app->request->get();
  51. $mediaId = isset($get['mediaId']) ? $get['mediaId'] : '';
  52. $imgList = weixinUtil::downloadmediaIdImg($this->merchant, $mediaId);
  53. if(!empty($imgList)){
  54. util::successInfo('操作成功','A0001',$imgList);
  55. }
  56. util::failInfo();
  57. }
  58. /**
  59. * 创建第一个商家
  60. */
  61. public function actionCallbackCreateFirstMerchant()
  62. {
  63. $get = Yii::$app->request->get();
  64. if(isset($get['auth_code']) == false){
  65. return $this->renderPartial('hint',['msg' => '公众号授权回调未成功']);
  66. }
  67. $code = $get['auth_code'];
  68. //使用授权码换取公众号的接口调用凭据和授权信息
  69. $authorizer = weixinOpenUtil::getAuthorizer($code);
  70. if(isset($authorizer['authorization_info']) == false){
  71. return $this->renderPartial('hint',['msg' => '未换取公众号的接口调用凭据和授权信息']);
  72. }
  73. $info = $authorizer['authorization_info'];
  74. $authorizer_appid = $info['authorizer_appid'];
  75. $authorizer_access_token = $info['authorizer_access_token'];
  76. $expires_in = $info['expires_in'];//7200
  77. $authorizer_refresh_token = $info['authorizer_refresh_token'];
  78. //获取授权方的公众号帐号基本信息
  79. $authorizerDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizer_appid);
  80. if(isset($authorizerDetailInfo['authorizer_info']) == false){
  81. return $this->renderPartial('hint',['msg' => '没有获取到公众号帐号基本信息']);
  82. }
  83. $authorizer_info = $authorizerDetailInfo['authorizer_info'];
  84. $nick_name = $authorizer_info['nick_name'];
  85. $head_img = $authorizer_info['head_img'];
  86. $service_type_info = $authorizer_info['service_type_info'];
  87. if($service_type_info['id'] != 2){
  88. return $this->renderPartial('hint',['msg' => '公众号不是服务号']);
  89. }
  90. $verify_type_info = $authorizer_info['verify_type_info'];
  91. if($verify_type_info['id'] != 0){
  92. return $this->renderPartial('hint',['msg' => '公众号还没有认证哦']);
  93. }
  94. $user_name = $authorizer_info['user_name'];
  95. $alias = $authorizer_info['alias'];
  96. $qrcode_url = $authorizer_info['qrcode_url'];
  97. $business_info = $authorizer_info['business_info'];
  98. $open_pay = $business_info['open_pay'];
  99. $merchant = xhMerchantService::getByAppId($authorizer_appid);
  100. $wxAccessTokenTime = date("Y-m-d H:i:s",(time()+$expires_in-100));//安全起见,将过期时间设置得比微信里还短100秒
  101. if(!empty($merchant)){
  102. return $this->renderPartial('hint',['msg' => '平台已经关联公众号了']);
  103. }
  104. //引导生成商家
  105. $date = date("Y-m-d H:i:s");
  106. $trainDemo = configDict::getConfig('trainDemo');
  107. $logo = xhMerchantService::logoSave($head_img, $authorizer_appid);
  108. $appData['logo'] = $logo;
  109. $wxQrcodeUrl = xhMerchantService::qrCodeSave($qrcode_url,$authorizer_appid);
  110. $appData['wxQrcodeUrl'] = $wxQrcodeUrl;
  111. $appData['wxAppId'] = $authorizer_appid;
  112. $appData['wxUserName'] = $user_name;
  113. $appData['wxAliasName'] = $alias;
  114. $appData['wxAccessToken'] = $authorizer_access_token;
  115. $appData['wxAccessTokenTime'] = $wxAccessTokenTime;
  116. $appData['wxRefreshToken'] = $authorizer_refresh_token;
  117. $appData['accountStyle'] = 0;//认证服务号
  118. $appData['status'] = 0;//待审核
  119. $appData['createTime'] = $date;
  120. $appData['updateTime'] = $date;
  121. $appData['shopLat'] = (string)0;
  122. $appData['shopLong'] = (string)0;
  123. $appData['shopPrecision'] = (string)0;
  124. $appData['adminId'] = 0;
  125. $appData['wxToken'] = stringUtil::buildWxToken(0);
  126. $appData['shopPhotoList'] = '';
  127. $appData['merchantName'] = $nick_name;
  128. $appData['shopProvince'] = '福建';
  129. $appData['shopCity'] = '厦门';
  130. $appData['shopDist'] = '思明区';
  131. $appData['alipayAccount'] = 'shishaohua8879@sina.com';
  132. $appData['alipayAccountName'] = '石少华';
  133. $appData['agentLevel'] = 1;//默认设置为代理商
  134. $merchant = xhMerchantService::applyCreateMerchant($appData);
  135. $wxOpenId = configDict::getConfig('openId');
  136. $merchantId = $merchant['id'];
  137. $account = $merchantId;
  138. $createTime = date("Y-m-d H:i:s");
  139. $orderId = 'A'.stringUtil::buildOrderNo();
  140. $orderData = ['id'=>$orderId,'userId'=>0,'applyName'=>$nick_name,'email'=>'',
  141. 'createTime'=>$createTime,'accountId'=>0,'inviteCode'=>'','merchantId'=>$merchantId,'invitedMerchantId'=>$merchantId,
  142. 'prePrice'=>0,'actPrice'=>0,'deadline'=>time()+2592000,'inviteCodeStatus'=>0,'payStatus'=>1,
  143. ];
  144. xhApplyOrderService::add($orderData);//保存订单信息
  145. xhWxOpenService::updateById($wxOpenId, ['merchantId'=>$merchantId]);
  146. xhWxMenuService::applyInit($merchant);//初始化微信菜单
  147. $openMerchant = xhWxOpenService::getMerchant();
  148. $openMerchantAccount = $openMerchant['id'];
  149. //将新生成的商家account加密传到后台绑定管理员,密钥有效期5分钟
  150. $newAccount = urlencode(stringUtil::authcode($account,'ENCODE',300));
  151. $this->redirect(Yii::$app->params['adminUrl'].'/admin/generate?account='.$openMerchantAccount.'&newAccount='.$newAccount);
  152. }
  153. public function actionCallback()
  154. {
  155. $get = Yii::$app->request->get();
  156. if(isset($get['auth_code']) == false){
  157. return $this->renderPartial('hint',['msg' => '公众号授权回调未成功']);
  158. }
  159. $code = $get['auth_code'];
  160. //使用授权码换取公众号的接口调用凭据和授权信息
  161. $authorizer = weixinOpenUtil::getAuthorizer($code);
  162. if(isset($authorizer['authorization_info']) == false){
  163. return $this->renderPartial('hint',['msg' => '未换取公众号的接口调用凭据和授权信息']);
  164. }
  165. $info = $authorizer['authorization_info'];
  166. $authorizer_appid = $info['authorizer_appid'];
  167. $authorizer_access_token = $info['authorizer_access_token'];
  168. $expires_in = $info['expires_in'];//7200
  169. $authorizer_refresh_token = $info['authorizer_refresh_token'];
  170. //获取授权方的公众号帐号基本信息
  171. $authorizerDetailInfo = weixinOpenUtil::getAuthorizerInfo($authorizer_appid);
  172. if(isset($authorizerDetailInfo['authorizer_info']) == false){
  173. return $this->renderPartial('hint',['msg' => '没有获取到公众号帐号基本信息']);
  174. }
  175. $authorizer_info = $authorizerDetailInfo['authorizer_info'];
  176. $nick_name = $authorizer_info['nick_name'];
  177. $head_img = $authorizer_info['head_img'];
  178. $service_type_info = $authorizer_info['service_type_info'];
  179. if($service_type_info['id'] != 2){
  180. return $this->renderPartial('hint',['msg' => '公众号不是服务号']);
  181. }
  182. $verify_type_info = $authorizer_info['verify_type_info'];
  183. if($verify_type_info['id'] != 0){
  184. return $this->renderPartial('hint',['msg' => '公众号还没有认证哦']);
  185. }
  186. $user_name = $authorizer_info['user_name'];
  187. $alias = $authorizer_info['alias'];
  188. $qrcode_url = $authorizer_info['qrcode_url'];
  189. $business_info = $authorizer_info['business_info'];
  190. $open_pay = $business_info['open_pay'];
  191. $merchant = xhMerchantService::getByAppId($authorizer_appid);
  192. $wxAccessTokenTime = date("Y-m-d H:i:s",(time()+$expires_in-100));//安全起见,将过期时间设置得比微信里还短100秒
  193. $set = xhSetMealService::getRandOne();
  194. if(empty($set)){
  195. return $this->renderPartial('hint',['msg' => '没有套餐信息']);
  196. }
  197. if(empty($merchant)){
  198. //没有生成商家,引导进去设置
  199. $session = Yii::$app->session;
  200. $session['wxAppId'] = $authorizer_appid;
  201. $session['wxUserName'] = $user_name;
  202. $session['wxAliasName'] = $alias;
  203. $session['wxAccessToken'] = $authorizer_access_token;
  204. $session['wxAccessTokenTime'] = $wxAccessTokenTime;
  205. $session['wxRefreshToken'] = $authorizer_refresh_token;
  206. $session['logo'] = $head_img;
  207. $session['wxQrcodeUrl'] = $qrcode_url;
  208. $session['allowGenerateMerchant'] = 'ok';//经过授权流程,允许允许生成商家
  209. $trainDemo = configDict::getConfig('trainDemo');
  210. $demoName = $trainDemo['merchant']['name'];//培训演示的公众号
  211. return $this->renderPartial('callback',['merchantName' => $nick_name,'set'=>$set,'demoName'=>$demoName]);
  212. }
  213. $cData = [];
  214. $cData['wxAccessToken'] = $authorizer_access_token;
  215. $cData['wxAccessTokenTime'] = $wxAccessTokenTime;
  216. $cData['wxRefreshToken'] = $authorizer_refresh_token;
  217. $cData['status'] = 2;
  218. $merchantId = $merchant['id'];
  219. xhMerchantService::updateById($merchantId, $cData);
  220. $price = $set['price'];
  221. $now = time();
  222. $apply = xhApplyOrderService::getByMerchantId($merchantId);
  223. if(empty($apply)){
  224. return $this->renderPartial('hint',['msg' => '没有申请记录']);
  225. }
  226. $orderId = $apply['id'];
  227. $price = $apply['actPrice'];
  228. $openMerchant = xhWxOpenService::getMerchant();
  229. $openMerchantAccount = $openMerchant['id'];
  230. $account = $merchant['id'];
  231. //将新生成的商家account加密传到后台绑定管理员,密钥有效期5分钟
  232. $newAccount = urlencode(stringUtil::authcode($account,'ENCODE',300));
  233. if($apply['payStatus'] == 1){
  234. if(empty($merchant['adminId'])){
  235. $this->redirect(Yii::$app->params['adminUrl'].'/admin/generate?account='.$openMerchantAccount.'&newAccount='.$newAccount);
  236. util::end();
  237. }
  238. return $this->renderPartial('hint',['msg' => '恭喜!您已申请成功。']);
  239. }
  240. if($price > 0){
  241. $this->redirect(Yii::$app->params['openUrl'].'/pay/apply?orderId='.$apply['id']);
  242. util::end();
  243. }else{
  244. $inviteCode = $order['inviteCode'];
  245. $now = time();
  246. if(!empty($inviteCode)){
  247. $invite = xhInviteService::getByCode($inviteCode);
  248. if(empty($invite)){
  249. return $this->renderPartial('hint',['msg' => '邀请码不存在']);
  250. }
  251. if($invite['takeStatus'] == 1){//邀请码未使用,并且没有过期
  252. return $this->renderPartial('hint',['msg' => '邀请码已经使用,请联系客服']);
  253. }
  254. $inviteDeadline = $invite['deadline'];
  255. if($now > $inviteDeadline){
  256. return $this->renderPartial('hint',['msg' => '邀请码已经过期,请联系客服']);
  257. }
  258. xhInviteService::updateByCode($inviteCode, ['takeStatus'=>1,'inviteUseTo'=>0,'targetId'=>$orderId,'invitedMerchantId'=>$merchantId]);
  259. }
  260. $updateData = ['inviteCodeStatus'=>1,'payStatus'=>1,'payWay'=>0,'invitedMerchantId'=>$merchantId];
  261. xhApplyOrderService::updateById($orderId, $updateData);
  262. $this->redirect(Yii::$app->params['adminUrl'].'/admin/generate?account='.$openMerchantAccount.'&newAccount='.$newAccount);
  263. util::end();
  264. }
  265. }
  266. public function actionResult()
  267. {
  268. $get = Yii::$app->request->get();
  269. $id = isset($get['id']) ? $get['id'] : 0;
  270. $merchant = xhMerchantService::getById($id);
  271. $status = configDict::getConfig('merchantStatusName');
  272. $orMechantId = configDict::getConfig('merchantId');
  273. $originalMerchant = xhMerchantService::getById($orMechantId);
  274. return $this->renderPartial('result',['originalMerchant' => $originalMerchant,'merchant' => $merchant,'status' => $status]);
  275. }
  276. /**
  277. * 商家申请入驻
  278. */
  279. public function actionAddMerchant()
  280. {
  281. $post = Yii::$app->request->post();
  282. $session = Yii::$app->session;
  283. if(isset($session['allowGenerateMerchant']) == false){
  284. //util::failInfo('没有经过授权的操作');
  285. }
  286. unset($post['_csrf']);
  287. if(isset($session['wxAppId']) == false || empty($session['wxAppId'])){
  288. util::failInfo('公众号appId不存在');
  289. }
  290. $wxAppId = $session['wxAppId'];
  291. if(isset($session['logo'])){
  292. $logo = xhMerchantService::logoSave($session['logo'], $wxAppId);
  293. $post['logo'] = $logo;
  294. }
  295. if(isset($session['wxQrcodeUrl'])){
  296. $wxQrcodeUrl = xhMerchantService::qrCodeSave($session['wxQrcodeUrl'],$wxAppId);
  297. $post['wxQrcodeUrl'] = $wxQrcodeUrl;
  298. }
  299. $date = date("Y-m-d H:i:s");
  300. $inviteCode = isset($post['inviteCode']) ? $post['inviteCode'] : '';
  301. $accountStyle = configDict::getConfig('accountStyle');
  302. $merchantStatus = configDict::getConfig('merchantStatus');
  303. $serviceAccount = $accountStyle['serviceAccount'];//认证服务号
  304. $checking = $merchantStatus['checking'];//审核中
  305. $merchantName = !empty($post['merchantName']) ? $post['merchantName'] : '';
  306. $exists = xhMerchantService::getByMerchantName($merchantName);
  307. if(!empty($exists)){
  308. util::failInfo('花店名称已经存在了');
  309. }
  310. $post['wxAppId'] = $wxAppId;
  311. $post['wxUserName'] = $session['wxUserName'];
  312. $post['wxAliasName'] = $session['wxAliasName'];
  313. $post['wxAccessToken'] = $session['wxAccessToken'];
  314. $post['wxAccessTokenTime'] = $session['wxAccessTokenTime'];
  315. $post['wxRefreshToken'] = $session['wxRefreshToken'];
  316. $post['accountStyle'] = $serviceAccount;//认证服务号
  317. $post['status'] = $checking;//待审
  318. $post['createTime'] = $date;
  319. $post['updateTime'] = $date;
  320. $post['shopLat'] = isset($post['shopLat']) ? (string)$post['shopLat'] : (string)0;
  321. $post['shopLong'] = isset($post['shopLong']) ? (string)$post['shopLong'] : (string)0;
  322. $post['shopPrecision'] = isset($post['shopPrecision']) ? (string)$post['shopPrecision'] : (string)0;
  323. $account = stringUtil::buildOrderNo(0);
  324. $trainDemo = configDict::getConfig('trainDemo');
  325. $demoName = $trainDemo['merchant']['name'];//培训演示的公众号
  326. if(isset($post['merchantName']) && $post['merchantName'] == $demoName){
  327. $account = $trainDemo['merchant']['account'];
  328. }
  329. $post['account'] = $account;
  330. $post['adminId'] = 0;
  331. $post['wxToken'] = stringUtil::buildWxToken(0);
  332. $shopPhotoListString = json_encode(["outdoor"=>$post['shopImg'],'indoor'=>$post['shopImg2']]);//将门店照片组合成json
  333. $post['shopPhotoList'] = $shopPhotoListString;
  334. $merchant = xhMerchantService::applyCreateMerchant($post);
  335. $id = $merchant['id'];
  336. $account = $merchant['id'];
  337. unset($session['allowGenerateMerchant']);
  338. xhWxMenuService::applyInit($merchant);
  339. $set = xhSetMealService::getRandOne();
  340. $price = $set['price'];
  341. $actPrice = $price;
  342. $mainMerchantId = 0;
  343. $now = time();
  344. $orderDeadline = 0;
  345. if(!empty($inviteCode)){
  346. $invite = xhInviteService::getByCode($inviteCode);
  347. if(empty($invite)){
  348. util::failInfo('邀请码不存在');
  349. }
  350. if($invite['takeStatus'] == 1){//邀请码未使用,并且没有过期
  351. util::failInfo('邀请码已经使用');
  352. }
  353. $inviteDeadline = $invite['deadline'];
  354. if($now > $inviteDeadline){
  355. util::failInfo('邀请码已经过期');
  356. }
  357. $mainMerchantId = isset($invite['merchantId']) ? $invite['merchantId'] : 0;
  358. $actPrice = stringUtil::calcSub($set['price'], $invite['save']);//扣除邀请码里的钱
  359. }
  360. $toPay = $actPrice <= 0 ? 'no' : 'yes';//是否需要付款
  361. $inviteCodeStatus = $actPrice <= 0 ? 1 : 0;//邀请码是否已使用
  362. $payStatus = $actPrice <= 0 ? 1 : 0;//付款是否成功
  363. $createTime = date("Y-m-d H:i:s");
  364. $orderId = 'A'.stringUtil::buildOrderNo($id);
  365. $orderData = ['id'=>$orderId,'userId'=>$this->userId,'applyName'=>$merchantName,'email'=>'',
  366. 'createTime'=>$createTime,'accountId'=>0,'inviteCode'=>$inviteCode,'merchantId'=>$mainMerchantId,'invitedMerchantId'=>$id,
  367. 'prePrice'=> $price,'actPrice'=>$actPrice,'deadline'=>$orderDeadline,'inviteCodeStatus'=>(int)$inviteCodeStatus,'payStatus'=>$payStatus,
  368. ];
  369. xhApplyOrderService::add($orderData);//保存订单信息
  370. if($actPrice <= 0 && !empty($inviteCode)){
  371. xhInviteService::updateByCode($inviteCode, ['targetId'=>$orderId,'takeStatus'=>1,'invitedMerchantId'=>$id,'inviteUseTo'=>0]);
  372. }
  373. $openMerchant = xhWxOpenService::getMerchant();
  374. $openMerchantAccount = $openmerchant['id'];
  375. $newAccount = urlencode(stringUtil::authcode($account,'ENCODE',300));
  376. util::successInfo('提交成功','A0001',['account'=>$openMerchantAccount,'newAccount'=>$newAccount,'toPay'=>$toPay,'orderId'=>$orderId,'actPrice'=>$actPrice]);
  377. }
  378. public function actionCheckInviteCode()
  379. {
  380. $get = Yii::$app->request->get();
  381. $code = isset($get['code']) ? $get['code'] : '';
  382. $invite = xhInviteService::getByCode($code);
  383. if(empty($invite)){
  384. util::failInfo('邀请码错误');
  385. }
  386. $now = time();
  387. if($now > $invite['deadline']){
  388. util::failInfo('邀请码已经失效');
  389. }
  390. if(!empty($invite['takeStatus'])){
  391. util::failInfo('邀请码已经使用');
  392. }
  393. util::successInfo('操作成功','A0001',$invite);
  394. }
  395. public function actionSelect()
  396. {
  397. $isWeixin = util::isWeixin();
  398. if($isWeixin == false){
  399. return $this->renderPartial('notWeixin');
  400. }
  401. return $this->renderPartial('select');
  402. }
  403. }