1
0

index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const fs = require('fs');
  2. const path = require('path')
  3. const XLSX = require('xlsx');
  4. // const app = require('express')();
  5. // const server = require('http').Server(app);
  6. const {RedisConf, CommonConf} = require('./config')
  7. const Redis = require('ioredis');
  8. let dataPath = "../assets/scripts/data";
  9. let dataPrefix = "taptapstar_";
  10. /**
  11. * excel表转换为json对象
  12. * @param {String} filename 文件名
  13. */
  14. function getSheet(filename) {
  15. let filePath = path.join(__dirname, "/excel", filename);
  16. // let buf = fs.readFile(filePath);
  17. let workbook = XLSX.readFileSync(filePath)
  18. let sheets = workbook.SheetNames;
  19. if(sheets.length == 1) {
  20. let firstSheet = sheets[0]
  21. let ws = workbook.Sheets[firstSheet]
  22. return XLSX.utils.sheet_to_json(ws, {range: 1})
  23. } else if(sheets.length > 1) {
  24. let sheetsObj = {}
  25. sheets.forEach(n => {
  26. let ws = workbook.Sheets[n]
  27. sheetsObj[n] = XLSX.utils.sheet_to_json(ws, {range: 1})
  28. })
  29. return sheetsObj;
  30. }
  31. // console.log(XLSX.utils.sheet_to_json(ws));
  32. // console.log(workbook.SheetNames);
  33. }
  34. /**
  35. * 写入json内容到客户端assets目录
  36. * @param {String} filename 文件名
  37. * @param {Object} content json内容
  38. */
  39. function writeToAssets (filename, content) {
  40. let writeFileName = filename.replace(/\.xlsx|\.xls/, '.js').replace(dataPrefix, "");
  41. let writePath = path.join(__dirname, dataPath, writeFileName);
  42. fs.writeFile(writePath, 'module.exports=' + JSON.stringify(content))
  43. console.log(JSON.stringify(content, null, 2));
  44. }
  45. function init () {
  46. let redis = new Redis(RedisConf.R1);
  47. fs.readdir("./excel", (err, file) => {
  48. file.forEach(n => {
  49. if(n.match(/\~\$/)) {
  50. return
  51. }
  52. let ws = getSheet(n);
  53. if(ws.length > 0) {
  54. //单个表
  55. let sheetName = 'client:' + n.replace("_", ":").replace(/\.xlsx|\.xls/, "");
  56. ws.forEach((value, index) => {
  57. let hkey = Object.keys(value)[0]
  58. redis.multi().hset(sheetName, value[hkey], JSON.stringify(value)).exec()
  59. })
  60. console.log(`Add ${sheetName} to redis`);
  61. } else {
  62. //多个表,redis的key要加上表名
  63. for(let i in ws) {
  64. let sheetName = 'client:' + n.replace("_", ":").replace(/\.xlsx|\.xls/, "") + ':' + i;
  65. ws[i].forEach((value, index) => {
  66. let hkey = Object.keys(value)[0]
  67. redis.multi().hset(sheetName, value[hkey], JSON.stringify(value)).exec()
  68. })
  69. console.log(`Add ${sheetName} to redis`);
  70. }
  71. }
  72. //把生成的js写入客户端assets目录
  73. writeToAssets(n, ws);
  74. })
  75. // process.exit()
  76. })
  77. }
  78. init();
  79. // let ws = getSheet("allstar_buildingtest_level.xlsx")
  80. // ws.forEach((value, index) => {
  81. // let key = `allstar:test`;
  82. // redis.multi().hset(key, index, JSON.stringify(value)).exec()
  83. // })
  84. // redis.get("allstar:building", (err, res) => {
  85. // console.log(res);
  86. // })
  87. // for(var i in BuildingInfo) {
  88. // for(var j in BuildingInfo[i]) {
  89. // let key2 = `allstar:test:${i}:${BuildingInfo[i][j].level}`;
  90. // redis.set(key2, JSON.stringify(BuildingInfo[i][j]))
  91. // }
  92. // }
  93. // app.get('/', function (req, res) {
  94. // res.body = getSheet("achievementAttribute.xlsx")
  95. // res.sendFile(__dirname + '/index.html');
  96. // });
  97. // server.listen(408, () => {
  98. // console.log('listening on *:408');
  99. // });