CustomController.php 17 KB

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