UserController.php 7.1 KB

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