app.js 3.14 KB
Newer Older
liujiangnan's avatar
liujiangnan committed
1 2 3
const express = require('express');
const os = require('os');
const readline = require('readline');
李维's avatar
李维 committed
4 5 6
const Base64 = require('js-base64');
const CryptoJS = require("crypto-js");
const http = require("http");
liujiangnan's avatar
liujiangnan committed
7
const { build, buildAndroid, buildIos } = require('./buildCocos');
liujiangnan's avatar
liujiangnan committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

const networkInfo = os.networkInterfaces();

let host = '';
for (const infos of Object.values(networkInfo)) {
  for (const info of infos) {
    if (info.family == 'IPv4') {
      if (info.address.split('.')[0] != "127") {
        host = info.address;
      }
    }
  }
}

var app = express();

李维's avatar
李维 committed
24 25
// Access-Control-Allow-Origin: *

liujiangnan's avatar
liujiangnan committed
26 27 28
app.use('/dist', express.static('dist'));

app.get('/', function (req, res) {
李维's avatar
李维 committed
29
  res.header("Access-Control-Allow-Origin", "*");
liujiangnan's avatar
liujiangnan committed
30
  res.send('Hello World');
李维's avatar
李维 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
});

app.get('/getSign', function (req, res) {
  res.header("Access-Control-Allow-Origin", "*");

  const gameId = req.query.gameId;
  const openId = req.query.openId;

  const secretKey = "060d81c7abaf24c8ce2afc5a725c152062676d35"; //替换为控制台上的 游戏 Key

  const nonce = Math.floor(Math.random() * 1000000000);
  const timestamp = Math.floor(new Date().getTime() / 1000);

  const str = `game_id=${gameId}&nonce=${nonce}&open_id=${openId}&timestamp=${timestamp}`;
  const sign = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(str, secretKey));

  res.send(JSON.stringify({ nonce, timestamp, sign }));
});

// app.get('/jump', function (req, res) {
//   res.header("Access-Control-Allow-Origin", "*");

//   const gameId = req.query.gameId;
//   const openId = req.query.openId;

//   http.get({
//     hostname: 'staging-teach.ireadabc.com',
//     port: 80,
//     path: `/api/oxford/game/v1/config?gameid=${gameId}&openid=${openId}`,
//     agent: false  // 仅为这个请求创建新代理
//   }, (getres) => {
//     getres.setEncoding('utf8');
//     let rawData = '';
//     getres.on('data', (chunk) => { rawData += chunk; });
//     getres.on('end', () => {
//       const secretKey = "060d81c7abaf24c8ce2afc5a725c152062676d35"; //替换为控制台上的 游戏 Key
//       const data = JSON.parse(rawData);

//       const str = `game_id=${gameId}&nonce=${data.nonce}&open_id=${openId}&timestamp=${data.timestamp}`;
//       const sign = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA1(str, secretKey));
//       console.log('sign = ' + sign);
//       res.send(rawData)
//     });
//   });
// });
liujiangnan's avatar
liujiangnan committed
76 77 78 79 80 81 82 83 84 85

let port = '';
var server = app.listen(8081, function () {

  port = server.address().port

  console.log("测试服务已启动:%s:%s", host, port)
  console.log("如果已经build可访问如下地址");
  console.log("http://%s:%s/dist/play/index.html", host, port)
  console.log("http://%s:%s/dist/play/index.html", "localhost", port)
liujiangnan's avatar
liujiangnan committed
86
  console.log('-----------------');
liujiangnan's avatar
liujiangnan committed
87
  console.log("    输入 build 构建安卓脚本");
liujiangnan's avatar
liujiangnan committed
88 89
  console.log("    输入 build ios 构建IOS脚本");
  console.log('-----------------');
liujiangnan's avatar
liujiangnan committed
90 91 92 93 94 95 96 97 98

})

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})
rl.on('line', async (str) => {
  if (str.trim() == 'build') {
liujiangnan's avatar
liujiangnan committed
99 100 101
    await buildAndroid();
  } else if (str.trim() == 'build ios') {
    await buildIos();
liujiangnan's avatar
liujiangnan committed
102
  }
liujiangnan's avatar
liujiangnan committed
103
  console.log("服务器:%s:%s", host, port)
liujiangnan's avatar
liujiangnan committed
104
})