1
0

local.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const fs = require('fs')
  2. const path = require('path')
  3. const XLSX = require('xlsx')
  4. // const Rsync = require('rsync')
  5. const {MongoConf} = require('./config')
  6. const MongoClient = require('mongodb').MongoClient
  7. const assert = require('assert')
  8. const dbIns = MongoConf['R1']
  9. const connStr = `mongodb://${dbIns.host}:${dbIns.port}`
  10. const connOpt = {
  11. auth : {
  12. user: dbIns.username,
  13. password: dbIns.password,
  14. },
  15. authSource: dbIns.dbname,
  16. authMechanism: "SCRAM-SHA-1",
  17. useNewUrlParser: true
  18. }
  19. const dataPath = "../assets/scripts/data"
  20. const dataPrefix = "taptapstar_"
  21. /**
  22. * excel表转换为json对象
  23. * @param {String} filename 文件名
  24. */
  25. const getSheet = function (filename) {
  26. let filePath = path.join(__dirname, "/excel", filename)
  27. // let buf = fs.readFile(filePath);
  28. let workbook = XLSX.readFileSync(filePath)
  29. let sheets = workbook.SheetNames
  30. let sheetsObj = {}
  31. sheets.forEach(n => {
  32. let ws = workbook.Sheets[n]
  33. let def = XLSX.utils.sheet_to_json(ws, {range: 0, header: 1})
  34. let typeMap = {}
  35. let typeArr = def[0]
  36. let keyArr = def[2]
  37. keyArr.forEach((item, index) => {
  38. typeMap[item] = typeArr[index]
  39. })
  40. let res = XLSX.utils.sheet_to_json(ws, {range: 2})
  41. res.forEach(item => {
  42. for(let i in item) {
  43. item[i] = formatType(item[i], typeMap[i])
  44. }
  45. })
  46. sheetsObj[n] = res
  47. })
  48. return sheetsObj
  49. // console.log(XLSX.utils.sheet_to_json(ws));
  50. // console.log(workbook.SheetNames);
  51. }
  52. /**
  53. * 写入json内容到客户端assets目录
  54. * @param {String} filename 文件名
  55. * @param {Object} content json内容
  56. */
  57. const writeToAssets = function (filename, content) {
  58. let writeFileName = filename.replace(/\.xlsx|\.xls/, '.js').replace(dataPrefix, "")
  59. let writePath = path.join(__dirname, dataPath, writeFileName)
  60. fs.writeFile(writePath, 'module.exports=' + JSON.stringify(content))
  61. // console.log(JSON.stringify(content, null, 2));
  62. }
  63. /**
  64. * 配置表生成的json内容rsync到静态资源服务器
  65. * @param {String} filename 文件名
  66. * @param {Object} content json内容
  67. */
  68. const rsyncJson = function (filename, content) {
  69. let writeFileName = filename.replace(/\.xlsx|\.xls/, '.json').replace(dataPrefix, "")
  70. let writePath = path.join(__dirname, "/sheet", writeFileName)
  71. fs.writeFile(writePath, JSON.stringify(content))
  72. }
  73. const formatType = function(target, type) {
  74. let _type = type.toLowerCase()
  75. switch (_type) {
  76. case 'integer':
  77. target = parseInt(target, 10)
  78. break;
  79. case 'double':
  80. target = parseFloat(target)
  81. break;
  82. case 'string':
  83. target = target.toString()
  84. break;
  85. case 'object':
  86. target = JSON.parse(target)
  87. break;
  88. default:
  89. break;
  90. }
  91. return target
  92. }
  93. const init = function () {
  94. fs.readdir("./excel", (err, file) => {
  95. file.forEach(n => {
  96. if(n.match(/\~\$/)) {
  97. return
  98. }
  99. let ws = getSheet(n)
  100. let sheetName = n.replace(/\.xlsx|\.xls/, "")
  101. let target
  102. if(ws.length > 0) {
  103. //单个表
  104. // ws.forEach(n => {
  105. // if(typeof n['id'] != undefined) {
  106. // n['_id'] = n['id']
  107. // }
  108. // })
  109. target = ws
  110. } else {
  111. //多个表合拼成一个Array
  112. let _array = []
  113. for(let i in ws) {
  114. _array = _array.concat(ws[i])
  115. }
  116. target = _array
  117. }
  118. // MongoClient.connect(connStr, connOpt, (err, client) => {
  119. // var db = client.db(dbIns.dbname)
  120. // var collection = db.collection(sheetName)
  121. // collection.drop(null, () => {
  122. // collection.insert(target, () => {
  123. // console.log(`成功更新配置表:${sheetName}`)
  124. // client.close()
  125. // })
  126. // })
  127. // });
  128. //把生成的js写入客户端assets目录
  129. let wsKeys = Object.keys(ws)
  130. if(wsKeys.length == 1) {
  131. writeToAssets(n, ws[wsKeys[0]])
  132. rsyncJson(n, ws[wsKeys[0]])
  133. } else {
  134. writeToAssets(n, ws)
  135. rsyncJson(n, ws)
  136. }
  137. })
  138. // process.exit()
  139. })
  140. }
  141. init()