123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import * as config from '@/config'
- let baseUrl = config.def().baseUrlNew
- // 定义基础请求路径(后端服务器地址)
- const baseRequest = (_gp, _mt, data = {}, failCallback) => {
- //异步请求数据
- return new Promise(resolve => {
- // if (!userInfo || !userInfo.accessToken) {
- // userInfo = uni.getStorageSync('userInfo')
- // }
- // let accessToken = userInfo ? userInfo.accessToken : ''
- let baseUrl = config.def().baseUrlNew
- uni.request({
- url: baseUrl + '/m.api',
- data: {
- ...data,
- _gp,
- _mt
- },
- method: 'POST',
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
- // 'ACCESSTOKEN': accessToken
- },
- success: (res) => {
- if (res.statusCode === 200) {
- if (res.data.errno === 200) {
- resolve(res.data);
- } else {
- if (failCallback) {
- failCallback(res.data)
- } else {
- uni.showToast({
- title: res.data.errmsg,
- icon: 'none'
- })
- }
- }
- }
- }
- })
- })
- }
- //带Token请求
- const TokenRequest = (method, url, data, header) => {
- var contentheader = 'application/json'
- if (header) {
- contentheader = header
- }
- let ac_token = "";
- uni.getStorage({
- key: 'userInfo',
- success: function(res) {
- ac_token = res.data.accessToken
- }
- });
- //此token是登录成功后后台返回保存在storage中的
- let DefaultOpts = {
- url: baseUrl + url,
- data: data,
- method: method,
- header: {
- 'content-type': contentheader,
- 'Token': ac_token,
- }
- }
- let promise = new Promise(function(resolve, reject) {
- uni.request(DefaultOpts).then(
- (res) => {
- console.log(JSON.stringify(res[1].data))
- if (res[1].data.code == '200' || res[1].data.code == 200) {
- // 后端返回的状态码100为成功状态,成功则返回请求结果,在app调试时可以通过console.log(JSON.stringify(res[1].data))来查看返回值(以项目实际情况为准)
- resolve(res[1].data)
- }
- if (res[1].data.code == '105' || res[1].data.code == 105) {
- // 后端返回状态码为105则为未登录状态(以项目实际情况为准)
- uni.showToast({
- icon: 'none',
- title: '尚未登录',
- duration: 2000
- });
- // 尚未登录的逻辑处理
- return false
- }
- }
- ).catch(
- (response) => {
- reject(response)
- }
- )
- })
- return promise
- }
- export default {
- baseUrl,
- baseRequest,
- TokenRequest
- }
|