123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- // const BASE_URL = "http://172.16.120.165:13200/prod-api"; //本地请求地址
- const BASE_URL = 'http://gateway.usky.cn:8099/prod-api'//线上请求地址
- const websiteUrl = "https://qhome.usky.cn";
- // 同时发送异步代码的次数,防止一次点击中有多次请求,用于处理
- let ajaxTimes = 0;
- function myRequest(options) {
- let showLoading = options.showLoading || false;
- // 显示加载中 效果
- if (showLoading) {
- ajaxTimes++;
- uni.showLoading({
- title: "加载中",
- mask: true,
- });
- }
- return new Promise((resolve, reject) => {
- uni.request({
- url: BASE_URL + options.url,
- method: options.method,
- data: options.data || {},
- header: {
- // 'Content-Type': 'multipart/form-data',//图片上传
- // 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',//form表单
- 'Content-Type': 'application/json;charset=utf-8',//json串
- // "Content-Type": options.header["Content-Type"],
- Authorization: uni.getStorageSync("Authorization"),
- },
- success: (res) => {
- if (res.data.code == 200) {
- uni.showToast({
- title: res.data.msg ? res.data.msg : "获取数据失败",
- icon: "none",
- });
- } else if (res.data.code == 401) {
- uni.removeStorageSync("Authorization");
- uni.navigateTo({
- url: "/pages/login/login",
- });
- } else if (res.data.status == "ERROR" || res.data.code == 500) {
- uni.showToast({
- title: res.data.msg ? res.data.msg : "获取数据失败",
- icon: "none",
- });
- }
- resolve(res);
- },
- fail: (err) => {
- uni.showModal({
- showCancel: false,
- content: "请求接口失败",
- });
- // uni.showToast({
- // title: '请求接口失败',
- // icon:"none"
- // })
- reject(err);
- },
- // 完成之后关闭加载效果
- complete: () => {
- if (showLoading) {
- ajaxTimes--;
- if (ajaxTimes === 0) {
- // 关闭正在等待的图标
- uni.hideLoading();
- }
- }
- },
- });
- });
- }
- function sendUploadFile(options) {
- let showLoading = options.showLoading || false;
- // 显示加载中 效果
- if (showLoading) {
- ajaxTimes++;
- uni.showLoading({
- title: "正在上传图片",
- mask: true,
- });
- }
- return new Promise(function (resolve, reject) {
- uni.uploadFile({
- url: BASE_URL + options.url,
- method: options.method,
- filePath: options.filePath,
- name: "file",
- header: {
- Authorization: uni.getStorageSync("Authorization"),
- },
- success: (res) => {
- const data = JSON.parse(res.data);
- if (data.code != 200) {
- uni.showToast({
- title: "获取图片成功",
- icon: "none",
- });
- } else if (data.code == 401) {
- uni.removeStorageSync("Authorization");
- uni.navigateTo({
- url: "/pages/login/login",
- });
- }
- resolve(data);
- },
- fail: (err) => {
- uni.showModal({
- showCancel: false,
- content: "请求接口失败",
- });
- reject(err);
- },
- // 完成之后关闭加载效果
- complete: () => {
- if (showLoading) {
- ajaxTimes--;
- if (ajaxTimes === 0) {
- // 关闭正在等待的图标
- uni.hideLoading();
- }
- }
- },
- });
- });
- }
- /**
- * @路由拦截器
- */
- function addInterceptor() {
- uni.addInterceptor(STRING, OBJECT);
- }
- export default {
- BASE_URL,
- myRequest,
- sendUploadFile,
- websiteUrl,
- };
|