UserController.php 16 KB

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