初次提交
This commit is contained in:
118
utils/api.js
Normal file
118
utils/api.js
Normal file
@@ -0,0 +1,118 @@
|
||||
import config from '../config.js'
|
||||
import storage from './storage.js'
|
||||
|
||||
const request = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: config.baseUrl + options.url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': storage.getToken() ? `Bearer ${storage.getToken()}` : '',
|
||||
...options.header
|
||||
},
|
||||
timeout: config.timeout,
|
||||
success: (res) => {
|
||||
const { statusCode, data } = res
|
||||
|
||||
if (statusCode === 200) {
|
||||
if (data.code === 0 || data.success === true) {
|
||||
resolve(data)
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: data.message || '请求失败',
|
||||
icon: 'none'
|
||||
})
|
||||
reject(data)
|
||||
}
|
||||
} else if (statusCode === 401) {
|
||||
storage.removeToken()
|
||||
storage.removeUserInfo()
|
||||
uni.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
reject(res)
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '网络错误',
|
||||
icon: 'none'
|
||||
})
|
||||
reject(res)
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.showToast({
|
||||
title: '网络连接失败',
|
||||
icon: 'none'
|
||||
})
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const api = {
|
||||
login(phone, code) {
|
||||
return request({
|
||||
url: config.api.login,
|
||||
method: 'POST',
|
||||
data: {
|
||||
phone,
|
||||
code
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
getQuestion() {
|
||||
return request({
|
||||
url: config.api.getQuestion,
|
||||
method: 'POST'
|
||||
})
|
||||
},
|
||||
|
||||
answerQuestion(sn, answer) {
|
||||
return request({
|
||||
url: config.api.answerQuestion,
|
||||
method: 'POST',
|
||||
data: {
|
||||
sn,
|
||||
answer
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
get(url, data = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'GET',
|
||||
data
|
||||
})
|
||||
},
|
||||
|
||||
post(url, data = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data
|
||||
})
|
||||
},
|
||||
|
||||
put(url, data = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data
|
||||
})
|
||||
},
|
||||
|
||||
delete(url, data = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'DELETE',
|
||||
data
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default api
|
||||
69
utils/storage.js
Normal file
69
utils/storage.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const TOKEN_KEY = 'access_token'
|
||||
const USER_INFO_KEY = 'user_info'
|
||||
const QUESTION_KEY = 'question'
|
||||
const ANSWERED_QUESTIONS_KEY = 'answered_questions'
|
||||
|
||||
const storage = {
|
||||
setToken(token) {
|
||||
uni.setStorageSync(TOKEN_KEY, token)
|
||||
},
|
||||
|
||||
getToken() {
|
||||
return uni.getStorageSync(TOKEN_KEY) || ''
|
||||
},
|
||||
|
||||
removeToken() {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
},
|
||||
|
||||
setUserInfo(userInfo) {
|
||||
uni.setStorageSync(USER_INFO_KEY, userInfo)
|
||||
},
|
||||
|
||||
getUserInfo() {
|
||||
return uni.getStorageSync(USER_INFO_KEY) || null
|
||||
},
|
||||
|
||||
removeUserInfo() {
|
||||
uni.removeStorageSync(USER_INFO_KEY)
|
||||
},
|
||||
|
||||
setQuestion(question) {
|
||||
uni.setStorageSync(QUESTION_KEY, question)
|
||||
},
|
||||
|
||||
getQuestion() {
|
||||
return uni.getStorageSync(QUESTION_KEY) || null
|
||||
},
|
||||
|
||||
removeQuestion() {
|
||||
uni.removeStorageSync(QUESTION_KEY)
|
||||
},
|
||||
|
||||
addAnsweredQuestion(sn) {
|
||||
const answeredQuestions = this.getAnsweredQuestions()
|
||||
if (!answeredQuestions.includes(sn)) {
|
||||
answeredQuestions.push(sn)
|
||||
uni.setStorageSync(ANSWERED_QUESTIONS_KEY, answeredQuestions)
|
||||
}
|
||||
},
|
||||
|
||||
getAnsweredQuestions() {
|
||||
return uni.getStorageSync(ANSWERED_QUESTIONS_KEY) || []
|
||||
},
|
||||
|
||||
isQuestionAnswered(sn) {
|
||||
const answeredQuestions = this.getAnsweredQuestions()
|
||||
return answeredQuestions.includes(sn)
|
||||
},
|
||||
|
||||
clearAnsweredQuestions() {
|
||||
uni.removeStorageSync(ANSWERED_QUESTIONS_KEY)
|
||||
},
|
||||
|
||||
clearAll() {
|
||||
uni.clearStorageSync()
|
||||
}
|
||||
}
|
||||
|
||||
export default storage
|
||||
Reference in New Issue
Block a user