diff --git a/common/requestServer.js b/common/requestServer.js index ec0ccf0..7ca0fbd 100644 --- a/common/requestServer.js +++ b/common/requestServer.js @@ -1,73 +1,64 @@ import common from "./common.js"; - export const myRequest = (options) => { - // 调接口加载 - uni.showLoading({ - title: "加载中", - mask: true, - }); - return new Promise((resolve, reject) => { - uni.request({ - url: options.url, - //默认参数 - data: options.data || {}, - // 配置请求头参数-例如token - header: { - 'content-type': 'application/json', - Token: common.SetToken, - // Accept: 'application/json', - // 'Content-Type': 'application/json', - // 'X-Requested-With': 'XMLHttpRequest' - }, - dataType: "json", - method: options.method || 'GET', - // sslVerify: true, - - // 接口请求成功 - success: (res) => { - // 关闭加载 - uni.hideLoading(); - - - // 调用成功且有数据 返回数据 组件内通过 .then() 或者async await 接受异步返回数据 - //resolve(res.data) - //在接口200 调用成功后 才能进行判断接口内的状态码 return_code 以此判定作何操作和提示 - let result=res.data - console.log( result); - let return_code = result.data.code - let return_message = result.data.message - console.log(typeof return_code) - console.log(return_code) - switch (return_code) { - case '0': - // 成功的数据data状态码 则直接返回数据 - resolve(res.data) - break - - default: - // 其他的如无特定要求 则做提示 - // reject(res.data) - return uni.showToast({ - title: return_message, - duration: 2000, - icon: 'none', - }) - } - }, - - // 接口接口失败 - fail: (error) => { - // 关闭加载 - uni.hideLoading(); - console.log(2, error); - uni.showToast({ - title: '请求接口失败', - icon: "none", - mask: true, - }) - // 失败数据 - reject(error) - } + // 调接口加载 + uni.showLoading({ + title: "加载中", + mask: true, + }); + return new Promise((resolve, reject) => { + uni.request({ + url: options.url, + //默认参数 + data: options.data || {}, + // 配置请求头参数-例如token + header: { + 'content-type': 'application/json', + Token: common.SetToken, + // Accept: 'application/json', + // 'Content-Type': 'application/json', + // 'X-Requested-With': 'XMLHttpRequest' + }, + dataType: "json", + method: options.method || 'GET', + // sslVerify: true, + // 接口请求成功 + success: (res) => { + // 关闭加载 + uni.hideLoading(); + // 调用成功且有数据 返回数据 组件内通过 .then() 或者async await 接受异步返回数据 + //resolve(res.data) + //在接口200 调用成功后 才能进行判断接口内的状态码 return_code 以此判定作何操作和提示 + let result = res.data + console.log(result); + console.log(result.code) + if (result.code == 0) { + resolve(res.data) + } else { + uni.hideLoading(); + uni.showToast({ + title: res.data.message, + icon: 'none', + duration: 2000 + }); + } + // let return_code = result.data.code + // let return_message = result.data.message + // console.log(typeof return_code) + // console.log(return_code) + }, + // 接口接口失败 + fail: (error) => { + // 关闭加载 + uni.hideLoading(); + console.log(2, error); + uni.showToast({ + title: '请求接口失败', + icon: "none", + mask: true, }) + // 失败数据 + reject(error) + } }) + }) } \ No newline at end of file diff --git a/pages/index/index.vue b/pages/index/index.vue index 7e27c1c..3f88e15 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -1,6 +1,6 @@