UserController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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($userId)
  21. {
  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. UserAssetService::updateByUserId($userId, ['member' => 0]);
  69. util::successInfo('操作成功', 'A0001', ['mobile' => $user['mobile']]);
  70. }
  71. $cacheKey = 'MINI_SESSION_KEY_' . $miniOpenId;
  72. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  73. if (empty($sessionKey)) {
  74. util::fail('sesstion key empty');
  75. }
  76. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  77. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  78. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  79. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  80. if ($errCode != 0) {
  81. util::fail('解密失败');
  82. }
  83. $arr = Json::decode($result);
  84. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  85. $userId = $user['id'];
  86. /**升级成为会员**/
  87. UserService::upgradeToMember($userId, $merchantId, ['mobile' => $mobile]);
  88. util::success(['mobile' => $mobile]);
  89. }
  90. //密码修改
  91. public function actionPassword()
  92. {
  93. $post = Yii::$app->request->post();
  94. $old = isset($post['old']) ? $post['old'] : '';
  95. $new = isset($post['new']) ? $post['new'] : '';
  96. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  97. if (empty($new)) {
  98. util::fail('请输入新密码');
  99. }
  100. if ($new != $confirm) {
  101. util::fail('二次密码不一致');
  102. }
  103. $user = $this->user;
  104. $userId = $this->userId;
  105. if (!empty($user['password'])) {
  106. if (empty($old)) {
  107. util::fail('请填写旧密码');
  108. }
  109. if (password_verify($old, $user['password']) == false) {
  110. util::fail('旧密码错误');
  111. }
  112. }
  113. UserService::updateById($userId, ['password' => password_hash($new, PASSWORD_BCRYPT)]);
  114. util::ok('修改成功');
  115. }
  116. //申请会员 shish 2019.12.18
  117. public function actionApplyMember()
  118. {
  119. $post = Yii::$app->request->post();
  120. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  121. if (stringUtil::isMobile($mobile) == false) {
  122. util::fail('请输入手机号');
  123. }
  124. $userId = $this->userId;
  125. $asset = UserAssetService::getByUserId($userId);
  126. if (isset($asset['member']) && $asset['member'] != -1) {
  127. util::fail('已经是会员了');
  128. }
  129. $code = isset($post['code']) ? $post['code'] : '';
  130. $cacheCode = SmsService::getApplyMemberCode($userId);
  131. if (empty($cacheCode) || $cacheCode != $code) {
  132. util::fail('验证码错误');
  133. }
  134. $update = [];
  135. if (isset($post['realName']) && !empty($post['realName'])) {
  136. $update['realName'] = $post['realName'];
  137. }
  138. if (isset($post['birthday']) && !empty($post['birthday'])) {
  139. $update['birthday'] = $post['birthday'];
  140. }
  141. $user = UserService::getByCondition(['merchantId' => $this->merchantId, 'mobile' => $mobile]);
  142. if (!empty($user)) {
  143. util::fail('手机号已经注册过了');
  144. }
  145. $update['mobile'] = $mobile;
  146. UserService::upgradeToMember($userId, $this->merchantId, $update);
  147. util::ok('注册成功');
  148. }
  149. //用户基本信息和资产 shish 2019.12.19
  150. public function actionMyProfile()
  151. {
  152. $user = UserService::getUserInfo($this->userId);
  153. $growth = $user['userAsset']['growth'];
  154. $extend = $this->merchantExtend;
  155. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  156. //下个等级的积分
  157. $nextLevelGrowth = 0;
  158. foreach ($grade as $val) {
  159. $currentGrowth = $val['growth'];
  160. if ($currentGrowth > $growth) {
  161. $nextLevelGrowth = $currentGrowth;
  162. break;
  163. }
  164. }
  165. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  166. util::success($user);
  167. }
  168. }