CustomController.php 18 KB

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