浏览代码

补充资讯前台和阅读数量

mylink 6 月之前
父节点
当前提交
f739525cce
共有 6 个文件被更改,包括 93 次插入11 次删除
  1. 1 0
      ffi/lib_news.h
  2. 31 0
      ffi/main.go
  3. 10 7
      route/api.php
  4. 37 4
      src/Controller/Api/NewsController.php
  5. 6 0
      src/Service/NewsFfiService.php
  6. 8 0
      src/Service/NewsService.php

+ 1 - 0
ffi/lib_news.h

@@ -85,6 +85,7 @@ extern char* GetNewsByID(int id);
 extern char* CreateNews(char* dataJson);
 extern char* UpdateNews(int id, char* dataJson);
 extern char* DeleteNews(int id);
+extern char* IncrementNewsViews(int id);
 
 #ifdef __cplusplus
 }

+ 31 - 0
ffi/main.go

@@ -385,5 +385,36 @@ func DeleteNews(id C.int) *C.char {
 	return C.CString(`{"status": "ok"}`)
 }
 
+//export IncrementNewsViews
+func IncrementNewsViews(id C.int) *C.char {
+	if db == nil {
+		return C.CString(`{"error": "database connection is not initialized"}`)
+	}
+
+	// 使用原生SQL更新views字段,避免并发问题
+	result := db.Exec("UPDATE cy_news SET views = views + 1 WHERE id = ? AND delete_time IS NULL", int(id))
+	if result.Error != nil {
+		return C.CString(fmt.Sprintf(`{"error": "increment views failed: %s"}`, result.Error.Error()))
+	}
+
+	if result.RowsAffected == 0 {
+		return C.CString(fmt.Sprintf(`{"error": "news with id %d not found"}`, id))
+	}
+
+	// 返回更新后的文章信息
+	var news model.News
+	if err := db.First(&news, int(id)).Error; err != nil {
+		// 即使查询失败,views已经更新成功,返回成功状态
+		return C.CString(`{"status": "ok", "message": "views incremented successfully"}`)
+	}
+
+	resultJson, err := json.Marshal(news)
+	if err != nil {
+		return C.CString(`{"status": "ok", "message": "views incremented successfully"}`)
+	}
+
+	return C.CString(string(resultJson))
+}
+
 // main is required for building a C shared library.
 func main() {}

+ 10 - 7
route/api.php

@@ -5,10 +5,13 @@ use SixShop\News\Controller\Api\CategoryController;
 use SixShop\News\Controller\Api\NewsController;
 use think\facade\Route;
 
-// 移动端/H5等API路由
-// 路由前缀: /admin/news
-
-// 分类API资源路由
-Route::resource('category', CategoryController::class);
-// 文章API资源路由
-Route::resource('news', NewsController::class);
+// 移动端/H5等 API 路由
+// 路由前缀: /api/extension/news
+Route::group('api/extension/news', function () {
+    // 分类API资源路由
+    Route::resource('category', CategoryController::class);
+    // 文章API资源路由
+    Route::resource('news', NewsController::class);
+    // 增加阅读数路由
+    Route::post('news/:id/views', [NewsController::class, 'incrementViews']);
+});

+ 37 - 4
src/Controller/Api/NewsController.php

@@ -23,11 +23,32 @@ class NewsController
     public function index(Request $request): Response
     {
         $params = $request->get();
+
+        // 兼容:若前端未传 where,而是顶层传 category_id / title,则转为 where 条件
+        $where = [];
+        if (!empty($params['where']) && is_array($params['where'])) {
+            $where = $params['where'];
+        }
+        // 顶层 category_id 兼容
+        if (isset($params['category_id']) && $params['category_id'] !== '') {
+            $where['category_id'] = (int) $params['category_id'];
+        }
+        // 顶层 title 兼容(模糊查询)
+        if (isset($params['title']) && $params['title'] !== '') {
+            // 具体模糊查询写法由 Go 侧处理,这里仅传递关键字
+            $where['title'] = (string) $params['title'];
+        }
+
+        // 分页/排序规范化
+        $order = $params['order'] ?? ['is_top' => 'desc', 'id' => 'desc'];
+        $limit = isset($params['limit']) ? (int) $params['limit'] : 20;
+        $page  = isset($params['page']) ? (int) $params['page'] : 1;
+
         $list = $this->service->getList(
-            $params['where'] ?? [],
-            $params['order'] ?? ['is_top' => 'desc', 'id' => 'desc'],
-            $params['limit'] ?? 20,
-            $params['page'] ?? 1
+            $where,
+            $order,
+            $limit,
+            $page
         );
         return json($list);
     }
@@ -43,6 +64,18 @@ class NewsController
         $item = $this->service->getById($id);
         return json($item);
     }
+
+    /**
+     * 增加文章阅读数
+     *
+     * @param  int  $id
+     * @return Response
+     */
+    public function incrementViews($id): Response
+    {
+        $result = $this->service->incrementViews($id);
+        return json($result);
+    }
     
     /**
      * Save a new resource in storage.

+ 6 - 0
src/Service/NewsFfiService.php

@@ -37,6 +37,7 @@ class NewsFfiService
                 char* CreateNews(char* dataJson);
                 char* UpdateNews(int id, char* dataJson);
                 char* DeleteNews(int id);
+                char* IncrementNewsViews(int id);
             ";
 
             $libPath = __DIR__ . '/../../ffi/lib_news.so';
@@ -166,4 +167,9 @@ class NewsFfiService
     {
         return $this->call('DeleteNews', $id);
     }
+
+    public function incrementNewsViews(int $id): array
+    {
+        return $this->call('IncrementNewsViews', $id);
+    }
 } 

+ 8 - 0
src/Service/NewsService.php

@@ -63,4 +63,12 @@ class NewsService
     {
         return $this->ffiService->deleteNews($id);
     }
+
+    /**
+     * 增加文章阅读数
+     */
+    public function incrementViews($id)
+    {
+        return $this->ffiService->incrementNewsViews($id);
+    }
 }