吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 621|回复: 4
收起左侧

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

[复制链接]
netusb 发表于 2021-11-28 19:55
[PHP] 纯文本查看 复制代码
问题遇到的现象和发生背景
 
//=============================以下是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.
     *
     * [url=home.php?mod=space&uid=718080]@access[/url] public
     * [url=home.php?mod=space&uid=155549]@Return[/url] 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"
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

chinadd 发表于 2021-11-29 01:53
[JavaScript] 纯文本查看 复制代码
//用JSON封装返回数据
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"
},
//增加到返回体中
"data"=data
//接收返回体res,取出数据
res.data
chengxuyuan01 发表于 2021-11-29 08:14
你这个是没有把你的返回data作为参数返回到响应中吧,上楼是正解

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
netusb + 1 + 1 谢谢@Thanks!

查看全部评分

stone-liu 发表于 2021-11-29 08:30
ky_wei 发表于 2021-11-29 08:54
login()返回的就是你要的数据,它是数组类型的,调用这个函数接收之后将数组类型转成json格式返回输出就行了,楼上正解
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 18:54

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表