You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

276 lines
8.8 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zhengmingwei
  5. * Date: 2019/10/25
  6. * Time: 11:09 下午
  7. */
  8. namespace addons\unishop\controller;
  9. use addons\unishop\extend\Wechat;
  10. use addons\unishop\model\UserExtend;
  11. use app\common\library\Sms;
  12. use think\Cache;
  13. use think\Session;
  14. use think\Validate;
  15. class User extends Base
  16. {
  17. protected $noNeedLogin = ['login', 'status', 'authSession', 'decryptData', 'register', 'resetpwd', 'loginForWechatMini'];
  18. /**
  19. * 会员登录
  20. *
  21. * @param string $account 账号
  22. * @param string $password 密码
  23. */
  24. public function login()
  25. {
  26. $mobile = $this->request->post('mobile');
  27. $password = $this->request->post('password');
  28. if (!$mobile || !$password) {
  29. $this->error(__('Invalid parameters'));
  30. }
  31. $ret = $this->auth->login($mobile, $password);
  32. if ($ret) {
  33. $data = $this->auth->getUserinfo();
  34. $data['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($data['avatar']);
  35. $this->success(__('Logged in successful'), $data);
  36. } else {
  37. $this->error($this->auth->getError());
  38. }
  39. }
  40. /**
  41. * 重置密码
  42. *
  43. * @param string $mobile 手机号
  44. * @param string $newpassword 新密码
  45. * @param string $captcha 验证码
  46. */
  47. public function resetpwd()
  48. {
  49. $mobile = $this->request->post("mobile");
  50. $newpassword = $this->request->post("password");
  51. $captcha = $this->request->post("captcha");
  52. if (!$newpassword || !$captcha) {
  53. $this->error(__('Invalid parameters'));
  54. }
  55. if (!Validate::regex($mobile, "^1\d{10}$")) {
  56. $this->error(__('Mobile is incorrect'));
  57. }
  58. $user = \app\common\model\User::getByMobile($mobile);
  59. if (!$user) {
  60. $this->error(__('User not found'));
  61. }
  62. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  63. if (!$ret) {
  64. $this->error(__('Captcha is incorrect'));
  65. }
  66. Sms::flush($mobile, 'resetpwd');
  67. //模拟一次登录
  68. $this->auth->direct($user->id);
  69. $ret = $this->auth->changepwd($newpassword, '', true);
  70. if ($ret) {
  71. $this->success(__('Reset password successful'), 1);
  72. } else {
  73. $this->error($this->auth->getError());
  74. }
  75. }
  76. /**
  77. * 注册会员
  78. *
  79. * @param string $username 用户名
  80. * @param string $password 密码
  81. * @param string $email 邮箱
  82. * @param string $mobile 手机号
  83. */
  84. public function register()
  85. {
  86. $username = $this->request->post('username');
  87. $password = $this->request->post('password');
  88. $mobile = $this->request->post('mobile');
  89. $captcha = $this->request->post("captcha");
  90. if (!$username || !$password) {
  91. $this->error(__('Invalid parameters'));
  92. }
  93. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  94. $this->error(__('Mobile is incorrect'));
  95. }
  96. $ret = Sms::check($mobile, $captcha, 'register');
  97. if (!$ret) {
  98. $this->error(__('Captcha is incorrect'));
  99. }
  100. Sms::flush($mobile, 'register');
  101. $avatar = \addons\unishop\model\Config::getByName('avatar')['value'] ?? '';
  102. $ret = $this->auth->register($username, $password, '', $mobile, ['avatar' => $avatar]);
  103. if ($ret) {
  104. $data = ['userinfo' => $this->auth->getUserinfo()];
  105. $this->success(__('Sign up successful'), $data);
  106. } else {
  107. $this->error($this->auth->getError());
  108. }
  109. }
  110. /**
  111. * 更改用户信息
  112. */
  113. public function edit()
  114. {
  115. $userInfo = $this->auth->getUserinfo();
  116. $username = $this->request->post('username', $userInfo['username']);
  117. $mobile = $this->request->post('mobile', $userInfo['mobile']);
  118. $avatar = $this->request->post('avatar', $userInfo['avatar']);
  119. $user = \app\common\model\User::get($this->auth->id);
  120. $user->username = $username;
  121. $user->mobile = $mobile;
  122. $user->avatar = $avatar;
  123. if ($user->save()) {
  124. $this->success(__('Modified'), 1);
  125. } else {
  126. $this->error(__('Fail'), 0);
  127. }
  128. }
  129. /**
  130. * 登录状态
  131. */
  132. public function status()
  133. {
  134. $this->success('', $this->auth->isLogin());
  135. }
  136. /**
  137. * 微信小程序登录
  138. */
  139. public function authSession()
  140. {
  141. $platform = $this->request->header('platform');
  142. switch ($platform) {
  143. case 'MP-WEIXIN':
  144. $code = $this->request->get('code');
  145. $data = Wechat::authSession($code);
  146. // 如果有手机号码,自动登录
  147. if (isset($data['userInfo']['mobile']) && (!empty($data['userInfo']['mobile']) || $data['userInfo']['mobile'] != '')) {
  148. $this->auth->direct($data['userInfo']['id']);
  149. if ($this->auth->isLogin()) {
  150. $data['userInfo']['token'] = $this->auth->getToken();
  151. // 支付的时候用
  152. Cache::set('openid_' . $data['userInfo']['id'], $data['openid'], 7200);
  153. }
  154. }
  155. break;
  156. default:
  157. $data = [];
  158. }
  159. $this->success('', $data);
  160. }
  161. /**
  162. * 微信小程序消息解密
  163. */
  164. public function decryptData()
  165. {
  166. $iv = $this->request->post('iv');
  167. $encryptedData = $this->request->post('encryptedData');
  168. $app = Wechat::initEasyWechat('miniProgram');
  169. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  170. $this->success('', $decryptedData);
  171. }
  172. /**
  173. * 微信小程序通过授权手机号登录
  174. */
  175. public function loginForWechatMini()
  176. {
  177. $iv = $this->request->post('iv');
  178. $encryptedData = $this->request->post('encryptedData');
  179. $app = Wechat::initEasyWechat('miniProgram');
  180. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  181. if (isset($decryptedData['phoneNumber'])) {
  182. $openid = Session::get('openid');
  183. // 看看有没有这个mobile的用户
  184. $user = \addons\unishop\model\User::getByMobile($decryptedData['phoneNumber']);
  185. if ($user) {
  186. // 有 处理:1,把;user_extend对应的user删除;2,把user_extend表的user_id字段换成已存在的用户id
  187. $userExtend = UserExtend::getByOpenid($openid);
  188. if ($userExtend) {
  189. if ($userExtend['user_id'] != $user->id) {
  190. \addons\unishop\model\User::destroy($userExtend['user_id']);
  191. $userExtend->user_id = $user->id;
  192. $userExtend->save();
  193. }
  194. } else {
  195. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  196. }
  197. } else {
  198. // 没有
  199. $userExtend = UserExtend::getByOpenid($openid);
  200. if ($userExtend) {
  201. $user = \addons\unishop\model\User::get($userExtend->user_id);
  202. $user->mobile = $decryptedData['phoneNumber'];
  203. $user->save();
  204. } else {
  205. $params = [
  206. 'level' => 1,
  207. 'score' => 0,
  208. 'jointime' => time(),
  209. 'joinip' => $_SERVER['REMOTE_ADDR'],
  210. 'logintime' => time(),
  211. 'loginip' => $_SERVER['REMOTE_ADDR'],
  212. 'prevtime' => time(),
  213. 'status' => 'normal',
  214. 'avatar' => '',
  215. 'username' => __('Tourist'),
  216. 'mobile' => $decryptedData['phoneNumber']
  217. ];
  218. $user = \addons\unishop\model\User::create($params, true);
  219. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  220. }
  221. }
  222. $userInfo['id'] = $user->id;
  223. $userInfo['openid'] = $openid;
  224. $userInfo['mobile'] = $user->mobile;
  225. $userInfo['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($user->avatar);
  226. $userInfo['username'] = $user->username;
  227. $this->auth->direct($userInfo['id']);
  228. if ($this->auth->isLogin()) {
  229. $userInfo['token'] = $this->auth->getToken();
  230. // 支付的时候用
  231. Cache::set('openid_' . $userInfo['id'], $openid, 7200);
  232. }
  233. $this->success('', $userInfo);
  234. } else {
  235. $this->error(__('Logged in failed'));
  236. }
  237. }
  238. }