UserController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace mall\controllers;
  3. use biz\merchant\services\MerchantExtendService;
  4. use biz\message\services\SmsService;
  5. use biz\user\classes\UserClass;
  6. use biz\user\services\UserAssetService;
  7. use biz\user\services\UserService;
  8. use common\components\stringUtil;
  9. use Yii;
  10. use common\components\util;
  11. use yii\helpers\Json;
  12. class UserController extends BaseController
  13. {
  14. //获取登陆客户的资产 shish 2019.11.22
  15. public function actionAsset()
  16. {
  17. $asset = UserAssetService::getByUserId($this->userId);
  18. util::success($asset);
  19. }
  20. //获取登陆客户的优惠情况 shish 2019.12.3
  21. public function actionDiscount()
  22. {
  23. $discount = UserAssetService::getDiscount($this->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 = 'MALL_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 = UserClass::$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::success(['mobile' => $user['mobile']]);
  71. }
  72. $cacheKey = 'MALL_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. UserService::becomeMember($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. $userId = $this->userId;
  104. $user = UserService::getUserInfo($userId, false);
  105. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  106. if (empty($old)) {
  107. util::fail('请填写旧密码');
  108. }
  109. if (password_verify($old, $user['payPassword']) == false) {
  110. util::fail('旧密码错误');
  111. }
  112. }
  113. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  114. util::complete('修改成功');
  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, $mobile);
  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 ($userId == $user['id']) {
  143. util::fail('手机号已验证');
  144. }
  145. if (!empty($user)) {
  146. util::fail('手机号已经注册过了');
  147. }
  148. $update['mobile'] = $mobile;
  149. UserService::becomeMember($userId, $this->merchantId, $update);
  150. util::complete('注册成功');
  151. }
  152. //用户基本信息和资产 shish 2019.12.19
  153. public function actionMyProfile()
  154. {
  155. $user = UserService::getUserInfo($this->userId);
  156. $growth = $user['userAsset']['growth'];
  157. $extend = $this->merchantExtend;
  158. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  159. //下个等级的积分
  160. $nextLevelGrowth = 0;
  161. foreach ($grade as $val) {
  162. $currentGrowth = $val['growth'];
  163. if ($currentGrowth > $growth) {
  164. $nextLevelGrowth = $currentGrowth;
  165. break;
  166. }
  167. }
  168. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  169. util::success($user);
  170. }
  171. //登陆客户的信息 shish 2020.3.12
  172. public function actionLoginDetail()
  173. {
  174. $user = $this->user;
  175. util::success($user);
  176. }
  177. }