CustomController.php 18 KB

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