Browse Source

[Mod] update api

XuPeng 1 year ago
parent
commit
77e5f85429
6 changed files with 177 additions and 17 deletions
  1. 41 0
      api/massmessage.js
  2. 126 0
      api/organization.js
  3. 4 0
      api/publish.js
  4. 4 0
      api/upload.js
  5. 0 16
      nginx.conf
  6. 2 1
      package.json

+ 41 - 0
api/massmessage.js

@@ -0,0 +1,41 @@
+/**
+ * 群发接口封装
+ * @author xupeng
+ */
+const micro = require('micro')
+const sdk = require("finchat-js-sdk")
+const { json, send } = micro
+
+module.exports = async (req, res) => {
+  const params = await json(req)
+  const { userId, baseUrl, accessToken, roomIds, content } = params
+  try {
+    const matrixClient = sdk.createClient({
+      baseUrl,
+      accessToken,
+      userId
+    })
+    const data = await sendMessage(matrixClient, roomIds, content)
+    send(res, 200, {
+      status: true,
+      data: data.filter(item => !!item)
+    })
+      
+  } catch (error) {
+    send(res, 400, {
+      status: false,
+      data: error
+    })
+  }
+}
+
+async function sendMessage(matrixClient, roomIds, content) {
+  return Promise.all(roomIds.map(async (roomId) => {
+    try {
+      const data = await matrixClient.sendMessage(roomId, content);
+      return roomId
+    } catch (error) {
+      return null
+    }
+  }))
+}

+ 126 - 0
api/organization.js

@@ -0,0 +1,126 @@
+/**
+ * 通讯录接口封装
+ * @author xupeng
+ */
+const micro = require('micro')
+const { FinChatNormal } = require('finchat-js-sdk')
+const { json, send } = micro
+
+const store = new Map()
+
+module.exports = async (req, res) => {
+  const params = await json(req)
+  try {
+    const data = await getData(params)
+    send(res, 200, {
+      status: true,
+      data
+    })
+      
+  } catch (error) {
+    console.log(error)
+    send(res, 400, {
+      status: false,
+      data: error
+    })
+  }
+}
+
+async function getData(params) {
+  const fcNormal = new FinChatNormal()
+  const { baseUrl, appType, userId, apiToken, accessToken } = params;
+  fcNormal.init(
+    baseUrl, appType, userId, apiToken, accessToken,
+  );
+
+  const friendList = await getFriendList(userId, fcNormal)
+  
+  const storedData = store.get(userId)
+  if (storedData && ((Date.now() - storedData.timeStamp) < 2 * 60 * 60 * 1000 || storedData.friendList.length === friendList.length)) {
+    return storedData.branches
+  }
+
+
+  const response = await fcNormal.getContactTopo(userId, 'branches');
+  let myBranches = response.data.branches || [];
+  if (response.status && myBranches.length > 0) {
+    const branches = {
+      id: myBranches[0].id,
+      name: myBranches[0].name,
+      type: 'branch',
+      children: [],
+      avatar: 'company',
+    };
+    branches.children = [];
+    branches.total = 0;
+    await getBranches(branches, fcNormal, { ...params, friendList });
+
+    store.set(userId, {
+      timeStamp: Date.now(),
+      friendList,
+      branches
+    })
+    return branches;
+  }
+  return []
+}
+
+async function getBranches(parentBranches, fcNormal, data) {
+  const id = parentBranches.id;
+  let response = {};
+  response = await fcNormal.getBranchesList(data.userId, id);
+  if (response.status && response.data.branches) {
+    for (let i = 0; i < response.data.branches.length; i += 1) {
+      const branch = {
+        id: response.data.branches[i].id,
+        name: response.data.branches[i].name,
+        type: 'branch',
+        children: [],
+        avatar: 'group',
+      };
+      const children = await getEmployees(branch.id, branch.name, fcNormal, data); // eslint-disable-line
+      if (children.length) {
+        branch.children = children;
+        branch.total = children.length;
+        parentBranches.children.push(branch);
+        parentBranches.total += branch.total;
+      }
+    }
+  }
+}
+
+async function getEmployees(branchId, branchName, fcNormal, data) {
+  let response1 = {};
+  response1 = await fcNormal.getEmployeesByBranchId(data.userId, branchId);
+
+  if (response1.status) {
+    const results = await Promise.all(response1.data.users.map(async (user) => {
+      const response2 = await fcNormal.getAvatarId(user.fcid);
+      let avatar = '';
+      if (response2.status) {
+        avatar = response2.data;
+      }
+      const friendInfo = data.friendList.find(friend => friend.toFcid === user.fcid);
+      return {
+        id: user.fcid,
+        name: user.name,
+        type: 'item',
+        roomId: friendInfo && friendInfo.roomId,
+        info: {
+          branch: branchName,
+        },
+        avatar,
+      };
+    }));
+    return results;
+  }
+  return [];
+}
+
+async function getFriendList(userId, fcNormal) {
+  const response = await fcNormal.getFriendsList(userId);
+  if (response.status) {
+    return response.data;
+  } 
+  return [];
+}

+ 4 - 0
api/publish.js

@@ -1,3 +1,7 @@
+/**
+ * 桌面版发布接口
+ * @author xupeng
+ */
 const micro = require('micro')
 const path = require('path')
 const low = require('lowdb')

+ 4 - 0
api/upload.js

@@ -1,3 +1,7 @@
+ /**
+ * 上传静态页面接口
+ * @author xupeng
+ */ 
 const micro = require('micro')
 const path = require('path')
 const childProcess = require("child_process")

+ 0 - 16
nginx.conf

@@ -101,22 +101,6 @@ http {
 
     rewrite /apps /finchat-publish-website last;
 
-    location ^~ /api/massmessage {
-      proxy_redirect off;
-      proxy_set_header Host $host;
-      proxy_set_header X-Real-IP $remote_addr;
-      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-      proxy_pass http://finchat-node-middleware.finchat-node-middleware:9000;
-    }
-
-    location ^~ /api/organization {
-      proxy_redirect off;
-      proxy_set_header Host $host;
-      proxy_set_header X-Real-IP $remote_addr;
-      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-      proxy_pass http://finchat-node-middleware.finchat-node-middleware:9000;
-    }
-
     location ^~ /api/ {
       proxy_redirect off;
       proxy_set_header Host $host;

+ 2 - 1
package.json

@@ -16,6 +16,7 @@
     "micro-cors": "^0.1.1",
     "micro-upload": "^1.0.1",
     "microrouter": "^3.1.3",
-    "pm2": "^3.1.3"
+    "pm2": "^3.1.3",
+    "finchat-js-sdk": "^1.3.10"
   }
 }