User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /**
  16. * 用户
  17. */
  18. class User extends Base
  19. {
  20. protected $noNeedLogin = ['login', 'status', 'authSession', 'decryptData', 'register', 'resetpwd', 'loginForWechatMini'];
  21. /**
  22. * @ApiTitle (会员登录)
  23. * @ApiSummary (会员登录)
  24. * @ApiMethod (POST)
  25. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  26. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  27. * @ApiParams (name="password", type="string", required=true, description="密码")
  28. * @ApiReturn ({"code":1,"msg":"登录成功","data":{}})
  29. *
  30. * @ApiReturnParams (name="user_id", type="integer", description="用户id")
  31. * @ApiReturnParams (name="username", type="string", description="用户名称")
  32. * @ApiReturnParams (name="mobile", type="string", description="用户电话")
  33. * @ApiReturnParams (name="avatar", type="string", description="用户头像")
  34. * @ApiReturnParams (name="score", type="string", description="用户积分")
  35. * @ApiReturnParams (name="token", type="string", description="登录token")
  36. */
  37. public function login()
  38. {
  39. $mobile = $this->request->post('mobile');
  40. $password = $this->request->post('password');
  41. if (!$mobile || !$password) {
  42. $this->error(__('Invalid parameters'));
  43. }
  44. $ret = $this->auth->login($mobile, $password);
  45. if ($ret) {
  46. $data = $this->auth->getUserinfo();
  47. $data['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($data['avatar']);
  48. $this->success(__('Logged in successful'), $data);
  49. } else {
  50. $this->error($this->auth->getError());
  51. }
  52. }
  53. /**
  54. * @ApiTitle (重置密码)
  55. * @ApiSummary (重置密码)
  56. * @ApiMethod (POST)
  57. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  58. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  59. * @ApiParams (name="password", type="string", required=true, description="新密码")
  60. * @ApiParams (name="captcha", type="string", required=true, description="验证码")
  61. * @ApiReturn ({"code":1,"msg":"重置成功","data":1})
  62. *
  63. */
  64. public function resetpwd()
  65. {
  66. $mobile = $this->request->post("mobile");
  67. $newpassword = $this->request->post("password");
  68. $captcha = $this->request->post("captcha");
  69. if (!$newpassword || !$captcha) {
  70. $this->error(__('Invalid parameters'));
  71. }
  72. if (!Validate::regex($mobile, "^1\d{10}$")) {
  73. $this->error(__('Mobile is incorrect'));
  74. }
  75. $user = \app\common\model\User::getByMobile($mobile);
  76. if (!$user) {
  77. $this->error(__('User not found'));
  78. }
  79. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  80. if (!$ret) {
  81. $this->error(__('Captcha is incorrect'));
  82. }
  83. Sms::flush($mobile, 'resetpwd');
  84. //模拟一次登录
  85. $this->auth->direct($user->id);
  86. $ret = $this->auth->changepwd($newpassword, '', true);
  87. if ($ret) {
  88. $this->success(__('Reset password successful'), 1);
  89. } else {
  90. $this->error($this->auth->getError());
  91. }
  92. }
  93. /**
  94. * @ApiTitle (注册会员)
  95. * @ApiSummary (注册会员)
  96. * @ApiMethod (POST)
  97. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  98. * @ApiParams (name="username", type="string", required=true, description="用户名称")
  99. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  100. * @ApiParams (name="password", type="string", required=true, description="密码")
  101. * @ApiParams (name="captcha", type="string", required=true, description="验证码")
  102. * @ApiReturn ({"code":1,"msg":"注册成功","data":1})
  103. *
  104. * @ApiReturnParams (name="userinfo.id", type="integer", description="用户id")
  105. * @ApiReturnParams (name="userinfo.username", type="string", description="用户名称")
  106. * @ApiReturnParams (name="userinfo.mobile", type="string", description="用户电话")
  107. * @ApiReturnParams (name="userinfo.avatar", type="string", description="用户头像")
  108. * @ApiReturnParams (name="userinfo.score", type="string", description="用户积分")
  109. *
  110. */
  111. public function register()
  112. {
  113. $username = $this->request->post('username');
  114. $password = $this->request->post('password');
  115. $mobile = $this->request->post('mobile');
  116. $captcha = $this->request->post("captcha");
  117. if (!$username || !$password) {
  118. $this->error(__('Invalid parameters'));
  119. }
  120. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  121. $this->error(__('Mobile is incorrect'));
  122. }
  123. $ret = Sms::check($mobile, $captcha, 'register');
  124. if (!$ret) {
  125. $this->error(__('Captcha is incorrect'));
  126. }
  127. Sms::flush($mobile, 'register');
  128. $avatar = \addons\unishop\model\Config::getByName('avatar')['value'] ?? '';
  129. $ret = $this->auth->register($username, $password, '', $mobile, ['avatar' => $avatar]);
  130. if ($ret) {
  131. $data = ['userinfo' => $this->auth->getUserinfo()];
  132. $this->success(__('Sign up successful'), $data);
  133. } else {
  134. $this->error($this->auth->getError());
  135. }
  136. }
  137. /**
  138. * @ApiTitle (更改用户信息)
  139. * @ApiSummary (更改用户信息)
  140. * @ApiMethod (POST)
  141. * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
  142. * @ApiHeaders (name=token, type=string, required=true, description="登录token")
  143. * @ApiParams (name="username", type="string", required=true, description="用户名称")
  144. * @ApiParams (name="mobile", type="string", required=true, description="手机号")
  145. * @ApiParams (name="avatar", type="string", required=true, description="头像")
  146. * @ApiReturn ({"code":1,"msg":"修改成功","data":1})
  147. *
  148. */
  149. public function edit()
  150. {
  151. $userInfo = $this->auth->getUserinfo();
  152. $username = $this->request->post('username', $userInfo['username']);
  153. $mobile = $this->request->post('mobile', $userInfo['mobile']);
  154. $avatar = $this->request->post('avatar', $userInfo['avatar']);
  155. $user = \app\common\model\User::get($this->auth->id);
  156. $user->username = $username;
  157. $user->mobile = $mobile;
  158. $user->avatar = $avatar;
  159. if ($user->save()) {
  160. $this->success(__('Modified'), 1);
  161. } else {
  162. $this->error(__('Fail'), 0);
  163. }
  164. }
  165. /**
  166. * 登录状态
  167. * @ApiInternal
  168. */
  169. public function status()
  170. {
  171. $this->success('', $this->auth->isLogin());
  172. }
  173. /**
  174. * @ApiTitle (微信小程序登录)
  175. * @ApiSummary (微信小程序登录)
  176. * @ApiMethod (GET)
  177. * @ApiHeaders (name=platform, type=string, required=false, description="平台")
  178. * @ApiParams (name="code", type="string", required=true, description="小程序调用wx.login返回的code")
  179. * @ApiReturn ({"code":1,"msg":"","data":{}})
  180. *
  181. * @ApiReturnParams (name="openid", type="integer", description="微信用户openid")
  182. * @ApiReturnParams (name="userInfo.id", type="integer", description="用户id")
  183. * @ApiReturnParams (name="userInfo.username", type="string", description="用户名称")
  184. * @ApiReturnParams (name="userInfo.mobile", type="string", description="用户电话")
  185. * @ApiReturnParams (name="userInfo.avatar", type="string", description="用户头像")
  186. * @ApiReturnParams (name="userInfo.score", type="string", description="用户积分")
  187. * @ApiReturnParams (name="userInfo.token", type="string", description="用户登录token")
  188. *
  189. */
  190. public function authSession()
  191. {
  192. $platform = $this->request->header('platform');
  193. switch ($platform) {
  194. case 'MP-WEIXIN':
  195. $code = $this->request->get('code');
  196. $data = Wechat::authSession($code);
  197. // 如果有手机号码,自动登录
  198. if (isset($data['userInfo']['mobile']) && (!empty($data['userInfo']['mobile']) || $data['userInfo']['mobile'] != '')) {
  199. $this->auth->direct($data['userInfo']['id']);
  200. if ($this->auth->isLogin()) {
  201. $data['userInfo']['token'] = $this->auth->getToken();
  202. // 支付的时候用
  203. Cache::set('openid_' . $data['userInfo']['id'], $data['openid'], 7200);
  204. }
  205. }
  206. break;
  207. default:
  208. $data = [];
  209. }
  210. $this->success('', $data);
  211. }
  212. /**
  213. * @ApiTitle (微信小程序消息解密)
  214. * @ApiSummary (微信小程序消息解密,必须先调用authSession获取到session_key)
  215. * @ApiMethod (POST)
  216. * @ApiParams (name="iv", type="string", required=true, description="")
  217. * @ApiParams (name="encryptedData", type="string", required=true, description="")
  218. * @ApiReturn ({"code":1,"msg":"","data":{手机号码,用户信息等等,具体看用户授权什么权限}})
  219. *
  220. */
  221. public function decryptData()
  222. {
  223. $iv = $this->request->post('iv');
  224. $encryptedData = $this->request->post('encryptedData');
  225. $app = Wechat::initEasyWechat('miniProgram');
  226. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  227. $this->success('', $decryptedData);
  228. }
  229. /**
  230. * @ApiTitle (微信小程序通过授权手机号登录)
  231. * @ApiSummary (微信小程序通过授权手机号登录)
  232. * @ApiMethod (POST)
  233. * @ApiHeaders (name=platform, type=string, required=false, description="平台")
  234. * @ApiParams (name="iv", type="string", required=true, description="")
  235. * @ApiParams (name="encryptedData", type="string", required=true, description="")
  236. * @ApiReturn ({"code":1,"msg":"","data":{}})
  237. *
  238. * @ApiReturnParams (name="openid", type="integer", description="微信用户openid")
  239. * @ApiReturnParams (name="id", type="integer", description="用户id")
  240. * @ApiReturnParams (name="username", type="string", description="用户名称")
  241. * @ApiReturnParams (name="mobile", type="string", description="用户电话")
  242. * @ApiReturnParams (name="avatar", type="string", description="用户头像")
  243. * @ApiReturnParams (name="score", type="string", description="用户积分")
  244. * @ApiReturnParams (name="token", type="string", description="用户登录token")
  245. *
  246. */
  247. public function loginForWechatMini()
  248. {
  249. $iv = $this->request->post('iv');
  250. $encryptedData = $this->request->post('encryptedData');
  251. $app = Wechat::initEasyWechat('miniProgram');
  252. $decryptedData = $app->encryptor->decryptData(Session::get('session_key'), $iv, $encryptedData);
  253. if (isset($decryptedData['phoneNumber'])) {
  254. $openid = Session::get('openid');
  255. // 看看有没有这个mobile的用户
  256. $user = \addons\unishop\model\User::getByMobile($decryptedData['phoneNumber']);
  257. if ($user) {
  258. // 有 处理:1,把;user_extend对应的user删除;2,把user_extend表的user_id字段换成已存在的用户id
  259. $userExtend = UserExtend::getByOpenid($openid);
  260. if ($userExtend) {
  261. if ($userExtend['user_id'] != $user->id) {
  262. \addons\unishop\model\User::destroy($userExtend['user_id']);
  263. $userExtend->user_id = $user->id;
  264. $userExtend->save();
  265. }
  266. } else {
  267. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  268. }
  269. } else {
  270. // 没有
  271. $userExtend = UserExtend::getByOpenid($openid);
  272. if ($userExtend) {
  273. $user = \addons\unishop\model\User::get($userExtend->user_id);
  274. $user->mobile = $decryptedData['phoneNumber'];
  275. $user->save();
  276. } else {
  277. $params = [
  278. 'level' => 1,
  279. 'score' => 0,
  280. 'jointime' => time(),
  281. 'joinip' => $_SERVER['REMOTE_ADDR'],
  282. 'logintime' => time(),
  283. 'loginip' => $_SERVER['REMOTE_ADDR'],
  284. 'prevtime' => time(),
  285. 'status' => 'normal',
  286. 'avatar' => '',
  287. 'username' => __('Tourist'),
  288. 'mobile' => $decryptedData['phoneNumber']
  289. ];
  290. $user = \addons\unishop\model\User::create($params, true);
  291. UserExtend::create(['user_id' => $user->id, 'openid' => $openid]);
  292. }
  293. }
  294. $userInfo['id'] = $user->id;
  295. $userInfo['openid'] = $openid;
  296. $userInfo['mobile'] = $user->mobile;
  297. $userInfo['avatar'] = \addons\unishop\model\Config::getImagesFullUrl($user->avatar);
  298. $userInfo['username'] = $user->username;
  299. $this->auth->direct($userInfo['id']);
  300. if ($this->auth->isLogin()) {
  301. $userInfo['token'] = $this->auth->getToken();
  302. // 支付的时候用
  303. Cache::set('openid_' . $userInfo['id'], $openid, 7200);
  304. }
  305. $this->success('', $userInfo);
  306. } else {
  307. $this->error('登录失败');
  308. }
  309. }
  310. }