headlines_local.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, "/headlines_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. let string = JSON.stringify(content);
  61. fs.writeFile(writePath, 'module.exports=' + string)
  62. // console.log(JSON.stringify(content, null, 2));
  63. }
  64. /**
  65. * 配置表生成的json内容rsync到静态资源服务器
  66. * @param {String} filename 文件名
  67. * @param {Object} content json内容
  68. */
  69. const rsyncJson = function (filename, content) {
  70. let writeFileName = filename.replace(/\.xlsx|\.xls/, '.json').replace(dataPrefix, "")
  71. let writePath = path.join(__dirname, "/sheet", writeFileName)
  72. fs.writeFile(writePath, JSON.stringify(content))
  73. }
  74. const formatType = function(target, type) {
  75. let _type = type.toLowerCase()
  76. switch (_type) {
  77. case 'integer':
  78. target = parseInt(target, 10)
  79. break;
  80. case 'double':
  81. target = parseFloat(target)
  82. break;
  83. case 'string':
  84. target = target.toString()
  85. break;
  86. case 'object':
  87. target = JSON.parse(target)
  88. break;
  89. default:
  90. break;
  91. }
  92. return target
  93. }
  94. const init = function () {
  95. fs.readdir("./excel", (err, file) => {
  96. file.forEach(n => {
  97. if(n.match(/\~\$/)) {
  98. return
  99. }
  100. let ws = getSheet(n)
  101. let sheetName = n.replace(/\.xlsx|\.xls/, "")
  102. let target
  103. if(ws.length > 0) {
  104. //单个表
  105. // ws.forEach(n => {
  106. // if(typeof n['id'] != undefined) {
  107. // n['_id'] = n['id']
  108. // }
  109. // })
  110. target = ws
  111. } else {
  112. //多个表合拼成一个Array
  113. let _array = []
  114. for(let i in ws) {
  115. _array = _array.concat(ws[i])
  116. }
  117. target = _array
  118. }
  119. // MongoClient.connect(connStr, connOpt, (err, client) => {
  120. // var db = client.db(dbIns.dbname)
  121. // var collection = db.collection(sheetName)
  122. // collection.drop(null, () => {
  123. // collection.insert(target, () => {
  124. // console.log(`成功更新配置表:${sheetName}`)
  125. // client.close()
  126. // })
  127. // })
  128. // });
  129. //把生成的js写入客户端assets目录
  130. let wsKeys = Object.keys(ws)
  131. if(wsKeys.length == 1) {
  132. writeToAssets(n, ws[wsKeys[0]])
  133. rsyncJson(n, ws[wsKeys[0]])
  134. } else {
  135. writeToAssets(n, ws)
  136. rsyncJson(n, ws)
  137. }
  138. })
  139. // process.exit()
  140. })
  141. }
  142. init()