ApplyController.php 18 KB

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