Selaa lähdekoodia

add basic comment

WuXinyu 7 vuotta sitten
vanhempi
commit
069068a12f

+ 1 - 1
mock/modules/auth/index.js

@@ -30,7 +30,7 @@ router.route("/login").post((req, res) => {
 
 router.route("/register").post((req, res) => {
   //注册
-  res.send({});
+  res.send({ data: UserSerializer() });
   // res.status(418).send({ message: '用户名已存在' });
 });
 

+ 23 - 0
mock/modules/group/index.js

@@ -0,0 +1,23 @@
+const express = require("express");
+const router = express.Router();
+const GroupSerializer = require("../../serializers/GroupSerializer");
+
+router
+  .route("")
+  .get((req, res) =>
+    res.send({
+      data: [GroupSerializer()]
+    })
+  )
+  .post((req, res) =>
+    res.send({
+      data: GroupSerializer()
+    })
+  )
+  .put((req, res) =>
+    res.send({
+      data: GroupSerializer()
+    })
+  );
+
+module.exports = router;

+ 4 - 2
mock/routes.js

@@ -3,7 +3,9 @@ const router = express.Router();
 
 router.use("/auth", require("./modules/auth"));
 router.use("/user", require("./modules/user"));
-router.use("/tag", require("./modules/tag"));
-router.use("/doc", require("./modules/doc"));
+// router.use("/tag", require("./modules/tag"));
+// router.use("/doc", require("./modules/doc"));
+router.use("/course", require("./modules/course"));
+router.use("/group", require("./modules/group"));
 
 module.exports = router;

+ 13 - 0
mock/serializers/CourseSerializer.js

@@ -1,6 +1,19 @@
 const makeId = require("../util/makeId");
 const UserSerializer = require("./UserSerializer");
 
+/**
+ * @description 课程信息
+ * @typedef {Object} CourseSerializerType
+ * @property {number|string} id 课程id
+ * @property {string} name 课程名称
+ * @property {string} code 选课码
+ * @property {UserSerializerType} teacher 老师信息
+ */
+
+/**
+ * @description 生成课程信息
+ * @returns {CourseSerializerType}
+ */
 module.exports = () => {
   return {
     id: makeId(),

+ 29 - 0
mock/serializers/GroupSerializer.js

@@ -0,0 +1,29 @@
+const makeId = require("../util/makeId");
+const UserSerializer = require("./UserSerializer");
+
+/**
+ * @description 小组信息
+ * @typedef {Object} GroupSerializerType
+ * @property {number|string} id 小组id
+ * @property {string} name 小组名称
+ * @property {string} code 选组码
+ * @property {UserSerializerType[]} members 组员信息
+ */
+
+/**
+ * @description 生成课程信息
+ * @returns {CourseSerializerType}
+ */
+module.exports = () => {
+  return {
+    id: makeId(),
+    name: "tag-maker",
+    code: "xxxxx",
+    members: [
+      UserSerializer(),
+      UserSerializer(),
+      UserSerializer(),
+      UserSerializer()
+    ]
+  };
+};

+ 14 - 0
mock/serializers/UserSerializer.js

@@ -1,5 +1,19 @@
 const makeId = require("../util/makeId");
 
+/**
+ * @description 用户基本信息
+ * @typedef {Object} UserSerializerType
+ * @property {number|string} id 用户id
+ * @property {string} role 用户身份
+ * @property {string} nickname 用户昵称
+ * @property {string} [bio] 用户描述
+ */
+
+/**
+ * 生成基本用户信息
+ * @param role
+ * @returns {UserSerializer}
+ */
 module.exports = (role = "STUDENT") => {
   return {
     id: makeId(),

+ 4 - 2
src/api/_prefix.js

@@ -1,6 +1,8 @@
-export const API_VERSION = "/api/v2";
+export const API_VERSION = "/api";
 
-export const AUTH_MODULE = `${API_VERSION}/authorization`;
+export const AUTH_MODULE = `${API_VERSION}/auth`;
 export const USER_MODULE = `${API_VERSION}/user`;
 export const PHOTO_MODULE = `${API_VERSION}/photo`;
 export const TAG_MODULE = `${API_VERSION}/tag`;
+export const COURSE_MODULE = `${API_VERSION}/course`;
+export const GROUP_MODULE = `${API_VERSION}/group`;

+ 12 - 4
src/api/authorization.js → src/api/auth.js

@@ -2,9 +2,13 @@ import request from "@/util/request";
 import { AUTH_MODULE } from "./_prefix";
 
 /**
- * 登陆 login
  * @param {{username,password}} 登陆信息
- * @returns {{username,password}}
+ */
+/**
+ * 登陆 login
+ * @param {string} username
+ * @param {string} password
+ * @returns {Promise<{data:UserSerializerType}>}
  */
 export const login = ({ username, password }) => {
   return request(`${AUTH_MODULE}/login`, {
@@ -18,12 +22,16 @@ export const login = ({ username, password }) => {
 
 /**
  * 注册 register
- * @param {{username,password}}  注册信息
+ * @param {string} nickname
+ * @param {string} username
+ * @param {string} password
+ * @returns {Promise<{data:UserSerializerType}>}
  */
-export const register = ({ username, password }) => {
+export const register = ({ nickname, username, password }) => {
   return request(`${AUTH_MODULE}/register`, {
     method: "POST",
     body: {
+      nickname: nickname || null,
       username: username || null,
       password: password || null
     }

+ 48 - 0
src/api/course.js

@@ -0,0 +1,48 @@
+import request from "@/util/request";
+import { COURSE_MODULE } from "./_prefix";
+
+/**
+ * 获得老师的课程
+ * @returns {Promise<{data:CourseSerializerType[]}>}
+ */
+export const getTeacherCourses = () => {
+  return request(`${COURSE_MODULE}/teacher`);
+};
+
+/**
+ * 获得学生的课程
+ * @returns {Promise<{data:CourseSerializerType[]}>}
+ */
+export const getStudentCourses = () => {
+  return request(`${COURSE_MODULE}/student`);
+};
+
+/**
+ * 添加老师的课程
+ * @param {string} name
+ * @param {string} code
+ * @returns {Promise<{data:CourseSerializerType}>}
+ */
+export const postTeacherCourse = ({ name, code }) => {
+  return request(`${COURSE_MODULE}/teacher`, {
+    method: "POST",
+    body: {
+      name: name || null,
+      code: code || null
+    }
+  });
+};
+
+/**
+ * 学生加入课程
+ * @param {string} code
+ * @returns {Promise<{data:CourseSerializerType}>}
+ */
+export const postStudentCourse = code => {
+  return request(`${COURSE_MODULE}/student`, {
+    method: "POST",
+    body: {
+      code
+    }
+  });
+};

+ 41 - 0
src/api/group.js

@@ -0,0 +1,41 @@
+import request from "@/util/request";
+import { GROUP_MODULE } from "./_prefix";
+
+/**
+ * 学生创建小组
+ * @param {string} name
+ * @param {string|number} courseId
+ * @returns {Promise<{data:GroupSerializerType}>}
+ */
+export const postStudentGroup = ({ name, courseId }) => {
+  return request(`${GROUP_MODULE}`, {
+    method: "POST",
+    body: {
+      name: name || null,
+      courseId: courseId || null
+    }
+  });
+};
+
+/**
+ * 获得学生小组
+ * @param {string|number} courseId 课程id
+ * @returns {Promise<{data:GroupSerializerType[]}>}
+ */
+export const getStudentGroup = (courseId = -1) => {
+  return request(`${GROUP_MODULE}?courseId=${courseId}`);
+};
+
+/**
+ * 加入学生分组
+ * @param {string} code
+ * @returns {Promise<{data:CourseSerializerType}>}
+ */
+export const putStudentGroup = code => {
+  return request(`${GROUP_MODULE}`, {
+    method: "POST",
+    body: {
+      code: code || null
+    }
+  });
+};

+ 38 - 4
src/api/user.js

@@ -2,10 +2,44 @@ import request from "@/util/request";
 import { USER_MODULE } from "./_prefix";
 
 /**
- * 获取用户数据
+ * 获得用户的信息
  * @param userId
- * @returns {Promise<{avatar:string,username:string,nickname:string,bios:string}>}
+ * @returns {Promise<{data:UserSerializerType}>}
  */
-export const fetchUserAuthProfile = userId => {
-  return request(`${USER_MODULE}/auth/${userId}`);
+export const fetchUserProfile = userId => {
+  return request(`${USER_MODULE}/${userId}`);
+};
+
+/**
+ * 修改用户信息
+ * @param userId
+ * @param {string} nickname 昵称
+ * @param {string} bio 自我介绍
+ * @returns {Promise<{data:UserSerializerType}>}
+ */
+export const postUserProfile = (userId, { nickname, bio }) => {
+  return request(`${USER_MODULE}/${userId}`, {
+    method: "POST",
+    body: {
+      nickname: nickname || null,
+      bio: bio || null
+    }
+  });
+};
+
+/**
+ * 更新用户密码
+ * @param userId
+ * @param {string} oldPassword 旧密码
+ * @param {string} newPassword 新密码
+ * @returns {Promise<{data:UserSerializerType}>}
+ */
+export const postUserPassword = (userId, { oldPassword, newPassword }) => {
+  return request(`${USER_MODULE}/${userId}/password`, {
+    method: "POST",
+    body: {
+      oldPassword: oldPassword || null,
+      newPassword: newPassword || null
+    }
+  });
 };

+ 2 - 2
src/store/auth.module.js

@@ -1,4 +1,4 @@
-import { login, register } from "@/api/authorization";
+import { login, register } from "@/api/auth";
 import { LOGIN, LOGOUT, REGISTER } from "@/store/type/actions.type";
 import {
   SET_AUTH,
@@ -7,7 +7,7 @@ import {
   SET_REGISTER_ERROR,
   SET_PROFILE
 } from "@/store/type/mutations.type";
-import { getToken, destroyToken, saveToken } from "@/util/token";
+import { getToken, destroyToken, saveToken } from "@/util/tokenStorage";
 
 const state = {
   isAuthenticated: !!getToken(),

+ 30 - 0
src/util/profileStorage.js

@@ -0,0 +1,30 @@
+const USER_PROFILE = "user_profile";
+
+/**
+ * 保存用户信息
+ * @param profile
+ */
+export const saveProfile = profile =>
+  profile && window.localStorage.setItem(USER_PROFILE, JSON.stringify(profile));
+
+/**
+ * 获得用户信息
+ * @return {UserSerializerType} 用户信息
+ * @exception {Error} profile not found
+ */
+export const getUserProfile = () => {
+  const profileText = window.localStorage.getItem(USER_PROFILE);
+  let profile = {};
+  try {
+    profile = JSON.parse(profileText);
+  } catch (e) {
+    throw new Error("profile not found");
+  }
+  return profile;
+};
+
+/**
+ * 删除profile
+ */
+export const destroyProfile = () =>
+  window.localStorage.removeItem(USER_PROFILE);

+ 1 - 1
src/util/request.js

@@ -36,7 +36,7 @@ function checkStatus(response) {
  * Requests a URL, returning a promise.
  *
  * @param  {string} url       The URL we want to request
- * @param  {object} [options] The options we want to pass to "fetch"
+ * @param  {{method:string,body}} [options] The options we want to pass to "fetch"
  * @return {object}           An object containing either "data" or "err"
  */
 export default async function request(url, options) {

+ 8 - 8
src/util/token.js → src/util/tokenStorage.js

@@ -1,23 +1,23 @@
 const ID_TOKEN_KEY = "id_token";
 const USER_PROFILE = "user_profile";
 
+/**
+ * 获得token
+ * @return {string|null} token
+ */
 export const getToken = () => {
   const token = window.localStorage.getItem(ID_TOKEN_KEY);
   if (token === "undefined") {
-    return false;
+    return null;
   } else {
-    return window.localStorage.getItem(ID_TOKEN_KEY);
+    return token;
   }
 };
 
-export const saveToken = (token, profile) => {
+export const saveToken = token =>
   token && window.localStorage.setItem(ID_TOKEN_KEY, token);
-  profile && window.localStorage.setItem(USER_PROFILE, JSON.stringify(profile));
-};
 
-export const destroyToken = () => {
-  window.localStorage.removeItem(ID_TOKEN_KEY);
-};
+export const destroyToken = () => window.localStorage.removeItem(ID_TOKEN_KEY);
 
 export const getUserProfile = () => {
   const profileText = window.localStorage.getItem(USER_PROFILE);