User.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Sms;
  5. use think\Validate;
  6. use think\Db;
  7. /**
  8. * 会员接口
  9. */
  10. class User extends Api
  11. {
  12. protected $noNeedLogin = ['mobilelogin'];
  13. protected $noNeedRight = '*';
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. }
  18. /**
  19. * 手机验证码登录
  20. *
  21. * @ApiMethod (POST)
  22. * @param string $mobile 手机号
  23. * @param string $captcha 验证码
  24. */
  25. public function mobilelogin()
  26. {
  27. $mobile = $this->request->post('mobile');
  28. $captcha = $this->request->post('captcha');
  29. if (!$mobile || !$captcha) {
  30. $this->error(__('Invalid parameters'));
  31. }
  32. if (!Validate::regex($mobile, "^1\d{10}$")) {
  33. $this->error(__('Mobile is incorrect'));
  34. }
  35. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  36. $this->error(__('Captcha is incorrect'));
  37. }
  38. $user = \app\common\model\User::getByMobile($mobile);
  39. if ($user) {
  40. if ($user->status != 1) {
  41. $this->error(__('Account is locked'));
  42. }
  43. //如果已经有账号则直接登录
  44. $ret = $this->auth->direct($user->id);
  45. } else {
  46. $this->error('不存在的用户');
  47. }
  48. if ($ret) {
  49. Sms::flush($mobile, 'mobilelogin');
  50. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  51. } else {
  52. $this->error($this->auth->getError());
  53. }
  54. }
  55. /**
  56. * 退出登录
  57. * @ApiMethod (POST)
  58. */
  59. public function logout()
  60. {
  61. if (!$this->request->isPost()) {
  62. $this->error(__('Invalid parameters'));
  63. }
  64. $this->auth->logout();
  65. $this->success(__('Logout successful'));
  66. }
  67. //用户详细资料
  68. public function getuserinfo(){
  69. $info = $this->auth->getUserinfo();
  70. $this->success(__('success'),$info);
  71. }
  72. /**
  73. * 修改会员个人信息
  74. *
  75. * @ApiMethod (POST)
  76. * @param string $avatar 头像地址
  77. * @param string $username 用户名
  78. * @param string $nickname 昵称
  79. * @param string $bio 个人简介
  80. */
  81. public function profile()
  82. {
  83. $field_array = ['avatar','nickname','contactname','address'];
  84. $data = [];
  85. foreach($field_array as $key => $field){
  86. //前端传不了post,改了
  87. /*if(!request()->has($field,'post')){
  88. continue;
  89. }*/
  90. if(!input('?'.$field)){
  91. continue;
  92. }
  93. $newone = input($field);
  94. if($field == 'avatar'){
  95. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  96. }
  97. $data[$field] = $newone;
  98. }
  99. if(empty($data)){
  100. $this->success();
  101. }
  102. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  103. if($update_rs === false){
  104. $this->error('修改资料失败');
  105. }
  106. $this->success();
  107. }
  108. }