审查视图

app/user/controller/LoginController.php 4.7 KB
xiaohu authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?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();
28
        if($ress && $ress['mobile'] != '' ){
xiaohu thinkpad authored
29
            //已支付学员
30
            if($ress['user_type'] == '2'){
xiaohu thinkpad authored
31
                if($ress['student_type'] == '1' and $ress['delete_time'] == '0'){
32 33 34 35 36 37 38 39
                    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');
            }
xiaohu authored
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        }

        $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){
86
xiaohu thinkpad authored
87 88
                //学员未支付 学员被删除 不可以登录
                if (($res['user_type'] == '2' and $res['student_type'] != '1')  or $res['delete_time'] != '0'){
89 90
                    return json(['code'=>'0','msg'=>'抱歉,此用户没有权限登录']);
                }
xiaohu thinkpad authored
91
xiaohu authored
92 93 94 95 96 97
                //后台添加用户,微信登陆后合并数据
                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){
98 99 100
                            if($res['user_type'] == '2'){
                             $dataup['headimgurl'] = $op_info['headimgurl'];
                            }
xiaohu authored
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                            $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'=>'非法请求']);
        }
    }




}