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