UserController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizMall\merchant\services\MerchantExtendService;
  5. use bizMall\message\services\SmsService;
  6. use bizMall\user\classes\UserClass;
  7. use bizMall\user\services\UserAssetService;
  8. use bizMall\user\services\UserService;
  9. use common\components\stringUtil;
  10. use Yii;
  11. use common\components\util;
  12. use yii\helpers\Json;
  13. class UserController extends BaseController
  14. {
  15. //获取登陆客户的资产 shish 2019.11.22
  16. public function actionAsset()
  17. {
  18. $asset = UserAssetService::getByUserId($this->userId);
  19. util::success($asset);
  20. }
  21. //获取登陆客户的优惠情况 shish 2019.12.3
  22. public function actionDiscount()
  23. {
  24. $discount = UserAssetService::getDiscount($this->userId);
  25. util::success($discount);
  26. }
  27. //小程序用户完整信息 shish 2019.12.12
  28. public function actionMiniFullInfo()
  29. {
  30. $post = Yii::$app->request->post();
  31. $iv = isset($post['iv']) ? $post['iv'] : '';
  32. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  33. $appId = $this->sj['miniAppId'];
  34. $sjId = $this->sjId;
  35. $miniOpenId = $this->user['miniOpenId'];
  36. if (empty($miniOpenId)) {
  37. util::fail('mini_open_id empty');
  38. }
  39. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  40. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  41. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  42. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  43. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  44. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  45. if ($errCode != 0) {
  46. Yii::info($result . ' ' . $errCode);
  47. util::fail('获取用户信息失败');
  48. }
  49. Yii::info('小程序获取用户信息:' . $result);
  50. $originalInfo = Json::decode($result);
  51. $source = UserClass::$userSourceId['mini']['name'];
  52. $user = UserClass::replaceUser($originalInfo, $source, $sjId);
  53. $userId = $user['id'];
  54. $userInfo = UserService::getUserInfo($userId);
  55. util::success($userInfo);
  56. }
  57. //小程序用户手机号
  58. public function actionMiniMobile()
  59. {
  60. $post = Yii::$app->request->post();
  61. $iv = $post['iv'];
  62. $encryptedData = $post['encryptedData'];
  63. $sjWx = $this->sjWx;
  64. $appId = $sjWx['miniAppId'];
  65. $user = $this->user;
  66. $miniOpenId = $user['miniOpenId'];
  67. $customId = $this->customId;
  68. $info = CustomClass::getCustomInfo($customId);
  69. if (isset($info['mobile']) && !empty($info['mobile'])) {
  70. util::success(['mobile' => $info['mobile']]);
  71. }
  72. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  73. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  74. if (empty($sessionKey)) {
  75. util::fail('系统错误,session 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. if (empty($mobile)) {
  87. util::fail('没有取到手机号,请重试');
  88. }
  89. CustomClass::updateCustomInfo($info, ['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. $userId = $this->userId;
  106. $user = UserService::getUserInfo($userId, false);
  107. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  108. if (empty($old)) {
  109. util::fail('请填写旧密码');
  110. }
  111. if (password_verify($old, $user['payPassword']) == false) {
  112. util::fail('旧密码错误');
  113. }
  114. }
  115. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  116. util::complete('修改成功');
  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. $userId = $this->userId;
  127. $asset = UserAssetService::getByUserId($userId);
  128. if (isset($asset['member']) && $asset['member'] != -1) {
  129. util::fail('已经是会员了');
  130. }
  131. $code = isset($post['code']) ? $post['code'] : '';
  132. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  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->sjId, 'mobile' => $mobile]);
  144. if ($userId == $user['id']) {
  145. util::fail('手机号已验证');
  146. }
  147. if (!empty($user)) {
  148. util::fail('手机号已经注册过了');
  149. }
  150. $update['mobile'] = $mobile;
  151. UserService::becomeMember($this->user, $this->sj, $update);
  152. util::complete('注册成功');
  153. }
  154. //用户基本信息和资产 shish 2019.12.19
  155. public function actionMyProfile()
  156. {
  157. $user = UserService::getUserInfo($this->userId);
  158. $growth = $user['userAsset']['growth'];
  159. $extend = $this->sjExtend;
  160. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  161. //下个等级的积分
  162. $nextLevelGrowth = 0;
  163. foreach ($grade as $val) {
  164. $currentGrowth = $val['growth'];
  165. if ($currentGrowth > $growth) {
  166. $nextLevelGrowth = $currentGrowth;
  167. break;
  168. }
  169. }
  170. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  171. util::success($user);
  172. }
  173. //登陆客户的信息 shish 2020.3.12
  174. public function actionLoginDetail()
  175. {
  176. $user = $this->user;
  177. util::success($user);
  178. }
  179. }