UserController.php 16 KB

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