1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // const ip = 'http://127.0.0.1:8080'; //线下
- const ip = 'http://hs-server.aseanbusiness.cn'; //线上
- //const ip = 'http://192.168.88.36:8080'; //线下
- /**
- * 封装的http请求
- */
- const request = (opt) => {
- opt = opt || {};
- opt.url = ip + opt.url || '';
- opt.data = opt.data || null;
- opt.method = opt.method || 'GET';
- opt.contentType = opt.contentType || 'application/x-www-form-urlencoded'
- opt.header = opt.header || {
- "Content-Type": opt.contentType,
- "satoken": uni.getStorageSync('token')
- };
- opt.loading = opt.loading || 'true';
- opt.success = opt.success || function() {};
- opt.fail = opt.fail || function() {};
- if (process.env.NODE_ENV) {
- console.log(
- "**************************************参数调式***************************************************");
- console.log("请求地址:" + opt.url + " 请求参数:" + JSON.stringify(opt.data));
- console.log(
- "************************************************************************************************");
- }
- if (opt.loading == 'true') {
- uni.showLoading({
- title: '正在加载',
- mask: true
- });
- }
- uni.request({
- url: opt.url,
- data: opt.data,
- method: opt.method,
- header: opt.header,
- dataType: 'json',
- responseType: opt.responseType,
- success: res => {
- setTimeout(() => {
- uni.hideLoading();
- }, 500)
- /*******************未授权***************************/
- if (res.data.code === 401) {
- uni.removeStorageSync('info');
- uni.redirectTo({url:'/pages/login/login'})
- return;
- }
- /*******************未授权***************************/
- if (res.data.code === 403) {
- uni.showModal({
- content: res.data.msg,
- showCancel: false
- });
- return;
- }
- /*******************系统内部错误***************************/
- if (res.data.code === 500) {
- uni.showModal({
- content: res.data.msg,
- 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
- })
- }
- }
- });
- }
- })
- }
- module.exports = {
- ip,
- request
- };
|