|
@@ -9,35 +9,68 @@ class User extends Lyapi_Controller{
|
|
|
parent::__construct(); // 暂时注释,或者使用一个新的不校验的基类
|
|
parent::__construct(); // 暂时注释,或者使用一个新的不校验的基类
|
|
|
// 简便做法:复制 Lyapi_Controller 的部分代码但不调用 _check_api_auth
|
|
// 简便做法:复制 Lyapi_Controller 的部分代码但不调用 _check_api_auth
|
|
|
// $this->load->driver('cache'); // 加载缓存驱动
|
|
// $this->load->driver('cache'); // 加载缓存驱动
|
|
|
|
|
+ $this->load->_model("Model_logic_tools","logic_tools");
|
|
|
|
|
+ $this->load->_model("Model_user",'user');
|
|
|
|
|
+ $this->load->_model("Model_power",'power');
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function login() {
|
|
public function login() {
|
|
|
- $user_id =123;
|
|
|
|
|
- // ... 验证账号密码成功,得到 $user_id
|
|
|
|
|
- // $data = [
|
|
|
|
|
- // 'user_id' => $user_id,
|
|
|
|
|
- // 'created_at' => time()
|
|
|
|
|
- // ];
|
|
|
|
|
- // $this->cache->save("lvhaoceshi1", $data, 100);
|
|
|
|
|
- echo "<pre>";
|
|
|
|
|
- print_r($this->cache->get("lvhaoceshi1"));
|
|
|
|
|
- die;
|
|
|
|
|
- //$this->load->driver('cache'); // 确保缓存可用
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if($this->input->method(TRUE) != 'POST'){
|
|
|
|
|
+
|
|
|
|
|
+ $this->_json_error('请求方式错误','405');
|
|
|
|
|
+ }
|
|
|
|
|
+ $json_str = $this->input->raw_input_stream;
|
|
|
|
|
+ $data = json_decode($json_str,true);
|
|
|
|
|
+ $account = $data['account'];
|
|
|
|
|
+ $pass = $data['pass'];
|
|
|
|
|
+ $account = $this->logic_tools->toolsjiemi($account,"v!frlbpnjgir6alv","k!2w94m6jt!6ook4");
|
|
|
|
|
+ $pass = $this->logic_tools->toolsjiemi($pass,"v!frlbpnjgir6alv","k!2w94m6jt!6ook4");
|
|
|
|
|
+
|
|
|
|
|
+ $pass = sha1($pass);
|
|
|
|
|
+ $userinfo = $this->user->find('userid = "'.$account.'"');
|
|
|
|
|
+ if(empty($userinfo)){
|
|
|
|
|
+ $this->_json_error('账号不存在','500');
|
|
|
|
|
+ }
|
|
|
|
|
+ if($userinfo['userpass'] != $pass){
|
|
|
|
|
+ $this->_json_error('密码错误','500');
|
|
|
|
|
+ }
|
|
|
|
|
+ $power = $this->power->read($userinfo['power']);
|
|
|
|
|
+ if(empty($power)){
|
|
|
|
|
+ $this->_json_error('角色未设置','500');
|
|
|
|
|
+ }
|
|
|
|
|
+ if(empty($power['lyapiid'])){
|
|
|
|
|
+ $this->_json_error('权限未设置','500');
|
|
|
|
|
+ }
|
|
|
|
|
+ $lyapiids = explode("|",trim($power['lyapiid'],"|"));
|
|
|
|
|
+ $res = $this->power->_lyapi();
|
|
|
|
|
+ $lyapi_list = $res['lyapi_list'];
|
|
|
|
|
+ $all_arr = [];
|
|
|
|
|
+ foreach($lyapi_list as $v){
|
|
|
|
|
+ if(in_array($v['id'],$lyapiids)){
|
|
|
|
|
+ $all_arr[] = $v['shortname'];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// 生成唯一 token(可以用 JWT 或随机字符串)
|
|
// 生成唯一 token(可以用 JWT 或随机字符串)
|
|
|
$token = bin2hex(random_bytes(32));
|
|
$token = bin2hex(random_bytes(32));
|
|
|
$cache_key = 'token:' . md5($token);
|
|
$cache_key = 'token:' . md5($token);
|
|
|
|
|
|
|
|
// 写入缓存,有效期7200秒(2小时)
|
|
// 写入缓存,有效期7200秒(2小时)
|
|
|
- $this->cache->save($cache_key, $user_id, 7200);
|
|
|
|
|
-
|
|
|
|
|
- // 返回 token 给客户端
|
|
|
|
|
- $this->output
|
|
|
|
|
- ->set_content_type('application/json')
|
|
|
|
|
- ->set_output(json_encode([
|
|
|
|
|
- 'status' => true,
|
|
|
|
|
- 'token' => $token,
|
|
|
|
|
- 'expire' => 7200
|
|
|
|
|
- ]));
|
|
|
|
|
|
|
+ $this->cache->save($cache_key, [
|
|
|
|
|
+ 'username'=>$userinfo['userid'],
|
|
|
|
|
+ 'mobile'=>'',
|
|
|
|
|
+ 'token'=>$token,
|
|
|
|
|
+ 'power'=>$all_arr
|
|
|
|
|
+ ], 7200);
|
|
|
|
|
+
|
|
|
|
|
+ $this->_json_error("登陆成功",200,[
|
|
|
|
|
+ 'username'=>$userinfo['userid'],
|
|
|
|
|
+ 'mobile'=>'',
|
|
|
|
|
+ 'token'=>$token,
|
|
|
|
|
+ 'lypower'=>implode(',',$all_arr)
|
|
|
|
|
+ ]);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|