UserController.php 7.4 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wuwu <15093565100@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\portal\model\MemberModel;
use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Request;
use think\Loader;
use think\Config;
use think\captcha\Captcha;
use think\Url;
use SmsDemo;
//use think\Route;
/**
 * @title 用户接口
 * @description 接口说明
 * @group 接口分组
 */

class UserController extends CommonController
{
    protected $postModel;
    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

//   验证码图片
    function getImgUrl($id = "")
    {
        \think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
        \think\Validate::extend('captcha', function ($value, $id = "") {
            return captcha_check($value, $id, (array)\think\Config::get('captcha'));
        });
        $middle_url =  \think\Url::build('/captcha/new' . ($id ? "/{$id}" : ''));
        $rand = str_replace(".","",substr(microtime(true),-5)).rand(1000,9999);
        $imgUrl = $middle_url."&time=".$rand;
        return $imgUrl;
    }
//    获取短信验证码
    public function getSmsResult(Request $request){
        //            短信验证码
            $tel = $request->param('tel');
            $code = 'SMS_137416617';
            $modelVal = rand(1000,9999);
            $sendResult = $this->sendLogin($tel,$code,$modelVal);
            if(($sendResult->Code) != 'OK'){
                $this->apiResponse('0','注册失败');
            }else{
                $_SESSION('code',$modelVal);
                $_SESSION('tel',$tel);
            }
    }

    /**
     * @title  用户注册
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/User/join
     * @method POST
     * @param name:name type:int require:1 default: other: desc:姓名
     * @param name:tel type:int require:1 default: other: desc:手机号
     * @param name:password type:int require:1 default: other: desc:密码
     * @param name:sure_password type:int require:1 default: other: desc:确认密码
     */
    public function join(Request $request)
    {
        if($request->Post()){
//            验证
            $validate = Loader::validate('User');
            if(!$validate->scene('add')->check($_POST)){
                return json(array('code'=>0,'msg'=>$validate->getError()));
            }
            $data['password'] = $this->md5($_POST['password']);
            $sure_password = $this->md5($_POST['sure_password']);
//            确认密码
            if($data['password'] != $sure_password){
                $this->apiResponse('0','两次密码不一致');
            }
            //            密码

//            短信验证码
//            $tel = $request->param('tel');
//            $code = 'SMS_137416617';
//            $modelVal = rand(1000,9999);
//            $sendResult = $this->sendLogin($tel,$code,$modelVal);
//            if(($sendResult->Code) != 'OK'){
//                $this->apiResponse('0','注册失败');
//            }
            $data['tel'] = $_POST['tel'];
            $code = $request->param('code');
            if($data['tel'] != $_SESSION['tel']){
                $this->apiResponse('0','验证码错误,请重新获取');
            }else{
                if($code != $_SESSION['code']){
                    $this->apiResponse('0','验证码错误,请重新获取');
                }
            }
            unset($_SESSION['code']);
            $data['name'] = $_POST['name'];

//            判断手机号是否已注册
            $user = new MemberModel();
            $where_user['tel'] = $data['tel'];
            $is_isset = $user->where($where_user)->find();
            if($is_isset){
                $this->apiResponse('0','您已注册过,请直接登录');
            }
            $str = rand(1000,9999).time().rand(100,999);
            $data['token'] = $this->md5($str);
            $add = $user->allowField(true)->save($data);
            if($add){
                $this->apiResponse('1','注册成功');
            }else{
                $this->apiResponse('0','注册失败');
            }

        }else{
//            服务协议
//            $service = new PostService();
//            $list = $service->publishedArticle(1,1)->toArray();
            $where_pro['status'] = 1;
            $where_pro['type'] = 1;
            $list = Db::name('Protocol')->where($where_pro)->order("update_time desc")->field("title,content")->find();
//              推荐人(未完)


            if($list){
                $this->apiResponse('1','成功',$list);
            }else{
                $this->apiResponse('0','暂无内容');
            }

        }
    }


//    登录
    public function login(Request $request){
        if($request->post()){
//            登录验证
//            判空
            $tel = $request->param('tel');
            $password = $request->param('password');
            $true = $request->param('true');
            $token = $request->param('token');
            if(empty($tel)){
                $this->apiResponse('0','手机号不能为空');
            }else if(empty($password)){
                $this->apiResponse('0','密码不能为空');
            }else if(empty($true)){
                $this->apiResponse('0','验证码不能为空');
            }
//            验证
            $where_member['tel'] = $tel;
            $where_member['password'] = $password;
//            $where_member['token'] = $token;
            $member = Db::name('Member')->where($where_member)->find();
            if($member){
                if($member['token'] != $token){
                    $this->apiResponse('0','登录失败');
                }
                if($member['update_time'] >= (time()+604800)){
                    $this->apiResponse('0','请重新登录');
                }else{
                    $this->apiResponse('1','登录成功');
                }
            }else{
                $this->apiResponse('0','您输入的账号或密码不正确');
            }


        }else{
//            返回验证码图片

        }
    }



//    退出登录
    public function outLogin(Request $request){
//        重置token
        $str = rand('1000,9999').time().rand('100,999');
        $change['token'] = $this->md5($str);
        $where_member['id'] = $request->param('user_id');
        $where_member['token'] = $request->param('token');
        $model = new MemberModel();
        $member = $model->where($where_member)->find();
        if($member){
            $change['id'] = $request->param('user_id');
            $update = $model->isUpdate(true)->allowField(true)->save($change);
            if($update){
                unset($_SESSION['user_id']);
                $this->apiResponse('1','退出成功');
            }else{
                $this->apiResponse('0','退出失败');
            }
        }else{
            $this->apiResponse('0','用户信息错误');
        }
    }
//  修改密码

}