mpvue promise+wx.request 封装请求

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_33871182/article/details/102743056

在这里插入图片描述
在页面中引用

this.$api.api.prototype 拿到api中定义的funciton

<script>


export default {
data() {
return {
Request: this.$api.api.prototype, //请求头
}
},

methods: {

indexShare(){
var that = this;
that.Request.indexShare('参数1','...')
.then(res =>{
//成功
// console.log(res)

})
.catch(res =>{
//失败
console.log(res)
})
}
}
</script>

接口(api.js)
名称 :登录
方法api.js

//用户名登录
 userlogin(account,password){
 let data = {
 account,
 password
 }
 return requestAll.postRequest(apiUrl + 'login', data);
 }

页面调用index.vue

var that = this;
 that.Request.userlogin(that.form.name,that.form.password)
 .then(res =>{
 //成功
 console.log(res)
 if(res.code == 1){
 wx.showModal({
 content: res.message,
 showCancel: false,
 })
 }else{
 wx.showToast({
 title: '登录中',
 icon: 'loading',
 duration: 2500,
 success:function(){
 wx.switchTab({
 url: '../home/main',
 })
 }
 })
 wx.setStorageSync('sessionid', res.data.id)
 } }) .catch(res =>{ //失败 })

猜你喜欢

转载自blog.csdn.net/qq_33871182/article/details/102743056
今日推荐