123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Sms;
- use think\Validate;
- use think\Db;
- use app\common\library\Tenim;
- /**
- * 会员接口
- */
- class User extends Api
- {
- protected $noNeedLogin = ['mobilelogin'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- }
- /**
- * 手机验证码登录
- *
- * @ApiMethod (POST)
- * @param string $mobile 手机号
- * @param string $captcha 验证码
- */
- public function mobilelogin()
- {
- $mobile = $this->request->post('mobile');
- $captcha = $this->request->post('captcha');
- if (!$mobile || !$captcha) {
- $this->error(__('Invalid parameters'));
- }
- if (!Validate::regex($mobile, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
- $this->error(__('Captcha is incorrect'));
- }
- $user = \app\common\model\User::getByMobile($mobile);
- if ($user) {
- if ($user->status != 1) {
- $this->error(__('Account is locked'));
- }
- //如果已经有账号则直接登录
- $ret = $this->auth->direct($user->id);
- } else {
- $this->error('不存在的用户');
- }
- if ($ret) {
- Sms::flush($mobile, 'mobilelogin');
- $this->success(__('Logged in successful'), $this->auth->getUserinfo());
- } else {
- $this->error($this->auth->getError());
- }
- }
- /**
- * 退出登录
- * @ApiMethod (POST)
- */
- public function logout()
- {
- if (!$this->request->isPost()) {
- $this->error(__('Invalid parameters'));
- }
- $this->auth->logout();
- $this->success(__('Logout successful'));
- }
- //用户详细资料
- public function getuserinfo(){
- $info = $this->auth->getUserinfo();
- $this->success(__('success'),$info);
- }
- /**
- * 修改会员个人信息
- *
- * @ApiMethod (POST)
- * @param string $avatar 头像地址
- * @param string $username 用户名
- * @param string $nickname 昵称
- * @param string $bio 个人简介
- */
- public function profile()
- {
- $field_array = ['avatar','nickname','contactname','address'];
- $data = [];
- foreach($field_array as $key => $field){
- //前端传不了post,改了
- /*if(!request()->has($field,'post')){
- continue;
- }*/
- if(!input('?'.$field)){
- continue;
- }
- $newone = input($field);
- if($field == 'avatar'){
- $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
- }
- $data[$field] = $newone;
- }
- if(empty($data)){
- $this->success();
- }
- $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
- if($update_rs === false){
- $this->error('修改资料失败');
- }
- //如果有修改头像或昵称,同步到im
- //user_用户端小程序,master_师傅,kefu_客服
- $tenim = new Tenim();
- $rs = $tenim->useredit('user_'. $this->auth->id, $data['nickname'], $data['avatar']);
- $this->success();
- }
- }
|