User.php 3.4 KB

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