123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import request from "./request";
- import config from "config";
- // import store from "../../store/index.js";
- uni.getStorage({
- key: 'storage_key',
- success: function (res) {
- console.log(res.data);
- }
- });
- //可以new多个request来支持多个域名请求
- let $http = new request({
- //接口请求地址
- baseUrl: config.baseUrl,
- //服务器本地上传文件地址
- fileUrl: config.baseUrl,
- //设置请求头
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded;',
- 'ApiVersion': config.version,
- 'Device': config.device,
- 'Platform': config.platform,
- "Authorization": 'Bearer ' + uni.getStorageSync('laocui_user_token')
- },
- //以下是默认值可不写
- //是否提示--默认提示
- isPrompt: true,
- //是否显示请求动画
- load: true,
- //是否使用处理数据模板
- isFactory: true,
- //列表接口是否有加载判断
- loadMore: true,
- });
- //当前接口请求数
- let requestNum = 0;
- //请求开始拦截器
- $http.requestStart = function (options) {
- // console.log(`$http.requestSta`, options)
- for (const key in options.data) {
- if (options.data[key] == null) {
- delete options.data[key]
- }
- }
- if (requestNum <= 0) {
- uni.showNavigationBarLoading();
- if (options.load) {
- //打开加载动画
- uni.showLoading({
- title: '加载中',
- mask: true
- });
- }
- }
- requestNum += 1;
- //请求前加入token
- options.headers['Authorization'] = 'Bearer ' + uni.getStorageSync('laocui_user_token');
- // console.log(uni.getStorageSync('USERINFO'))
- options.headers['requestHanderAuth'] = uni.getStorageSync('USERINFO') ? JSON.parse(uni.getStorageSync('USERINFO')).requestHanderAuth : '';
- // console.log("请求开始前", options);
- return options;
- }
- //请求结束
- $http.requestEnd = function (options, resolve) {
- //判断当前接口是否需要加载动画
- requestNum = requestNum - 1;
- if (requestNum <= 0) {
- uni.hideLoading();
- uni.hideNavigationBarLoading();
- }
- if (resolve.errMsg && (resolve.errMsg != "request:ok" || resolve.statusCode && resolve.statusCode != 200)) {
- uni.showToast({
- title: "网络错误,请检查一下网络",
- mask: true,
- icon: "none"
- });
- }
- }
- //登录弹窗次数
- let loginPopupNum = 0;
- //所有接口数据处理(可在接口里设置不调用此方法)
- $http.dataFactory = function (options, resolve) {
- //设置回调默认值
- var callback = {
- //success数据是否请求成功状态
- success: false,
- //这里返回的数据就是调用请求方法收到的数据
- result: ""
- };
- // 特殊接口处理 接口报请先登录 跳转登录
- if (resolve.data.code === 101) {
- uni.showModal({
- title: '提示',
- content: resolve.data.msg,
- showCancel: false,
- success: () => {
- uni.setStorageSync('token', null);
- uni.navigateTo({
- url: '/pages/login/login'
- });
- }
- });
- return false;
- }
- if (resolve.data) {
- callback.result = resolve.data;
- callback.success = true;
- uni.stopPullDownRefresh();
- return callback;
- }
- //判断数据是否请求成功
- if (resolve.data.errmsg == "成功") {
- callback.success = true;
- callback.result = resolve.data;
- } else if (resolve.data.errno == 303) {
- uni.redirectTo({
- url: '/pages/subPackages/pages/login/login'
- });
- this.$showToast('令牌过期,请重新登陆')
- } else {
- uni.showToast({
- title: resolve.data.errmsg,
- icon: "none",
- mask: true,
- duration: 2000
- });
- //设置可以提示的时候
- // if (options.isPrompt) {
- // setTimeout(function () {
- //提示后台接口抛出的错误信息
- // uni.showToast({
- // title: resolve.data.msg,
- // icon: "none",
- // duration: 3000
- // });
- // }, 500);
- // }
- callback.result = resolve.data;
- callback.success = true;
- }
- uni.stopPullDownRefresh();
- console.log('callback', callback)
- return callback;
- };
- export default $http;
|