CustomController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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('客户信息缺失');
  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 2019.11.30
  181. public function actionDetail()
  182. {
  183. $userId = Yii::$app->request->get('userId');
  184. $info = CustomClass::getCustomInfo($userId);
  185. CustomClass::valid($info, $this->shopId);
  186. util::success($info);
  187. }
  188. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  189. public function actionUserDetail()
  190. {
  191. $userId = Yii::$app->request->get('userId');
  192. $info = UserService::getFullUserInfo($userId);
  193. UserService::valid($info, $this->sjId);
  194. $list = MerchantRemarkService::getRemarkList($userId);
  195. $info['remarkList'] = $list;
  196. util::success($info);
  197. }
  198. //添加客户,申请会员时会自动同步资产 ssh 2019.11.30
  199. public function actionAdd()
  200. {
  201. $post = Yii::$app->request->post();
  202. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  203. util::fail('请输入手机号');
  204. }
  205. if ($post['mobile'] != $post['confirmMobile']) {
  206. util::fail('二次输入手机号不一样');
  207. }
  208. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  209. AdminService::verifyPayPassword($this->adminId, $payPassword);
  210. $mobile = $post['mobile'];
  211. $user = UserService::getByMobile($mobile, $this->sjId);
  212. if (!empty($user)) {
  213. util::fail('手机号已使用');
  214. }
  215. $post['sjId'] = $this->sjId;
  216. $post['member'] = $post['memberLevel'];
  217. UserService::addUser($post);
  218. util::complete();
  219. }
  220. //充值 ssh 2019.12.1
  221. public function actionRecharge()
  222. {
  223. $post = Yii::$app->request->post();
  224. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  225. AdminService::verifyPayPassword($this->adminId, $payPassword);
  226. $userId = isset($post['userId']) ? $post['userId'] : 0;
  227. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  228. $user = UserService::getById($userId);
  229. if (empty($user)) {
  230. util::fail('找不到客户');
  231. }
  232. if ($user['sjId'] != $this->sjId) {
  233. util::fail('您无法操作');
  234. }
  235. $userAsset = UserAssetService::getByUserId($userId);
  236. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  237. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  238. $upData['balance'] = $totalBalance;
  239. $upData['totalRecharge'] = $totalRecharge;
  240. $merchantAsset = MerchantAssetService::getBySjId($this->sjId);
  241. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj->attributes, $this->sjExtend->attributes, $merchantAsset);
  242. if ($return['code'] == 'A0001') {
  243. util::complete();
  244. }
  245. util::fail();
  246. }
  247. //升级 ssh 2019.12.1
  248. public function actionUpgrade()
  249. {
  250. $post = Yii::$app->request->post();
  251. $userId = isset($post['userId']) ? $post['userId'] : 0;
  252. $member = isset($post['member']) ? $post['member'] : 0;
  253. $user = UserService::getById($userId);
  254. if (empty($user)) {
  255. util::fail('客户不存在');
  256. }
  257. //验证是否商家客户
  258. UserService::valid($user, $this->sjId);
  259. //验证确认密码
  260. $payPassword = $post['payPassword'];
  261. AdminService::verifyPayPassword($this->adminId, $payPassword);
  262. $grade = xhMerchantExtendService::getGradeList($this->sjExtend->attributes, 'desc');
  263. //升级后的成长值
  264. $newLevelGrowth = $grade[$member]['growth'];
  265. $newDiscount = $grade[$member]['discount'];
  266. $userAsset = UserAssetService::getByUserId($userId);
  267. if ($userAsset['member'] >= $member) {
  268. util::fail('客户已经达到此级别了');
  269. }
  270. $now = time();
  271. $date = date("Y-m-d H:i", $now);
  272. $connection = Yii::$app->db;
  273. $transaction = $connection->beginTransaction();
  274. try {
  275. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  276. $upData['growth'] = $newLevelGrowth;
  277. $upData['member'] = $member;
  278. $upData['discount'] = $newDiscount;
  279. UserAssetService::updateByUserId($userId, $upData);
  280. UserService::updateById($userId, ['member' => $member]);
  281. $upgradeData = [
  282. 'member' => $member,
  283. 'prevMember' => $userAsset['member'],
  284. 'sjId' => $this->sjId,
  285. 'gainType' => 0,
  286. 'addTime' => $now,
  287. 'userId' => $userId,
  288. ];
  289. $upgradeRespond = UserUpgradeService::add($upgradeData);
  290. $growthData = [
  291. 'userId' => $userId,
  292. 'userName' => $user['userName'],
  293. 'sjId' => $this->sjId,
  294. 'relateId' => $upgradeRespond['id'],
  295. 'growth' => $newLevelGrowth,
  296. 'num' => $addGrowth,
  297. 'io' => 1,
  298. 'payWay' => 4,
  299. 'alipayId' => '',
  300. 'event' => '管理员操作升到' . $member . '级增加成长值',
  301. 'operatorId' => $userId,
  302. 'createTime' => $date,
  303. 'gainType' => 0,
  304. 'addTime' => $now,
  305. ];
  306. UserGrowthService::add($growthData);
  307. $transaction->commit();
  308. } catch (\Exception $e) {
  309. $transaction->rollBack();
  310. util::fail();
  311. }
  312. $allTM = xhTMessageService::getList($this->sjId);
  313. $openId = isset($user['openId']) ? $user['openId'] : '';
  314. if (empty($openId)) {
  315. util::complete();
  316. }
  317. $shortTempId = 'OPENTM205211943';//升级通知
  318. if (isset($allTM[$shortTempId]) == false) {
  319. util::complete();
  320. }
  321. $tempId = $allTM[$shortTempId];
  322. $data = [
  323. "touser" => $openId, "template_id" => $tempId, "url" => '',
  324. "data" => [
  325. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  326. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  327. "keyword2" => ["value" => $date, "color" => "#173177"],
  328. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  329. ]];
  330. wxUtil::sendTaskInform($data, $this->sj);
  331. util::complete();
  332. }
  333. //重置客户密码
  334. public function actionResetPayPassword()
  335. {
  336. $post = Yii::$app->request->post();
  337. $userId = isset($post['userId']) ? $post['userId'] : 0;
  338. $password = isset($post['password']) ? $post['password'] : 0;
  339. $info = UserService::getById($userId);
  340. UserService::valid($info, $this->sjId);
  341. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  342. util::complete();
  343. }
  344. //获取登陆客户的资产 ssh 2019.11.22
  345. public function actionAsset()
  346. {
  347. $asset = UserAssetService::getByUserId($this->adminId);
  348. util::success($asset);
  349. }
  350. //获取登陆客户的优惠情况 ssh 2019.12.3
  351. public function actionDiscount()
  352. {
  353. $discount = UserAssetService::getDiscount($this->adminId);
  354. util::success($discount);
  355. }
  356. //小程序用户完整信息 ssh 2019.12.12
  357. public function actionMiniFullInfo()
  358. {
  359. $post = Yii::$app->request->post();
  360. $iv = isset($post['iv']) ? $post['iv'] : '';
  361. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  362. $appId = $this->sj['miniAppId'];
  363. $sjId = $this->sjId;
  364. $miniOpenId = $this->user['miniOpenId'];
  365. if (empty($miniOpenId)) {
  366. util::fail('mini_open_id empty');
  367. }
  368. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  369. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  370. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  371. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  372. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  373. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  374. if ($errCode != 0) {
  375. Yii::info($result . ' ' . $errCode);
  376. util::fail('获取用户信息失败');
  377. }
  378. Yii::info('小程序获取用户信息:' . $result);
  379. $originalInfo = Json::decode($result);
  380. $source = UserClass::$userSourceId['mini']['name'];
  381. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  382. $userId = $user['id'];
  383. $userInfo = UserService::getUserInfo($userId);
  384. util::success($userInfo);
  385. }
  386. //小程序用户手机号
  387. public function actionMiniMobile()
  388. {
  389. $post = Yii::$app->request->post();
  390. $iv = $post['iv'];
  391. $encryptedData = $post['encryptedData'];
  392. $sjId = $this->sjId;
  393. $merchant = $this->sj->attributes;
  394. $appId = $merchant['miniAppId'];
  395. $miniOpenId = $this->user['miniOpenId'];
  396. $user = $this->user;
  397. if (!empty($user) && !empty($user['mobile'])) {
  398. $userId = $user['id'];
  399. util::success(['mobile' => $user['mobile']]);
  400. }
  401. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  402. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  403. if (empty($sessionKey)) {
  404. util::fail('系统错误,sesstion empty');
  405. }
  406. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  407. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  408. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  409. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  410. if ($errCode != 0) {
  411. util::fail('解密失败');
  412. }
  413. $arr = Json::decode($result);
  414. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  415. if (empty($mobile)) {
  416. util::fail('没有获取手机号,请重试');
  417. }
  418. $userId = $user['id'];
  419. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  420. util::success(['mobile' => $mobile]);
  421. }
  422. //密码修改
  423. public function actionPassword()
  424. {
  425. $post = Yii::$app->request->post();
  426. $old = isset($post['old']) ? $post['old'] : '';
  427. $new = isset($post['new']) ? $post['new'] : '';
  428. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  429. if (empty($new)) {
  430. util::fail('请输入新密码');
  431. }
  432. if ($new != $confirm) {
  433. util::fail('二次密码不一致');
  434. }
  435. $userId = $this->adminId;
  436. $user = UserService::getUserInfo($userId);
  437. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  438. if (empty($old)) {
  439. util::fail('请填写旧密码');
  440. }
  441. if (password_verify($old, $user['payPassword']) == false) {
  442. util::fail('旧密码错误');
  443. }
  444. }
  445. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  446. util::complete('修改成功');
  447. }
  448. //申请会员 ssh 2019.12.18
  449. public function actionApplyMember()
  450. {
  451. $post = Yii::$app->request->post();
  452. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  453. if (stringUtil::isMobile($mobile) == false) {
  454. util::fail('请输入手机号');
  455. }
  456. $userId = $this->adminId;
  457. $asset = UserAssetService::getByUserId($userId);
  458. if (isset($asset['member']) && $asset['member'] != -1) {
  459. util::fail('已经是会员了');
  460. }
  461. $code = isset($post['code']) ? $post['code'] : '';
  462. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  463. if (empty($cacheCode) || $cacheCode != $code) {
  464. util::fail('验证码错误');
  465. }
  466. $update = [];
  467. if (isset($post['realName']) && !empty($post['realName'])) {
  468. $update['realName'] = $post['realName'];
  469. }
  470. if (isset($post['birthday']) && !empty($post['birthday'])) {
  471. $update['birthday'] = $post['birthday'];
  472. }
  473. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  474. if ($userId == $user['id']) {
  475. util::fail('手机号已验证');
  476. }
  477. if (!empty($user)) {
  478. util::fail('手机号已经注册过了');
  479. }
  480. $update['mobile'] = $mobile;
  481. UserService::becomeMember($this->user, $this->sj->attributes, $update);
  482. util::complete('注册成功');
  483. }
  484. //用户基本信息和资产 ssh 2019.12.19
  485. public function actionMyProfile()
  486. {
  487. $custom = $this->custom;
  488. $user = UserService::getUserInfo($this->adminId);
  489. $growth = $custom['growth'] ?? 0;
  490. $extend = $this->sjExtend->attributes;
  491. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  492. //下个等级的积分
  493. $nextLevelGrowth = 0;
  494. foreach ($grade as $val) {
  495. $currentGrowth = $val['growth'];
  496. if ($currentGrowth > $growth) {
  497. $nextLevelGrowth = $currentGrowth;
  498. break;
  499. }
  500. }
  501. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  502. util::success($user);
  503. }
  504. //登陆客户的信息 ssh 2020.3.12
  505. public function actionLoginDetail()
  506. {
  507. $user = $this->user;
  508. util::success($user);
  509. }
  510. }