CustomController.php 18 KB

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