Selaa lähdekoodia

更新热更接口

zhanwencai 1 vuosi sitten
vanhempi
sitoutus
a7eabde833

+ 57 - 6
webServer/src/controller/ApiController.ts

@@ -1,17 +1,18 @@
 import Msg from '../msg'; // 确保路径是正确的
 
-const servers = require('../serverList.json')
 const CryptoJS = require("crypto-js");
 const crypto = require('crypto');
 const AppSecret = "67db708fdc1b3f61341db6e8551143c0"
 const AppKey = "57afbdc608db9aa423e1b15b321d7de0"
 const Account = "H1EqhbpA80jt0Jw6Q3T2"//ws请求密钥
 const Order = require('../model/OrderModel')
+const Server = require('../model/ServerModel')
+const Version = require('../model/VersionModel')
 const logger = require('../log')
-var orderList = []
 
 //根据区服id获取地址
-const getServerList = (serverId) => {
+const getServerList = async (serverId, tag) => {
+    const servers = (await Server.getServerList(tag))
     for (let i = 0; i < servers.length; i++) {
         if (servers[i].id == serverId) {
             return 'ws://' + servers[i].ip + ':' + servers[i].port
@@ -60,7 +61,7 @@ const callPay = async (ctx) => {
         return ret
     }
 
-    let url = getServerList(serverId)
+    let url = await getServerList(serverId,'default')
     if (!url) {
         logger.info(`区服id错误: serverId ${serverId}`)
         ret.msg = `区服id错误: serverId ${serverId}`
@@ -104,6 +105,50 @@ const callPay = async (ctx) => {
 
 }
 
+
+const compareVersions = (v1: string, v2: string) => {
+    const parts1 = v1.split('.').map(Number);
+    const parts2 = v2.split('.').map(Number);
+    
+    for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
+      const num1 = parts1[i] || 0;
+      const num2 = parts2[i] || 0;
+      
+      if (num1 > num2) return 1;
+      if (num1 < num2) return -1;
+    }
+    
+    return 0;
+  }
+
+const checkVersion = async (ctx) => {
+    let ret = {
+        code: 0,
+        msg: '无需更新',
+        url: '',
+    }
+    let data = ctx.request.body
+    let version = data.version
+    let tag = data.tag || 'default'
+    logger.info("checkVersion params:", { "url": ctx.href, "params": data })
+
+    const versionInfo = (await Version.getGameVersion(tag))[0]
+
+    if (!versionInfo) {
+        return ret
+    }
+
+    if (compareVersions(versionInfo.version,version) === 1) {
+
+        if(versionInfo.download_url){
+            ret.code = 1
+            ret.msg = '需要更新'
+            ret.url = versionInfo.download_url
+        }
+    }
+    return ret
+}
+
 //验证账号
 const checkUserToken = async (ctx) => {
     let data = ctx.request.body
@@ -209,9 +254,15 @@ class ApiController {
         ctx.body = result
     }
 
+    async checkVersion(ctx) {
+        var result = await checkVersion(ctx)
+        console.log('校验版本', result)
+        ctx.body = result
+    }
+
     async getServerList(ctx) {
-        let id = ctx.query.id
-        console.log(getServerList(id))
+        let tag = ctx.query.tag || 'default'
+        const servers = (await Server.getServerList(tag))
         ctx.body = servers
     }
 }

+ 10 - 0
webServer/src/model/ServerModel.ts

@@ -0,0 +1,10 @@
+import { query } from '../sql/query'; // 确保路径是正确的
+
+class ServerModel {
+    //获取订单
+    async getServerList(tag: any) {
+        return await query(`SELECT id,status,name,ip,port FROM game_server WHERE tag = ?`,[tag])
+    }
+}
+
+module.exports = new ServerModel()

+ 10 - 0
webServer/src/model/VersionModel.ts

@@ -0,0 +1,10 @@
+import { query } from '../sql/query'; // 确保路径是正确的
+
+class VersionModel {
+    //获取订单
+    async getGameVersion(tag: any) {
+        return await query(`SELECT * FROM game_version WHERE tag = ? ORDER BY ID `,[tag])
+    }
+}
+
+module.exports = new VersionModel()

+ 2 - 0
webServer/src/router/index.ts

@@ -16,5 +16,7 @@ router.get('/serverList', ApiController.getServerList)
 //获取订单信息
 router.post('/createOrder', ApiController.createOrder)
 
+//获取订单信息
+router.post('/checkVersion', ApiController.checkVersion)
 
 module.exports = router

+ 40 - 0
webServer/src/sql/sdk.sql

@@ -0,0 +1,40 @@
+DROP TABLE IF EXISTS `game_order`;
+CREATE TABLE `game_order` (
+  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
+  `order_id` varchar(50) NOT NULL DEFAULT '' COMMENT '订单id',
+  `uid` varchar(100) NOT NULL DEFAULT '' COMMENT '用户id',
+  `status` int(11) NOT NULL DEFAULT '1' COMMENT '订单状态 1未支付 2成功 3失败',
+  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
+  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
+  `out_trade_no` varchar(255) NOT NULL DEFAULT '' COMMENT '第三方订单id',
+  `level` int(11) NOT NULL DEFAULT '0' COMMENT '角色等级',
+  `role_id` varchar(50) NOT NULL DEFAULT '' COMMENT '角色id',
+  `role_name` varchar(100) NOT NULL DEFAULT '' COMMENT '角色名',
+  `product_id` int(11) NOT NULL DEFAULT '0' COMMENT '商品id',
+  `amount` int(11) NOT NULL DEFAULT '0' COMMENT '订单金额 单位分',
+  `server_id` int(11) NOT NULL DEFAULT '0' COMMENT '区服id',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='订单表';
+
+
+DROP TABLE IF EXISTS `game_server`;
+CREATE TABLE `game_server` (
+  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
+  `name` varchar(50) NOT NULL DEFAULT '' COMMENT '区服名',
+  `status` int(11) NOT NULL DEFAULT '1' COMMENT '状态',
+  `ip` varchar(50) NOT NULL DEFAULT '' COMMENT 'ip',
+  `port` int(11) NOT NULL DEFAULT '0' COMMENT '端口',
+  `tag` varchar(50) NOT NULL DEFAULT 'default' COMMENT '标识',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='区服表';
+
+
+DROP TABLE IF EXISTS `game_version`;
+CREATE TABLE `game_version` (
+  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
+  `version` varchar(50) NOT NULL DEFAULT '' COMMENT '版本',
+  `download_url` varchar(100) NOT NULL DEFAULT '' COMMENT '下载链接',
+  `tag` varchar(50) NOT NULL DEFAULT 'default' COMMENT '标识',
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='游戏版本';
+