CustomController.php 17 KB

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