User.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use think\Db;
  10. /**
  11. * 会员接口
  12. */
  13. class User extends Api
  14. {
  15. protected $noNeedLogin = ['login', 'mobilelogin', 'wxmini_login', 'resetpwd', 'changemobile'];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. if (!Config::get('fastadmin.usercenter')) {
  21. $this->error(__('User center already closed'));
  22. }
  23. }
  24. /**
  25. * 会员中心
  26. */
  27. public function index()
  28. {
  29. $this->success('', ['welcome' => $this->auth->nickname]);
  30. }
  31. /**
  32. * 会员登录
  33. *
  34. * @ApiMethod (POST)
  35. * @param string $account 账号
  36. * @param string $password 密码
  37. */
  38. public function login()
  39. {
  40. $account = $this->request->post('account');
  41. $password = $this->request->post('password');
  42. if (!$account || !$password) {
  43. $this->error(__('Invalid parameters'));
  44. }
  45. $ret = $this->auth->login($account, $password);
  46. if ($ret) {
  47. $data = ['userinfo' => $this->auth->getUserinfo()];
  48. $this->success(__('Logged in successful'), $data);
  49. } else {
  50. $this->error($this->auth->getError());
  51. }
  52. }
  53. /**
  54. * 手机验证码登录
  55. *
  56. * @ApiMethod (POST)
  57. * @param string $mobile 手机号
  58. * @param string $captcha 验证码
  59. */
  60. public function mobilelogin()
  61. {
  62. $mobile = $this->request->post('mobile');
  63. $captcha = $this->request->post('captcha');
  64. if (!$mobile || !$captcha) {
  65. $this->error(__('Invalid parameters'));
  66. }
  67. if (!Validate::regex($mobile, "^1\d{10}$")) {
  68. $this->error(__('Mobile is incorrect'));
  69. }
  70. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  71. $this->error(__('Captcha is incorrect'));
  72. }
  73. $user = \app\common\model\User::getByMobile($mobile);
  74. if ($user) {
  75. if ($user->status != 1) {
  76. $this->error(__('Account is locked'));
  77. }
  78. //如果已经有账号则直接登录
  79. $ret = $this->auth->direct($user->id);
  80. } else {
  81. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  82. }
  83. if ($ret) {
  84. Sms::flush($mobile, 'mobilelogin');
  85. $data = ['userinfo' => $this->auth->getUserinfo()];
  86. $this->success(__('Logged in successful'), $data);
  87. } else {
  88. $this->error($this->auth->getError());
  89. }
  90. }
  91. /**
  92. * 退出登录
  93. * @ApiMethod (POST)
  94. */
  95. public function logout()
  96. {
  97. if (!$this->request->isPost()) {
  98. $this->error(__('Invalid parameters'));
  99. }
  100. $this->auth->logout();
  101. $this->success(__('Logout successful'));
  102. }
  103. /**
  104. * 修改会员个人信息
  105. *
  106. * @ApiMethod (POST)
  107. * @param string $avatar 头像地址
  108. * @param string $username 用户名
  109. * @param string $nickname 昵称
  110. * @param string $bio 个人简介
  111. */
  112. public function profile()
  113. {
  114. $user = $this->auth->getUser();
  115. $username = $this->request->post('username');
  116. $nickname = $this->request->post('nickname');
  117. $bio = $this->request->post('bio');
  118. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  119. if ($username) {
  120. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  121. if ($exists) {
  122. $this->error(__('Username already exists'));
  123. }
  124. $user->username = $username;
  125. }
  126. if ($nickname) {
  127. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  128. if ($exists) {
  129. $this->error(__('Nickname already exists'));
  130. }
  131. $user->nickname = $nickname;
  132. }
  133. $user->bio = $bio;
  134. $user->avatar = $avatar;
  135. $user->save();
  136. $this->success();
  137. }
  138. /**
  139. * 修改手机号
  140. *
  141. * @ApiMethod (POST)
  142. * @param string $mobile 手机号
  143. * @param string $captcha 验证码
  144. */
  145. public function changemobile()
  146. {
  147. $user = $this->auth->getUser();
  148. $mobile = $this->request->post('mobile');
  149. $captcha = $this->request->post('captcha');
  150. if (!$mobile || !$captcha) {
  151. $this->error(__('Invalid parameters'));
  152. }
  153. if (!Validate::regex($mobile, "^1\d{10}$")) {
  154. $this->error(__('Mobile is incorrect'));
  155. }
  156. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  157. $this->error(__('Mobile already exists'));
  158. }
  159. $result = Sms::check($mobile, $captcha, 'changemobile');
  160. if (!$result) {
  161. $this->error(__('Captcha is incorrect'));
  162. }
  163. $verification = $user->verification;
  164. $verification->mobile = 1;
  165. $user->verification = $verification;
  166. $user->mobile = $mobile;
  167. $user->save();
  168. Sms::flush($mobile, 'changemobile');
  169. $this->success();
  170. }
  171. /**
  172. * 重置密码
  173. *
  174. * @ApiMethod (POST)
  175. * @param string $mobile 手机号
  176. * @param string $newpassword 新密码
  177. * @param string $captcha 验证码
  178. */
  179. public function resetpwd()
  180. {
  181. $type = $this->request->post("type", "mobile");
  182. $mobile = $this->request->post("mobile");
  183. $email = $this->request->post("email");
  184. $newpassword = $this->request->post("newpassword");
  185. $captcha = $this->request->post("captcha");
  186. if (!$newpassword || !$captcha) {
  187. $this->error(__('Invalid parameters'));
  188. }
  189. //验证Token
  190. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  191. $this->error(__('Password must be 6 to 30 characters'));
  192. }
  193. if ($type == 'mobile') {
  194. if (!Validate::regex($mobile, "^1\d{10}$")) {
  195. $this->error(__('Mobile is incorrect'));
  196. }
  197. $user = \app\common\model\User::getByMobile($mobile);
  198. if (!$user) {
  199. $this->error(__('User not found'));
  200. }
  201. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  202. if (!$ret) {
  203. $this->error(__('Captcha is incorrect'));
  204. }
  205. Sms::flush($mobile, 'resetpwd');
  206. } else {
  207. if (!Validate::is($email, "email")) {
  208. $this->error(__('Email is incorrect'));
  209. }
  210. $user = \app\common\model\User::getByEmail($email);
  211. if (!$user) {
  212. $this->error(__('User not found'));
  213. }
  214. $ret = Ems::check($email, $captcha, 'resetpwd');
  215. if (!$ret) {
  216. $this->error(__('Captcha is incorrect'));
  217. }
  218. Ems::flush($email, 'resetpwd');
  219. }
  220. //模拟一次登录
  221. $this->auth->direct($user->id);
  222. $ret = $this->auth->changepwd($newpassword, '', true);
  223. if ($ret) {
  224. $this->success(__('Reset password successful'));
  225. } else {
  226. $this->error($this->auth->getError());
  227. }
  228. }
  229. ///////////////////////////////////////////
  230. /**
  231. * 微信小程序登录+注册
  232. */
  233. public function wxmini_login() {
  234. $code = input('code');
  235. if (!$code) {
  236. $this->error(__('Invalid parameters'));
  237. }
  238. /*$config = config('wxMiniProgram');
  239. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  240. $openidInfo = $this->getJson($getopenid);
  241. if(!isset($openidInfo['openid'])) {
  242. $this->error('用户openid获取失败',$openidInfo);
  243. }
  244. $openid = $openidInfo['openid'];
  245. if (!$openid) {
  246. $this->error('用户openid获取失败');
  247. }*/
  248. $openid = 'asdf';
  249. //用户信息
  250. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  251. if($userInfo) {
  252. if ($userInfo['status'] != 1) {
  253. $this->error(__('Account is locked'));
  254. }
  255. //如果已经有账号则直接登录
  256. $res = $this->auth->direct($userInfo['id']);
  257. } else {
  258. // 注册
  259. /*$extend = [
  260. 'mini_openid' => $openid,
  261. ];*/
  262. $res = $this->auth->openid_register($openid);
  263. if (!$res) {
  264. $this->error($this->auth->getError());
  265. }
  266. }
  267. if($res) {
  268. $this->success("登录成功!",$this->auth->getUserinfo());
  269. } else {
  270. $this->error("登录失败!");
  271. }
  272. }
  273. /**
  274. * json 请求
  275. * @param $url
  276. * @return mixed
  277. */
  278. private function getJson($url){
  279. $ch = curl_init();
  280. curl_setopt($ch, CURLOPT_URL, $url);
  281. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  282. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  283. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  284. $output = curl_exec($ch);
  285. curl_close($ch);
  286. return json_decode($output, true);
  287. }
  288. //用户详细资料
  289. public function getUserinfo(){
  290. $info = $this->auth->getUserinfo();
  291. $this->success(__('success'),$info);
  292. }
  293. }