当前位置:网站首页>微信小程序接口封装(uniapp)
微信小程序接口封装(uniapp)
2022-04-22 05:58:00 【herry-弟弟】
1.新建common文件夹,里面新建一个http.js文件,代码如下:
const BASE_URL = "http://localhost:8087" //公共请求头,端口号写你的
// const TOKEN = uni.getStorageSync('TOKEN') //TOKEN
const request = (url, method, data) => {
return new Promise((resolve, reject) => {
uni.request({
url: BASE_URL + url, //仅为示例,并非真实接口地址。
data: data,
method: method,
header: {
//请求头可自定义
'Content-Type': 'application/json'
// 'X-Access-Token': TOKEN
},
success: (res) => {
//具体捕获请看自己接口返回的形式
if (res.data.status == 200) {
resolve(res)
} else {
uni.showModal({
title: '提示',
showCancel: false,
content: res.data.msg,
success(res) {
if (res.confirm) {
// console.log('用户点击确定')
uni.navigateBack({
})
} else if (res.cancel) {
// console.log('用户点击取消')
}
}
})
}
},
fail(error) {
reject(error)
},
complete() {
}
});
})
}
export default {
request //向外暴露request
}
2.同样在common文件夹中新建request.js文件,代码如下:
import request from './http.js' //引入common中的http.js
const api = request
export default {
// 登录
userLogin: (data) => {
return api.request('/user/login', 'POST', data) //接口地址
},
// 注册
register:(data)=>{
return api.request('/user/register','POST',data)
}
}
3.在main.js中引入
import myRequest from './common/request.js'
// 挂载到全局使用
Vue.prototype.$myRequest = myRequest
4.在页面中使用
data() {
return {
login: {
userName: '某某某',
password: '00000000'
}
};
},
methods:{
login(){
this.$myRequest.userLogin(this.login).then(res => {
console.log(res)
})
}
}
版权声明
本文为[herry-弟弟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43123560/article/details/120726630
边栏推荐
- Solutions for minors protection in the social industry of digital beauty technology: guarding the "social circle" of minors
- 瀑布流布局 js定位
- Pgdoucer best practices: a series
- COM in Wine(2)——基本代码分析
- Usage rules of cast function in MySQL
- Mysql 与Oracle使用
- 数美科技未成年人保护解决方案重磅上线,开启未成年人网络护航新时代
- 创新实训(十二)爬虫
- 从零开始学安卓(kotlin)五——UI
- MYSQL 使用OR查询SQL执行很慢
猜你喜欢
随机推荐
Functions and differences between synchronized keyword and volatile keyword
数美科技社交行业未成年人保护解决方案:守护未成年人的“社交圈”
Solutions for minors protection in the social industry of digital beauty technology: guarding the "social circle" of minors
TiDB分表唯一主键ID——sequence 与gorm无法获取主键的解决
利用win自带功能让处于同一局域网的两个电脑之间互传文件(速度和本地磁盘间互传相同)
花费四天时间呕心沥血写了MySQL的全部笔记,很适合初学者的笔记。
Flink源码之StreamExecutionEnvironment
获取日期之间的天数、获取中文日期、获得日期的下一个星期一的日期、获取工作日、获取休息日
Golang select优先级执行
Mysql 与Oracle使用
小程序自定义原生底部导航
2021-07-07
创新实训(七)FBV视图&CBV视图
使用Navicat 备份mysql数据库
MySQL——索引
TCP和UDP区别详解
Mapping of oid and relfilenode in PostgreSQL
小程序定时任务的多种写法
报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column
2021-07-07









