ajax只返回了登陆成功信息,登陆成功后得信息如何返回

晓兰 发布于 2021/11/28 19:39
阅读 357
收藏 0
问题遇到的现象和发生背景
 
//=============================以下是login.ctr.php文件==========================================================
 
class Login extends Ctrl {
 
    protected function c_init($param = array()) {
        parent::c_init();
 
        $this->mdl_login    = Loader::model('Login'); //调用了下边login.mdl.php
    }
 
 
    function login() {
        $_mix_init = $this->init();
 
        if ($_mix_init !== true) {
            return $this->fetchJson($_mix_init['msg'], $_mix_init['rcode']);
        }
 
        if (!$this->isPost) {
            return $this->fetchJson('Access denied', '', 405);
        }
        $_arr_inputSubmit = $this->mdl_login->inputSubmit($this->decryptRow);
 
        if ($_arr_inputSubmit['rcode'] != 'y010201') {
            return $this->fetchJson($_arr_inputSubmit['msg'], $_arr_inputSubmit['rcode']);
        }
 
        $_arr_userRow  = $this->mdl_login->read($_arr_inputSubmit['user_str'], $_arr_inputSubmit['user_by']);
 
        if ($_arr_userRow['rcode'] != 'y010102') {
            return $this->fetchJson($_arr_userRow['msg'], $_arr_userRow['rcode']);
        }
 
        if ($_arr_userRow['user_status'] == 'disabled') {
            return $this->fetchJson('User is disabled', 'x010402');
        }
 
        $_str_crypt = Crypt::crypt($_arr_inputSubmit['user_pass'], $_arr_userRow['user_rand'], true);
 
        if ($_str_crypt != $_arr_userRow['user_pass']) {
            return $this->fetchJson('Password is incorrect', 'x010201');
        }
 
        $this->mdl_login->inputSubmit['user_id'] = $_arr_userRow['user_id'];
 
        $_arr_loginResult = $this->mdl_login->login();
 
        if ($_arr_loginResult['rcode'] != 'y010103') {
            return $this->fetchJson($_arr_loginResult['msg'], $_arr_loginResult['rcode']);
        }
 
        $_arr_loginResult['timestamp'] = GK_NOW;
 
        $_str_src       = Arrays::toJson($_arr_loginResult);
 
        $_str_sign      = Sign::make($_str_src, $this->appRow['app_key'] . $this->appRow['app_secret']);
 
        $_str_encrypt   = Crypt::encrypt($_str_src, $this->appRow['app_key'], $this->appRow['app_secret']);
 
        if ($_str_encrypt === false) {
            $_str_error = Crypt::getError();
            return $this->fetchJson($_str_error, 'x050405', 200, 'api.common');
        }
 
        $_arr_data = array(
            'rcode' => $_arr_loginResult['rcode'],
            'msg'   => $this->obj_lang->get($_arr_loginResult['msg']),
            'code'  => $_str_encrypt,
            'sign'  => $_str_sign,
        );
 
        $_arr_tpl = array_replace_recursive($this->version, $_arr_data);
 
        return $this->json($_arr_tpl);
    }
}
 
 
 
 
//=========================================以下是login.mdl.php文件=====================================================
 
class Login extends User {
 
    public $inputSubmit;
 
    protected $table = 'user';
 
    function login() {
        $_arr_userData = array(
            'user_time_login'   => GK_NOW,
        );
 
        if (isset($this->inputSubmit['user_ip']) && !Func::isEmpty($this->inputSubmit['user_ip'])) {
            $_str_userIp = $this->inputSubmit['user_ip'];
        } else {
            $_str_userIp = $this->obj_request->ip();
        }
        $_arr_userData['user_ip'] = $_str_userIp;
 
        $_arr_userRow = $this->read($this->inputSubmit['user_id']);
 
        if ($_arr_userRow['user_access_expire'] <= GK_NOW) { //如果访问口令过期
            $_str_accessToken   = Func::rand();
            $_tm_accessExpire   = GK_NOW + $this->configBase['access_expire'] * GK_MINUTE;
 
            $_arr_userData['user_access_token']     = $_str_accessToken;
            $_arr_userData['user_access_expire']    = $_tm_accessExpire;
        } else {
            $_str_accessToken   = $_arr_userRow['user_access_token'];
            $_tm_accessExpire   = $_arr_userRow['user_access_expire'];
        }
 
        if ($_arr_userRow['user_refresh_expire'] <= GK_NOW) { //如果刷新口令过期
            $_str_refreshToken  = Func::rand();
            $_tm_refreshExpire  = GK_NOW + $this->configBase['refresh_expire'] * GK_DAY;
 
            $_arr_userData['user_refresh_token']    = $_str_refreshToken;
            $_arr_userData['user_refresh_expire']   = $_tm_refreshExpire;
        } else {
            $_str_refreshToken  = $_arr_userRow['user_refresh_token'];
            $_tm_refreshExpire  = $_arr_userRow['user_refresh_expire'];
        }
 
        $_num_count     = $this->where('user_id', '=', $_arr_userRow['user_id'])->update($_arr_userData);
 
        if ($_num_count > 0) {
            $_str_rcode = 'y010103'; //更新成功
            $_str_msg   = 'Login successful';
        } else {
            $_str_rcode = 'x010103';
            $_str_msg   = 'Login failed';
        }
 
        return array(
            'user_id'               => $_arr_userRow['user_id'],
            'user_name'             => $_arr_userRow['user_name'],
            'user_status'           => $_arr_userRow['user_status'],
            'user_ip'               => $_str_userIp,
            'user_time_login'       => GK_NOW,
            'user_access_token'     => Crypt::crypt($_str_accessToken, $_arr_userRow['user_name']),
            'user_access_expire'    => $_tm_accessExpire,
            'user_refresh_token'    => Crypt::crypt($_str_refreshToken, $_arr_userRow['user_name']),
            'user_refresh_expire'   => $_tm_refreshExpire,
            'rcode'                 => $_str_rcode, //成功
            'msg'                   => $_str_msg,
        );
    }
 
 
 
    /** api 登录表单验证
     * inputSubmit_api function.
     *
     * @access public
     * @return void
     */
    function inputSubmit($arr_data) {
        $_arr_inputParam = array(
            'user_str'  => array('txt', ''),
            'user_by'   => array('txt', ''),
            'user_pass' => array('txt', ''),
            'user_ip'   => array('txt', ''),
            'timestamp' => array('int', 0),
        );
 
        $_arr_inputSubmit  = $this->obj_request->fillParam($arr_data, $_arr_inputParam);
 
        if (isset($arr_data['user_id']) && $arr_data['user_id'] > 0) {
            $_arr_inputSubmit['user_by']  = 'user_id';
            $_arr_inputSubmit['user_str'] = $arr_data['user_id'];
        } else if (isset($arr_data['user_name']) && !Func::isEmpty($arr_data['user_name'])) {
            $_arr_inputSubmit['user_by']  = 'user_name';
            $_arr_inputSubmit['user_str'] = $arr_data['user_name'];
        } else if (isset($arr_data['user_mail']) && !Func::isEmpty($arr_data['user_mail'])) {
            $_arr_inputSubmit['user_by']  = 'user_mail';
            $_arr_inputSubmit['user_str'] = $arr_data['user_mail'];
        }
 
        $_mix_vld = $this->validate($_arr_inputSubmit, '', 'login');
 
        if ($_mix_vld !== true) {
            return array(
                'rcode' => 'x010201',
                'msg'   => end($_mix_vld),
            );
        }
 
        if ($_arr_inputSubmit['user_by'] == 'user_mail') {
            $_arr_search  = array(
                'user_mail' => $_arr_inputSubmit['user_str'],
            );
 
            $_num_userCount = $this->count($_arr_search);
 
            if ($_num_userCount > 0) {
                return array(
                    'rcode' => 'x010201',
                    'msg'   => 'There are duplicate emails in the system',
                );
            }
        }
 
        $_arr_inputSubmit['rcode'] = 'y010201';
 
        $this->inputSubmit = $_arr_inputSubmit;
 
        return $_arr_inputSubmit;
    }
}
 
 
######
 //==========================返回的结果应该是这样的=========================
 
{
    "user_id": "1",
    "user_name": "baigo",
    "user_mail": "baigo@baigo.net",
    "user_nick": "nickname",
    "user_status": "wait",
    "user_time": "1550198497",
    "user_time_login": "1550198497",
    "user_access_token": "0VHBRPQUICBKGVWXTBDQBHVEPWK",
    "user_access_expire": "1550198497",
    "user_refresh_token": "0VHBRPQUICBKGVWXTBDQBHVEPWK",
    "user_refresh_expire": "1550198497",
    "timestamp": "1550198497"
}
 
//======================可实际返回来的是这样的=============================
 
{
    "code": "CSMEIFh7AHYBOFIlXQwAaQE0UXENawF2WUxXUQNFVD4Ac1R%2BUSUFdQgnBmYMcARb", //加密参数
    "sign": "0VHBRPQUICBKGVWXTBDQBHVEPWK", //签名
    "rcode": "y010102" //返回代码
    "msg": "登录成功",
    "prd_sso_ver": "1.1.1", //SSO 版本号
    "prd_sso_pub": 20150923, //SSO 版本发布时间
}
 
 
 
```javascrip
// 前端使用的ajax
<script>
$(document).ready.(function(){
$.aja({
url:'./encrpty.php',
data:'username=admin&password=admin'
type:'post',
sync:'flase',
success:function(s){
var eme = s ;
$.ajax({
url:'./login/login',
data:eme,
type:'post',
sync:'flase',
dataType:'json',
success:function(data){
console.log(data)
}
})
}
})
})
我想要返回的是这样的结果

 
{
    "user_id": "1",
    "user_name": "baigo",
    "user_mail": "baigo@baigo.net",
    "user_nick": "nickname",
    "user_status": "wait",
    "user_time": "1550198497",
    "user_time_login": "1550198497",
    "user_access_token": "0VHBRPQUICBKGVWXTBDQBHVEPWK",
    "user_access_expire": "1550198497",
    "user_refresh_token": "0VHBRPQUICBKGVWXTBDQBHVEPWK",
    "user_refresh_expire": "1550198497",
    "timestamp": "1550198497"
}

 

加载中
0
叫我蝴蝶吧
叫我蝴蝶吧

改返回数据的代码逻辑

晓兰
晓兰
蟹蟹,调试半天,发现把数据给加密了
0
ArchitectureMaster
ArchitectureMaster

不光是加密的问题:

$_str_encrypt = Crypt::encrypt($_str_src, $this->appRow['app_key'], $this->appRow['app_secret']);

你要返回的是$_arr_userRow ,而你为什么在login这个action最后返回的是$_arr_data。题主返回的就是$_arr_data。而如题主所期待的$_arr_userRow并没有返回呀,如果题主需要返回直接在login最后返回$_arr_userRow就可以了。

 $_arr_data = array(
            'rcode' => $_arr_loginResult['rcode'],
            'msg'   => $this->obj_lang->get($_arr_loginResult['msg']),
            'code'  => $_str_encrypt,
            'sign'  => $_str_sign,
        );
 
        $_arr_tpl = array_replace_recursive($this->version, $_arr_data);
 
        return $this->json($_arr_tpl);

 

ArchitectureMaster
ArchitectureMaster
当然题主可以直接在array里加一个'data'=>$_arr_userRow 也是可以的。
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部