ApplyController.php 19 KB

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