123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- //const ip = 'http://127.0.0.1:9191';
- const ip = 'https://chenglantimes.com/prod-api';
- //const ip = 'http://192.168.1.21:9191';
- /**
- * 封装的http请求
- */
- const request = (opt) => {
- opt = opt || {};
- opt.url = opt.url || '';
- opt.data = opt.data || null;
- opt.method = opt.method || 'GET';
- opt.contentType = opt.contentType || 'application/json;charset=UTF-8'
- opt.header = opt.header || {
- "Content-Type": opt.contentType,
- "Authorization": getUser().token ? getUser().token : ''
- };
- opt.loading = opt.loading || 'true';
- opt.success = opt.success || function () {};
- opt.fail = opt.fail || function () {};
- // console.log("**************************************参数调式***************************************************");
- // console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
- // console.log("************************************************************************************************");
- if (opt.loading == 'true') {
- uni.showLoading({
- title: '正在加载',
- mask: true
- });
- }
- uni.request({
- url: ip + opt.url,
- data: opt.data,
- method: opt.method,
- header: opt.header,
- dataType: 'json',
- success: res => {
- setTimeout(() => {
- uni.hideLoading();
- }, 500)
- /*******************未授权或未登录***************************/
- if (res.data.code === 401 || res.data.code === 403) {
- uni.showModal({
- title: '提示',
- content: res.data.msg,
- showCancel: false,
- success: () => {
- uni.removeStorageSync('user');
- if (res.data.code === 401) {
- uni.navigateTo({
- url: '/pages/user/login'
- })
- }
- opt.fail();
- return;
- }
- });
- return;
- }
- /*******************系统内部错误***************************/
- if (res.data.code === 500 || res.data.status === 404) {
- uni.showModal({
- content: res.data.msg || res.data.message,
- showCancel: false
- });
- opt.fail(res);
- return;
- }
- opt.success(res);
- },
- fail: e => {
- uni.hideLoading();
- uni.getNetworkType({
- success: res => {
- if (res.networkType == 'none') {
- uni.showModal({
- content: '当前网络不可用,请检查网络稍后重试',
- showCancel: false
- });
- } else {
- uni.showModal({
- content: '服务异常,请稍后重试',
- showCancel: false
- })
- }
- }
- });
- }
- })
- }
- const getUser = () => {
- return uni.getStorageSync('user');
- }
- module.exports = {
- ip: ip,
- request,
- getUser
- };
|