1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- //页面中想用.then()就必须是Prnmise实例
- export default {
- config: {
- baseUrl: "http://localhost:8201", //本地开发
- // baseUrl: "https://talent.younggee.com:8201", //默认的公共域名
- // baseUrl: "https://139.9.53.143:8201", //默认的公共域名
- },
- request(options) {
- return new Promise((resolve, reject) => { //把调取的的接口给了一个Promise实例
- uni.request({ //uni-app调接口
- ...options, //所有的数据通过options传过来
- success: res => {
- if (options.native) {
- //假如用户得到res的原型数据,就在页面第三个参数把native:true就好了
- resolve(res)
- }
- if (res.statusCode === 200) {
- //等于200证明请求接口成功,就把里面的data数据给用户返回,
- //这里只返回data数据,用户想要原型数据,就在页面的第三个参数里写:native:true
- resolve(res)
- } else {
- //调取接口错误时返回给程序员
- reject(res)
- }
- }
- })
- })
- },
- //get方法
- get(url, data = {}, header = {}, options = {}) {
- options.url = this.config.baseUrl + url
- options.data = data
- options.method = "get"
- return this.request(options)
- },
- //post方法
- post(url, data = {}, options = {}) {
- options.url = this.config.baseUrl + url
- options.data = data
- options.method = "post"
- return this.request(options)
- }
- }
|