Bläddra i källkod

Signed-off-by: 1004749546@qq.com <1004749546@qq.com>
微信登录

1004749546@qq.com 4 år sedan
förälder
incheckning
600d2cb2f2
3 ändrade filer med 118 tillägg och 73 borttagningar
  1. 4 11
      api/login.js
  2. 47 23
      pages/index/index.vue
  3. 67 39
      pages/login/login.vue

+ 4 - 11
api/login.js

@@ -1,21 +1,14 @@
-import request from '@/libs/axios.js';
+import axios from '@/libs/axios.js';
 
-export const code2Session = params => {
-  return request({
-    url: 'code2Session',
-    data: params,
-    method: 'get'
-  }).then(res => res.data);
-};
 export const login = params => {
-  return request({
+  return axios.request({
     url: `loginVerify`,
     data: params,
     method: 'post',
   }).then(res => res.data);
 };
 export const logout = params => {
-  return request({
+  return axios.request({
     url: `logout`,
     data: params,
     method: 'get'
@@ -26,7 +19,7 @@ export const logout = params => {
 export const checkLogin = () => {
   let token = uni.getStorageSync('token')
   if (token&&token!='') {
-    return request({
+    return axios.request({
       url: `checkLogin`,
       method: 'get'
     }).then(res => res.data);

+ 47 - 23
pages/index/index.vue

@@ -47,12 +47,14 @@
 <script>
 	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
 	import uniPopup from '@/components/uni-popup/uni-popup.vue'
+	import {checkLogin} from '@/api/login.js'
 	export default {
 		components: {
 			uniNavBar, uniPopup
 		},
 		data() {
 			return {
+				hasLogin: false, // 用户是否登录
 				mapCtx: null, // 地图对象
 				markers: [], // 标注点
 				centerX: '',
@@ -105,10 +107,32 @@
 		onReady() {
 			this.mapCtx = uni.createMapContext('map')
 		},
+		onShow() {
+			this.checkLogin()
+		},
 		onLoad() {
 			this.getUserLocation()
 		},
 		methods: {
+			// 检验登录token是否过期
+			checkLogin () {
+				uni.checkSession({
+					success:(res) => {
+						console.log('已登录')
+						checkLogin().then(res =>{
+							if (res.status == '200'){
+								this.hasLogin = true
+							} else {
+								this.hasLogin = false
+							}
+						})
+					},
+					fail: (err) => {
+						console.log('登录过期')
+						this.hasLogin = false
+					}
+				})
+			},
 			// 用户位置信息授权
 			getUserLocation() {
 				let _this = this
@@ -180,9 +204,16 @@
 			},
 			// 进入个人中心
 			toUserCenter () {
-				uni.navigateTo({
-				    url: '/pages/userCenter/index'
-				})
+				if (this.hasLogin) {
+					uni.navigateTo({
+					    url: '/pages/userCenter/index'
+					})
+				} else {
+					uni.navigateTo({
+					  url: `/pages/login/login?lanuch=false&path=` + encodeURIComponent(`/pages/userCenter/index`)
+					});
+				}
+				
 			},
 			// 打开网点列表
 			intoList() {
@@ -218,26 +249,19 @@
 			},
 			// 扫码洗车
 			scanCode () {
-				uni.checkSession({
-					success: (res) => {
-						console.log("已经登陆")
-						uni.navigateTo({
-							url: "/pages/getOrder/getOrder"
-						})
-					},
-					fail: (err) => {
-						console.log("需要重新登录")
-						uni.navigateTo({
-							url: "/pages/login/login"
-						})
-					}
-				})
-				// uni.scanCode({
-				//     success: function (res) {
-				//         console.log('条码类型:' + res.scanType);
-				//         console.log('条码内容:' + res.result);
-				//     }
-				// });
+				if (this.hasLogin) {
+					uni.scanCode({
+					    success: function (res) {
+					        console.log('条码类型:' + res.scanType);
+					        console.log('条码内容:' + res.result);
+					    }
+					});
+				} else {
+					uni.navigateTo({
+						url: '/pages/login/login'
+					})
+				}
+				
 			},
 			// 创建标记点
 			createMarker(point) {

+ 67 - 39
pages/login/login.vue

@@ -1,62 +1,90 @@
 <template>
 	<view class="content">
-		<u-button type="success" open-type="getUserInfo" @click="loginIn">微信一键登录</u-button>
+		<u-button type="success" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</u-button>
 	</view>
 </template>
 
 <script>
-	import {code2Session} from "@/api/login.js"
+	import {
+		login
+	} from "@/api/login.js"
 	export default {
-		components: {
-		},
+		components: {},
 		data() {
 			return {
-				
+				path: '/pages/index/index',
+				lanuch: true
 			}
 		},
-		onShow () {
-		 
-		},
+		onShow() {},
+		mounted() {},
 		onLoad(option) {
-			
+			console.log(option)
+			const {
+				path,
+				lanuch
+			} = option;
+			this.lanuch = (lanuch + '') === 'true';
+			if (path) {
+				this.path = decodeURIComponent(path);
+			}
 		},
 		methods: {
-			loginIn () {
-				let _this = this
-				// uni.login({
-				//   provider: provider,
-				//   success(result) {
-				// 	console.log(result)
-				// 	if (result.code) {
-				// 	  code2Session({
-				// 		code: result.code
-				// 	  }).then(res => {
-				// 		if (res.status === '200') {
-				// 			_this.$u.vuex('vuex_openid',res.data)
-				// 			uni.navigateTo({
-				// 				url: '/pages/index/index'
-				// 			})
-				// 		};
-				// 	  });
-				// 	}
-				//   }
-				// });
-				uni.login({
-				  provider: 'weixin',
-				  success: function (loginRes) {
-				    console.log(loginRes);  // 返回code
-					uni.navigateTo({
-						url: '/pages/index/index'
+			getPhoneNumber(e) {
+				uni.showLoading({
+					mask: true,
+					title: '加载中'
+				});
+				if (e.target.errMsg === 'getPhoneNumber:ok') {
+					uni.login({
+						success(result) {
+							if (result.code) {
+								login({
+									code: result.code,
+									encryptedData: e.target.encryptedData,
+									iv: e.target.iv
+								}).then(res => {
+									uni.hideLoading();
+									if (res.status === '200') {
+										console.log(res.data, 'login data')
+										uni.setStorageSync('token', res.data.token);
+										uni.setStorageSync('phone', res.data.phone);
+										uni.setStorageSync('userId', res.data.userId);
+										if (this.path === '/pages/index/main' || this.lanuch) {
+											uni.reLaunch({
+												url: this.path
+											});
+										} else {
+											uni.redirectTo({
+												url: this.path
+											});
+										}
+									} else {
+										uni.showToast({
+											title: res.message,
+											icon: 'none',
+											duration: 2500
+										});
+									}
+								});
+							}
+						}
 					})
-				  }
-				})
-			},
+
+				} else {
+					uni.showToast({
+						title: '授权失败',
+						icon: 'none',
+						duration: 2500
+					});
+				}
+			}
 		}
 	}
 </script>
 
 <style>
-	.content{
+	.content {
 		padding: 0;
 		display: flex;
 		align-items: center;