Browse Source

更新请求封装

李书文 1 year ago
parent
commit
5e40fa0d8e
3 changed files with 100 additions and 4 deletions
  1. 91 0
      common/http.js
  2. 2 0
      main.js
  3. 7 4
      pages/login/login.vue

+ 91 - 0
common/http.js

@@ -0,0 +1,91 @@
+const ip = 'http://192.168.88.34:8080'; //线下
+//const ip = 'http://114.118.9.159:8080/prod-api'; //线上
+/**
+ * 封装的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
+};

+ 2 - 0
main.js

@@ -3,6 +3,7 @@ import api from '@/apis/api.js'
 import common from '@/common/js/common.js'
 import uView from "uview-ui";
 import verify from './common/graceChecker.js' //数据校验证
+import http from './common/http.js'
 
 Vue.use(uView);
 // #ifndef VUE3
@@ -12,6 +13,7 @@ App.mpType = 'app'
 Vue.prototype.$api = api
 Vue.prototype.$common = common
 Vue.prototype.verify = verify;
+Vue.prototype.http = http;
 const app = new Vue({
     ...App
 })

+ 7 - 4
pages/login/login.vue

@@ -50,13 +50,16 @@ export default {
 				uni.showModal({ content: this.verify.error, showCancel: false });
 				return false;
 			}
-			this.$api.doLogin(this.form).then(resp => {
-				if (resp.code == 200) {
-					let data = resp.data;
+			this.http.request({
+				url: '/sp-admin/app/AppUser/login',
+				data: this.form,
+				method: 'POST',
+				success: resp => {
+					let data = resp.data.data;
 					uni.setStorageSync('token', data.tokenInfo.tokenValue);
 					uni.setStorageSync('info', data.appUser);
 					uni.setStorageSync('menu', data.per_list);
-					this.$common.toBar('/pages/index/index');
+					uni.switchTab({ url: '/pages/index/index' });
 				}
 			});
 		},