import config from '../config.js' import common from './alert.js' import store from '../store.js' const request = { getToken(){ let user=store.state.token // console.log(user); if(user){ this.post("/api/Accounts/RefreshToken",{token:user.token,refreshToken:user.refreshToken},(data,res)=>{ store.commit('saveToken', res) uni.setStorageSync("token", res) }) }else{ return } }, uploadFile(filePath, callback) { const user = store.state.token || {}; // if(!user.lawyerInfo) return common.askLogin(); console.log(filePath, callback,2222222); uni.uploadFile({ url: config.domain + '/api/Objects/integration/xxx', filePath: filePath, name: 'file', header: { 'Authorization': "Bearer "+ user.token || '', }, success (res) { console.log(res,333333); const data = JSON.parse(res.data); if (data.status == -100) { common.askLogin() } else if (data.status == -1) { common.showError(data.msg) } else { callback(data.data, data) } } }) }, uploadFile2(filePath, formdata, callback) { const user = store.state.user || {}; if(!user.lawyerInfo) return common.askLogin(); console.log('1111111111111'+filePath) console.log('2222222222222'+formdata) console.log('3333333333333'+callback) uni.uploadFile({ url: config.domain + '/file/uploadFile2', filePath: filePath, name: 'file', formData: formdata, header: { 'token': user.lawyerInfo.token || '', 'login_type': user.login_type || 0, 'who': user.who }, success (res) { console.log(res) const data = JSON.parse(res.data); if (data.status == -100) { common.askLogin() } else if (data.status == -1) { common.showError(data.msg) } else { callback(data.data, data) } } }) }, get(url, data, callback) { console.log(data,8080); this.getToken() const user = store.state.token || {}; uni.request({ url: config.api + url, // 仅为示例,并非真实的接口地址 method: 'GET', data: data, header: { 'Authorization': "Bearer "+ user.token || '', 'content-type': 'application/json' }, success(res) { const data = res.data; // console.log(data, '请求返回的数据', 1000000000000000) if (data.status == -100) { console.log('没有登录'); common.askLogin() } else if (data.status == -1) { common.showError(data.msg) } else { callback(data.data, data) } } }) }, post(url, data, callback) { if(url!="/api/Accounts/RefreshToken"&&user!={}){ this.getToken() } const user = store.state.token || {}; try { uni.request({ url: config.api + url, // 仅为示例,并非真实的接口地址 method: 'POST', data: data, header: { 'Authorization': "Bearer "+ user.token || '', }, success(res) { const data = res.data; console.log(res, '------------- 请求返回的数据 -------------') if (data.status == -100) { console.log('没有登录'); common.askLogin() } else if (data.status == -1) { common.showError(data.msg) callback(data.data, data) } else { // 第一个参数是data,第二个是全部数据,有时候会用到msg之类的数据, // 默认只要第一个值即可获取数据 callback(data.data, data) } }, fail: err => { console.error(err, 8877897) } }) } catch (e) { console.log(e) //TODO handle the exception } }, get2(url, data, callback) { const user = store.state.user || {}; uni.request({ url: config.domain + url, // 仅为示例,并非真实的接口地址 method: 'GET', data: data, header: { 'token': user.token || '', 'login_type': user.login_type || 0 }, success(res) { const data = res.data; // console.log(data, '请求返回的数据', 1000000000000000) if (data.status == -100) { console.log('没有登录'); common.askLogin() } else if (data.status == -1) { common.showError(data.msg) } else { callback(data.data, data) } } }) }, post2(url, data, callback) { const user = store.state.user || {}; try { uni.request({ url: config.domain + url, // 仅为示例,并非真实的接口地址 method: 'POST', data: data, header: { 'token': user.lawyerInfo.token || '', 'login_type': user.login_type || 0, 'who': user.who }, success(res) { const data = res.data; console.log(data, '------------- 请求返回的数据 -------------') if (data.status == -100) { console.log('没有登录'); common.askLogin() } else if (data.status == -1) { common.showError(data.msg) callback(data.data, data) } else { // 第一个参数是data,第二个是全部数据,有时候会用到msg之类的数据, // 默认只要第一个值即可获取数据 callback(data.data, data) } }, fail: err => { console.error(err, 8877897) } }) } catch (e) { console.log(e) //TODO handle the exception } }, } export default request