Bladeren bron

Merge branch '0.6' of http://svn.ouj.com:3000/DWG/allstar into 0.6

hwt 6 jaren geleden
bovenliggende
commit
e7f892de14
3 gewijzigde bestanden met toevoegingen van 192 en 2 verwijderingen
  1. 23 1
      tools/config.js
  2. 1 1
      tools/package.json
  3. 168 0
      tools/test.js

+ 23 - 1
tools/config.js

@@ -18,11 +18,33 @@ const MongoConf = {
         username: "ojiatest",
         password: "ojia305",
         dbname: "allstar_home"
+    },
+    R2: {
+        port: 10001,
+        host: "58.215.52.59",
+        username: "hyqmxmongo",
+        password: "55wn2NK37UrB",
+        dbname: "hyqmx"
+    }
+}
+
+const ReplSetConf = {
+    R1 : {
+        servers: [
+            "58.215.52.59:10001",
+            "58.215.52.21:10001",
+            "14.215.104.240:10001"
+        ],
+        replicaSet: "hyqmxmongo",
+        username: "hyqmx_rw",
+        password: "55wn2NK37UrB",
+        dbname: "hyqmx",
     }
 }
 
 module.exports = {
     RedisConf,
     CommonConf,
-    MongoConf
+    MongoConf,
+    ReplSetConf
 }

+ 1 - 1
tools/package.json

@@ -4,7 +4,7 @@
   "description": "",
   "main": "index.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 1",
+    "test": "node test.js",
     "dev": "node index.js",
     "mongo": "node mongo.js",
     "local": "node local.js"

+ 168 - 0
tools/test.js

@@ -0,0 +1,168 @@
+const fs = require('fs')
+const path = require('path')
+const XLSX = require('xlsx')
+// const Rsync = require('rsync')
+const {MongoConf} = require('./config')
+
+const MongoClient = require('mongodb').MongoClient
+const assert = require('assert')
+
+const dbIns = MongoConf['R2']
+
+const connStr = `mongodb://${dbIns.host}:${dbIns.port}`
+const connOpt = {
+    auth : {
+        user: dbIns.username,
+        password: dbIns.password,
+    },
+    authSource: dbIns.dbname,
+    authMechanism: "SCRAM-SHA-1",
+    useNewUrlParser: true
+}
+
+const dataPath = "../assets/scripts/data"
+const dataPrefix =  "allstar_"
+
+
+/**
+ * excel表转换为json对象
+ * @param {String} filename 文件名
+ */
+const getSheet = function (filename) {
+    let filePath = path.join(__dirname, "/excel", filename)
+    // let buf = fs.readFile(filePath);
+    let workbook = XLSX.readFileSync(filePath)
+    let sheets = workbook.SheetNames
+    let sheetsObj = {}
+    
+    sheets.forEach(n => {
+        let ws = workbook.Sheets[n]
+        let def = XLSX.utils.sheet_to_json(ws, {range: 0, header: 1})
+        let typeMap = {}
+        let typeArr = def[0]
+        let keyArr = def[2]
+        keyArr.forEach((item, index) => {
+            typeMap[item] = typeArr[index]
+        })
+
+        let res = XLSX.utils.sheet_to_json(ws, {range: 2})
+        res.forEach(item => {
+            for(let i in item) {
+                item[i] = formatType(item[i], typeMap[i])
+            }
+        })
+        sheetsObj[n] = res
+    })
+    return sheetsObj
+    
+    // console.log(XLSX.utils.sheet_to_json(ws));
+    // console.log(workbook.SheetNames);
+}
+
+/**
+ * 写入json内容到客户端assets目录
+ * @param {String} filename 文件名
+ * @param {Object} content json内容
+ */
+const writeToAssets = function (filename, content) {
+    let writeFileName = filename.replace(/\.xlsx|\.xls/, '.js').replace(dataPrefix, "")
+    let writePath = path.join(__dirname, dataPath, writeFileName)
+    fs.writeFileSync(writePath, 'module.exports=' + JSON.stringify(content))
+    // console.log(JSON.stringify(content, null, 2));
+}
+
+/**
+ * 配置表生成的json内容rsync到静态资源服务器
+ * @param {String} filename 文件名
+ * @param {Object} content json内容
+ */
+const writeJson = function (filename, content) {
+    let writeFileName = filename.replace(/\.xlsx|\.xls/, '.json').replace(dataPrefix, "")
+    let writePath = path.join(__dirname, "/sheet", writeFileName)
+
+    fs.writeFileSync(writePath, JSON.stringify(content))
+}
+
+const formatType = function(target, type) {
+    let _type = type.toLowerCase()
+    switch (_type) {
+        case 'integer':
+            target = parseInt(target, 10)
+            break;
+        case 'double':
+            target = parseFloat(target)
+            break;
+        case 'string':
+            target = target.toString()
+            break;
+        case 'object':
+            target = JSON.parse(target)
+            break;
+        case 'array':
+            target = JSON.parse(target)
+            break;
+        default:
+            break;
+    }
+    return target
+}
+
+const init = function () {
+    fs.readdir("./excel", (err, file) => {
+        file.forEach(n => {
+            if(n.match(/\~\$/)) {
+                return
+            }
+            let ws = getSheet(n)
+            let sheetName = n.replace(/\.xlsx|\.xls/, "")
+
+            let target
+            if(ws.length > 0) {
+                //单个表
+                // ws.forEach(n => {
+                //     if(typeof n['id'] != undefined) {
+                //         n['_id'] = n['id']
+                //     }
+                // })
+
+                target = ws
+            } else {
+                //多个表合拼成一个Array
+                let _array = []
+                for(let i in ws) {
+                    _array = _array.concat(ws[i])
+                }
+                target = _array                
+            }
+
+            MongoClient.connect(connStr, connOpt, (err, client) => {
+                if(err) {
+                    console.error(err.message)
+                } else {
+                    var db = client.db(dbIns.dbname)
+                    var collection = db.collection(sheetName)
+                    collection.drop(null, () => {
+                        collection.insert(target, () => {
+                            console.log(`成功更新配置表:${sheetName}`)
+                            client.close()
+                        })
+                    })
+                }
+            });
+
+            //把生成的js写入客户端assets目录
+            let wsKeys = Object.keys(ws)
+            if(wsKeys.length == 1) {
+                writeToAssets(n, ws[wsKeys[0]])
+                writeJson(n, ws[wsKeys[0]])
+            } else {
+                writeToAssets(n, ws)
+                writeJson(n, ws)
+            }
+        })
+
+        // process.exit()
+    })
+}
+
+init()