Browse Source

✨ feat: 新增用户管理和 ServerAPI 模块

 - 新增 `User` 类及相关方法,包含用户的创建、存储和权限管理。
 - 新增 `ServerAPI` 类,用于与后端 API 进行交互,包含用户管理和考勤相关的 API 调用。
Pchen. 8 months ago
parent
commit
53617b4365
3 changed files with 326 additions and 0 deletions
  1. 94 0
      src/app/app.js
  2. 66 0
      src/app/lib/Core.js
  3. 166 0
      src/app/lib/ServerAPI.js

+ 94 - 0
src/app/app.js

@@ -0,0 +1,94 @@
+import { ServerAPI } from "./lib/ServerAPI";
+
+class User {
+    constructor() {
+        this.user = undefined;
+        this.avatar = undefined;
+        this.wxid = undefined;
+        this.session = undefined;
+        this.uuid = undefined;
+    }
+
+    static getLocalUser() {
+        try {
+            let user = User.formatUser(JSON.parse(window.localStorage['user']));
+            return user;
+        } catch (e) {
+            return undefined;
+        }
+    }
+
+    static formatUser(data) {
+        try {
+            let user = new User();
+            user.uuid = data.uuid;
+            user.session = data.session;
+            user.username = data.username;
+            user.wxid = data.wxid;
+            user.avatar = data.avatar;
+            return user;
+        } catch (e) {
+            return undefined;
+        }
+    }
+
+    static setLocalUser(user) {
+        window.localStorage['user'] = JSON.stringify(user);
+    }
+
+    getPermissions(callback = function () { }) {
+        //检查缓存
+        if (this.permissionsCache != undefined) {
+            callback(this.permissionsCache);
+            return;
+        }
+        ServerAPI.getPermissions(this.uuid, this.session, (res) => {
+            if (res == undefined || res.code != 0) {
+                callback();
+            }
+            callback(res.data);
+            this.permissionsCache = res.data;
+        });
+    }
+
+    getPermissions_sync() {
+        return new Promise((solve) => {
+            this.getPermissions(solve);
+        });
+    }
+}
+
+class _App {
+    constructor() {
+        this.user = User.getLocalUser();
+    }
+
+    setUser(user) {
+        this.user = user;
+    }
+
+    cleanUser() {
+        this.user = undefined;
+        try {
+            window.localStorage.removeItem('user');
+        } catch (e) {
+
+        }
+    }
+
+    hasUser() {
+        return this.user instanceof User;
+    }
+
+    refershUser(userJson) {
+        let user = User.formatUser(userJson);
+        this.setUser(user);
+        User.setLocalUser(user);
+    }
+}
+
+let App = new _App();
+
+export {
+    App
+}

+ 66 - 0
src/app/lib/Core.js

@@ -0,0 +1,66 @@
+class Core {
+    constructor(endpoint) {
+        this.endpoint = endpoint;
+    }
+
+    static request(path, params, requestOptions = {}, callback = () => { }) {
+        // 构建URL,如果是GET请求,将参数附加到URL中
+        let url = this.endpoint + path;
+        if (requestOptions.method === 'GET' && params) {
+            const queryString = new URLSearchParams(params).toString();
+            url += '?' + queryString;
+        }
+
+        // 设置默认的请求选项
+        const options = {
+            method: 'GET',
+            headers: {
+                'Content-Type': 'application/json',
+            },
+            ...requestOptions,
+        };
+
+        // 如果请求不是GET,将参数作为请求体发送
+        if (options.method !== 'GET' && params) {
+            options.body = JSON.stringify(params);
+        }
+
+        // 发起请求
+        fetch(url, options)
+            .then(response => {
+                if (!response.ok) {
+                    throw new Error('网络请求出错:' + response.statusText);
+                }
+                return response.json();
+            })
+            .then(data => callback(null, data))
+            .catch(error => callback(error));
+    }
+
+    // GET方法
+    static get(path, params, callback) {
+        this.request(path, params, { method: 'GET' }, callback);
+    }
+
+    // POST方法
+    static post(path, params, callback) {
+        this.request(path, params, { method: 'POST' }, callback);
+    }
+
+    // PUT方法
+    static put(path, params, callback) {
+        this.request(path, params, { method: 'PUT' }, callback);
+    }
+
+    // DELETE方法
+    static delete(path, params, callback) {
+        this.request(path, params, { method: 'DELETE' }, callback);
+    }
+
+    // PATCH方法
+    static patch(path, params, callback) {
+        this.request(path, params, { method: 'PATCH' }, callback);
+    }
+}
+
+module.exports.Core = Core;

+ 166 - 0
src/app/lib/ServerAPI.js

@@ -0,0 +1,166 @@
+import { Core } from "./Core";
+
+let core = new Core('http://127.0.0.1:8091');
+
+class ServerAPI {
+    //用户相关
+    static WXWorkUrl(num, type, callback = function () { }) {
+        core.requset(
+            "/User/WXWorkUrl",
+            {
+                num,
+                type
+            },
+            { method: 'GET' },
+            callback
+        )
+    }
+
+    static WXWorkLogin(code, callback = function () { }) {
+        core.requset(
+            "/User/WXWorkLogin",
+            {
+                code
+            },
+            { method: 'POST' },
+            callback
+        )
+    }
+
+    static UpdateInfo(uuid, session, code, callback = function () { }) {
+        core.requset(
+            "/User/UpdateInfo",
+            {
+                uuid,
+                session,
+                code
+            },
+            { method: 'POST' },
+            callback
+        )
+    }
+
+    static getPermissions(uuid, session, callback = function () { }) {
+        core.requset(
+            "/User/GetPermissions",
+            {
+                uuid,
+                session
+            },
+            { method: 'GET' },
+            callback
+        )
+    }
+
+    static checkLoginSession(uuid, session, callback = function () { }) {
+        core.requset(
+            "/User/CheckLoginSession",
+            {
+                uuid,
+                session
+            },
+            { method: 'GET' },
+            callback
+        )
+    }
+
+    //考勤相关
+    static GetAttendanceItemList(uuid, session, callback) {
+        core.requset(
+            "/Attendance",
+            {
+                uuid,
+                session
+            },
+            { method: 'Get' },
+            callback
+        )
+    }
+
+    static DeleteAttendanceItem(uuid, session, id, callback) {
+        core.requset(
+            "/Attendance",
+            {
+                uuid,
+                session,
+                project_id: id
+            },
+            { method: 'DELETE' },
+            callback
+        )
+    }
+
+    static AddAttendanceItems(data, callback) {
+        core.requset(
+            "/Attendance",
+            data,
+            { method: 'POST' },
+            callback
+        )
+    }
+
+    static Attendance(data, callback) {
+        core.requset(
+            "/Attendance",
+            data,
+            { method: 'PUT' },
+            callback
+        )
+    }
+
+    static GetMyAttendanceItems(uuid, session, callback) {
+        core.requset(
+            "/GetMyAttendanceItems",
+            {
+                uuid,
+                session
+            },
+            { method: 'Get' },
+            callback
+        )
+    }
+
+    static GetAttendanceItemDetail(uuid, session, id, callback) {
+        core.requset(
+            "/GetAttendanceItemDetail",
+            { uuid, session, project_id: id },
+            { method: 'Get' },
+            callback
+        )
+    }
+
+    static AddAttendanceRecord(uuid, session, id, callback) {
+        core.requset(
+            "/AddAttendanceRecord",
+            { uuid, session, project_id: id },
+            { method: 'Get' },
+            callback
+        )
+    }
+
+    static SupplementRecord(uuid, session, user, id, callback) {
+        core.requset(
+            "/SupplementRecord",
+            { uuid, session, user, project_id: id },
+            { method: 'Get' },
+            callback
+        )
+    }
+
+    static UserList(uuid, session, callback) {
+        core.requset(
+            "/UserList",
+            {
+                uuid,
+                session
+            },
+            { method: 'Get' },
+            callback
+        )
+    }
+
+}
+
+export {
+    ServerAPI
+}