http.js
const baseUrl = "http://localhost:8080/api"
const $http ={
httpRequest:function(options={}){
// console.log(options,'options')
// 设置请求头信息
options.header={
'Content-Type': 'application/json;charset=UTF-8',
}
//请求地址
var url=baseUrl+options.url
return new Promise((resolve,reject)=>{
uni.request({
url,
method:options.method,
data:options.data || {},
header:options.header,
success:(res)=> {
if(res.data.code==200){
resolve(res.data)
}else{
uni.hideLoading();
uni.showToast({
title:res.data.msg,
icon: 'none',
duration: 2000
});
}
},
fail: (res) => {
reject(res)
}
})
})
},
$httpRequest:function(options={}){
console.log(options,'options')
// 设置请求头信息 每次请求添加token
options.header={
'Content-Type': 'application/json;charset=UTF-8',
"Authorization":'Bearer '+ uni.getStorageSync('token'),
}
//请求地址
var url=baseUrl+options.url
console.log(url,'url')
return new Promise((resolve,reject)=>{
//手动将params的信息添加都url中
if(options.params){
url+='?'
for(var key in options.params){
console.log(key,'key')
console.log(options.params[key],'params[key]')
url+=key+'='+options.params[key]+'&'
}
url=url.slice(0, url.lastIndexOf('&'))
}
uni.request({
url,
method:options.method,
data:options.data,
header:options.header,
success:(res)=> {
console.log(res,'res')
if(res.data.code==200){
resolve(res.data)
}else{
// uni.hideLoading();
uni.showToast({
title:res.data.msg,
icon: 'none',
duration: 2000
});
}
},
fail: (res) => {
reject(res)
}
})
})
},
}
export default $http
app.js
import $http from "./http.js"
export default {
//获取验证码图片
getCodeImg(){
return $http.httpRequest({
url: '/captchaImage',
method: 'get'
})
},
//登陆
login(data){
return $http.httpRequest({
url: '/login',
method: 'post',
data,
})
},
//获取用户详情
getInfo() {
return $http.$httpRequest({
url: '/getInfo',
method: 'get',
data:{}
})
},
//退出登陆
logout(){
return $http.$httpRequest({
url: '/logout',
method: 'post'
})
},
// 查询法规列表
listRegulation(query) {
return $http.$httpRequest({
url: '/system/regulation/list',
method: 'get',
params: query
})
},
// 修改用户个人信息
updateUserProfile(data) {
return $http.$httpRequest({
url: '/system/user/profile',
method: 'put',
data: data
})
},
// 用户密码重置
updateUserPwd(oldPassword, newPassword) {
const data = {
oldPassword,
newPassword
}
return $http.$httpRequest({
url: '/system/user/profile/updatePwd',
method: 'put',
params: data
})
},
}
main.js
import App from './App'
// #ifndef VUE3
import Vue from 'vue'
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
Vue.config.productionTip = false
import api from './util/api.js'
// 引入封装$api
Vue.prototype.$api=api
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
// #endif
// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
return {
app
}
}
// #endif
页面中js代码
methods: {
submitForm() {
var _that=this;
console.log(11111)
this.$refs.userInfo.validate().then(res=>{
_that.$api.login(_that.userInfo).then(restule=>{
uni.setStorageSync('token',restule.token)
_that.$api.getInfo().then(data=>{
uni.setStorageSync('userInfo',data.user)
ni.switchTab({
url: '/pages/home/home'
});
console.log(data,'用户详情')
})
});
console.log('表单数据信息:', res);
}).catch(err =>{
console.log('表单错误信息:', err);
})
},
//获取验证码
getCaptchaImage(){
this.$api.getCodeImg().then(res=>{
console.log(res,'图片')
this.codeUrl = "data:image/gif;base64," + res.img;
this.userInfo.uuid=res.uuid
});
},
//表单验证
setRulesUserInfo(callback){
console.log(callback,'callback')
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)