12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- //请求头
- import { baseUrl } from '@/https/url.js'
- export default (url, method, params) => { // 传参: 接口url, method类型, params参数
- const header = {
- 'accept': 'application/json, text/plain, */*',
- 'Content-type': 'application/json',
- 'Authorization': uni.getStorageSync('token') ? uni.getStorageSync('token') : "",
- }
- return new Promise((resolve, reject) => {
- uni.request({
- method: method,
- url: baseUrl + url,
- data: method == 'GET' ? params : JSON.stringify(params),
- header: header
- })
- .then((response) => {
- // console.log(response);
- // 请求成功后的处理
- if (response[1].data.code == 201) {
- uni.removeStorageSync('token');
- uni.showToast({
- title: '登录失效,请刷新界面.',
- duration: 2000,
- icon: 'none'
- });
- } else if (response[1].data.code == 0) {
- if (response[1].data.message == 'token不能为空') {
- uni.removeStorageSync('token');
- uni.showToast({
- title: '登录失效,请刷新界面.',
- duration: 2000,
- icon: 'none'
- });
- uni.reLaunch({
- url: '/pages/login/login'
- });
- }
- }
- resolve(response[1])
- // if(response[1].)
- }).catch((reject) => {
- // 请求失败后的处理
- console.log('请求失败')
- reject(reject)
- })
- })
- }
|