UserController.php 7.1 KB

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