Created
          February 19, 2019 11:10 
        
      - 
      
- 
        Save sdlong/7d020572fa0c3155c5efb1f571ed7fa8 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | .all('/get/goto_*', [ValidateToken], (req, res, next)=>{ | |
| var jwt = req.jwt | |
| var jwtInfo = req.jwtInfo | |
| var userIP = req.userIP | |
| var secret = req.secret | |
| // 從 request 進來取得的值 | |
| var get = req.query; // 取得GET值 | |
| var post = req.body; | |
| var actCode = req.params[0] | |
| if (!post || !post.EnterpriseID || !post.UserCode || !post.FunctionID) return res.send(Err("未輸入必填值!")); | |
| // API 指向設定 | |
| var troute = actCode.replace('goto_', '').split('_').map(v=> v.replace(/\$/g, '_')); | |
| var apiBaseUrl = "http://59.120.117.73:8000"; | |
| var api_attributes = {} | |
| api_attributes.url = [apiBaseUrl].concat(troute).join('/') | |
| api_attributes.contentType = "application/json" | |
| // 從 request 裡取 Kong gateway 所需的 Key, Secret | |
| var kong = JSON.parse(libs.aesDecrypt(jwtInfo.note, secret)); | |
| // 設定 Kong JWT | |
| var kongJWT = libs.createToken( { iss: kong.key }, '60s', kong.secret ); | |
| // api header 權限放入 Kong JWT | |
| api_attributes.header = { Authorization: 'Bearer '+ kongJWT }; | |
| // 判斷到期時間 | |
| var timeout = Math.floor(new Date().getTime() / 1000); // 目前時間 | |
| var overtime = (jwtInfo.exp - timeout) / 60; // 到期時間轉為分鐘 | |
| var needReloadJwt = Math.floor(overtime) < 5; // 小於5分鐘時 | |
| // 回傳新的 JWT 的設定 | |
| var newjwt = (needReloadJwt) ? libs.createToken({ | |
| EnterpriseID: jwtInfo.EnterpriseID, // 企業號 | |
| dbLocation: jwtInfo.dbLocation, // 此企業號的 DB 位置 | |
| StoreID: jwtInfo.OrgCode, // 門店ID | |
| StoreName: jwtInfo.OrgName, // 門店名稱 | |
| UserCode: jwtInfo.UserCode, // 請求人員名稱 | |
| UserName: jwtInfo.UserName, // 請求人員名稱 | |
| note: libs.aesEncrypt(JSON.stringify(kong), secret), // 給 Kong 用的 jwt key & secret | |
| ip: userIP | |
| }, | |
| '1200s', // 有效時間 20 分鐘 | |
| secret | |
| ) : jwt; | |
| // 取使用者的選單權限 | |
| db8012Request | |
| .input('EnterpriseID', mssql.VarChar, post.EnterpriseID) | |
| .input('UserCode', mssql.VarChar, post.UserCode) | |
| .query("SELECT SUBSTRING (CONVERT (NVARCHAR (MAX),Roleuniquetext),2,DATALENGTH(Roleuniquetext)-2) as Data FROM S_Role WHERE EnterPriseID='jinher' AND RoleCode IN (SELECT RoleCode FROM S_User_Role WHERE EnterPriseID='jinher' AND UserCode='allentest')", (err, bc) => { | |
| if (bc.recordsets[0].length === 0) return _next(Err('選單權限錯誤')); | |
| // 將取得的 FunctionIDs 合併 + 拆成 array + unique (去除重複) + 排序 | |
| var FunctionArray = bc.recordsets[0].map(item => {return item.Data}).join("#").split("#").filter((value, index, self) => {return self.indexOf(value) === index;}).sort() | |
| var FunctionIDs = "" | |
| FunctionArray.forEach(item => { FunctionIDs += `'${item}', ` }) | |
| // 將整理出來的 FunctionIDs 塞入 api_attributes.data | |
| if (!post.values) post.values = {} | |
| post.values.canAccessFunctionIDs = (FunctionIDs.slice(0, -2)) | |
| api_attributes.data = JSON.stringify(post); | |
| // 打外部 request API | |
| requestModel.init().request(api_attributes, (backData)=>{ | |
| // 文字轉成 JSON.parse 可吃的格式 (ref: https://stackoverflow.com/questions/16213864) | |
| var rawResData = backData.replace(/([a-zA-Z0-9]+?):/g, '"$1":') | |
| var resData = JSON.parse(rawResData) | |
| res.send({ | |
| note: newjwt, | |
| response: resData | |
| }); | |
| }) | |
| }); | |
| }) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment