UserController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace client\controllers;
  3. use biz\merchant\services\MerchantExtendService;
  4. use biz\message\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. UserAssetService::updateByUserId($userId, ['member' => 0]);
  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::isMobile($mobile) == false) {
  123. util::fail('请输入手机号');
  124. }
  125. $userId = $this->userId;
  126. $asset = UserAssetService::getByUserId($userId);
  127. if (isset($asset['member']) && $asset['member'] != -1) {
  128. util::fail('已经是会员了');
  129. }
  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. //用户基本信息和资产 shish 2019.12.19
  151. public function actionMyProfile()
  152. {
  153. $user = UserService::getUserInfo($this->userId);
  154. $growth = $user['userAsset']['growth'];
  155. $extend = $this->merchantExtend;
  156. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  157. //下个等级的积分
  158. $nextLevelGrowth = 0;
  159. foreach ($grade as $val) {
  160. $currentGrowth = $val['growth'];
  161. if ($currentGrowth > $growth) {
  162. $nextLevelGrowth = $currentGrowth;
  163. break;
  164. }
  165. }
  166. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  167. util::success($user);
  168. }
  169. }