美文网首页
uni-app 封装接口以及使用

uni-app 封装接口以及使用

作者: undefined汪少 | 来源:发表于2021-03-29 15:08 被阅读0次

1,新建api.js

const BASE_URL = 'https://222222222 /hengyuaniot-propaganda/' //后端给的url

export const myRequest = (options) => {

// console.log('进来了')

return new Promise((resolve,reject)=> {

// let authtoken = uni.getStorageSync('authtoken')

uni.request({

url:BASE_URL + options.url,

data:options.data || {},

method:options.method || 'GET',

// header:{

//    'Content-type': 'application/json',

// "X-Auth-Token": uni.getStorageSync('authtoken')  //这里的token指的是微信授权登录的token,用来判断小程序是否属于登录状态

// },

dataType:'json',

success:res => {

if(res.data.code !== 0){

}

resolve(res)

},

fail:err => {

// uni.showToast({

// title:"请求接口失败",

// })

console.log('err',err)

reject(err)

}

})

})

}

2,在全局  main.js 引入

//引入公共接口

import {myRequest} from './app/api.js'

//挂载全局

Vue.prototype.$myRequest = myRequest

3,使用

integral() {

let me =this

  me.$myRequest({

url: 'api/resource/index',

method: 'POST',

data: {

token:me.$store.state.token,

pageNum: 1,

pageSize:me.pageSize

},

}).then(res => {

// this.integrall = res.data.integral

console.log(res.data.result.list)

//成功返回的code码

var code = res.data.code

console.log(code)

//成功返回的数据

var res =res.data.result.list

if(code===200){

},

cracted(){

调用

    this.integral()

}

相关文章

网友评论

      本文标题:uni-app 封装接口以及使用

      本文链接:https://www.haomeiwen.com/subject/yysbhltx.html