feat: add service module; 🌟

新增: 数据服务模块;
master
iczer 4 years ago
parent 3f742a4dc1
commit 3ff12474cd
  1. 10
      src/pages/login/Login.vue
  2. 5
      src/services/api.js
  3. 5
      src/services/index.js
  4. 17
      src/services/user.js
  5. 29
      src/utils/request.js

@ -75,6 +75,7 @@
<script> <script>
import CommonLayout from '@/layouts/CommonLayout' import CommonLayout from '@/layouts/CommonLayout'
import {login} from '@/services'
export default { export default {
name: 'Login', name: 'Login',
@ -97,10 +98,11 @@ export default {
this.form.validateFields((err) => { this.form.validateFields((err) => {
if (!err) { if (!err) {
this.logging = true this.logging = true
this.$axios.post('/login', { const name = this.form.getFieldValue('name')
name: this.form.getFieldValue('name'), const password = this.form.getFieldValue('password')
password: this.form.getFieldValue('password') //
}).then((res) => { login(name, password)
.then((res) => {
this.logging = false this.logging = false
const result = res.data const result = res.data
if (result.code >= 0) { if (result.code >= 0) {

@ -0,0 +1,5 @@
// const BASE_URL = 'http://localhost:8080' your service base url
const BASE_URL = '' // mock base url
module.exports = {
LOGIN: `${BASE_URL}/login`
}

@ -0,0 +1,5 @@
import {login} from './user'
export {
login
}

@ -0,0 +1,17 @@
import {LOGIN} from '@/services/api'
import {request, METHOD} from '@/utils/request'
/**
* 登录服务
* @param name 账户名
* @param password 账户密码
* @returns {Promise<AxiosResponse<T>>}
*/
function login(name, password) {
return request(LOGIN, METHOD.POST, {
name: name,
password: password
})
}
export {login}

@ -0,0 +1,29 @@
import axios from 'axios'
const METHOD = {
GET: 'get',
POST: 'post'
}
/**
* axios请求函数
* @param url 请求地址
* @param method {METHOD} http method
* @param params 请求参数
* @returns {Promise<AxiosResponse<T>>}
*/
async function request(url, method, params) {
switch (method) {
case METHOD.GET:
return axios.get(url, {params})
case METHOD.POST:
return axios.post(url, params)
default:
return axios.get(url, {params})
}
}
export {
METHOD,
request
}
Loading…
Cancel
Save