User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. $field_array = [
  115. 'avatar','mobile',
  116. ];
  117. $data = [];
  118. foreach($field_array as $key => $field){
  119. //前端传不了post,改了
  120. /*if(!request()->has($field,'post')){
  121. continue;
  122. }*/
  123. if(!input('?'.$field)){
  124. continue;
  125. }
  126. $newone = input($field);
  127. if($field == 'avatar'){
  128. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  129. }
  130. $data[$field] = $newone;
  131. }
  132. //dump($data);
  133. if(empty($data)){
  134. $this->success();
  135. }
  136. //修改用户
  137. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  138. if($update_rs === false){
  139. $this->error('修改资料失败');
  140. }
  141. $this->success();
  142. }
  143. /**
  144. * 修改手机号
  145. *
  146. * @ApiMethod (POST)
  147. * @param string $mobile 手机号
  148. * @param string $captcha 验证码
  149. */
  150. public function changemobile()
  151. {
  152. $user = $this->auth->getUser();
  153. $mobile = $this->request->post('mobile');
  154. $captcha = $this->request->post('captcha');
  155. if (!$mobile || !$captcha) {
  156. $this->error(__('Invalid parameters'));
  157. }
  158. if (!Validate::regex($mobile, "^1\d{10}$")) {
  159. $this->error(__('Mobile is incorrect'));
  160. }
  161. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  162. $this->error(__('Mobile already exists'));
  163. }
  164. $result = Sms::check($mobile, $captcha, 'changemobile');
  165. if (!$result) {
  166. $this->error(__('Captcha is incorrect'));
  167. }
  168. $verification = $user->verification;
  169. $verification->mobile = 1;
  170. $user->verification = $verification;
  171. $user->mobile = $mobile;
  172. $user->save();
  173. Sms::flush($mobile, 'changemobile');
  174. $this->success();
  175. }
  176. /**
  177. * 重置密码
  178. *
  179. * @ApiMethod (POST)
  180. * @param string $mobile 手机号
  181. * @param string $newpassword 新密码
  182. * @param string $captcha 验证码
  183. */
  184. public function resetpwd()
  185. {
  186. $type = $this->request->post("type", "mobile");
  187. $mobile = $this->request->post("mobile");
  188. $email = $this->request->post("email");
  189. $newpassword = $this->request->post("newpassword");
  190. $captcha = $this->request->post("captcha");
  191. if (!$newpassword || !$captcha) {
  192. $this->error(__('Invalid parameters'));
  193. }
  194. //验证Token
  195. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  196. $this->error(__('Password must be 6 to 30 characters'));
  197. }
  198. if ($type == 'mobile') {
  199. if (!Validate::regex($mobile, "^1\d{10}$")) {
  200. $this->error(__('Mobile is incorrect'));
  201. }
  202. $user = \app\common\model\User::getByMobile($mobile);
  203. if (!$user) {
  204. $this->error(__('User not found'));
  205. }
  206. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  207. if (!$ret) {
  208. $this->error(__('Captcha is incorrect'));
  209. }
  210. Sms::flush($mobile, 'resetpwd');
  211. } else {
  212. if (!Validate::is($email, "email")) {
  213. $this->error(__('Email is incorrect'));
  214. }
  215. $user = \app\common\model\User::getByEmail($email);
  216. if (!$user) {
  217. $this->error(__('User not found'));
  218. }
  219. $ret = Ems::check($email, $captcha, 'resetpwd');
  220. if (!$ret) {
  221. $this->error(__('Captcha is incorrect'));
  222. }
  223. Ems::flush($email, 'resetpwd');
  224. }
  225. //模拟一次登录
  226. $this->auth->direct($user->id);
  227. $ret = $this->auth->changepwd($newpassword, '', true);
  228. if ($ret) {
  229. $this->success(__('Reset password successful'));
  230. } else {
  231. $this->error($this->auth->getError());
  232. }
  233. }
  234. ///////////////////////////////////////////
  235. /**
  236. * 微信小程序登录+注册
  237. * code得到注册手机号
  238. */
  239. public function wxregmobile_login(){
  240. $code = input('code');
  241. if (!$code) {
  242. $this->error(__('Invalid parameters'));
  243. }
  244. $config = config('wxMiniProgram');
  245. $wechat = new Wechat($config['appid'],$config['secret']);
  246. $getuserphonenumber = $wechat->getuserphonenumber($code);
  247. if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
  248. $this->error('授权获取手机号失败');
  249. }
  250. $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
  251. $userInfo = Db::name('user')->where('mobile',$mobile)->find();
  252. // 判断用户是否已经存在
  253. if($userInfo) { // 登录
  254. if ($userInfo['status'] != 1) {
  255. $this->error(__('Account is locked'));
  256. }
  257. //如果已经有账号则直接登录
  258. $res = $this->auth->direct($userInfo['id']);
  259. } else {
  260. $res = $this->auth->register('', '', '',$mobile, []);
  261. }
  262. if($res) {
  263. $this->success("登录成功!",$this->auth->getUserinfo());
  264. } else {
  265. $this->error($this->auth->getError());
  266. }
  267. }
  268. /**
  269. * 微信小程序登录+注册
  270. * code得到openid
  271. */
  272. public function wxmini_login() {
  273. $code = input('code');
  274. if (!$code) {
  275. $this->error(__('Invalid parameters'));
  276. }
  277. $config = config('wxMiniProgram');
  278. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  279. $openidInfo = $this->getJson($getopenid);
  280. if(!isset($openidInfo['openid'])) {
  281. $this->error('用户openid获取失败',$openidInfo);
  282. }
  283. $openid = $openidInfo['openid'];
  284. if (!$openid) {
  285. $this->error('用户openid获取失败');
  286. }
  287. //用户信息
  288. $userInfo = Db::name('user')->where(['mini_openid'=>$openid])->find();
  289. if($userInfo) {
  290. if ($userInfo['status'] != 1) {
  291. $this->error(__('Account is locked'));
  292. }
  293. //如果已经有账号则直接登录
  294. $res = $this->auth->direct($userInfo['id']);
  295. } else {
  296. $res = $this->auth->openid_register($openid);
  297. }
  298. if($res) {
  299. $this->success("登录成功!",$this->auth->getUserinfo());
  300. } else {
  301. $this->error($this->auth->getError());
  302. }
  303. }
  304. /**
  305. * json 请求
  306. * @param $url
  307. * @return mixed
  308. */
  309. private function getJson($url){
  310. $ch = curl_init();
  311. curl_setopt($ch, CURLOPT_URL, $url);
  312. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  313. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  314. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  315. $output = curl_exec($ch);
  316. curl_close($ch);
  317. return json_decode($output, true);
  318. }
  319. //用户详细资料
  320. public function getUserinfo(){
  321. $info = $this->auth->getUserinfo();
  322. $this->success(__('success'),$info);
  323. }
  324. }