|
@@ -1,33 +1,43 @@
|
|
|
<?php
|
|
<?php
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
|
|
|
|
-class User extends CI_Controller
|
|
|
|
|
-{
|
|
|
|
|
- public function __construct()
|
|
|
|
|
- {
|
|
|
|
|
- parent::__construct();
|
|
|
|
|
- $this->output->set_content_type('application/json');
|
|
|
|
|
|
|
+class User extends Lyapi_Controller{
|
|
|
|
|
+
|
|
|
|
|
+ // 注意:登录接口不能受基础控制器的登录校验,可以覆盖构造方法或单独处理
|
|
|
|
|
+ public function __construct() {
|
|
|
|
|
+ // 这里不执行登录校验,只加载缓存驱动
|
|
|
|
|
+ parent::__construct(); // 暂时注释,或者使用一个新的不校验的基类
|
|
|
|
|
+ // 简便做法:复制 Lyapi_Controller 的部分代码但不调用 _check_api_auth
|
|
|
|
|
+ // $this->load->driver('cache'); // 加载缓存驱动
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 访问:GET /api/v1/user/center
|
|
|
|
|
- public function center()
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public function login() {
|
|
|
|
|
+ $user_id =123;
|
|
|
|
|
+ // ... 验证账号密码成功,得到 $user_id
|
|
|
$data = [
|
|
$data = [
|
|
|
- 'code' => 200,
|
|
|
|
|
- 'version' => 'v1',
|
|
|
|
|
- 'data' => [
|
|
|
|
|
- 'username' => 'John Doe',
|
|
|
|
|
- 'email' => 'john@example.com'
|
|
|
|
|
- ]
|
|
|
|
|
|
|
+ 'user_id' => $user_id,
|
|
|
|
|
+ 'created_at' => time()
|
|
|
];
|
|
];
|
|
|
- $this->output->set_output(json_encode($data));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ $this->cache->save("lvhaoceshi1", $data, 100);
|
|
|
|
|
+ echo "<pre>";
|
|
|
|
|
+ print_r($this->cache->get("lvhaoceshi1"));
|
|
|
|
|
+ die;
|
|
|
|
|
+ //$this->load->driver('cache'); // 确保缓存可用
|
|
|
|
|
+
|
|
|
|
|
+ // 生成唯一 token(可以用 JWT 或随机字符串)
|
|
|
|
|
+ $token = bin2hex(random_bytes(32));
|
|
|
|
|
+ $cache_key = 'token:' . md5($token);
|
|
|
|
|
+
|
|
|
|
|
+ // 写入缓存,有效期7200秒(2小时)
|
|
|
|
|
+ $this->cache->save($cache_key, $user_id, 7200);
|
|
|
|
|
|
|
|
- // 访问:POST /api/v1/user/login
|
|
|
|
|
- public function login()
|
|
|
|
|
- {
|
|
|
|
|
- // 从输入流获取JSON数据
|
|
|
|
|
- $input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
|
- // 业务逻辑...
|
|
|
|
|
|
|
+ // 返回 token 给客户端
|
|
|
|
|
+ $this->output
|
|
|
|
|
+ ->set_content_type('application/json')
|
|
|
|
|
+ ->set_output(json_encode([
|
|
|
|
|
+ 'status' => true,
|
|
|
|
|
+ 'token' => $token,
|
|
|
|
|
+ 'expire' => 7200
|
|
|
|
|
+ ]));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|