UserController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. public $guestAccess = ['recent-shop'];
  16. //最近店铺 ssh 2021.2.27
  17. public function actionRecentShop()
  18. {
  19. $list = UserClass::getRecentShop($this->user);
  20. util::success(['list' => $list, 'showIndex' => 0]);
  21. }
  22. //获取登陆客户的资产 ssh 2019.11.22
  23. public function actionAsset()
  24. {
  25. $asset = UserAssetService::getByUserId($this->userId);
  26. util::success($asset);
  27. }
  28. //获取登陆客户的优惠情况 ssh 2019.12.3
  29. public function actionDiscount()
  30. {
  31. $discount = UserAssetService::getDiscount($this->userId);
  32. util::success($discount);
  33. }
  34. //小程序用户完整信息 ssh 2019.12.12
  35. public function actionMiniFullInfo()
  36. {
  37. $post = Yii::$app->request->post();
  38. $iv = isset($post['iv']) ? $post['iv'] : '';
  39. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  40. $appId = $this->sj['miniAppId'];
  41. $sjId = $this->sjId;
  42. $miniOpenId = $this->user['miniOpenId'];
  43. if (empty($miniOpenId)) {
  44. util::fail('mini_open_id empty');
  45. }
  46. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  47. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  48. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  49. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  50. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  51. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  52. if ($errCode != 0) {
  53. Yii::info($result . ' ' . $errCode);
  54. util::fail('获取用户信息失败');
  55. }
  56. Yii::info('小程序获取用户信息:' . $result);
  57. $originalInfo = Json::decode($result);
  58. $source = UserClass::$userSourceId['mini']['name'];
  59. $user = UserClass::replaceUser($originalInfo, $source, $sjId);
  60. $userId = $user['id'];
  61. $userInfo = UserService::getUserInfo($userId);
  62. util::success($userInfo);
  63. }
  64. //小程序用户手机号
  65. public function actionMiniMobile()
  66. {
  67. $post = Yii::$app->request->post();
  68. $iv = $post['iv'];
  69. $encryptedData = $post['encryptedData'];
  70. $sjWx = $this->sjWx;
  71. $appId = $sjWx['miniAppId'];
  72. $user = $this->user;
  73. $miniOpenId = $user['miniOpenId'];
  74. $customId = $this->customId;
  75. $info = CustomClass::getCustomInfo($customId);
  76. if (isset($info['mobile']) && !empty($info['mobile'])) {
  77. util::success(['mobile' => $info['mobile']]);
  78. }
  79. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  80. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  81. if (empty($sessionKey)) {
  82. util::fail('系统错误,session empty');
  83. }
  84. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  85. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  86. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  87. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  88. if ($errCode != 0) {
  89. util::fail('解密失败');
  90. }
  91. $arr = Json::decode($result);
  92. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  93. if (empty($mobile)) {
  94. util::fail('没有取到手机号,请重试');
  95. }
  96. CustomClass::updateCustomInfo($info, ['mobile' => $mobile]);
  97. util::success(['mobile' => $mobile]);
  98. }
  99. //密码修改
  100. public function actionPassword()
  101. {
  102. $post = Yii::$app->request->post();
  103. $old = isset($post['old']) ? $post['old'] : '';
  104. $new = isset($post['new']) ? $post['new'] : '';
  105. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  106. if (empty($new)) {
  107. util::fail('请输入新密码');
  108. }
  109. if ($new != $confirm) {
  110. util::fail('二次密码不一致');
  111. }
  112. $userId = $this->userId;
  113. $user = UserService::getUserInfo($userId, false);
  114. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  115. if (empty($old)) {
  116. util::fail('请填写旧密码');
  117. }
  118. if (password_verify($old, $user['payPassword']) == false) {
  119. util::fail('旧密码错误');
  120. }
  121. }
  122. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  123. util::complete('修改成功');
  124. }
  125. //申请会员 ssh 2019.12.18
  126. public function actionApplyMember()
  127. {
  128. $post = Yii::$app->request->post();
  129. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  130. if (stringUtil::isMobile($mobile) == false) {
  131. util::fail('请输入手机号');
  132. }
  133. $userId = $this->userId;
  134. $asset = UserAssetService::getByUserId($userId);
  135. if (isset($asset['member']) && $asset['member'] != -1) {
  136. util::fail('已经是会员了');
  137. }
  138. $code = isset($post['code']) ? $post['code'] : '';
  139. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  140. if (empty($cacheCode) || $cacheCode != $code) {
  141. util::fail('验证码错误');
  142. }
  143. $update = [];
  144. if (isset($post['realName']) && !empty($post['realName'])) {
  145. $update['realName'] = $post['realName'];
  146. }
  147. if (isset($post['birthday']) && !empty($post['birthday'])) {
  148. $update['birthday'] = $post['birthday'];
  149. }
  150. $user = UserService::getByCondition(['merchantId' => $this->sjId, 'mobile' => $mobile]);
  151. if ($userId == $user['id']) {
  152. util::fail('手机号已验证');
  153. }
  154. if (!empty($user)) {
  155. util::fail('手机号已经注册过了');
  156. }
  157. $update['mobile'] = $mobile;
  158. UserService::becomeMember($this->user, $this->sj, $update);
  159. util::complete('注册成功');
  160. }
  161. //用户基本信息和资产 ssh 2019.12.19
  162. public function actionMyProfile()
  163. {
  164. $user = $this->custom;
  165. $growth = $user['growth'] ?? 0;
  166. $extend = $this->sjExtend;
  167. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  168. //下个等级的积分
  169. $nextLevelGrowth = 0;
  170. foreach ($grade as $val) {
  171. $currentGrowth = $val['growth'];
  172. if ($currentGrowth > $growth) {
  173. $nextLevelGrowth = $currentGrowth;
  174. break;
  175. }
  176. }
  177. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  178. util::success($user);
  179. }
  180. //登陆客户的信息 ssh 2020.3.12
  181. public function actionLoginDetail()
  182. {
  183. $user = $this->user;
  184. util::success($user);
  185. }
  186. }