Browse Source

接口重构封装

asd26269546 2 years ago
parent
commit
cba30b0a7f
4 changed files with 174 additions and 2 deletions
  1. 142 0
      libs/utils/http-2.js
  2. 4 0
      main.js
  3. 28 2
      pages/index/index.vue
  4. 0 0
      unpackage/dist/dev/app-plus/app-service.js

+ 142 - 0
libs/utils/http-2.js

@@ -0,0 +1,142 @@
+import Vue from 'vue'
+//const BASE_URL = 'https://ymt.prpiano.com/api'
+//const BASE_URL = 'https://ht.test.prpiano.com/api'
+//xw
+//const BASE_URL = 'http://10.0.52.100:82'
+//dh
+var BASE_URL
+var BASE_URL2
+//lb
+//const BASE_URL = 'http://10.0.126.219:82'
+if (process.env.NODE_ENV === 'development') {
+	//线上 https://bytesailing.cn/api/storage/cloudApi
+	BASE_URL = 'http://36.134.91.96:10001/api'
+	BASE_URL2 = 'http://36.134.91.96:10001/api'
+} else {
+	//线上
+	BASE_URL = 'http://36.134.91.96:10001/api'
+	BASE_URL2 = 'http://36.134.91.96:10001/api'
+}
+/**
+ * 封装get方法
+ * @param url
+ * @param data
+ * @returns {Promise}
+ */
+
+
+export function get(url, req = {}) {
+	return new Promise((resolve, reject) => {
+		if (this.$storage.getStorageSync('token')) {
+			var hander = {
+				'Blade-Auth': 'bearer ' + this.$storage.getStorageSync('token'),
+				'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
+				'Content-Type':'application/json;charset=UTF-8',
+			}
+		} else {
+			var hander = {}
+		}
+		uni.request({
+			url: BASE_URL + url,
+			data: req,
+			method: 'GET',
+			header: hander,
+			success: (res) => {
+				resolve(res.data);
+			},
+			fail: (err) => {
+				return Promise.reject(err);
+			}
+		});
+	})
+}
+
+/**
+ * 封装post请求
+ * @param url
+ * @param data
+ * @returns {Promise}
+ */
+var urlList = []
+export function post(url, req = {}, headers) {
+	var httpUrl
+	if(url.indexOf('/cloudApi') == -1){
+		httpUrl = BASE_URL
+	}else{
+		httpUrl = BASE_URL2
+	}
+	if(urlList.indexOf(url) == -1 || url == '/wx/inventory/selectFabricWTypeDetails' || '/wx/inventory/selectInkEW'){
+		urlList.push(url)
+		setTimeout(()=>{
+			urlList.splice(urlList.indexOf(url),1)
+		},500)
+	}else{
+		return Promise.reject('请勿频繁提交');
+	}
+	return new Promise((resolve, reject) => {
+		if (Vue.prototype.$token) {
+			
+			var hander = {
+				'Blade-Auth': 'bearer ' + this.$storage.getStorageSync('token'),
+				'Authorization':'Basic c2FiZXI6c2FiZXJfc2VjcmV0',
+				'Content-Type':'application/json;charset=UTF-8',
+			}
+		} else {
+			var hander = {}
+		}
+		if(headers) hander = headers
+		uni.request({
+			url: httpUrl + url,
+			data: req,
+			method: 'POST',
+			header: hander,
+			success: (res) => {
+				if (res.data.code == 401) {
+					uni.showToast({
+						icon: "error",
+						title: '前往登录',
+						duration: 2000
+					});
+					Vue.prototype.$token = null
+					uni.redirectTo({
+						url: "/pages/user/login"
+					})
+				} else if (res.data.code == 500) {
+					uni.showToast({
+						icon: "error",
+						title: res.data.msg,
+						duration: 2000
+					});
+					
+				} else if(res.data.code == 200){
+					resolve(res.data);
+				} else if(!res.data.code){
+					uni.showToast({
+						icon: "error",
+						title: '服务器错误',
+						duration: 2000
+					});
+				} else {
+					uni.showToast({
+						icon: "error",
+						title: res.data.msg,
+						duration: 2000
+					});
+				}
+			},
+			fail: (err) => {
+				uni.showToast({
+					icon: "error",
+					title: '服务器错误',
+					duration: 2000
+				});
+				return Promise.reject(err);
+			}
+		});
+	})
+}
+
+export default {
+	post,
+	get
+}

+ 4 - 0
main.js

@@ -10,4 +10,8 @@ App.mpType = 'app'
 const app = new Vue({
     ...App
 })
+
+import httpServer from "./libs/utils/http-2.js"
+Vue.prototype.$get = httpServer.get
+Vue.prototype.$post = httpServer.post
 app.$mount()

+ 28 - 2
pages/index/index.vue

@@ -11,17 +11,29 @@
 		</uni-nav-bar>
 		
 		<view class="container">
-			<view v-for="(menu, menuIndex) in menus" :key="menuIndex">
+			
+			<view v-for="(menu, menuIndex) in menusData.children" :key="menuIndex">
 				<view class="title">
 					<span>{{ menu.name }}</span>
 				</view>
 				<view class="list">
 					<view class="item flex-column-center" @tap="go(item.key)" v-for="(item, index) in menu.children" :key="index">
-						<u-image width="60%" mode="widthFix" :src="item.icon"></u-image>
+						<u-image width="60%" mode="widthFix" src="../../static/images/smtb.png"></u-image>
 						<span class="title">{{ item.name }}</span>
 					</view>
 				</view>
 			</view>
+			<!-- <view v-for="(menu, menuIndex) in menus" :key="menuIndex">
+				<view class="title">
+					<span>{{ menu.name }}</span>
+				</view>
+				<view class="list">
+					<view class="item flex-column-center" @tap="go(item.key)" v-for="(item, index) in menu.children" :key="index">
+						<u-image width="60%" mode="widthFix" :src="item.icon"></u-image>
+						<span class="title">{{ item.name }}</span>
+					</view>
+				</view>
+			</view> -->
 		</view>
 		<u-modal v-model="show" content="是否退出账号" :show-cancel-button="true" @confirm="logOut"></u-modal>
 	</view>
@@ -36,10 +48,23 @@
 				menus: this.$storage.getStorageSync('userInfo').menus,
 				show: false,
 				timer: null,
+				menusData:{},
 				
 			};
 		},
 		methods: {
+			//请求导航
+			getRouters(){
+				this.$get('/blade-system/menu/routes').then(res=>{
+					if(res.code == 200){
+						res.data.map(item=>{
+							if(item.id === '1585526165777817601') this.menusData = item
+						})
+						console.log(this.menusData)
+					}
+					
+				})
+			},
 			logOut() {
 				this.$http.LoginQuit().then(res => {
 					if(res.code === 0) {
@@ -167,6 +192,7 @@
 			}
 		},
 		onLoad() {
+			this.getRouters()
 			uni.$on('scan', res => {
 				this.scanPaste()
 			})

File diff suppressed because it is too large
+ 0 - 0
unpackage/dist/dev/app-plus/app-service.js


Some files were not shown because too many files changed in this diff