ソースを参照

增加获取金10资讯内容接口 /Common/GetJ10News

zhou.xiaoning 2 年 前
コミット
2d5438b6e2
6 ファイル変更256 行追加2 行削除
  1. 37 2
      controllers/common/news.go
  2. 67 0
      docs/docs.go
  3. 67 0
      docs/swagger.json
  4. 43 0
      docs/swagger.yaml
  5. 41 0
      models/common.go
  6. 1 0
      routers/router.go

+ 37 - 2
controllers/common/news.go

@@ -18,7 +18,7 @@ import (
 // @Param   page     query    int false "页码"
 // @Param   pagesize query    int false "每页条数"
 // @Success 200      {object} models.QueryNewTitlesRsp
-// @Failure 500 {object} app.Response
+// @Failure 500   {object} app.Response
 // @Router  /Common/QueryNewTitles [get]
 // @Tags    通用服务
 func QueryNewTitles(c *gin.Context) {
@@ -54,7 +54,7 @@ type QueryNewContentsReq struct {
 // @Produce json
 // @Param   ids query    string false "资讯ID列表,格式 1,2,3"
 // @Success 200 {object} models.QueryNewContentsRsp
-// @Failure 500      {object} app.Response
+// @Failure 500 {object} app.Response
 // @Router  /Common/QueryNewContents [get]
 // @Tags    通用服务
 func QueryNewContents(c *gin.Context) {
@@ -83,3 +83,38 @@ func QueryNewContents(c *gin.Context) {
 	// 查询成功
 	appG.Response(http.StatusOK, e.SUCCESS, rsp)
 }
+
+type GetJ10NewsReq struct {
+	Limit int `form:"limit"` // 获取最新limit条内容
+}
+
+// GetJ10News 获取金10资讯内容
+// @Summary 获取金10资讯内容
+// @Produce json
+// @Param   limit query    int false "获取最新limit条内容"
+// @Success 200   {array}  models.GetJ10NewsRsp
+// @Failure 500      {object} app.Response
+// @Router  /Common/GetJ10News [get]
+// @Tags    通用服务
+func GetJ10News(c *gin.Context) {
+	appG := app.Gin{C: c}
+
+	// 获取请求参数
+	var req GetJ10NewsReq
+	if err := appG.C.ShouldBindQuery(&req); err != nil {
+		logger.GetLogger().Errorf("GetJ10News failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
+		return
+	}
+
+	rsp, err := models.GetJ10News(req.Limit)
+	if err != nil {
+		// 查询失败
+		logger.GetLogger().Errorf("GetJ10News failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
+		return
+	}
+
+	// 查询成功
+	appG.Response(http.StatusOK, e.SUCCESS, rsp)
+}

+ 67 - 0
docs/docs.go

@@ -625,6 +625,42 @@ const docTemplate = `{
                 }
             }
         },
+        "/Common/GetJ10News": {
+            "get": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "通用服务"
+                ],
+                "summary": "获取金10资讯内容",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "获取最新limit条内容",
+                        "name": "limit",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "array",
+                            "items": {
+                                "$ref": "#/definitions/models.GetJ10NewsRsp"
+                            }
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/Common/GetPCWebMenus": {
             "get": {
                 "security": [
@@ -36059,6 +36095,37 @@ const docTemplate = `{
                 }
             }
         },
+        "models.GetJ10NewsData": {
+            "type": "object",
+            "properties": {
+                "content": {
+                    "type": "string"
+                },
+                "pic": {
+                    "type": "string"
+                },
+                "title": {
+                    "type": "string"
+                }
+            }
+        },
+        "models.GetJ10NewsRsp": {
+            "type": "object",
+            "properties": {
+                "data": {
+                    "$ref": "#/definitions/models.GetJ10NewsData"
+                },
+                "id": {
+                    "type": "string"
+                },
+                "time": {
+                    "type": "string"
+                },
+                "type": {
+                    "type": "integer"
+                }
+            }
+        },
         "models.Goods": {
             "type": "object",
             "required": [

+ 67 - 0
docs/swagger.json

@@ -616,6 +616,42 @@
                 }
             }
         },
+        "/Common/GetJ10News": {
+            "get": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "通用服务"
+                ],
+                "summary": "获取金10资讯内容",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "获取最新limit条内容",
+                        "name": "limit",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "array",
+                            "items": {
+                                "$ref": "#/definitions/models.GetJ10NewsRsp"
+                            }
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/Common/GetPCWebMenus": {
             "get": {
                 "security": [
@@ -36050,6 +36086,37 @@
                 }
             }
         },
+        "models.GetJ10NewsData": {
+            "type": "object",
+            "properties": {
+                "content": {
+                    "type": "string"
+                },
+                "pic": {
+                    "type": "string"
+                },
+                "title": {
+                    "type": "string"
+                }
+            }
+        },
+        "models.GetJ10NewsRsp": {
+            "type": "object",
+            "properties": {
+                "data": {
+                    "$ref": "#/definitions/models.GetJ10NewsData"
+                },
+                "id": {
+                    "type": "string"
+                },
+                "time": {
+                    "type": "string"
+                },
+                "type": {
+                    "type": "integer"
+                }
+            }
+        },
         "models.Goods": {
             "type": "object",
             "required": [

+ 43 - 0
docs/swagger.yaml

@@ -11031,6 +11031,26 @@ definitions:
         description: 仓库类型 - 1 厂库  2 自有库  3 合作库
         type: integer
     type: object
+  models.GetJ10NewsData:
+    properties:
+      content:
+        type: string
+      pic:
+        type: string
+      title:
+        type: string
+    type: object
+  models.GetJ10NewsRsp:
+    properties:
+      data:
+        $ref: '#/definitions/models.GetJ10NewsData'
+      id:
+        type: string
+      time:
+        type: string
+      type:
+        type: integer
+    type: object
   models.Goods:
     properties:
       agreeunit:
@@ -29138,6 +29158,29 @@ paths:
       summary: 查询区域信息
       tags:
       - 通用服务
+  /Common/GetJ10News:
+    get:
+      parameters:
+      - description: 获取最新limit条内容
+        in: query
+        name: limit
+        type: integer
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            items:
+              $ref: '#/definitions/models.GetJ10NewsRsp'
+            type: array
+        "500":
+          description: Internal Server Error
+          schema:
+            $ref: '#/definitions/app.Response'
+      summary: 获取金10资讯内容
+      tags:
+      - 通用服务
   /Common/GetPCWebMenus:
     get:
       parameters:

+ 41 - 0
models/common.go

@@ -1,11 +1,14 @@
 package models
 
 import (
+	"bytes"
+	"encoding/json"
 	"errors"
 	"fmt"
 	"mtp2_if/db"
 	"mtp2_if/mtpcache"
 	"mtp2_if/utils"
+	"net/http"
 	"strconv"
 	"strings"
 	"time"
@@ -1296,3 +1299,41 @@ func GetNewContents(ids []string) (rsp []QueryNewContentsRsp, error error) {
 
 	return
 }
+
+type GetJ10NewsData struct {
+	Pic     string `json:"pic"`
+	Title   string `json:"title"`
+	Content string `json:"content"`
+}
+
+// GetJ10NewsRsp 金10资讯数据
+type GetJ10NewsRsp struct {
+	Id   string         `json:"id"`
+	Time string         `json:"time"`
+	Type int            `json:"type"`
+	Data GetJ10NewsData `json:"data"`
+}
+
+func GetJ10News(limit int) (datas []GetJ10NewsRsp, err error) {
+	datas = make([]GetJ10NewsRsp, 0)
+
+	rsp, err := http.Get("https://www.jin10.com/flash_newest.js")
+	if err != nil {
+		return
+	}
+	defer rsp.Body.Close()
+	buf := new(bytes.Buffer)
+	buf.ReadFrom(rsp.Body)
+	newStr := buf.String()
+	if strings.Contains(newStr, "var newest = ") {
+		a := strings.Replace(newStr, "var newest = ", "", -1)
+		b := a[:len(a)-1]
+
+		err = json.Unmarshal([]byte(b), &datas)
+		if len(datas) > limit {
+			datas = datas[:limit]
+		}
+	}
+
+	return
+}

+ 1 - 0
routers/router.go

@@ -141,6 +141,7 @@ func InitRouter() *gin.Engine {
 		commonR.GET("/QueryNewTitles", common.QueryNewTitles)
 		// 获取资讯内容
 		commonR.GET("/QueryNewContents", common.QueryNewContents)
+		commonR.GET("/GetJ10News", common.GetJ10News)
 
 		// 通知公告系统消息查询
 		commonR.Use(token.Auth()).GET("/QueryNotice", common.QueryNotice)