From fb73be20fcdc285c46f1a350960e69c894361709 Mon Sep 17 00:00:00 2001
From: whycq <913841844@qq.com>
Date: 星期三, 01 十一月 2023 22:50:26 +0800
Subject: [PATCH] #

---
 pages/api/common/common.js    |   29 +++++++++
 pages/LoginDemo/LoginDemo.vue |   95 +++++++++++++++----------------
 2 files changed, 75 insertions(+), 49 deletions(-)

diff --git a/pages/LoginDemo/LoginDemo.vue b/pages/LoginDemo/LoginDemo.vue
index 53318bc..fca64cc 100644
--- a/pages/LoginDemo/LoginDemo.vue
+++ b/pages/LoginDemo/LoginDemo.vue
@@ -22,7 +22,7 @@
 			<!-- <view class="button" @click="login" :loading="load.loading">
 				{{load.btnText}} 
 			</view> -->
-			<button class="button" @click="onLogin()" :loading="load.loading">{{load.btnText}}</button>
+			<button class="button" @click="login()" :loading="load.loading">{{load.btnText}}</button>
 		</view>
 		<!-- 璁剧疆寮圭獥鍖哄煙 -->
 		<uni-popup ref="settings" type="dialog">
@@ -67,6 +67,7 @@
 		mapState
 	} from 'vuex' //寮曞叆mapState
 	import md5 from '../../static/js/md5.js'
+	import common from '../api/common/common.js'
 	export default {
 		data() {
 			return {
@@ -112,9 +113,9 @@
 			}
 			// 鎵嬫満绔増鏈彿
 			// #ifdef APP-PLUS
-			var that = this
+			var _this = this
 			plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
-				that.version = wgtinfo.version
+				_this.version = wgtinfo.version
 			});
 			// #endif
 		},
@@ -135,55 +136,51 @@
 				uni.setStorageSync('Network', this.network);
 				this.$refs.settings.close()
 			},
-			onLogin() {
-				let _this = this,
-					path = 'demo',
-					network = uni.getStorageSync('Network');
-				if (!network) {
+			async login() {
+				if (!uni.getStorageSync('Network')) {
+					uni.showToast({ icon: 'error', title: '璇烽厤缃綉缁滀俊鎭�' }) 
+					return
+				}
+				if (!this.user.username ) {
+					uni.showToast({ icon: 'none', title: '璇疯緭鍏ヨ处鍙�' }) 
+					return
+				}
+				if (!this.user.password ) {
+					uni.showToast({ icon: 'none', title: '璇疯緭鍏ュ瘑鐮�' }) 
+					return
+				}
+				this.load.loading = true;
+				this.load.btnText = '鐧诲綍涓�';
+				uni.setStorageSync('user', this.user);
+				let res = await common.onLogin(this.user)
+				if (res.code === 200) {
+					setTimeout(() => {
+						uni.showToast({
+							title: '鐧诲綍鎴愬姛'
+						})
+						setTimeout(() => {
+							uni.reLaunch({
+								url: `/pages/home/home`,
+							});
+						}, 300)
+					}, 700)
+				} else if (res.code === 0) {
+					this.load.loading = false;
+					this.load.btnText = '鐧诲綍';
 					uni.showToast({
-						icon: 'error',
-						title: '璇烽厤缃綉缁滀俊鎭�'
+						icon: 'none',
+						title: '杩炴帴澶辫触锛岃妫�鏌ョ綉缁�'
+					})
+				} else {
+					this.load.loading = false;
+					this.load.btnText = '鐧诲綍';
+					uni.showToast({
+						icon: 'none',
+						title: `${this.user.username} ${res.msg}`
 					})
 				}
-				// path = network[0].address
-				// path = path.substring(0, path.length - 3);
-				path = _this.project.name
-				let baseUrl = `http://${network[0].ip}:${network[0].port}/${network[0].address}`
-				_this.load.loading = true;
-				_this.load.btnText = '鐧诲綍涓�';
-				uni.setStorageSync('user', _this.user);
-				uni.request({
-					url: `${baseUrl}/login.action`,
-					data: {
-						username: _this.user.username,
-						password: md5.hex_md5(_this.user.password)
-					},
-					success(res) {
-						res = res.data
-						if (res.code === 200) {
-							uni.setStorageSync('token', res.data.token);
-							_this.load.btnText = '鐧诲綍涓�';
-							setTimeout(() => {
-								uni.showToast({
-									title: '鐧诲綍鎴愬姛'
-								})
-								setTimeout(() => {
-									uni.reLaunch({
-										url: `/pages/home/home`,
-									});
-								}, 300)
-							}, 700)
-						}
-						// uni.navigateTo({
-						// 	url: `/pages/project/${path}/home/home`,
-						// 	fail(res) {
-						// 		console.log(`娌℃湁${path}椤圭洰,璇疯仈绯荤鐞嗗厓`);
-						// 	}
-
-						// })
-					}
-				})
-			}
+			},
+			// end
 		}
 	}
 </script>
diff --git a/pages/api/common/common.js b/pages/api/common/common.js
index e69de29..67a13a7 100644
--- a/pages/api/common/common.js
+++ b/pages/api/common/common.js
@@ -0,0 +1,29 @@
+import md5 from '@/static/js/md5.js'
+
+let network = uni.getStorageSync('Network')
+let baseUrl = `http://${network[0].ip}:${network[0].port}/${network[0].address}`
+
+async function onLogin(user) {
+	let _this = this,
+		item = {};
+	var login = await uni.request({
+		url: `${baseUrl}/login.action`,
+		data: {
+			username: user.username,
+			password: md5.hex_md5(user.password)
+		},
+	}).then((result) => {
+		result = result.data
+		if (result.code === 200 && result.data.token) {
+			uni.setStorageSync('token', result.data.token);
+		}
+		item = result
+	}, (res) => {
+		item = {code: 0}
+	})
+	return item
+}
+
+module.exports = {
+	onLogin: onLogin
+}
\ No newline at end of file

--
Gitblit v1.9.1