CustomController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\admin\services\AdminService;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\custom\classes\HdClass;
  6. use bizHd\custom\services\CustomService;
  7. use biz\sj\services\MerchantAssetService;
  8. use biz\sj\services\MerchantRemarkService;
  9. use bizHd\member\classes\MemberLevelClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\shop\classes\ShopClass;
  12. use bizHd\stat\classes\StatVisitClass;
  13. use bizHd\user\services\UserAssetService;
  14. use bizHd\user\services\UserGrowthService;
  15. use bizHd\user\services\UserService;
  16. use bizHd\user\services\UserUpgradeService;
  17. use common\components\wxUtil;
  18. use common\services\xhMerchantExtendService;
  19. use common\services\xhRechargeService;
  20. use common\services\xhTMessageService;
  21. use Yii;
  22. use common\components\stringUtil;
  23. use common\components\util;
  24. use biz\sj\services\MerchantExtendService;
  25. use bizHd\message\services\SmsService;
  26. use bizHd\user\classes\UserClass;
  27. use yii\helpers\Json;
  28. use Overtrue\ChineseCalendar\Calendar;
  29. class CustomController extends BaseController
  30. {
  31. public $guestAccess = ['list'];
  32. //修改生日 ssh 20250814
  33. public function actionModifyBirthday()
  34. {
  35. date_default_timezone_set('PRC');
  36. $get = Yii::$app->request->get();
  37. $customId = $get['customId'];
  38. $custom = CustomClass::getById($customId, true);
  39. if (empty($custom)) {
  40. util::fail('没有找到客户');
  41. }
  42. $shopId = $this->shopId;
  43. if ($custom->shopId != $shopId) {
  44. util::fail('不是你的客户');
  45. }
  46. $lunar = $get['lunar'] ?? 0;
  47. $birthdayMonth = $get['birthdayMonth'] ?? 1;
  48. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  49. util::fail('月份必须1到12月');
  50. }
  51. $birthdayDate = $get['birthdayDate'] ?? 1;
  52. if ($birthdayDate > 31 || $birthdayDate < 1) {
  53. util::fail('日期必须1到31号');
  54. }
  55. $userId = $custom->userId;
  56. $user = UserClass::getById($userId, true);
  57. if (empty($user)) {
  58. util::fail('客户信息缺失');
  59. }
  60. if ($lunar == 1 && $custom->birthdayDate == 31) {
  61. util::fail('农历没有31号');
  62. }
  63. $calendar = new Calendar();
  64. if ($lunar == 1) {
  65. try {
  66. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  67. } catch (\InvalidArgumentException $e) {
  68. $msg = $e->getMessage();
  69. util::fail($msg);
  70. }
  71. //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
  72. $month = $result['gregorian_month'] ?? 1;
  73. $date = $result['gregorian_day'] ?? 1;
  74. $birthday = date("Y") . '-' . $month . '-' . $date;
  75. } else {
  76. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  77. }
  78. $birthdayTime = strtotime($birthday);
  79. if ($user->birthdaySet == 1) {
  80. util::fail('客户已填写,不能修改');
  81. }
  82. //用户自己没有设置过生日,直接改
  83. $custom->lunar = $lunar;
  84. $custom->birthdayDate = $birthdayDate;
  85. $custom->birthdayMonth = $birthdayMonth;
  86. $custom->birthday = $birthday;
  87. $custom->birthdayTime = $birthdayTime;
  88. $custom->save();
  89. $user->lunar = $lunar;
  90. $user->birthdayDate = $birthdayDate;
  91. $user->birthdayMonth = $birthdayMonth;
  92. $user->birthday = $birthday;
  93. $user->birthdayTime = $birthdayTime;
  94. $user->save();
  95. util::complete('修改成功');
  96. }
  97. public function actionChangeLevel()
  98. {
  99. $get = Yii::$app->request->get();
  100. $customId = $get['customId'];
  101. $level = $get['level'] ?? 0;
  102. if ($level < 0) {
  103. util::fail('等级选择错误');
  104. }
  105. $custom = CustomClass::getById($customId, true);
  106. if (empty($custom)) {
  107. util::fail('没有找到客户');
  108. }
  109. $shopId = $this->shopId;
  110. if ($custom->shopId != $shopId) {
  111. util::fail('不是你的客户');
  112. }
  113. $hdId = $custom->hdId;
  114. $hd = HdClass::getById($hdId, true);
  115. if (empty($hd)) {
  116. util::fail('客户信息缺失,编号3445');
  117. }
  118. $staff = $this->shopAdmin;
  119. $staffId = $staff->id;
  120. $staffName = $staff->name;
  121. $params = [
  122. 'style' => 1,//手动修改
  123. 'staffId' => $staffId,
  124. 'staffName' => $staffName,
  125. ];
  126. $shop = $this->shop;
  127. $connection = Yii::$app->db;
  128. $transaction = $connection->beginTransaction();
  129. try {
  130. $beforeLevel = $custom->member ?? 0;
  131. CustomClass::levelChange($beforeLevel, $level, $shop, $custom, $hd, $params);
  132. $transaction->commit();
  133. util::complete('修改成功');
  134. } catch (\Exception $exception) {
  135. $transaction->rollBack();
  136. Yii::info("修改失败:" . $exception->getMessage());
  137. util::fail('修改失败');
  138. }
  139. }
  140. //修改客户地址 ssh 20231109
  141. public function actionModifyAddress()
  142. {
  143. $post = Yii::$app->request->post();
  144. $id = $post['id'] ?? 0;
  145. unset($post['id']);
  146. $custom = CustomClass::getById($id, true);
  147. if (empty($custom)) {
  148. util::fail('没有找到客户信息');
  149. }
  150. $userId = $custom->userId ?? 0;
  151. $city = $post['city'] ?? '';
  152. $dist = $post['dist'] ?? '';
  153. $address = $post['address'] ?? '';
  154. $floor = $post['floor'] ?? '';
  155. $post['fullAddress'] = $city . $dist . $address . $floor;
  156. UserClass::updateByCondition(['id' => $userId], $post);
  157. CustomClass::updateByCondition(['id' => $id], $post);
  158. util::complete('修改成功');
  159. }
  160. //修改名称 ssh 2022096
  161. public function actionChangeName()
  162. {
  163. $get = Yii::$app->request->get();
  164. $name = $get['name'] ?? '';
  165. $id = $get['id'] ?? 0;
  166. if (empty($name)) {
  167. util::fail('请填写名称');
  168. }
  169. $py = stringUtil::py($name);
  170. $custom = CustomClass::getById($id, true);
  171. CustomClass::valid($custom, $this->shopId);
  172. $custom->name = $name;
  173. $custom->py = $py;
  174. $custom->save();
  175. OrderClass::updateByCondition(['customId' => $id], ['customName' => $name, 'customNamePy' => $py]);
  176. util::complete();
  177. }
  178. //批量添加客户 ssh 20220919
  179. public function actionBatchAddCustom()
  180. {
  181. $post = Yii::$app->request->post();
  182. $connection = Yii::$app->db;
  183. $transaction = $connection->beginTransaction();
  184. try {
  185. for ($i = 1; $i <= 5; $i++) {
  186. $name = $post['name' . $i] ?? '';
  187. $mobile = $post['mobile' . $i] ?? '';
  188. $confirmMobile = $post['confirmMobile' . $i] ?? '';
  189. if (empty($name) && empty($mobile) && empty($confirmMobile)) {
  190. continue;
  191. }
  192. if (empty($name)) {
  193. util::fail('客户名称不能为空');
  194. }
  195. if (stringUtil::getWordNum($name) > 15) {
  196. util::fail('名称不能超过15个汉字');
  197. }
  198. if (empty($mobile)) {
  199. util::fail($name . " 手机号不能为空");
  200. }
  201. if (stringUtil::isMobile($mobile) == false) {
  202. util::fail($name . " 手机号格式不正确");
  203. }
  204. if ($mobile != $confirmMobile) {
  205. util::fail($name . " 二次手机号不一致");
  206. }
  207. $user = \bizMall\user\classes\UserClass::replaceUser(['name' => $name, 'mobile' => $mobile]);
  208. if (isset($this->shop) && !empty($this->shop)) {
  209. //花店增加客户,散客增加门店
  210. CustomClass::buildRelation($this->shop, $user);
  211. }
  212. }
  213. $transaction->commit();
  214. util::complete();
  215. } catch (\Exception $e) {
  216. $transaction->rollBack();
  217. util::fail('添加失败');
  218. }
  219. }
  220. //客户列表 ssh 2019.11.30
  221. public function actionList()
  222. {
  223. $get = Yii::$app->request->get();
  224. $type = $get['type'] ?? 0;
  225. $searchStyle = $get['searchStyle'] ?? 0;
  226. $where = ['shopId' => $this->shopId];
  227. if ($type == 1) {
  228. //消费排行
  229. $sort = 'buyAmount DESC';
  230. } else if ($type == 2) {
  231. //余额客户
  232. $sort = 'balance DESC';
  233. } else if ($type == 3) {
  234. //生日
  235. $sort = 'birthdayTime ASC';
  236. $where['birthdayTime>'] = 0;
  237. } else {
  238. $sort = 'visitTime DESC';
  239. }
  240. $name = $get['name'] ?? '';
  241. if (empty($name)) {
  242. $name = $get['keyword'] ?? '';
  243. }
  244. if (empty($name)) {
  245. $name = $get['searchText'] ?? '';
  246. }
  247. if (!empty($name)) {
  248. if ($searchStyle == 0) {
  249. $where['mobile'] = ['like', $name];
  250. } else {
  251. if (stringUtil::isLetter($name)) {
  252. $where['py'] = ['like', $name];
  253. } else {
  254. $where['name'] = ['like', $name];
  255. }
  256. }
  257. }
  258. $list = CustomService::getCustomList($where, $sort);
  259. util::success($list);
  260. }
  261. //获取客户信息 ssh 20250510
  262. public function actionInfo()
  263. {
  264. $get = Yii::$app->request->get();
  265. $id = $get['id'] ?? 0;
  266. $custom = CustomClass::getById($id, true);
  267. if (empty($custom)) {
  268. util::fail('没有找到客户信息');
  269. }
  270. if ($custom->shopId != $this->shopId) {
  271. util::fail('不是你的客户');
  272. }
  273. util::success(['custom' => $custom]);
  274. }
  275. //获取客户基本和资产信息 ssh 2019.11.30
  276. public function actionDetail()
  277. {
  278. $userId = Yii::$app->request->get('userId');
  279. $info = CustomClass::getCustomInfo($userId);
  280. CustomClass::valid($info, $this->shopId);
  281. util::success($info);
  282. }
  283. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  284. public function actionUserDetail()
  285. {
  286. $userId = Yii::$app->request->get('userId');
  287. $info = UserService::getFullUserInfo($userId);
  288. UserService::valid($info, $this->sjId);
  289. $list = MerchantRemarkService::getRemarkList($userId);
  290. $info['remarkList'] = $list;
  291. util::success($info);
  292. }
  293. //添加客户,申请会员时会自动同步资产 ssh 2019.11.30
  294. public function actionAdd()
  295. {
  296. $post = Yii::$app->request->post();
  297. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  298. util::fail('请输入手机号');
  299. }
  300. if ($post['mobile'] != $post['confirmMobile']) {
  301. util::fail('二次输入手机号不一样');
  302. }
  303. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  304. AdminService::verifyPayPassword($this->adminId, $payPassword);
  305. $mobile = $post['mobile'];
  306. $user = UserService::getByMobile($mobile, $this->sjId);
  307. if (!empty($user)) {
  308. util::fail('手机号已使用');
  309. }
  310. $post['sjId'] = $this->sjId;
  311. $post['member'] = $post['memberLevel'];
  312. UserService::addUser($post);
  313. util::complete();
  314. }
  315. //充值 ssh 2019.12.1
  316. public function actionRecharge()
  317. {
  318. $post = Yii::$app->request->post();
  319. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  320. AdminService::verifyPayPassword($this->adminId, $payPassword);
  321. $userId = isset($post['userId']) ? $post['userId'] : 0;
  322. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  323. $user = UserService::getById($userId);
  324. if (empty($user)) {
  325. util::fail('找不到客户');
  326. }
  327. if ($user['sjId'] != $this->sjId) {
  328. util::fail('您无法操作');
  329. }
  330. $userAsset = UserAssetService::getByUserId($userId);
  331. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  332. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  333. $upData['balance'] = $totalBalance;
  334. $upData['totalRecharge'] = $totalRecharge;
  335. $merchantAsset = MerchantAssetService::getBySjId($this->sjId);
  336. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj->attributes, $this->sjExtend->attributes, $merchantAsset);
  337. if ($return['code'] == 'A0001') {
  338. util::complete();
  339. }
  340. util::fail();
  341. }
  342. //升级 ssh 2019.12.1
  343. public function actionUpgrade()
  344. {
  345. $post = Yii::$app->request->post();
  346. $userId = isset($post['userId']) ? $post['userId'] : 0;
  347. $member = isset($post['member']) ? $post['member'] : 0;
  348. $user = UserService::getById($userId);
  349. if (empty($user)) {
  350. util::fail('客户不存在');
  351. }
  352. //验证是否商家客户
  353. UserService::valid($user, $this->sjId);
  354. //验证确认密码
  355. $payPassword = $post['payPassword'];
  356. AdminService::verifyPayPassword($this->adminId, $payPassword);
  357. $grade = xhMerchantExtendService::getGradeList($this->sjExtend->attributes, 'desc');
  358. //升级后的成长值
  359. $newLevelGrowth = $grade[$member]['growth'];
  360. $newDiscount = $grade[$member]['discount'];
  361. $userAsset = UserAssetService::getByUserId($userId);
  362. if ($userAsset['member'] >= $member) {
  363. util::fail('客户已经达到此级别了');
  364. }
  365. $now = time();
  366. $date = date("Y-m-d H:i", $now);
  367. $connection = Yii::$app->db;
  368. $transaction = $connection->beginTransaction();
  369. try {
  370. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  371. $upData['growth'] = $newLevelGrowth;
  372. $upData['member'] = $member;
  373. $upData['discount'] = $newDiscount;
  374. UserAssetService::updateByUserId($userId, $upData);
  375. UserService::updateById($userId, ['member' => $member]);
  376. $upgradeData = [
  377. 'member' => $member,
  378. 'prevMember' => $userAsset['member'],
  379. 'sjId' => $this->sjId,
  380. 'gainType' => 0,
  381. 'addTime' => $now,
  382. 'userId' => $userId,
  383. ];
  384. $upgradeRespond = UserUpgradeService::add($upgradeData);
  385. $growthData = [
  386. 'userId' => $userId,
  387. 'userName' => $user['userName'],
  388. 'sjId' => $this->sjId,
  389. 'relateId' => $upgradeRespond['id'],
  390. 'growth' => $newLevelGrowth,
  391. 'num' => $addGrowth,
  392. 'io' => 1,
  393. 'payWay' => 4,
  394. 'alipayId' => '',
  395. 'event' => '管理员操作升到' . $member . '级增加成长值',
  396. 'operatorId' => $userId,
  397. 'createTime' => $date,
  398. 'gainType' => 0,
  399. 'addTime' => $now,
  400. ];
  401. UserGrowthService::add($growthData);
  402. $transaction->commit();
  403. } catch (\Exception $e) {
  404. $transaction->rollBack();
  405. util::fail();
  406. }
  407. $allTM = xhTMessageService::getList($this->sjId);
  408. $openId = isset($user['openId']) ? $user['openId'] : '';
  409. if (empty($openId)) {
  410. util::complete();
  411. }
  412. $shortTempId = 'OPENTM205211943';//升级通知
  413. if (isset($allTM[$shortTempId]) == false) {
  414. util::complete();
  415. }
  416. $tempId = $allTM[$shortTempId];
  417. $data = [
  418. "touser" => $openId, "template_id" => $tempId, "url" => '',
  419. "data" => [
  420. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  421. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  422. "keyword2" => ["value" => $date, "color" => "#173177"],
  423. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  424. ]];
  425. wxUtil::sendTaskInform($data, $this->sj);
  426. util::complete();
  427. }
  428. //重置客户密码
  429. public function actionResetPayPassword()
  430. {
  431. $post = Yii::$app->request->post();
  432. $userId = isset($post['userId']) ? $post['userId'] : 0;
  433. $password = isset($post['password']) ? $post['password'] : 0;
  434. $info = UserService::getById($userId);
  435. UserService::valid($info, $this->sjId);
  436. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  437. util::complete();
  438. }
  439. //获取登陆客户的资产 ssh 2019.11.22
  440. public function actionAsset()
  441. {
  442. $asset = UserAssetService::getByUserId($this->adminId);
  443. util::success($asset);
  444. }
  445. //获取登陆客户的优惠情况 ssh 2019.12.3
  446. public function actionDiscount()
  447. {
  448. $discount = UserAssetService::getDiscount($this->adminId);
  449. util::success($discount);
  450. }
  451. //小程序用户完整信息 ssh 2019.12.12
  452. public function actionMiniFullInfo()
  453. {
  454. $post = Yii::$app->request->post();
  455. $iv = isset($post['iv']) ? $post['iv'] : '';
  456. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  457. $appId = $this->sj['miniAppId'];
  458. $sjId = $this->sjId;
  459. $miniOpenId = $this->user['miniOpenId'];
  460. if (empty($miniOpenId)) {
  461. util::fail('mini_open_id empty');
  462. }
  463. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  464. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  465. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  466. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  467. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  468. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  469. if ($errCode != 0) {
  470. Yii::info($result . ' ' . $errCode);
  471. util::fail('获取用户信息失败');
  472. }
  473. Yii::info('小程序获取用户信息:' . $result);
  474. $originalInfo = Json::decode($result);
  475. $source = UserClass::$userSourceId['mini']['name'];
  476. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  477. $userId = $user['id'];
  478. $userInfo = UserService::getUserInfo($userId);
  479. util::success($userInfo);
  480. }
  481. //小程序用户手机号
  482. public function actionMiniMobile()
  483. {
  484. $post = Yii::$app->request->post();
  485. $iv = $post['iv'];
  486. $encryptedData = $post['encryptedData'];
  487. $sjId = $this->sjId;
  488. $merchant = $this->sj->attributes;
  489. $appId = $merchant['miniAppId'];
  490. $miniOpenId = $this->user['miniOpenId'];
  491. $user = $this->user;
  492. if (!empty($user) && !empty($user['mobile'])) {
  493. $userId = $user['id'];
  494. util::success(['mobile' => $user['mobile']]);
  495. }
  496. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  497. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  498. if (empty($sessionKey)) {
  499. util::fail('系统错误,sesstion empty');
  500. }
  501. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  502. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  503. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  504. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  505. if ($errCode != 0) {
  506. util::fail('解密失败');
  507. }
  508. $arr = Json::decode($result);
  509. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  510. if (empty($mobile)) {
  511. util::fail('没有获取手机号,请重试');
  512. }
  513. $userId = $user['id'];
  514. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  515. util::success(['mobile' => $mobile]);
  516. }
  517. //密码修改
  518. public function actionPassword()
  519. {
  520. $post = Yii::$app->request->post();
  521. $old = isset($post['old']) ? $post['old'] : '';
  522. $new = isset($post['new']) ? $post['new'] : '';
  523. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  524. if (empty($new)) {
  525. util::fail('请输入新密码');
  526. }
  527. if ($new != $confirm) {
  528. util::fail('二次密码不一致');
  529. }
  530. $userId = $this->adminId;
  531. $user = UserService::getUserInfo($userId);
  532. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  533. if (empty($old)) {
  534. util::fail('请填写旧密码');
  535. }
  536. if (password_verify($old, $user['payPassword']) == false) {
  537. util::fail('旧密码错误');
  538. }
  539. }
  540. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  541. util::complete('修改成功');
  542. }
  543. //申请会员 ssh 2019.12.18
  544. public function actionApplyMember()
  545. {
  546. $post = Yii::$app->request->post();
  547. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  548. if (stringUtil::isMobile($mobile) == false) {
  549. util::fail('请输入手机号');
  550. }
  551. $userId = $this->adminId;
  552. $asset = UserAssetService::getByUserId($userId);
  553. if (isset($asset['member']) && $asset['member'] != -1) {
  554. util::fail('已经是会员了');
  555. }
  556. $code = isset($post['code']) ? $post['code'] : '';
  557. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  558. if (empty($cacheCode) || $cacheCode != $code) {
  559. util::fail('验证码错误');
  560. }
  561. $update = [];
  562. if (isset($post['realName']) && !empty($post['realName'])) {
  563. $update['realName'] = $post['realName'];
  564. }
  565. if (isset($post['birthday']) && !empty($post['birthday'])) {
  566. $update['birthday'] = $post['birthday'];
  567. }
  568. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  569. if ($userId == $user['id']) {
  570. util::fail('手机号已验证');
  571. }
  572. if (!empty($user)) {
  573. util::fail('手机号已经注册过了');
  574. }
  575. $update['mobile'] = $mobile;
  576. UserService::becomeMember($this->user, $this->sj->attributes, $update);
  577. util::complete('注册成功');
  578. }
  579. //用户基本信息和资产 ssh 2019.12.19
  580. public function actionMyProfile()
  581. {
  582. $custom = $this->custom;
  583. $user = UserService::getUserInfo($this->adminId);
  584. $growth = $custom['growth'] ?? 0;
  585. $extend = $this->sjExtend->attributes;
  586. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  587. //下个等级的积分
  588. $nextLevelGrowth = 0;
  589. foreach ($grade as $val) {
  590. $currentGrowth = $val['growth'];
  591. if ($currentGrowth > $growth) {
  592. $nextLevelGrowth = $currentGrowth;
  593. break;
  594. }
  595. }
  596. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  597. util::success($user);
  598. }
  599. //登陆客户的信息 ssh 2020.3.12
  600. public function actionLoginDetail()
  601. {
  602. $user = $this->user;
  603. util::success($user);
  604. }
  605. //总挂账和总余额汇总 ssh 20250620
  606. public function actionShowTotalBalance()
  607. {
  608. $lookMoney = \bizGhs\shop\classes\ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  609. if ($lookMoney == 0) {
  610. util::fail('无法查看哦');
  611. }
  612. $lsShop = $this->shop;
  613. $pfShopId = $lsShop->pfShopId;
  614. $lsShopId = $this->shopId;
  615. $respond = \bizGhs\custom\classes\CustomClass::showTotalBalance($pfShopId, $lsShopId);
  616. $respond['pfShopId'] = $pfShopId;
  617. $respond['lsShopId'] = $lsShopId;
  618. util::success($respond);
  619. }
  620. //今日访客
  621. public function actionGetVisit()
  622. {
  623. $userIds = StatVisitClass::getVisitCustomIds($this->shopId);
  624. if (empty($userIds)) {
  625. util::success(['list' => []]);
  626. }
  627. $sort = 'visitTime DESC';
  628. $where = [
  629. 'shopId' => $this->shopId,
  630. 'userId' => ['in', $userIds]
  631. ];
  632. $list = CustomService::getCustomList($where, $sort);
  633. util::success($list);
  634. }
  635. //删除客户 ssh 20250923
  636. public function actionDelCustom()
  637. {
  638. $get = Yii::$app->request->get();
  639. $id = $get['id'] ?? 0;
  640. $custom = CustomClass::getById($id, true);
  641. if (empty($custom)) {
  642. util::fail('没有找到客户');
  643. }
  644. if ($custom->shopId != $this->shopId) {
  645. util::fail('不是你的客户');
  646. }
  647. $hdId = $custom->hdId ?? 0;
  648. $hd = HdClass::getById($hdId, true);
  649. if (empty($hd)) {
  650. util::fail('客户信息缺失');
  651. }
  652. $custom->delStatus = 1;
  653. $custom->save();
  654. $hd->delStatus = 1;
  655. $hd->save();
  656. util::complete('删除成功');
  657. }
  658. //重新统计客户的消费总额
  659. public function actionUpdateBuyAmount()
  660. {
  661. $post = Yii::$app->request->post();
  662. $customId = $post['customId'];
  663. $c = CustomClass::getLockById($customId);
  664. // 1.重新统计客户的消费总额
  665. $orderList = OrderClass::getAllByCondition(['customId' => $customId], null, 'id, forward, actPrice');
  666. $buyAmount= 0;
  667. if (!empty($orderList)) {
  668. foreach ($orderList as $order) {
  669. if ($order['forward'] == 0) { //0 常规订单
  670. $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
  671. } elseif ($order['forward'] == 1) { //1 售后付款单(红冲)
  672. $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
  673. } else {
  674. Yii::info('订单类型不存在');
  675. continue;
  676. }
  677. }
  678. }
  679. $c->buyAmount = $buyAmount;
  680. $c->save();
  681. // 2.更新此客户所对应花店的消费金额 expendAmount ---- xhHd -> expendAmount
  682. HdClass::updateById($c->hdId, ['expendAmount'=>$buyAmount]);
  683. util::success(['customId'=>$customId, 'buyAmount'=>$buyAmount]);
  684. }
  685. // 把所有客户的累计消费和成长值、积分都刷新一下
  686. public function actionRefreshCustomData()
  687. {
  688. // 1. 查询 xhShop 表,获取所有 ptStyle = 1 的门店数据
  689. $allShops = ShopClass::getAllByCondition(['ptStyle'=>1, 'status'=>1], 'id ASC', 'id, mainId');
  690. // 2. 把每个门店下的所有用户进行遍历,刷新他们的累计消费和成长值、积分
  691. if (!empty($allShops)) {
  692. foreach ($allShops as $shop) {
  693. $shopId = $shop['id'];
  694. $mainId = $shop['mainId'];
  695. $customs = CustomClass::getAllByCondition(['shopId' => $shopId], null, 'id, userId, buyAmount, growth, member, memberName', null, true);
  696. $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
  697. if (!empty($customs) && !empty($levelDatas)) {
  698. foreach ($customs as $c) {
  699. $buyAmount = 0;
  700. // 获取所有零售订单,计算消费金额
  701. $orderList = OrderClass::getAllByCondition(['customId' => $c->id], null, 'id, forward, actPrice');
  702. if (!empty($orderList)) {
  703. foreach ($orderList as $order) {
  704. if ($order['forward'] == 0) { //0 常规订单
  705. $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
  706. } elseif ($order['forward'] == 1) { //1 售后付款单(红冲)
  707. $buyAmount = bcsub($buyAmount, $order['actPrice'], 2);
  708. } else {
  709. Yii::info('订单类型不存在');
  710. continue;
  711. }
  712. }
  713. }
  714. $c->buyAmount = $buyAmount;
  715. $c->growth = intval($buyAmount);
  716. $c->integral = intval($buyAmount);
  717. // 根据 growth 设置等级
  718. $memberData = CustomClass::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
  719. $c->member = $memberData['level'];
  720. $c->memberName = $memberData['name'];
  721. $c->save();
  722. // 更新客户所对应花店的消费金额
  723. HdClass::updateByCondition(['shopId'=>$shopId, 'userId'=>$c->userId], ['expendAmount'=>$buyAmount]);
  724. }
  725. }
  726. }
  727. }
  728. util::complete();
  729. }
  730. }