UserController.php 5.7 KB

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