CustomController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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\MemberWealClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\user\services\UserAssetService;
  12. use bizHd\user\services\UserGrowthService;
  13. use bizHd\user\services\UserService;
  14. use bizHd\user\services\UserUpgradeService;
  15. use common\components\wxUtil;
  16. use common\services\xhMerchantExtendService;
  17. use common\services\xhRechargeService;
  18. use common\services\xhTMessageService;
  19. use common\services\xhUserIntegralService;
  20. use Yii;
  21. use common\components\stringUtil;
  22. use common\components\util;
  23. use biz\sj\services\MerchantExtendService;
  24. use bizHd\message\services\SmsService;
  25. use bizHd\user\classes\UserClass;
  26. use yii\helpers\Json;
  27. class CustomController extends BaseController
  28. {
  29. public $guestAccess = ['list'];
  30. public function actionChangeLevel()
  31. {
  32. $get = Yii::$app->request->get();
  33. $customId = $get['customId'];
  34. $level = $get['level'] ?? 0;
  35. if ($level < 0) {
  36. util::fail('等级选择错误');
  37. }
  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. $hdId = $custom->hdId;
  47. $hd = HdClass::getById($hdId, true);
  48. if (empty($hd)) {
  49. util::fail('客户信息缺失,编号3445');
  50. }
  51. $staff = $this->shopAdmin;
  52. $staffId = $staff->id;
  53. $staffName = $staff->name;
  54. $params = [
  55. 'style' => 1,//手动修改
  56. 'staffId' => $staffId,
  57. 'staffName' => $staffName,
  58. ];
  59. $shop = $this->shop;
  60. $connection = Yii::$app->db;
  61. $transaction = $connection->beginTransaction();
  62. try {
  63. $beforeLevel = $custom->member ?? 0;
  64. CustomClass::levelChange($beforeLevel, $level, $shop, $custom, $hd, $params);
  65. $transaction->commit();
  66. util::complete('修改成功');
  67. } catch (\Exception $exception) {
  68. $transaction->rollBack();
  69. Yii::info("修改失败:" . $exception->getMessage());
  70. util::fail('修改失败');
  71. }
  72. }
  73. //修改客户地址 ssh 20231109
  74. public function actionModifyAddress()
  75. {
  76. $post = Yii::$app->request->post();
  77. $id = $post['id'] ?? 0;
  78. unset($post['id']);
  79. $custom = CustomClass::getById($id, true);
  80. if (empty($custom)) {
  81. util::fail('没有找到客户信息');
  82. }
  83. $userId = $custom->userId ?? 0;
  84. $city = $post['city'] ?? '';
  85. $dist = $post['dist'] ?? '';
  86. $address = $post['address'] ?? '';
  87. $floor = $post['floor'] ?? '';
  88. $post['fullAddress'] = $city . $dist . $address . $floor;
  89. UserClass::updateByCondition(['id' => $userId], $post);
  90. CustomClass::updateByCondition(['id' => $id], $post);
  91. util::complete('修改成功');
  92. }
  93. //修改名称 ssh 2022096
  94. public function actionChangeName()
  95. {
  96. $get = Yii::$app->request->get();
  97. $name = $get['name'] ?? '';
  98. $id = $get['id'] ?? 0;
  99. if (empty($name)) {
  100. util::fail('请填写名称');
  101. }
  102. $py = stringUtil::py($name);
  103. $custom = CustomClass::getById($id, true);
  104. CustomClass::valid($custom, $this->shopId);
  105. $custom->name = $name;
  106. $custom->py = $py;
  107. $custom->save();
  108. OrderClass::updateByCondition(['customId' => $id], ['customName' => $name, 'customNamePy' => $py]);
  109. util::complete();
  110. }
  111. //批量添加客户 ssh 20220919
  112. public function actionBatchAddCustom()
  113. {
  114. $post = Yii::$app->request->post();
  115. $connection = Yii::$app->db;
  116. $transaction = $connection->beginTransaction();
  117. try {
  118. for ($i = 1; $i <= 5; $i++) {
  119. $name = $post['name' . $i] ?? '';
  120. $mobile = $post['mobile' . $i] ?? '';
  121. $confirmMobile = $post['confirmMobile' . $i] ?? '';
  122. if (empty($name) && empty($mobile) && empty($confirmMobile)) {
  123. continue;
  124. }
  125. if (empty($name)) {
  126. util::fail('客户名称不能为空');
  127. }
  128. if (stringUtil::getWordNum($name) > 15) {
  129. util::fail('名称不能超过15个汉字');
  130. }
  131. if (empty($mobile)) {
  132. util::fail($name . " 手机号不能为空");
  133. }
  134. if (stringUtil::isMobile($mobile) == false) {
  135. util::fail($name . " 手机号格式不正确");
  136. }
  137. if ($mobile != $confirmMobile) {
  138. util::fail($name . " 二次手机号不一致");
  139. }
  140. $user = \bizMall\user\classes\UserClass::replaceUser(['name' => $name, 'mobile' => $mobile]);
  141. if (isset($this->shop) && !empty($this->shop)) {
  142. //花店增加客户,散客增加门店
  143. CustomClass::buildRelation($this->shop, $user);
  144. }
  145. }
  146. $transaction->commit();
  147. util::complete();
  148. } catch (\Exception $e) {
  149. $transaction->rollBack();
  150. util::fail('添加失败');
  151. }
  152. }
  153. //客户列表 ssh 2019.11.30
  154. public function actionList()
  155. {
  156. $get = Yii::$app->request->get();
  157. $type = isset($get['type']) ? $get['type'] : 0;
  158. $name = isset($get['name']) ? $get['name'] : '';
  159. $where = ['shopId' => $this->shopId];
  160. if ($type == 1) {
  161. //消费排行
  162. $sort = 'buyAmount DESC';
  163. } else if ($type == 2) {
  164. //欠款客户
  165. $where['isDebt'] = 1;
  166. $sort = 'debtAmount DESC';
  167. } else {
  168. $sort = 'visitTime DESC';
  169. }
  170. if (!empty($name)) {
  171. if (stringUtil::isLetter($name)) {
  172. $where['py'] = ['like', $name];
  173. } else {
  174. $where['name'] = ['like', $name];
  175. }
  176. }
  177. $list = CustomService::getCustomList($where, $sort);
  178. util::success($list);
  179. }
  180. //获取客户信息 ssh 20250510
  181. public function actionInfo()
  182. {
  183. $get = Yii::$app->request->get();
  184. $id = $get['id'] ?? 0;
  185. $custom = CustomClass::getById($id, true);
  186. if (empty($custom)) {
  187. util::fail('没有找到客户信息');
  188. }
  189. if ($custom->shopId != $this->shopId) {
  190. util::fail('不是你的客户');
  191. }
  192. util::success(['custom' => $custom]);
  193. }
  194. //获取客户基本和资产信息 ssh 2019.11.30
  195. public function actionDetail()
  196. {
  197. $userId = Yii::$app->request->get('userId');
  198. $info = CustomClass::getCustomInfo($userId);
  199. CustomClass::valid($info, $this->shopId);
  200. util::success($info);
  201. }
  202. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  203. public function actionUserDetail()
  204. {
  205. $userId = Yii::$app->request->get('userId');
  206. $info = UserService::getFullUserInfo($userId);
  207. UserService::valid($info, $this->sjId);
  208. $list = MerchantRemarkService::getRemarkList($userId);
  209. $info['remarkList'] = $list;
  210. util::success($info);
  211. }
  212. //添加客户,申请会员时会自动同步资产 ssh 2019.11.30
  213. public function actionAdd()
  214. {
  215. $post = Yii::$app->request->post();
  216. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  217. util::fail('请输入手机号');
  218. }
  219. if ($post['mobile'] != $post['confirmMobile']) {
  220. util::fail('二次输入手机号不一样');
  221. }
  222. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  223. AdminService::verifyPayPassword($this->adminId, $payPassword);
  224. $mobile = $post['mobile'];
  225. $user = UserService::getByMobile($mobile, $this->sjId);
  226. if (!empty($user)) {
  227. util::fail('手机号已使用');
  228. }
  229. $post['sjId'] = $this->sjId;
  230. $post['member'] = $post['memberLevel'];
  231. UserService::addUser($post);
  232. util::complete();
  233. }
  234. //充值 ssh 2019.12.1
  235. public function actionRecharge()
  236. {
  237. $post = Yii::$app->request->post();
  238. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  239. AdminService::verifyPayPassword($this->adminId, $payPassword);
  240. $userId = isset($post['userId']) ? $post['userId'] : 0;
  241. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  242. $user = UserService::getById($userId);
  243. if (empty($user)) {
  244. util::fail('找不到客户');
  245. }
  246. if ($user['sjId'] != $this->sjId) {
  247. util::fail('您无法操作');
  248. }
  249. $userAsset = UserAssetService::getByUserId($userId);
  250. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  251. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  252. $upData['balance'] = $totalBalance;
  253. $upData['totalRecharge'] = $totalRecharge;
  254. $merchantAsset = MerchantAssetService::getBySjId($this->sjId);
  255. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj->attributes, $this->sjExtend->attributes, $merchantAsset);
  256. if ($return['code'] == 'A0001') {
  257. util::complete();
  258. }
  259. util::fail();
  260. }
  261. //升级 ssh 2019.12.1
  262. public function actionUpgrade()
  263. {
  264. $post = Yii::$app->request->post();
  265. $userId = isset($post['userId']) ? $post['userId'] : 0;
  266. $member = isset($post['member']) ? $post['member'] : 0;
  267. $user = UserService::getById($userId);
  268. if (empty($user)) {
  269. util::fail('客户不存在');
  270. }
  271. //验证是否商家客户
  272. UserService::valid($user, $this->sjId);
  273. //验证确认密码
  274. $payPassword = $post['payPassword'];
  275. AdminService::verifyPayPassword($this->adminId, $payPassword);
  276. $grade = xhMerchantExtendService::getGradeList($this->sjExtend->attributes, 'desc');
  277. //升级后的成长值
  278. $newLevelGrowth = $grade[$member]['growth'];
  279. $newDiscount = $grade[$member]['discount'];
  280. $userAsset = UserAssetService::getByUserId($userId);
  281. if ($userAsset['member'] >= $member) {
  282. util::fail('客户已经达到此级别了');
  283. }
  284. $now = time();
  285. $date = date("Y-m-d H:i", $now);
  286. $connection = Yii::$app->db;
  287. $transaction = $connection->beginTransaction();
  288. try {
  289. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  290. $upData['growth'] = $newLevelGrowth;
  291. $upData['member'] = $member;
  292. $upData['discount'] = $newDiscount;
  293. UserAssetService::updateByUserId($userId, $upData);
  294. UserService::updateById($userId, ['member' => $member]);
  295. $upgradeData = [
  296. 'member' => $member,
  297. 'prevMember' => $userAsset['member'],
  298. 'sjId' => $this->sjId,
  299. 'gainType' => 0,
  300. 'addTime' => $now,
  301. 'userId' => $userId,
  302. ];
  303. $upgradeRespond = UserUpgradeService::add($upgradeData);
  304. $growthData = [
  305. 'userId' => $userId,
  306. 'userName' => $user['userName'],
  307. 'sjId' => $this->sjId,
  308. 'relateId' => $upgradeRespond['id'],
  309. 'growth' => $newLevelGrowth,
  310. 'num' => $addGrowth,
  311. 'io' => 1,
  312. 'payWay' => 4,
  313. 'alipayId' => '',
  314. 'event' => '管理员操作升到' . $member . '级增加成长值',
  315. 'operatorId' => $userId,
  316. 'createTime' => $date,
  317. 'gainType' => 0,
  318. 'addTime' => $now,
  319. ];
  320. UserGrowthService::add($growthData);
  321. $transaction->commit();
  322. } catch (\Exception $e) {
  323. $transaction->rollBack();
  324. util::fail();
  325. }
  326. $allTM = xhTMessageService::getList($this->sjId);
  327. $openId = isset($user['openId']) ? $user['openId'] : '';
  328. if (empty($openId)) {
  329. util::complete();
  330. }
  331. $shortTempId = 'OPENTM205211943';//升级通知
  332. if (isset($allTM[$shortTempId]) == false) {
  333. util::complete();
  334. }
  335. $tempId = $allTM[$shortTempId];
  336. $data = [
  337. "touser" => $openId, "template_id" => $tempId, "url" => '',
  338. "data" => [
  339. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  340. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  341. "keyword2" => ["value" => $date, "color" => "#173177"],
  342. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  343. ]];
  344. wxUtil::sendTaskInform($data, $this->sj);
  345. util::complete();
  346. }
  347. //重置客户密码
  348. public function actionResetPayPassword()
  349. {
  350. $post = Yii::$app->request->post();
  351. $userId = isset($post['userId']) ? $post['userId'] : 0;
  352. $password = isset($post['password']) ? $post['password'] : 0;
  353. $info = UserService::getById($userId);
  354. UserService::valid($info, $this->sjId);
  355. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  356. util::complete();
  357. }
  358. //获取登陆客户的资产 ssh 2019.11.22
  359. public function actionAsset()
  360. {
  361. $asset = UserAssetService::getByUserId($this->adminId);
  362. util::success($asset);
  363. }
  364. //获取登陆客户的优惠情况 ssh 2019.12.3
  365. public function actionDiscount()
  366. {
  367. $discount = UserAssetService::getDiscount($this->adminId);
  368. util::success($discount);
  369. }
  370. //小程序用户完整信息 ssh 2019.12.12
  371. public function actionMiniFullInfo()
  372. {
  373. $post = Yii::$app->request->post();
  374. $iv = isset($post['iv']) ? $post['iv'] : '';
  375. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  376. $appId = $this->sj['miniAppId'];
  377. $sjId = $this->sjId;
  378. $miniOpenId = $this->user['miniOpenId'];
  379. if (empty($miniOpenId)) {
  380. util::fail('mini_open_id empty');
  381. }
  382. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  383. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  384. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  385. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  386. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  387. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  388. if ($errCode != 0) {
  389. Yii::info($result . ' ' . $errCode);
  390. util::fail('获取用户信息失败');
  391. }
  392. Yii::info('小程序获取用户信息:' . $result);
  393. $originalInfo = Json::decode($result);
  394. $source = UserClass::$userSourceId['mini']['name'];
  395. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  396. $userId = $user['id'];
  397. $userInfo = UserService::getUserInfo($userId);
  398. util::success($userInfo);
  399. }
  400. //小程序用户手机号
  401. public function actionMiniMobile()
  402. {
  403. $post = Yii::$app->request->post();
  404. $iv = $post['iv'];
  405. $encryptedData = $post['encryptedData'];
  406. $sjId = $this->sjId;
  407. $merchant = $this->sj->attributes;
  408. $appId = $merchant['miniAppId'];
  409. $miniOpenId = $this->user['miniOpenId'];
  410. $user = $this->user;
  411. if (!empty($user) && !empty($user['mobile'])) {
  412. $userId = $user['id'];
  413. util::success(['mobile' => $user['mobile']]);
  414. }
  415. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  416. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  417. if (empty($sessionKey)) {
  418. util::fail('系统错误,sesstion empty');
  419. }
  420. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  421. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  422. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  423. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  424. if ($errCode != 0) {
  425. util::fail('解密失败');
  426. }
  427. $arr = Json::decode($result);
  428. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  429. if (empty($mobile)) {
  430. util::fail('没有获取手机号,请重试');
  431. }
  432. $userId = $user['id'];
  433. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  434. util::success(['mobile' => $mobile]);
  435. }
  436. //密码修改
  437. public function actionPassword()
  438. {
  439. $post = Yii::$app->request->post();
  440. $old = isset($post['old']) ? $post['old'] : '';
  441. $new = isset($post['new']) ? $post['new'] : '';
  442. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  443. if (empty($new)) {
  444. util::fail('请输入新密码');
  445. }
  446. if ($new != $confirm) {
  447. util::fail('二次密码不一致');
  448. }
  449. $userId = $this->adminId;
  450. $user = UserService::getUserInfo($userId);
  451. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  452. if (empty($old)) {
  453. util::fail('请填写旧密码');
  454. }
  455. if (password_verify($old, $user['payPassword']) == false) {
  456. util::fail('旧密码错误');
  457. }
  458. }
  459. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  460. util::complete('修改成功');
  461. }
  462. //申请会员 ssh 2019.12.18
  463. public function actionApplyMember()
  464. {
  465. $post = Yii::$app->request->post();
  466. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  467. if (stringUtil::isMobile($mobile) == false) {
  468. util::fail('请输入手机号');
  469. }
  470. $userId = $this->adminId;
  471. $asset = UserAssetService::getByUserId($userId);
  472. if (isset($asset['member']) && $asset['member'] != -1) {
  473. util::fail('已经是会员了');
  474. }
  475. $code = isset($post['code']) ? $post['code'] : '';
  476. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  477. if (empty($cacheCode) || $cacheCode != $code) {
  478. util::fail('验证码错误');
  479. }
  480. $update = [];
  481. if (isset($post['realName']) && !empty($post['realName'])) {
  482. $update['realName'] = $post['realName'];
  483. }
  484. if (isset($post['birthday']) && !empty($post['birthday'])) {
  485. $update['birthday'] = $post['birthday'];
  486. }
  487. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  488. if ($userId == $user['id']) {
  489. util::fail('手机号已验证');
  490. }
  491. if (!empty($user)) {
  492. util::fail('手机号已经注册过了');
  493. }
  494. $update['mobile'] = $mobile;
  495. UserService::becomeMember($this->user, $this->sj->attributes, $update);
  496. util::complete('注册成功');
  497. }
  498. //用户基本信息和资产 ssh 2019.12.19
  499. public function actionMyProfile()
  500. {
  501. $custom = $this->custom;
  502. $user = UserService::getUserInfo($this->adminId);
  503. $growth = $custom['growth'] ?? 0;
  504. $extend = $this->sjExtend->attributes;
  505. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  506. //下个等级的积分
  507. $nextLevelGrowth = 0;
  508. foreach ($grade as $val) {
  509. $currentGrowth = $val['growth'];
  510. if ($currentGrowth > $growth) {
  511. $nextLevelGrowth = $currentGrowth;
  512. break;
  513. }
  514. }
  515. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  516. util::success($user);
  517. }
  518. //登陆客户的信息 ssh 2020.3.12
  519. public function actionLoginDetail()
  520. {
  521. $user = $this->user;
  522. util::success($user);
  523. }
  524. }