Browse Source

灵活配置ip

蒋科 1 month ago
parent
commit
f9aea0b65c
5 changed files with 39 additions and 16 deletions
  1. 1 1
      common/request.js
  2. 4 1
      components/Navbar/Navbar.vue
  3. 5 2
      config.js
  4. 13 10
      pages/index/index.vue
  5. 16 2
      pages/login/index.vue

+ 1 - 1
common/request.js

@@ -41,7 +41,7 @@ export default {
 					reject(error);
 					if(error.errMsg.includes('timeout')){
 						uni.showToast({
-							title: '请求接口失败!',
+							title: '请求接口失败,请检查IP地址!',
 							icon: 'none',
 						})
 					}else{

+ 4 - 1
components/Navbar/Navbar.vue

@@ -122,7 +122,10 @@
 					content: "",
 					success: function(res) {
 						if (res.confirm) {
-							that.$storage.clear(); //清除所有本地存储
+							// that.$storage.clear(); //清除所有本地存储
+							that.$storage.remove("users")
+							that.$storage.remove("token")
+							that.$store.commit("DELETEUSERS")
 							uni.navigateTo({
 								url: "../../pages/login/index"
 							})

+ 5 - 2
config.js

@@ -1,8 +1,11 @@
+import storage from "./common/storage";
 export default {
 	web_name: "智小狸",
 	// uni_app_web_api_url: "http://10.12.22.137:8080/api", //后端统一接口路径
 	// web_socket_url:'ws://10.12.22.137:8080/ws/',
-	uni_app_web_api_url: "http://192.168.3.177:8080/api",
-	web_socket_url:'ws://192.168.3.177:8080/ws/',
+	// uni_app_web_api_url: "http://192.168.3.177:8080/api",
+	// web_socket_url:'ws://192.168.3.177:8080/ws/',
+	uni_app_web_api_url:storage.getJson("addressIp")?'http://'+storage.getJson("addressIp")+'/api':'',
+	web_socket_url:storage.getJson("addressIp")?'ws://'+storage.getJson("addressIp")+'/ws/':'',
 	debug: true
 }

+ 13 - 10
pages/index/index.vue

@@ -171,25 +171,28 @@
 				stateList: [],
 				heartbeatTimer:null,
 				onMessageTime:null,
+				ws: null, //websuokit 链接器
 			}
 		},
 		mounted() {
 			this.onMessageTime = new Date();
 			this.InitWs()
-			this.heartbeatTimer = setInterval(()=>{
-				let newDate = new Date();
-				if(newDate-this.onMessageTime>3000){
-					if(this.ws!=null){
-						this.ws.close()
-						this.ws=null
-					}
-				}
-			},3000)
+			// this.heartbeatTimer = setInterval(()=>{
+			// 	let newDate = new Date();
+			// 	if(newDate-this.onMessageTime>3000){
+			// 		if(this.ws!=null){
+			// 			this.ws.close()
+			// 			this.ws=null
+			// 		}
+			// 	}
+			// },3000)
 			this.getDataView()
 		},
 		destroyed(){
 			clearInterval(this.timer)
-			this.ws.close()
+			if(this.ws){
+				this.ws.close()
+			}
 		},
 		computed: {
 			robotResult() {

+ 16 - 2
pages/login/index.vue

@@ -12,6 +12,10 @@
 							<input class="psd_input" v-model="userInfo.passWord" :password="eye"></input>
 							<image :src="eye?Frame:browse" class='login_icon_eye' alt=""  @click="showpsd">
 					</u-form-item>
+					<u-form-item prop="userInfo.name" class="label_left">
+						<image :src="user_img" class='login_icon' alt="">
+							<u--input v-model="addressIp"></u--input>
+					</u-form-item>
 				</u--form>
 				<u-button type="primary" text="登录" class="btn" @click="submit"></u-button>
 			</view>
@@ -28,6 +32,7 @@
 	import lockon from "@/static/login_img/lock-on.png"
 	import user from "@/static/login_img/user.png"
 	import browse from "@/static/login_img/browse.png"
+	import config from "@/config";
 	export default {
 		name: "son",
 		data() {
@@ -43,7 +48,8 @@
 				},
 				eye: true,
 				popupType:'',
-				popupMessage:''
+				popupMessage:'',
+				addressIp:this.$storage.getJson('addressIp')?this.$storage.getJson('addressIp'):'192.168.3.177:8080'
 			};
 		},
 		methods: {
@@ -57,6 +63,13 @@
 				this.$refs.Exchangepopup.open()
 			},
 			submit() {
+				
+				config.uni_app_web_api_url='http://'+this.addressIp+'/api'
+				config.web_socket_url='ws://'+this.addressIp+'/ws/'
+				if(!this.addressIp){
+					this.ExchangeMessage('error', '请输入IP地址')
+					return 
+				}
 				this.$utils.showLoading('登录中')
 				if (this.userInfo.userAccount && this.userInfo.passWord) {
 					this.$http.wxLogin(this.userInfo).then(res => {
@@ -64,6 +77,7 @@
 						if(res.code == 0){
 							this.$storage.setJson("token",res.data.token)
 							this.$storage.setJson("userId",res.data.userId)
+							this.$storage.setJson("addressIp",this.addressIp)
 							uni.navigateTo({
 								url: '/pages/home/index'
 							})
@@ -113,7 +127,7 @@
 
 			.u-form-item__body {
 				padding: 0px 0px;
-				margin-bottom: 32px;
+				margin-bottom: 28px;
 			}
 
 			.u-form-item__body__right__content ,.psd_input{