123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- /**
- * 接口请求封装
- *
- * 1、需要成功提示的,带 additional.successMessage ,如果值为'msg',提示内容为返回的msg
- * 2、默认loading,不需要的传 additional.needLoading = false。
- */
- import { URL } from "./requestUrl";
- import { stringify } from 'qs';
- const BASE_URL = `http://${URL}`;
- let AllRequestNum = 0; // 存放请求数,以保证有请求未返回就持续loading
- /**
- *
- * @param obj 接口配置数据{url,method,successMessage,needLoading}
- * @param data 接口请求数据
- */
- function creatRequest(obj, data) {
- if (obj) {
- let additional = {}, url = obj.url;
- if (obj.successMessage !== undefined) additional.successMessage = obj.successMessage;
- if (obj.needLoading !== undefined) additional.needLoading = obj.needLoading;
- // DELETE 参数以问号形式拼接到url
- if (obj.method === 'DELETE') {
- url = data ? `${url}?${stringify(data)}` : url;
- data = null;
- }
- console.log({data});
- return request(url, { method: obj.method, data }, additional);
- } else {
- uni.showModal({
- title: '错误提示',
- content: '请求的接口未定义!',
- showCancel: false
- });
- }
- }
- /**
- * url: 请求的url
- * options: uni.request的一系列参数,常用的method和data
- * additional: { successMessage、needLoading }
- */
- function request(url, options, additional) {
- if (additional.needLoading === undefined) additional.needLoading = true;
- // 加载中提示打开
- openCloseLoading(additional, true);
- // 模拟token,做登录的同学将这行代码移到登录成功函数中.
- // uni.setStorageSync('token', 'gfkZlq36bBPefzvIoukl5exPJ5/jkil2gi9fNPTABSk=');
- const token = uni.getStorageSync('token');
- return uni.request({
- url: `${BASE_URL}${url}`,
- ...options,
- header: {
- token: token ? token : null,
- 'Content-Type': 'application/json',
- }
- }).then((res) => notifyException(res, additional))
- .catch((error) => {
- if ('stack' in error && 'message' in error) {
- uni.showModal({
- title: '错误提示',
- content: '服务器异常,请稍后重试!',
- showCancel: false
- });
- console.error(`请求错误: ${url} ${error.message}`);
- }
- return error;
- });
- }
- // 加载中提示打开/关闭
- function openCloseLoading(additional, isOpen) {
- if (additional.needLoading) {
- if (isOpen) { // 打开(如果已经打开不必重复打开)
- if (AllRequestNum === 0)
- uni.showLoading();
- AllRequestNum++;
- } else { // 关闭
- if (AllRequestNum > 0) { // 防止数值错乱
- AllRequestNum--;
- if (AllRequestNum === 0)
- uni.hideLoading();
- } else {
- AllRequestNum = 0;
- }
- }
- }
- }
- // 登录过期,重置请求计数,强制关闭loading
- function LoginExpired() {
- AllRequestNum = 0;
- uni.navigateTo({
- url: '/pages/login/login'
- });
- }
- function notifyException(resArr, additional) {
- openCloseLoading(additional, false);
- const [error, res] = resArr;
- if (error) {
- uni.showModal({
- title: '错误提示',
- content: JSON.stringify(error),
- showCancel: false
- });
- console.error(`请求错误: ${error.errMsg}`);
- return false;
- } else if (res.data.code !== 'SUCCESS') {
- const { code, msg } = res.data;
- console.log('res.data',res.data);
- if (code === '410' || code === '401') {
- LoginExpired();
- } else if (code === '403') {
- uni.showModal({
- title: '提示',
- content: '登录过期,请重新登录!',
- success: function (_res) {
- if (_res.confirm) {
- LoginExpired();
- } else if (_res.cancel) {
- console.log('用户点击取消');
- }
- }
- });
- } else {
- uni.showModal({
- title: '错误提示',
- content: msg,
- showCancel: false
- });
- }
- console.error(`错误信息: ${msg}`);
- return false;
- } else if (additional.successMessage) {
- let msg = additional.successMessage;
- uni.showToast({
- title: msg === 'msg' ? res.data.msg : msg,
- icon:'none',
- duration:1000
- });
- }
- return res.data.data ? res.data.data : true;
- }
- export {
- request,
- creatRequest
- }
|