Procházet zdrojové kódy

设置服务器ip地址

wangc01 před 13 hodinami
rodič
revize
3eaca28b57
5 změnil soubory, kde provedl 153 přidání a 27 odebrání
  1. 0 2
      App.vue
  2. 25 25
      manifest.json
  3. 8 0
      pages.json
  4. 115 0
      pages/sample/settings.vue
  5. 5 0
      pages/sample/webView.vue

+ 0 - 2
App.vue

@@ -11,8 +11,6 @@
 				uni.setStorageSync('versionCode', inf.versionCode); // 小版本号
 			});
 			// 设置默认的服务器地址(如果还没配置过)
-			var defaultIP = "192.168.111.200";
-			var defaultPORT = "8800";
 			var ip = plus.storage.getItem("ip");
 			var port = plus.storage.getItem("port");
 			if (!ip) {

+ 25 - 25
manifest.json

@@ -10,26 +10,26 @@
         "usingComponents" : true,
         "nvueCompiler" : "uni-app",
         "compilerVersion" : 3,
-		"permissions": {
-		    "Speech": { // 语音权限
-		      "description": "用于语音播报"
-		    },
-		    "Network": { // 网络权限
-		      "description": "用于访问服务器页面"
-		    }
-		},
-		  "webview": {
-		    "titleNView": false, // 全局禁用 WebView 标题栏
-		    "addressBar": false, // 全局禁用地址栏
-		    "toolbar": false,
-		    "scrollIndicator": "none"
-		  },
-		  "launchwebview": {
-		    "titleNView": false, // 启动页也禁用标题栏
-		    "addressBar": false
-		  },
-		  
-		  
+        "permissions" : {
+            "Speech" : {
+                // 语音权限
+                "description" : "用于语音播报"
+            },
+            "Network" : {
+                // 网络权限
+                "description" : "用于访问服务器页面"
+            }
+        },
+        "webview" : {
+            "titleNView" : false, // 全局禁用 WebView 标题栏
+            "addressBar" : false, // 全局禁用地址栏
+            "toolbar" : false,
+            "scrollIndicator" : "none"
+        },
+        "launchwebview" : {
+            "titleNView" : false, // 启动页也禁用标题栏
+            "addressBar" : false
+        },
         /* 模块配置 */
         "modules" : {
             "Bluetooth" : {},
@@ -70,11 +70,11 @@
                 ],
                 "minSdkVersion" : 21,
                 "abiFilters" : [ "armeabi-v7a", "arm64-v8a", "x86" ],
-				 "webviewWhitelist": [
-								"http://*",  // 允许所有http协议(调试用,正式环境建议限定IP/域名)
-								"https://*",
-								"http://192.168.*.*" // 限定你的HTML服务器IP段
-							]
+                "webviewWhitelist" : [
+                    "http://*", // 允许所有http协议(调试用,正式环境建议限定IP/域名)
+                    "https://*",
+                    "http://192.168.*.*" // 限定你的HTML服务器IP段
+                ]
             },
             /* ios打包配置 */
             "ios" : {

+ 8 - 0
pages.json

@@ -13,6 +13,14 @@
 			        }
 			      }
 		},
+		{
+			"path": "pages/sample/settings",
+			"style": {
+				"navigationBarTitleText": "服务器配置",
+				"navigationBarBackgroundColor": "#0039a6",
+				"navigationBarTextStyle": "white"
+			}
+		},
 		{
 			"path": "pages/sample/richAlert",
 			"style": {

+ 115 - 0
pages/sample/settings.vue

@@ -0,0 +1,115 @@
+<template>
+  <view class="settings">
+    <view class="title">服务器配置</view>
+
+    <view class="form-item">
+      <text class="label">服务器 IP</text>
+      <input
+        class="input"
+        v-model="ip"
+        placeholder="请输入服务器 IP,如 192.168.111.200"
+      />
+    </view>
+
+    <view class="form-item">
+      <text class="label">端口</text>
+      <input
+        class="input"
+        v-model="port"
+        placeholder="请输入端口,如 8800"
+        type="number"
+      />
+    </view>
+
+    <button class="btn-save" @click="save">保存</button>
+    <button class="btn-reset" @click="reset">恢复默认</button>
+  </view>
+</template>
+
+<script>
+export default {
+  data() {
+    return {
+      ip: '',
+      port: '',
+      DEFAULT_IP: '192.168.111.200',
+      DEFAULT_PORT: '8800',
+    };
+  },
+  onLoad() {
+    // #ifdef APP-PLUS
+    this.ip = plus.storage.getItem('ip') || this.DEFAULT_IP;
+    this.port = plus.storage.getItem('port') || this.DEFAULT_PORT;
+    // #endif
+  },
+  methods: {
+    save() {
+      if (!this.ip) {
+        uni.showToast({ title: '请输入 IP', icon: 'none' });
+        return;
+      }
+      if (!this.port) {
+        uni.showToast({ title: '请输入端口', icon: 'none' });
+        return;
+      }
+      // #ifdef APP-PLUS
+      plus.storage.setItem('ip', this.ip);
+      plus.storage.setItem('port', this.port);
+      // #endif
+      uni.showToast({ title: '保存成功', icon: 'success' });
+      setTimeout(() => {
+        uni.reLaunch({ url: '/pages/sample/webView' });
+      }, 800);
+    },
+    reset() {
+      this.ip = this.DEFAULT_IP;
+      this.port = this.DEFAULT_PORT;
+      // #ifdef APP-PLUS
+      plus.storage.setItem('ip', this.DEFAULT_IP);
+      plus.storage.setItem('port', this.DEFAULT_PORT);
+      // #endif
+      uni.showToast({ title: '已恢复默认', icon: 'none' });
+    },
+  },
+};
+</script>
+
+<style scoped>
+.settings {
+  padding: 40rpx 30rpx;
+}
+.title {
+  font-size: 40rpx;
+  font-weight: bold;
+  margin-bottom: 50rpx;
+  color: #333;
+}
+.form-item {
+  margin-bottom: 30rpx;
+}
+.label {
+  display: block;
+  font-size: 28rpx;
+  color: #666;
+  margin-bottom: 12rpx;
+}
+.input {
+  border: 1px solid #ddd;
+  border-radius: 8rpx;
+  padding: 20rpx;
+  font-size: 30rpx;
+  background: #fff;
+}
+.btn-save {
+  margin-top: 40rpx;
+  background: #0039a6;
+  color: #fff;
+  border-radius: 8rpx;
+}
+.btn-reset {
+  margin-top: 20rpx;
+  background: #f5f5f5;
+  color: #666;
+  border-radius: 8rpx;
+}
+</style>

+ 5 - 0
pages/sample/webView.vue

@@ -6,6 +6,8 @@
       @error="onWebError"
       @message="onWebMessage"
     ></web-view>
+    <!-- 设置按钮:点击进入服务器配置页面 -->
+    <view class="fab-btn" @tap="goSettings">⚙</view>
   </view>
 </template>
 
@@ -90,6 +92,9 @@ export default {
           duration: 6,
         });
       }
+    },
+    goSettings() {
+      uni.navigateTo({ url: '/pages/sample/settings' });
     }
   }
 };