UserController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace client\controllers;
  3. use biz\MQ\services\SmsService;
  4. use biz\user\services\UserAssetService;
  5. use biz\user\services\UserService;
  6. use common\components\stringUtil;
  7. use Yii;
  8. use common\components\util;
  9. use yii\helpers\Json;
  10. class UserController extends BaseController
  11. {
  12. //获取登陆客户的资产 shish 2019.11.22
  13. public function actionAsset()
  14. {
  15. $asset = UserAssetService::getByUserId($this->userId);
  16. util::success($asset);
  17. }
  18. //获取登陆客户的优惠情况 shish 2019.12.3
  19. public function actionDiscount()
  20. {
  21. $userId = Yii::$app->params['userId'];
  22. $discount = UserAssetService::getDiscount($userId);
  23. util::success($discount);
  24. }
  25. //小程序用户完整信息 shish 2019.12.12
  26. public function actionMiniFullInfo()
  27. {
  28. $post = Yii::$app->request->post();
  29. $iv = isset($post['iv']) ? $post['iv'] : '';
  30. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  31. $appId = $this->merchant['miniAppId'];
  32. $merchantId = $this->merchantId;
  33. $miniOpenId = $this->user['miniOpenId'];
  34. if (empty($miniOpenId)) {
  35. util::fail('mini_open_id empty');
  36. }
  37. $cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
  38. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  39. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  40. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  41. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  42. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  43. if ($errCode != 0) {
  44. Yii::info($result . ' ' . $errCode);
  45. util::fail('获取用户信息失败');
  46. }
  47. Yii::info('小程序获取用户信息:' . $result);
  48. $originalInfo = Json::decode($result);
  49. $source = UserService::$userSourceId['mini']['name'];
  50. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  51. $userId = $user['id'];
  52. $userInfo = UserService::getUserInfo($userId);
  53. util::success($userInfo);
  54. }
  55. //小程序用户手机号
  56. public function actionMiniMobile()
  57. {
  58. $post = Yii::$app->request->post();
  59. $iv = $post['iv'];
  60. $encryptedData = $post['encryptedData'];
  61. $merchantId = $this->merchantId;
  62. $merchant = $this->merchant;
  63. $appId = $merchant['miniAppId'];
  64. $miniOpenId = $this->user['miniOpenId'];
  65. $user = $this->user;
  66. if (!empty($user) && !empty($user['mobile'])) {
  67. $userId = $user['id'];
  68. UserService::updateById($userId, ['isMember' => 1]);
  69. UserAssetService::updateByUserId($userId, ['isMember' => 1]);
  70. util::successInfo('操作成功', 'A0001', ['mobile' => $user['mobile']]);
  71. }
  72. $cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
  73. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  74. if (empty($sessionKey)) {
  75. util::fail('sesstion key empty');
  76. }
  77. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  78. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  79. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  80. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  81. if ($errCode != 0) {
  82. util::fail('解密失败');
  83. }
  84. $arr = Json::decode($result);
  85. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  86. $userId = $user['id'];
  87. /**升级成为会员**/
  88. UserService::upgradeToMember($userId, $merchantId, ['mobile' => $mobile]);
  89. util::success(['mobile' => $mobile]);
  90. }
  91. //密码修改
  92. public function actionPassword()
  93. {
  94. $post = Yii::$app->request->post();
  95. $old = isset($post['old']) ? $post['old'] : '';
  96. $new = isset($post['new']) ? $post['new'] : '';
  97. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  98. if (empty($new)) {
  99. util::fail('请输入新密码');
  100. }
  101. if ($new != $confirm) {
  102. util::fail('二次密码不一致');
  103. }
  104. $user = $this->user;
  105. $userId = $this->userId;
  106. if (!empty($user['password'])) {
  107. if (empty($old)) {
  108. util::fail('请填写旧密码');
  109. }
  110. if (password_verify($old, $user['password']) == false) {
  111. util::fail('旧密码错误');
  112. }
  113. }
  114. UserService::updateById($userId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  115. util::ok('修改成功');
  116. }
  117. //申请会员 shish 2019.12.18
  118. public function actionApplyMember()
  119. {
  120. $post = Yii::$app->request->post();
  121. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  122. if (stringUtil::isMobieNumber($mobile) == false) {
  123. util::fail('请输入手机号');
  124. }
  125. $user = $this->user;
  126. if (isset($user['isMember']) && $user['isMember'] == 1) {
  127. util::fail('已经是会员了');
  128. }
  129. $userId = $this->userId;
  130. $code = isset($post['code']) ? $post['code'] : '';
  131. $cacheCode = SmsService::getApplyMemberCode($userId);
  132. if (empty($cacheCode) || $cacheCode != $code) {
  133. util::fail('验证码错误');
  134. }
  135. $update = [];
  136. if (isset($post['realName']) && !empty($post['realName'])) {
  137. $update['realName'] = $post['realName'];
  138. }
  139. if (isset($post['birthday']) && !empty($post['birthday'])) {
  140. $update['birthday'] = $post['birthday'];
  141. }
  142. $user = UserService::getByCondition(['merchantId' => $this->merchantId, 'mobile' => $mobile]);
  143. if (!empty($user)) {
  144. util::fail('手机号已经注册过了');
  145. }
  146. $update['mobile'] = $mobile;
  147. UserService::upgradeToMember($userId, $this->merchantId, $update);
  148. util::ok('注册成功');
  149. }
  150. }