Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

278 righe
8.9 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. $this->error(__('暂未开放'));
  87. die();
  88. $username = $this->request->post('username');
  89. $password = $this->request->post('password');
  90. $mobile = $this->request->post('mobile');
  91. $captcha = $this->request->post("captcha");
  92. if (!$username || !$password) {
  93. $this->error(__('Invalid parameters'));
  94. }
  95. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  96. $this->error(__('Mobile is incorrect'));
  97. }
  98. $ret = Sms::check($mobile, $captcha, 'register');
  99. if (!$ret) {
  100. $this->error(__('Captcha is incorrect'));
  101. }
  102. Sms::flush($mobile, 'register');
  103. $avatar = \addons\unishop\model\Config::getByName('avatar')['value'] ?? '';
  104. $ret = $this->auth->register($username, $password, '', $mobile, ['avatar' => $avatar]);
  105. if ($ret) {
  106. $data = ['userinfo' => $this->auth->getUserinfo()];
  107. $this->success(__('Sign up successful'), $data);
  108. } else {
  109. $this->error($this->auth->getError());
  110. }
  111. }
  112. /**
  113. * 更改用户信息
  114. */
  115. public function edit()
  116. {
  117. $userInfo = $this->auth->getUserinfo();
  118. $username = $this->request->post('username', $userInfo['username']);
  119. $mobile = $this->request->post('mobile', $userInfo['mobile']);
  120. $avatar = $this->request->post('avatar', $userInfo['avatar']);
  121. $user = \app\common\model\User::get($this->auth->id);
  122. $user->username = $username;
  123. $user->mobile = $mobile;
  124. $user->avatar = $avatar;
  125. if ($user->save()) {
  126. $this->success(__('Modified'), 1);
  127. } else {
  128. $this->error(__('Fail'), 0);
  129. }
  130. }
  131. /**
  132. * 登录状态
  133. */
  134. public function status()
  135. {
  136. $this->success('', $this->auth->isLogin());
  137. }
  138. /**
  139. * 微信小程序登录
  140. */
  141. public function authSession()
  142. {
  143. $platform = $this->request->header('platform');
  144. switch ($platform) {
  145. case 'MP-WEIXIN':
  146. $code = $this->request->get('code');
  147. $data = Wechat::authSession($code);
  148. // 如果有手机号码,自动登录
  149. if (isset($data['userInfo']['mobile']) && (!empty($data['userInfo']['mobile']) || $data['userInfo']['mobile'] != '')) {
  150. $this->auth->direct($data['userInfo']['id']);
  151. if ($this->auth->isLogin()) {
  152. $data['userInfo']['token'] = $this->auth->getToken();
  153. // 支付的时候用
  154. Cache::set('openid_' . $data['userInfo']['id'], $data['openid'], 7200);
  155. }
  156. }
  157. break;
  158. default:
  159. $data = [];
  160. }
  161. $this->success('', $data);
  162. }
  163. /**
  164. * 微信小程序消息解密
  165. */
  166. public function decryptData()
  167. {
  168. $iv = $this->request->post('iv');
  169. $encryptedData = $this->request->post('encryptedData');
  170. $app = Wechat::initEasyWechat('miniProgram');
  171. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  172. $this->success('', $decryptedData);
  173. }
  174. /**
  175. * 微信小程序通过授权手机号登录
  176. */
  177. public function loginForWechatMini()
  178. {
  179. $iv = $this->request->post('iv');
  180. $encryptedData = $this->request->post('encryptedData');
  181. $app = Wechat::initEasyWechat('miniProgram');
  182. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  183. if (isset($decryptedData['phoneNumber'])) {
  184. $openid = Session::get('openid');
  185. // 看看有没有这个mobile的用户
  186. $user = \addons\unishop\model\User::getByMobile($decryptedData['phoneNumber']);
  187. if ($user) {
  188. // 有 处理:1,把;user_extend对应的user删除;2,把user_extend表的user_id字段换成已存在的用户id
  189. $userExtend = UserExtend::getByOpenid($openid);
  190. if ($userExtend) {
  191. if ($userExtend['user_id'] != $user->id) {
  192. \addons\unishop\model\User::destroy($userExtend['user_id']);
  193. $userExtend->user_id = $user->id;
  194. $userExtend->save();
  195. }
  196. } else {
  197. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  198. }
  199. } else {
  200. // 没有
  201. $userExtend = UserExtend::getByOpenid($openid);
  202. if ($userExtend) {
  203. $user = \addons\unishop\model\User::get($userExtend->user_id);
  204. $user->mobile = $decryptedData['phoneNumber'];
  205. $user->save();
  206. } else {
  207. $params = [
  208. 'level' => 1,
  209. 'score' => 0,
  210. 'jointime' => time(),
  211. 'joinip' => $_SERVER['REMOTE_ADDR'],
  212. 'logintime' => time(),
  213. 'loginip' => $_SERVER['REMOTE_ADDR'],
  214. 'prevtime' => time(),
  215. 'status' => 'normal',
  216. 'avatar' => '',
  217. 'username' => __('Tourist'),
  218. 'mobile' => $decryptedData['phoneNumber']
  219. ];
  220. $user = \addons\unishop\model\User::create($params, true);
  221. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  222. }
  223. }
  224. $userInfo['id'] = $user->id;
  225. $userInfo['openid'] = $openid;
  226. $userInfo['mobile'] = $user->mobile;
  227. $userInfo['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($user->avatar);
  228. $userInfo['username'] = $user->username;
  229. $this->auth->direct($userInfo['id']);
  230. if ($this->auth->isLogin()) {
  231. $userInfo['token'] = $this->auth->getToken();
  232. // 支付的时候用
  233. Cache::set('openid_' . $userInfo['id'], $openid, 7200);
  234. }
  235. $this->success('', $userInfo);
  236. } else {
  237. $this->error(__('Logged in failed'));
  238. }
  239. }
  240. }