ApplyController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\renew\classes\RenewClass;
  5. use biz\shop\services\ShopAdminService;
  6. use biz\sj\classes\SjClass;
  7. use bizGhs\admin\classes\AdminClass;
  8. use bizGhs\admin\services\AdminService;
  9. use bizHd\saas\services\ApplyService;
  10. use bizHd\wx\classes\WxOpenClass;
  11. use common\components\dict;
  12. use common\components\imgUtil;
  13. use common\components\jwt;
  14. use common\components\miniUtil;
  15. use common\components\noticeUtil;
  16. use common\components\sms;
  17. use common\components\stringUtil;
  18. use Yii;
  19. use common\components\util;
  20. use bizHd\saas\classes\ApplyClass;
  21. use bizHd\saas\services\RegionService;
  22. use bizHd\shop\classes\MainInviteClass;
  23. class ApplyController extends BaseController
  24. {
  25. public $guestAccess = ['register', 'get-auth-code', 'register-fee-preview', 'register-invite-context'];
  26. //申请开通批发店 ssh 20231125
  27. public function actionOpenPf()
  28. {
  29. $shop = $this->shop;
  30. $pfShopId = $shop->pfShopId ?? 0;
  31. if (!empty($pfShopId)) {
  32. util::fail('已经开通过了');
  33. }
  34. $mobile = $shop->mobile ?? 0;
  35. if (empty($mobile)) {
  36. util::fail('没有找到手机号');
  37. }
  38. $apply = ApplyClass::getByCondition(['mobile' => $mobile], true);
  39. if (empty($apply)) {
  40. util::fail('找不到历史申请记录');
  41. }
  42. $apply->introSjId = 1;
  43. $apply->introStyle = 0;
  44. $apply->introSjName = 0;
  45. $apply->introShopId = 0;
  46. $apply->style = 2;
  47. $apply->status = 1;
  48. $apply->save();
  49. $shopName = $shop->shopName ?? '';
  50. $sjName = $shop->merchantName ?? '';
  51. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  52. noticeUtil::push("重要提醒,零售店【{$name}】申请开通批发店", '15280215347');
  53. util::complete('已提交申请');
  54. }
  55. //获取验证码 ssh 20210117
  56. public function actionGetAuthCode()
  57. {
  58. $get = Yii::$app->request->get();
  59. $mobile = $get['mobile'] ?? '';
  60. if (stringUtil::isMobile($mobile) == false) {
  61. util::fail('请输入正确手机号');
  62. }
  63. $rand = rand(11111, 99999);
  64. $minute = 10;
  65. sms::send($mobile . ',' . $rand . ',' . $minute, '注册验证码:{$var},{$var}分钟内有效');
  66. $ptStyle = Yii::$app->params['ptStyle'];
  67. $cacheKey = 'register_mobile_code_' . $ptStyle . '_' . $mobile;
  68. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 600, $rand]);
  69. util::complete();
  70. }
  71. /**
  72. * 花店注册页:会员费、邀请减免、应付(未登录可访问)
  73. * 已登录时传入 mainId,用于拦截自己的邀请码减免
  74. */
  75. public function actionRegisterFeePreview()
  76. {
  77. $get = Yii::$app->request->get();
  78. $inviteCode = trim($get['inviteCode'] ?? '');
  79. $allowInviteBenefit = $this->canUseRegisterInviteBenefit();
  80. $data = MainInviteClass::resolveRegisterFeePreview(
  81. $inviteCode,
  82. $allowInviteBenefit,
  83. (int) $this->mainId
  84. );
  85. util::success($this->appendInviteBenefitFlags($data));
  86. }
  87. /**
  88. * 邀请开通页上下文:是否已有 xhShop、xhApply 邀请码与减免快照(未登录返回 hasShop=0)
  89. */
  90. public function actionRegisterInviteContext()
  91. {
  92. $hasShop = $this->shopId > 0 ? 1 : 0;
  93. $shopName = '';
  94. $shopMobile = '';
  95. if ($hasShop && !empty($this->shop)) {
  96. $shopAttrs = is_object($this->shop) ? $this->shop->attributes : (array) $this->shop;
  97. $shopName = (string) ($shopAttrs['shopName'] ?? ($shopAttrs['name'] ?? ''));
  98. $shopMobile = (string) ($shopAttrs['mobile'] ?? '');
  99. }
  100. $data = [
  101. 'hasShop' => $hasShop,
  102. 'shopId' => (int) $this->shopId,
  103. 'applyId' => 0,
  104. 'inviteCode' => '',
  105. 'hdDiscountAmount' => 0,
  106. 'inviteCommissionAmount' => 0,
  107. 'shopName' => $shopName,
  108. 'shopMobile' => $shopMobile,
  109. ];
  110. if ($this->adminId > 0) {
  111. $apply = $this->findRegisterApplyForAdmin($this->adminId, $this->shopId);
  112. if (!empty($apply)) {
  113. $data['applyId'] = (int) ($apply['id'] ?? 0);
  114. $data['inviteCode'] = trim((string) ($apply['inviteCode'] ?? ''));
  115. $data['hdDiscountAmount'] = round((float) ($apply['hdDiscountAmount'] ?? 0), 2);
  116. $data['inviteCommissionAmount'] = round((float) ($apply['inviteCommissionAmount'] ?? 0), 2);
  117. }
  118. }
  119. util::success($this->appendInviteBenefitFlags($data));
  120. }
  121. /**
  122. * 已开店用户:支付前保存邀请码并刷新 xhApply 减免快照
  123. */
  124. public function actionSaveRegisterInvite()
  125. {
  126. if ($this->adminId <= 0 || $this->shopId <= 0) {
  127. util::fail('请先登录并选择门店');
  128. }
  129. $allowInviteBenefit = $this->canUseRegisterInviteBenefit();
  130. $apply = $this->findRegisterApplyForAdmin($this->adminId, $this->shopId);
  131. if (empty($apply)) {
  132. util::fail('未找到注册申请记录');
  133. }
  134. $applyId = (int) ($apply['id'] ?? 0);
  135. // 已有 xhRenew 付费成功:续费不再更新邀请减免
  136. if (!$allowInviteBenefit) {
  137. $preview = MainInviteClass::resolveRegisterFeePreview('', false);
  138. util::success($this->appendInviteBenefitFlags(array_merge($preview, [
  139. 'applyId' => $applyId,
  140. ])));
  141. return;
  142. }
  143. $post = Yii::$app->request->post();
  144. $inviteCode = trim((string) ($post['inviteCode'] ?? ''));
  145. $fee = MainInviteClass::resolveRegisterFeeSubmit($inviteCode, true, (int) $this->mainId);
  146. // 自邀 / 无效 / 不存在:清空邀请码落库,无减免无佣金,仍允许支付
  147. if (
  148. $inviteCode !== '' && (
  149. !empty($fee['inviteNotFound']) ||
  150. !empty($fee['inviteSelf']) ||
  151. !empty($fee['inviteInvalid'])
  152. )
  153. ) {
  154. $inviteCode = '';
  155. $fee = MainInviteClass::resolveRegisterFeeSubmit('', true, (int) $this->mainId);
  156. }
  157. $hdDiscountAmount = round((float) ($fee['hdDiscountAmount'] ?? 0), 2);
  158. $inviteCommissionAmount = round((float) ($fee['inviteCommissionAmount'] ?? 0), 2);
  159. ApplyClass::updateById($applyId, [
  160. 'inviteCode' => $inviteCode,
  161. 'hdDiscountAmount' => $hdDiscountAmount,
  162. 'inviteCommissionAmount' => $inviteCommissionAmount,
  163. ]);
  164. $preview = MainInviteClass::resolveRegisterFeePreview($inviteCode, true, (int) $this->mainId);
  165. util::success($this->appendInviteBenefitFlags(array_merge($preview, [
  166. 'applyId' => $applyId,
  167. 'inviteCode' => $inviteCode,
  168. 'hdDiscountAmount' => $hdDiscountAmount,
  169. 'inviteCommissionAmount' => $inviteCommissionAmount,
  170. ])));
  171. }
  172. //邀请花店海报 ssh 20210610
  173. public function actionInviteHdPoster()
  174. {
  175. $merchant = WxOpenClass::getWxInfo();
  176. $page = 'pagesClient/official/index';
  177. $ptStyle = dict::getDict('ptStyle', 'hd');
  178. $scene = 'hdShopAdminId=' . $this->shopAdminId;
  179. $imgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  180. $url = imgUtil::groupImg($imgUrl);
  181. $respond = [
  182. 'imgUrl' => $url,
  183. ];
  184. util::success($respond);
  185. }
  186. //邀请花店海报 ssh 20210610
  187. public function actionInviteGhsPoster()
  188. {
  189. $merchant = WxOpenClass::getGhsWxInfo();
  190. $page = 'pagesClient/official/index';
  191. $ptStyle = dict::getDict('ptStyle', 'ghs');
  192. $scene = 'hdShopAdminId=' . $this->shopAdminId;
  193. $imgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  194. $url = imgUtil::groupImg($imgUrl);
  195. $respond = [
  196. 'imgUrl' => $url,
  197. ];
  198. util::success($respond);
  199. }
  200. //我的代理 ssh 2019.12.26
  201. public function actionList()
  202. {
  203. $get = Yii::$app->request->get();
  204. $where = [];
  205. $where['introSjId'] = $this->sjId;
  206. $status = $get['status'] ?? 0;
  207. $style = $get['style'] ?? SjClass::STYLE_RETAIL;
  208. if (!empty($status)) {
  209. $where['status'] = $status;
  210. }
  211. $where['style'] = $style;
  212. $data = ApplyService::getApplyList($where);
  213. util::success($data);
  214. }
  215. //申请试用 ssh 2020.1.11
  216. public function actionRegister()
  217. {
  218. $connection = Yii::$app->db;
  219. $transaction = $connection->beginTransaction();
  220. $post = Yii::$app->request->post();
  221. //前期限制只有批发店邀请了才能注册
  222. $ghsShopAdminId = $post['ghsShopAdminId'] ?? 0;
  223. $hdShopAdminId = $post['hdShopAdminId'] ?? 0;
  224. if (empty($ghsShopAdminId) && empty($hdShopAdminId)) {
  225. //util::fail('批发店推荐才能注册,编号:' . $ghsShopAdminId . ' ' . $hdShopAdminId);
  226. }
  227. $shopName = $post['name'] ?? '';
  228. if (empty($shopName)) {
  229. util::fail('名称不能为空');
  230. }
  231. $mobile = $post['mobile'] ?? '';
  232. if (stringUtil::isMobile($mobile) == false) {
  233. util::fail('请填写正确手机号');
  234. }
  235. if (stringUtil::getWordNum($shopName) > 8) {
  236. util::fail('名称不能超过8个汉字');
  237. }
  238. $arr = ['斗南鲜花', '惠雅鲜花', '纯彩花艺', '泉城鲜花', '杭州斗南', '惠雅', '国恋', '花悠星', '勇记', '丰行', '昱成', '问境', '珑松'];
  239. foreach ($arr as $it) {
  240. if (strstr($shopName, $it) != null) {
  241. util::fail('花店名称已经存在,请换个名称');
  242. }
  243. }
  244. $has = SjClass::getByCondition(['name' => $shopName]);
  245. if (!empty($has)) {
  246. //util::fail('名称已经被别人使用,请换个名字,或者名字后面加个2');
  247. }
  248. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  249. if (!empty($has)) {
  250. //手机号开了零售店不能再开批发店,开了批发店不能再开零售店!!
  251. util::fail('手机号已经申请过了');
  252. }
  253. $hasShop = ShopClass::getByCondition(['mobile' => $mobile]);
  254. if (!empty($hasShop)) {
  255. //手机号开了零售店不能再开批发店,开了批发店不能再开零售店!!
  256. util::fail('手机号已经被使用');
  257. }
  258. try {
  259. $post['adminId'] = $this->adminId;
  260. $authCode = $post['authCode'] ?? '';
  261. $mobile = $post['mobile'] ?? '';
  262. $fillMobile = $post['fillMobile'] ?? 1;
  263. //避免重复提交
  264. util::checkRepeatCommit($mobile, 10);
  265. $ptStyle = Yii::$app->params['ptStyle'];
  266. $cacheKey = 'register_mobile_code_' . $ptStyle . '_' . $mobile;
  267. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  268. if ($fillMobile == 1) {
  269. if ($authCode != 19640123) {
  270. if (empty($saveAuthCode)) {
  271. util::fail('请填写验证码哦');
  272. }
  273. if (empty($authCode)) {
  274. util::fail('请填写验证码');
  275. }
  276. if ($saveAuthCode != $authCode) {
  277. util::fail('验证码错误');
  278. }
  279. }
  280. } else {
  281. $cacheKey = 'getMobile_' . $mobile;
  282. $hasGet = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  283. if (empty($hasGet)) {
  284. util::fail('请获取手机');
  285. }
  286. }
  287. $adminName = $post['adminName'] ?? '';
  288. $miniOpenId = $post['miniOpenId'] ?? '';
  289. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'style' => $ptStyle, 'miniOpenId' => $miniOpenId];
  290. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  291. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  292. $adminId = $admin['id'] ?? 0;
  293. $currentShopId = $admin['currentShopId'] ?? 0;
  294. if (!empty($currentShopId)) {
  295. util::fail('您已提交过申请,管理员ID:' . $adminId);
  296. }
  297. if (isset($admin['openShop']) && $admin['openShop'] == 2) {
  298. util::fail('审核中,无需重复申请');
  299. }
  300. $post['adminId'] = $adminId;
  301. $post['adminName'] = $admin['name'] ?? '';
  302. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  303. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  304. $post['from'] = ApplyClass::FROM_RETAIL;
  305. $post['style'] = SjClass::STYLE_RETAIL;
  306. //增加或更新申请
  307. $respond = ApplyService::replaceRetail($post);
  308. $id = $respond['id'] ?? 0;
  309. if (empty($id)) {
  310. util::fail('申请失败');
  311. }
  312. // 邀请码快照写入 xhApply(选填);自邀/无效/不存在则清空后落库,仍可注册支付
  313. $inviteCode = trim((string) ($post['inviteCode'] ?? ''));
  314. $fee = MainInviteClass::resolveRegisterFeeSubmit($inviteCode, true, (int) $this->mainId);
  315. if (
  316. $inviteCode !== '' && (
  317. !empty($fee['inviteNotFound']) ||
  318. !empty($fee['inviteSelf']) ||
  319. !empty($fee['inviteInvalid'])
  320. )
  321. ) {
  322. $inviteCode = '';
  323. $fee = MainInviteClass::resolveRegisterFeeSubmit('', true, (int) $this->mainId);
  324. }
  325. ApplyClass::updateById($id, [
  326. 'inviteCode' => $inviteCode,
  327. 'hdDiscountAmount' => round((float) ($fee['hdDiscountAmount'] ?? 0), 2),
  328. 'inviteCommissionAmount' => round((float) ($fee['inviteCommissionAmount'] ?? 0), 2),
  329. ]);
  330. $passInfo = ApplyService::pass($id);
  331. $mobile = $post['mobile'] ?? '';
  332. //输出登录信息
  333. $newShop = $passInfo['shop'] ?? [];
  334. $newMainId = $newShop->mainId ?? 0;
  335. $newShopId = $newShop->id ?? 0;
  336. $pfShopId = $newShop->pfShopId ?? 0;
  337. $onlyCg = $newShop->onlyCg ?? 1;
  338. ApplyClass::updateById($id, ['shopId' => $newShopId]);
  339. $inviterMainId = (int) ($fee['inviterMainId'] ?? 0);
  340. // 新店 mainId 与邀请人相同(极端自邀)时不挂邀请关系
  341. if ($inviterMainId > 0 && $inviterMainId === (int) $newMainId) {
  342. $inviterMainId = 0;
  343. }
  344. MainInviteClass::createInviteeRecord($newMainId, $inviterMainId);
  345. $shareLogo = imgUtil::groupImg('buy_logo.jpg');
  346. if (!empty($newShop->shareLogo)) {
  347. $shareLogo = imgUtil::groupImg($newShop->shareLogo);
  348. }
  349. //前端用于判断是否注册
  350. $admin['currentShopId'] = $newShopId;
  351. $adminId = $admin['id'];
  352. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'mainId' => $newMainId], true);
  353. if (empty($shopAdmin)) {
  354. util::fail('没有找到管理员');
  355. }
  356. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  357. $shopAdmin->save();
  358. $shopAdminId = $shopAdmin->id ?? 0;
  359. //是否有切换门店的权限
  360. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  361. $showDemo = 1;
  362. $token = jwt::getNewToken($adminId);
  363. $remind = getenv('HD_UPDATE_REMIND') == false ? 0 : getenv('HD_UPDATE_REMIND');
  364. $hdUpgrading = getenv('HD_UPGRADING') == false ? 0 : getenv('HD_UPGRADING');
  365. if ($hdUpgrading == 1) {
  366. $allowShopAdminIdsStr = getenv('HD_UPGRADE_ALLOW_SHOP_ADMIN_IDS') ?: '';
  367. $allowShopAdminIds = !empty($allowShopAdminIdsStr) ? explode(',', $allowShopAdminIdsStr) : [];
  368. if (!in_array($shopAdminId, $allowShopAdminIds)) {
  369. util::fail('系统升级中,稍后再试');
  370. }
  371. }
  372. $transaction->commit();
  373. //noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 注册", '15280215347');
  374. $apiHost = Yii::$app->params['hdHost'];
  375. $imgUploadApi = $apiHost . '/upload/save-file';
  376. //惠雅鲜花员工不能看收入情况
  377. if (in_array($adminId, [2366, 2812, 4004])) {
  378. $shopAdmin->super = 0;
  379. }
  380. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  381. util::success([
  382. 'token' => $token,
  383. 'admin' => $admin,
  384. 'shopId' => $newShopId,
  385. 'applyId' => (int) $id,
  386. 'shopAdminId' => $shopAdminId,
  387. 'switchShop' => $switchShop,
  388. 'pfShopId' => $pfShopId,
  389. 'onlyCg' => $onlyCg,
  390. 'shareLogo' => $shareLogo,
  391. 'showDemo' => $showDemo,
  392. 'imgUploadApi' => $imgUploadApi,
  393. //有小程序新版本提示更新
  394. 'update' => $remind,
  395. 'staff' => $shopAdmin,
  396. ]);
  397. } catch (\Exception $exception) {
  398. $transaction->rollBack();
  399. $message = $exception->getMessage() ?? '';
  400. Yii::info("申请报错:" . $message);
  401. noticeUtil::push("花店注册失败,手机号{$mobile},原因:" . $message, '15280215347');
  402. util::fail('提交失败');
  403. }
  404. }
  405. //申请状态
  406. public function actionApplyStatus()
  407. {
  408. //-1申请试用 0审核中 1审核通过、试用中 2审核未通过 3已付费,立即订购 4试用到期失效,待付款,立即订购,价格不显示
  409. $status = -1;
  410. util::success(['status' => $status]);
  411. }
  412. //地图相关信息 ssh 2020.1.14
  413. public function actionRegionRelate()
  414. {
  415. $regionTree = RegionService::tree();
  416. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  417. $out = ['region' => $regionTree, 'thirdMapKey' => $mapKey];
  418. util::success($out);
  419. }
  420. //获取申请的验证码 ssh 2020.2.23
  421. public function actionGetValidateCode()
  422. {
  423. $mobile = Yii::$app->request->get('mobile');
  424. util::success(['code' => rand(1111, 9999)]);
  425. }
  426. /**
  427. * 按 adminId / shopId / 手机号查找 xhApply 注册记录
  428. */
  429. private function findRegisterApplyForAdmin(int $adminId, int $shopId): array
  430. {
  431. $apply = [];
  432. if ($adminId > 0) {
  433. $row = ApplyClass::getByCondition(['adminId' => $adminId], false);
  434. if (!empty($row)) {
  435. $apply = is_array($row) ? $row : (array) $row;
  436. }
  437. }
  438. if (empty($apply) && $shopId > 0) {
  439. $row = ApplyClass::getByCondition(['shopId' => $shopId], false);
  440. if (!empty($row)) {
  441. $apply = is_array($row) ? $row : (array) $row;
  442. }
  443. }
  444. if (empty($apply) && $adminId > 0 && !empty($this->admin)) {
  445. $mobile = $this->shop->mobile ?? '';
  446. if ($mobile !== '') {
  447. $row = ApplyClass::getByCondition(['mobile' => $mobile], false);
  448. if (!empty($row)) {
  449. $apply = is_array($row) ? $row : (array) $row;
  450. }
  451. }
  452. }
  453. return $apply;
  454. }
  455. /**
  456. * 是否仍可使用邀请码减免与佣金(无 xhRenew 付费成功记录时为 true)
  457. */
  458. private function canUseRegisterInviteBenefit(): bool
  459. {
  460. if ($this->mainId <= 0 && $this->shopId <= 0) {
  461. return true;
  462. }
  463. return !RenewClass::hasPaidMemberRenew((int) $this->mainId, (int) $this->shopId);
  464. }
  465. /**
  466. * 补充首次付费邀请权益标记
  467. */
  468. private function appendInviteBenefitFlags(array $data): array
  469. {
  470. $available = $this->canUseRegisterInviteBenefit();
  471. $data['inviteBenefitAvailable'] = $available ? 1 : 0;
  472. $data['hasPaidRegisterRenew'] = $available ? 0 : 1;
  473. return $data;
  474. }
  475. }