uniapp 请求request 封装

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')
	}
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值