lvhao hace 6 horas
padre
commit
ae2dc97673

+ 7 - 0
core/CoreApp/config/cache.php

@@ -0,0 +1,7 @@
+<?php
+defined('BASEPATH') OR exit('No direct script access allowed');
+
+$config['adapter']     = 'file';
+$config['backup']      = 'file';
+$config['key_prefix']  = 'api_';     // 统一前缀,便于管理
+$config['file_path']   = APPPATH . 'cache/data/';  // 自定义存储目录

+ 33 - 23
core/CoreApp/controllers/Lyerpapi/v1/User.php

@@ -1,33 +1,43 @@
 <?php
 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 = [
-            '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
+            ]));
     }
 }

+ 12 - 1
core/CoreApp/core/Lin_Controller.php

@@ -160,4 +160,15 @@ abstract class Admin_Controller extends Lin_Controller {
        function made_admin_url($uri,$qs = '')
 		{
 			return site_url('gold'.'/'.$uri).($qs == '' ? '' : '?'.$qs);
-		}
+		}
+
+
+abstract class Lyapi_Controller extends CI_Controller {
+	public $cache;
+	function __construct()
+	{
+		parent::__construct();
+		$this->load->driver('cache');
+		$this->cache = $this->cache;
+	}
+}