Browse Source

feat: 添加登录接口

wll8 5 months ago
parent
commit
c5743c2a76
2 changed files with 60 additions and 10 deletions
  1. 45 0
      mm/mm.config.js
  2. 15 10
      src/views/Login.vue

+ 45 - 0
mm/mm.config.js

@@ -49,6 +49,51 @@ module.exports = async (util) => {
           )
         },
       }),
+      'post /api/login': util.side({
+        tags: [`公用`],
+        summary: `登录`,
+        schema: {
+          body: joi
+            .object({
+              userName: joi
+                .string()
+                .default(`admin`)
+                .required()
+                .description(`用户名`),
+              userPwd: joi
+                .string()
+                .default(`123456`)
+                .required()
+                .description(`密码`),
+            })
+            .description(`用户信息`),
+        },
+        async action(req, res) {
+          const { userName, userPwd } = req.body
+          if (userName == `admin` && userPwd == `123456`) {
+            res.json(
+              wrapApiData(
+                util.libObj.mockjs.mock({
+                  data: {
+                    token: `@uuid`,
+                  },
+                })
+              )
+            )
+          } else {
+            res.status(401).json(
+              wrapApiData(
+                util.libObj.mockjs.mock({
+                  code: 401,
+                  data: {
+                    message: `用户名或密码错误`,
+                  },
+                })
+              )
+            )
+          }
+        },
+      }),
       'get /api/mapOfTongren': util.side({
         tags: [`公用`],
         summary: `铜仁地图数据`,

+ 15 - 10
src/views/Login.vue

@@ -91,18 +91,23 @@ export default {
   created() {},
   mounted() {},
   methods: {
-    login() {
-      if (this.userName == `admin` && this.userPwd == `123456`) {
-        this.$router.push({
-          path: `/page1`,
+    async login() {
+      await this.$http
+        .post(`/api/login`, {
+          userName: this.userName,
+          userPwd: this.userPwd,
         })
-      } else {
-        this.$Toast({
-          content: `请输入正确的用户名和密码`,
-          type: `error`,
-          // hasClose: true
+        .then((res) => {
+          this.$router.push({
+            path: `/page1`,
+          })
+        })
+        .catch(() => {
+          this.$Toast({
+            content: `请输入正确的用户名和密码`,
+            type: `error`,
+          })
         })
-      }
     },
     confirm() {
       this.visible = false