LoginController.php 4.5 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use think\Validate;
use think\Db;
use cmf\controller\HomeBaseController;
use app\user\model\UserModel;

class LoginController extends HomeBaseController
{

    /**
     * 登录
     */
    public function index()
    {
        //session('user.id','10');
        $ress = Db::name('user')->where(['openid'=>session('openid')])->find();
        if($ress && $ress['mobile'] != '' ){
            //已支付学员
            if($ress['user_type'] == '2'){
                if($ress['student_type'] == '1' and $ress['delete_time'] == '0'){
                    session('user.id',$ress['id']);
                    return redirect($this->request->root() . '/user/profile/percenter');
                }
            }else{
                session('user.id',$ress['id']);
                return redirect($this->request->root() . '/user/profile/percenter');
            }

        }

        $redirect = $this->request->post("redirect");
        if (empty($redirect)) {
            $redirect = $this->request->server('HTTP_REFERER');
        } else {
            $redirect = base64_decode($redirect);
        }
        session('login_http_referer', $redirect);

        if (cmf_is_user_login()) { //已经登录时直接跳到首页
            return redirect($this->request->root() . '/user/profile/percenter');
        } else {
            return $this->fetch(":login");
        }
    }

    /**
     * 登录验证提交
     */
    public function do_login()
    {
        if ($this->request->isPost()) {
            $validate = new Validate([
                'sms_code'  => 'require',
                'mobile'    => 'require',
            ]);
            $validate->message([
                'mobile.require' => '用户名不能为空',
                'sms_code.require'  => '验证码不能为空',
            ]);
            //验证参数
            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            //验证验证码
            $sres = cmf_check_verification_code($data['mobile'],$data['sms_code']);
            if($sres!=''){
                return json(['code'=>'0','msg'=>$sres]);
            }
            //登陆验证
            $mobile = $data['mobile'];
            $res = Db::name('user')->where(['mobile'=>$mobile])->find();

            if($res){

                //学员未支付 学员被删除 不可以登录
                if (($res['user_type'] == '2' and $res['student_type'] != '1')  or $res['delete_time'] != '0'){
                    return json(['code'=>'0','msg'=>'抱歉,此用户没有权限登录']);
                }

                //后台添加用户,微信登陆后合并数据
                if($res['add_type'] == '2'){
                    $op_tel_info = Db::name('user')->where(['openid'=>session('openid'),'mobile'=>$mobile])->find();
                    if (!$op_tel_info){
                        $op_info = Db::name('user')->where(['openid'=>session('openid')])->find();
                        if($op_info){
                            $dataup['openid'] = session('openid');
                            $dataup['province'] = $op_info['province'];
                            $dataup['city'] = $op_info['city'];
                            $dataup['country'] = $op_info['country'];
                            Db::name('user')->where(['mobile'=>$mobile])->update($dataup);
                            Db::name('user')->where(['id'=>$op_info['id']])->delete();
                        }
                    }
                //学员成功支付后才可以登陆
                }
                session('user.id',$res['id']);
                return json(['code'=>'1','msg'=>'登陆成功']);
            }else{
                return json(['code'=>'0','msg'=>'抱歉,此手机号没有权限登录']);
            }
        } else {
            return json(['code'=>'0','msg'=>'非法请求']);
        }
    }




}