User.php 10.2 KB
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\mobile\controller\Sms;
use app\mobile\model\Company;
use app\mobile\model\Agreement;
use app\mobile\model\Exam;

/**
 * 个人中心接口
 */
class User extends Api
{
	protected $noNeedLogin = ['registerUser','agreementUser','registerCompany','agreementCompany','login','resetpwd','exam','noLogin'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 注册-个人
     * @param string $mobile   手机号
     * @param string $code   验证码
     * @param string $password 密码
     */
    public function registerUser()
    {
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        $password = $this->request->param('password');

        empty($mobile) && $this->error('请输入手机号');
        empty($code) && $this->error('请输入验证码');
        empty($password) && $this->error('请输入密码');
        !Validate::regex($mobile, "^1\d{10}$") && $this->error('手机号格式不正确');
        $ret = Sms::check($mobile, $code, 'register');
        !$ret && $this->error('验证码不正确');

        $ret = $this->auth->register($username='', $password, $email='', $mobile, []);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success('注册成功', $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * @ApiTitle    (注册协议-个人)
     * @ApiSummary  (注册协议-个人)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
		"code": 1,
		"msg": "成功",
		"time": "1599017563",
		"data": "用户协议内容" //协议内容
	})
     */
    public function agreementUser()
    {
        $content = Db::name('mobile_config')->where('id',1)->value('user_agreement');
        $this->success('成功', $content);
    }

    /**
     * 注册-公司
     * @param string $name   公司名称
     * @param string $address   公司地址
     * @param string $license   公司执照
     * @param string $legal_person   法人名称
     * @param string $mobile   手机号
     * @param string $code   验证码
     * @param string $password 密码
     */
    public function registerCompany()
    {
    	$name = $this->request->param('name');
        $address = $this->request->param('address');
        $license = $this->request->param('license');
        $legal_person = $this->request->param('legal_person');
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        $password = $this->request->param('password');

        empty($name) && $this->error('请输入公司名称');
        empty($address) && $this->error('请输入地址');
        empty($license) && $this->error('请上传执照');
        empty($legal_person) && $this->error('请输入法人名称');
        empty($mobile) && $this->error('请输入手机号');
        empty($code) && $this->error('请输入验证码');
        empty($password) && $this->error('请输入密码');
        !Validate::regex($mobile, "^1\d{10}$") && $this->error('手机号格式不正确');
        $ret = Sms::check($mobile, $code, 'register');
        !$ret && $this->error('验证码不正确');

        $ret = $this->auth->register($username='', $password, $email='', $mobile, ['group_id'=>1]);
        if ($ret) {
        	Company::create([
                'user_id' => $this->auth->id,
                'name' => $name,
                'address' => $address,
                'license' => $license,
                'legal_person' => $legal_person
            ],true);
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success('注册成功', $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * @ApiTitle    (注册协议-公司)
     * @ApiSummary  (注册协议-公司)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
		"code": 1,
		"msg": "成功",
		"time": "1599017563",
		"data": "用户协议内容" //协议内容
	})
     */
    public function agreementCompany()
    {
        $content = Db::name('mobile_config')->where('id',1)->value('company_agreement');
        $this->success('成功', $content);
    }

    /**
     * 登录
     * @param string $mobile  手机号
     * @param string $password 密码
     */
    public function login()
    {
        $mobile = $this->request->param('mobile');
        $password = $this->request->param('password');

        empty($mobile) && $this->error('请输入手机号');
        empty($password) && $this->error('请输入密码');
        !Validate::regex($mobile, "^1\d{10}$") && $this->error('手机号格式不正确');

        $ret = $this->auth->login($mobile, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success('登录成功', $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 忘记密码
     *
     * @param string $mobile      手机号
     * @param string $newpassword 新密码
     * @param string $captcha     验证码
     */
    public function resetpwd()
    {
        $mobile = $this->request->request("mobile");
        $code = $this->request->request("code");
        $newpassword = $this->request->request("newpassword");

        empty($mobile) && $this->error('请输入手机号');
        empty($newpassword) && $this->error('请输入密码');
        !Validate::regex($mobile, "^1\d{10}$") && $this->error('手机号格式不正确');
        $user = \app\common\model\User::getByMobile($mobile);
        !$user && $this->error('用户不存在');
        $ret = Sms::check($mobile, $code, 'resetpwd');
        !$ret && $this->error('验证码不正确');

        Smslib::flush($mobile, 'resetpwd');
        //模拟一次登录
        $this->auth->direct($user->id);
        $ret = $this->auth->changepwd($newpassword, '', true);
        if ($ret) {
            $this->success('重置密码成功');
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * @ApiTitle    (选择参加的考试)
     * @ApiSummary  (选择参加的考试)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
		"code": 1,
		"msg": "成功",
		"time": "1599018234",
		"data": [{
			"id": 1, //一级ID
			"pid": 0,
			"name": "建筑工程", //一级名称
			"nickname": "", //一级昵称
			"children": [{ //二级
				"id": 3, //二级ID
				"pid": 1, //父ID
				"name": "一级建造师", //二级名称
				"nickname": "" //二级昵称
			}]
		}]
	})
     */
    public function exam()
    {
        $list = Exam::where('pid',0)->field('id,pid,name,nickname')->select();
        foreach($list as &$v){
        	$v['children'] = Exam::where('pid',$v['id'])->field('id,pid,name,nickname')->select();
        }
        $this->success('成功', $list);
    }

    /**
     * @ApiTitle    (暂不登录提示)
     * @ApiSummary  (暂不登录提示)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
		"code": 1,
		"msg": "成功",
		"time": "1599017563",
		"data": "暂不登录提示内容" //暂不登录提示内容
	})
     */
    public function noLogin()
    {
        $content = Db::name('mobile_config')->where('id',1)->value('no_login');
        $this->success('成功', $content);
    }

    /**
     * @ApiTitle    (我的-首页)
     * @ApiSummary  (我的-首页)
     * @ApiMethod   (POST)
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1599017563",
        "data": "暂不登录提示内容" //暂不登录提示内容
    })
     */
    public function index()
    {
        $user = $this->auth->getUser();
        $this->success('成功', $content);
    }

    /**
     * 修改会员个人信息
     *
     * @param string $image   头像地址
     * @param string $username 用户名
     * @param string $nickname 真实姓名
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $username = $this->request->param('username');
        $nickname = $this->request->param('nickname');
        $image = $this->request->param('image', '', 'trim,strip_tags,htmlspecialchars');
        if($username || $nickname || $image) {
            if ($username) {
                $user->username = $username;
            }
            if ($nickname) {
                $user->nickname = $nickname;
            }
            if ($image) {
                $user->image = $image;
            }
            $user->save();
        }
        $this->success();
    }

    /**
     * 修改手机号-第一步
     * @param string $code 验证码
     */
    public function changemobile1()
    {
        $user = $this->auth->getUser();
        $code = $this->request->param('code');
        !$code && $this->error(__('请输入验证码'));
        $ret = Sms::check($user['mobile'], $code, 'changemobile1');
        !$ret && $this->error('验证码不正确');
        Smslib::flush($user['mobile'], 'changemobile1');
        $this->success();
    }

    /**
     * 修改手机号-第二步
     *
     * @param string $mobile 新手机号
     * @param string $code 验证码
     */
    public function changemobile2()
    {
        $user = $this->auth->getUser();
        $mobile = $this->request->param('mobile');
        $code = $this->request->param('code');
        if (!$mobile || !$code) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Mobile already exists'));
        }
        $result = Sms::check($mobile, $code, 'changemobile2');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->mobile = 1;
        $user->verification = $verification;
        $user->mobile = $mobile;
        $user->save();

        Smslib::flush($mobile, 'changemobile2');
        $this->success();
    }
}