2312970463@qq.com 4 lat temu
rodzic
commit
177490dd06
7 zmienionych plików z 326 dodań i 281 usunięć
  1. 14 0
      .hbuilderx/launch.json
  2. 14 2
      api/public.js
  3. 252 246
      components/Authorize.vue
  4. 36 30
      libs/login.js
  5. 7 1
      libs/routine.js
  6. 1 0
      pages/index/index.vue
  7. 2 2
      utils/request.js

+ 14 - 0
.hbuilderx/launch.json

@@ -1,3 +1,4 @@
+<<<<<<< HEAD
 { // launch.json 配置了启动调试时相关设置,configurations下节点名称可为 app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/
   // launchtype项可配置值为local或remote, local代表前端连本地云函数,remote代表前端连云端云函数
     "version": "0.0",
@@ -9,3 +10,16 @@
         }
     ]
 }
+=======
+{ // launch.json 配置了启动调试时相关设置,configurations下节点名称可为 app-plus/h5/mp-weixin/mp-baidu/mp-alipay/mp-qq/mp-toutiao/mp-360/
+  // launchtype项可配置值为local或remote, local代表前端连本地云函数,remote代表前端连云端云函数
+    "version": "0.0",
+    "configurations": [{
+            "type": "uniCloud",
+            "default": {
+                "launchtype": "local"
+            }
+        }
+    ]
+}
+>>>>>>> eea344d73e59310033ff67dfc6a4ecd7f2d07ed5

+ 14 - 2
api/public.js

@@ -40,9 +40,20 @@ export function getLogo() {
 		noAuth: true
 	});
 }
-
+// #ifdef MP-WEIXIN
+/**
+ * 微信小程序用户登录
+ * @param data object 小程序用户登陆信息
+ */
+export function login(data) {
+	return request.post("auth/mp", data, {
+		noAuth: true
+	});
+}
+// #endif
+// #ifdef MP-TOUTIAO
 /**
- * 小程序用户登录
+ * 头条小程序用户登录
  * @param data object 小程序用户登陆信息
  */
 export function login(data) {
@@ -50,6 +61,7 @@ export function login(data) {
 		noAuth: true
 	});
 }
+// #endif
 /**
  * 分享
  * @returns {*}

+ 252 - 246
components/Authorize.vue

@@ -1,297 +1,303 @@
 <template>
 	<view>
-		<view class='Popup' v-if='isShowAuth && code'>
-			<view class="logo-auth">
-				<image :src='logoUrl' mode="aspectFit"></image>
-			</view>
-			<view class='title'>授权提醒</view>
-			<view class='tip'>请授权头像等信息,以便为您提供更好的服务</view>
-			<view class='bottom flex'>
-				<view class='item' @click='close'>随便逛逛</view>
+		<view class="Popup" v-if="isShowAuth && code">
+			<view class="logo-auth"><image :src="logoUrl" mode="aspectFit"></image></view>
+			<view class="title">授权提醒</view>
+			<view class="tip">请授权头像等信息,以便为您提供更好的服务</view>
+			<view class="bottom flex">
+				<view class="item" @click="close">随便逛逛</view>
 				<!-- #ifdef APP-PLUS -->
-				<button class='item grant' @click="setUserInfo">去授权</button>
+				<button class="item grant" @click="setUserInfo">去授权</button>
 				<!-- #endif -->
 				<!-- #ifdef MP -->
-				<button v-if="canUseGetUserProfile" class="item grant" hover-class="none"
-					@tap="getUserProfile">去授权</button>
-				<button v-else class='item grant' type="primary" open-type="getUserInfo" lang="zh_CN"
-					@getuserinfo="setUserInfo">去授权</button>
+				<button v-if="canUseGetUserProfile" class="item grant" hover-class="none" @tap="getUserProfile">去授权</button>
+				<button v-else class="item grant" type="primary" open-type="getUserInfo" lang="zh_CN" @getuserinfo="setUserInfo">去授权</button>
 				<!-- #endif -->
 			</view>
 		</view>
-		<view class='mask' v-if='isShowAuth && code' @click='close'></view>
+		<view class="mask" v-if="isShowAuth && code" @click="close"></view>
 	</view>
 </template>
 
 <script>
-	const app = getApp();
-	import Cache from '../utils/cache';
-	import {
-		getLogo
-	} from '../api/public';
-	import {
-		LOGO_URL
-	} from '../config/cache';
-	import {
-		mapGetters
-	} from 'vuex';
-	import Routine from '../libs/routine';
+const app = getApp();
+import Cache from '../utils/cache';
+import { getLogo } from '../api/public';
+import { LOGO_URL } from '../config/cache';
+import { mapGetters } from 'vuex';
+import Routine from '../libs/routine';
 
-	export default {
-		name: 'Authorize',
-		props: {
-			isAuto: {
-				type: Boolean,
-				default: true
-			},
-			isGoIndex: {
-				type: Boolean,
-				default: true
-			},
-			isShowAuth: {
-				type: Boolean,
-				default: false
-			}
+export default {
+	name: 'Authorize',
+	props: {
+		isAuto: {
+			type: Boolean,
+			default: true
 		},
-		data() {
-			return {
-				logoUrl: app.globalData.routine_logo,
-				canUseGetUserProfile: false,
-				code: null,
-			}
+		isGoIndex: {
+			type: Boolean,
+			default: true
 		},
-		computed: mapGetters(['isLogin', 'userInfo']),
-		watch: {
-			isLogin(n) {
-				n === true && this.$emit('onLoadFun', this.userInfo);
-			},
-			isShowAuth(n) {
-				if (n) {
-					uni.showLoading({
-						title: '正在登录中'
-					});
-					Routine.getCode().then(code => {
+		isShowAuth: {
+			type: Boolean,
+			default: false
+		}
+	},
+	data() {
+		return {
+			logoUrl: app.globalData.routine_logo,
+			canUseGetUserProfile: false,
+			code: null
+		};
+	},
+	computed: mapGetters(['isLogin', 'userInfo']),
+	watch: {
+		isLogin(n) {
+			console.log(n, '判断是否登录');
+			n === true && this.$emit('onLoadFun', this.userInfo);
+		},
+		isShowAuth(n) {
+			if (n) {
+				uni.showLoading({
+					title: '正在登录中'
+				});
+				Routine.getCode()
+					.then(code => {
 						uni.hideLoading();
 						this.code = code;
-					}).catch(e => {
+					})
+					.catch(e => {
 						uni.hideLoading();
 						uni.showToast({
 							title: '登录失败',
 							duration: 2000
 						});
-					})
-				} else {
-					this.code = null;
-				}
-			}
-		},
-		created() {
-			if (wx.getUserProfile) {
-				this.canUseGetUserProfile = true
+					});
+			} else {
+				this.code = null;
 			}
-			this.getLogoUrl();
-			this.setAuthStatus();
-			uni.$on('update', (data) => {
-				this.logoUrl = data.login_logo
-			})
-		},
-		mounted: function() {
-			this.$nextTick(() => {
-				this.logoUrl = app.globalData.login_logo
-			});
-		},
-		methods: {
-			setAuthStatus() {
-				Routine.authorize().then(res => {
-					if (res.islogin === false)
+		}
+	},
+	created() {
+		if (wx.getUserProfile) {
+			this.canUseGetUserProfile = true;
+		}
+		this.getLogoUrl();
+		this.setAuthStatus();
+		uni.$on('update', data => {
+			this.logoUrl = data.login_logo;
+		});
+	},
+	mounted: function() {
+		this.$nextTick(() => {
+			this.logoUrl = app.globalData.login_logo;
+		});
+	},
+	methods: {
+		setAuthStatus() {
+			Routine.authorize()
+				.then(res => {
+					console.log('获取授权信息',res);
+					// #ifdef MP-TOUTIAO
+						this.getUserInfo();
+					// #endif
+					// #ifdef MP-WEIXIN
+					if (res.islogin === false) {
 						this.$emit('onLoadFun', this.userInfo);
-				}).catch(res => {
-					if (this.isAuto)
-						this.$emit('authColse', true);
+					}
+					// #endif
 				})
-			},
-			getUserInfo(code) {
-				Routine.getUserInfo().then(res => {
-					let userInfo = res.userInfo
+				.catch(res => {
+					if (this.isAuto) {
+						this.$emit('authColse', true);
+					}
+				});
+		},
+		getUserInfo(code) {
+			Routine.getUserInfo()
+				.then(res => {
+					console.log(res, '授权数据');
+					let userInfo = res.userInfo;
 					userInfo.code = code;
-					userInfo.spread_spid = this.$Cache.get("spread") || app.globalData.spid; //获取推广人ID
+					userInfo.spread_spid = this.$Cache.get('spread') || app.globalData.spid; //获取推广人ID
 					userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
-					Routine.authUserInfo(userInfo).then(res => {
-						uni.hideLoading();
-						this.$emit('authColse', false);
-						this.$emit('onLoadFun', this.userInfo);
-					}).catch(res => {
-						uni.hideLoading();
-						uni.showToast({
-							title: res.msg,
-							icon: 'none',
-							duration: 2000
+					Routine.authUserInfo(userInfo)
+						.then(res => {
+							uni.hideLoading();
+							this.$emit('authColse', false);
+							this.$emit('onLoadFun', this.userInfo);
+						})
+						.catch(res => {
+							uni.hideLoading();
+							uni.showToast({
+								title: res.msg,
+								icon: 'none',
+								duration: 2000
+							});
 						});
-					})
-				}).catch(res => {
-					uni.hideLoading();
 				})
-			},
-			getUserProfile() {
-				let self = this;
-
-				Routine.getUserProfile()
-					.then(res => {
-						let userInfo = res.userInfo;
-						userInfo.code = this.code;
-						userInfo.spread_spid = app.globalData.spid; //获取推广人ID
-						userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
-						Routine.authUserInfo(userInfo)
-							.then(res => {
-								if (res.data.key !== undefined && res.data.key) {
-									uni.hideLoading();
-									self.authKey = res.data.key;
-									self.isPhoneBox = true;
-								} else {
-									uni.hideLoading();
-									let time = res.data.expires_time - self.$Cache.time();
-									self.$store.commit('LOGIN', {
-										token: res.data.token,
-										time: time
-									});
-								}
-							})
-							.catch(res => {
+				.catch(res => {
+					uni.hideLoading();
+				});
+		},
+		getUserProfile() {
+			let self = this;
+			Routine.getUserProfile()
+				.then(res => {
+					let userInfo = res.userInfo;
+					userInfo.code = this.code;
+					userInfo.spread_spid = app.globalData.spid; //获取推广人ID
+					userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
+					Routine.authUserInfo(userInfo)
+						.then(res => {
+							if (res.data.key !== undefined && res.data.key) {
 								uni.hideLoading();
-								uni.showToast({
-									title: res.msg,
-									icon: 'none',
-									duration: 2000
+								self.authKey = res.data.key;
+								self.isPhoneBox = true;
+							} else {
+								uni.hideLoading();
+								let time = res.data.expires_time - self.$Cache.time();
+								self.$store.commit('LOGIN', {
+									token: res.data.token,
+									time: time
 								});
+							}
+						})
+						.catch(res => {
+							uni.hideLoading();
+							uni.showToast({
+								title: res.msg,
+								icon: 'none',
+								duration: 2000
 							});
-					})
-					.catch(res => {
-						uni.hideLoading();
-					});
-
-			},
+						});
+				})
+				.catch(res => {
+					uni.hideLoading();
+				});
+		},
 
-			setUserInfo() {
-				uni.showLoading({
-					title: '正在登录中'
+		setUserInfo() {
+			uni.showLoading({
+				title: '正在登录中'
+			});
+			this.getUserInfo(this.code);
+			this.$emit('authColse', false);
+		},
+		getLogoUrl() {
+			this.logoUrl = app.globalData.routine_logo;
+			// let that = this;
+			// if (Cache.has(LOGO_URL)) {
+			// 	this.logoUrl = Cache.get(LOGO_URL);
+			// 	return;
+			// }
+			// getLogo().then(res=>{
+			// 	that.logoUrl = res.data.logo_url
+			// 	Cache.set(LOGO_URL,that.logoUrl);
+			// })
+		},
+		close() {
+			let pages = getCurrentPages(),
+				currPage = pages[pages.length - 1];
+			if (this.isGoIndex) {
+				uni.switchTab({
+					url: '/pages/index/index'
 				});
-				this.getUserInfo(this.code);
+			} else {
 				this.$emit('authColse', false);
-			},
-			getLogoUrl() {
-				this.logoUrl = app.globalData.routine_logo
-				// let that = this;
-				// if (Cache.has(LOGO_URL)) {
-				// 	this.logoUrl = Cache.get(LOGO_URL);
-				// 	return;
-				// }
-				// getLogo().then(res=>{
-				// 	that.logoUrl = res.data.logo_url
-				// 	Cache.set(LOGO_URL,that.logoUrl);
-				// })
-			},
-			close() {
-				let pages = getCurrentPages(),
-					currPage = pages[pages.length - 1];
-				if (this.isGoIndex) {
-					uni.switchTab({
-						url: '/pages/index/index'
-					});
-				} else {
-					this.$emit('authColse', false);
-				}
-				// if (currPage && currPage.isShowAuth != undefined){
-				// 	currPage.isShowAuth = true;
-				// }
-			},
+			}
+			// if (currPage && currPage.isShowAuth != undefined){
+			// 	currPage.isShowAuth = true;
+			// }
 		}
 	}
+};
 </script>
 
-<style scoped lang='scss'>
-	.Popup {
-		width: 500rpx;
-		background-color: #fff;
-		position: fixed;
-		top: 50%;
+<style scoped lang="scss">
+.Popup {
+	width: 500rpx;
+	background-color: #fff;
+	position: fixed;
+	top: 50%;
+	left: 50%;
+	margin-left: -250rpx;
+	transform: translateY(-50%);
+	z-index: 1000;
+}
+
+.Popup {
+	.logo-auth {
+		z-index: -1;
+		position: absolute;
 		left: 50%;
-		margin-left: -250rpx;
-		transform: translateY(-50%);
-		z-index: 1000;
+		top: 0%;
+		transform: translate(-50%, -50%);
+		width: 150rpx;
+		height: 150rpx;
+		display: flex;
+		align-items: center;
+		justify-content: center;
+		border: 8rpx solid #fff;
+		border-radius: 50%;
+		background: #fff;
 	}
 
-	.Popup {
-		.logo-auth {
-			z-index: -1;
-			position: absolute;
-			left: 50%;
-			top: 0%;
-			transform: translate(-50%, -50%);
-			width: 150rpx;
-			height: 150rpx;
-			display: flex;
-			align-items: center;
-			justify-content: center;
-			border: 8rpx solid #fff;
-			border-radius: 50%;
-			background: #fff;
-		}
-
-		image {
-			height: 42rpx;
-			margin-top: -54rpx;
-		}
+	image {
+		height: 42rpx;
+		margin-top: -54rpx;
 	}
+}
 
-	.Popup .title {
-		font-size: 28rpx;
-		color: #000;
-		text-align: center;
-		margin-top: 30rpx
-	}
+.Popup .title {
+	font-size: 28rpx;
+	color: #000;
+	text-align: center;
+	margin-top: 30rpx;
+}
 
-	.Popup .tip {
-		font-size: 22rpx;
-		color: #555;
-		padding: 0 24rpx;
-		margin-top: 25rpx;
-	}
+.Popup .tip {
+	font-size: 22rpx;
+	color: #555;
+	padding: 0 24rpx;
+	margin-top: 25rpx;
+}
 
-	.Popup .bottom .item {
-		width: 50%;
-		height: 80rpx;
-		background-color: #eeeeee;
-		text-align: center;
-		line-height: 80rpx;
-		font-size: 24rpx;
-		color: #666;
-		margin-top: 54rpx;
-	}
+.Popup .bottom .item {
+	width: 50%;
+	height: 80rpx;
+	background-color: #eeeeee;
+	text-align: center;
+	line-height: 80rpx;
+	font-size: 24rpx;
+	color: #666;
+	margin-top: 54rpx;
+}
 
-	.Popup .bottom .item.on {
-		width: 100%
-	}
+.Popup .bottom .item.on {
+	width: 100%;
+}
 
-	.flex {
-		display: flex;
-	}
+.flex {
+	display: flex;
+}
 
-	.Popup .bottom .item.grant {
-		font-size: 28rpx;
-		color: #fff;
-		font-weight: bold;
-		background-color: #e93323;
-		border-radius: 0;
-		padding: 0;
-	}
+.Popup .bottom .item.grant {
+	font-size: 28rpx;
+	color: #fff;
+	font-weight: bold;
+	background-color: #e93323;
+	border-radius: 0;
+	padding: 0;
+}
 
-	.mask {
-		position: fixed;
-		top: 0;
-		right: 0;
-		left: 0;
-		bottom: 0;
-		background-color: rgba(0, 0, 0, 0.65);
-		z-index: 999;
-	}
+.mask {
+	position: fixed;
+	top: 0;
+	right: 0;
+	left: 0;
+	bottom: 0;
+	background-color: rgba(0, 0, 0, 0.65);
+	z-index: 999;
+}
 </style>

+ 36 - 30
libs/login.js

@@ -1,13 +1,20 @@
 import store from "../store";
 import Cache from '../utils/cache';
 // #ifdef H5 || APP-PLUS
-import { isWeixin } from "../utils";
+import {
+	isWeixin
+} from "../utils";
 import auth from './wechat';
 // #endif
 
-import { LOGIN_STATUS, USER_INFO, EXPIRES_TIME, STATE_R_KEY} from './../config/cache';
+import {
+	LOGIN_STATUS,
+	USER_INFO,
+	EXPIRES_TIME,
+	STATE_R_KEY
+} from './../config/cache';
 
-function prePage(){
+function prePage() {
 	let pages = getCurrentPages();
 	let prePage = pages[pages.length - 2];
 	// #ifdef H5
@@ -19,60 +26,59 @@ function prePage(){
 export function toLogin(push, pathLogin) {
 	store.commit("LOGOUT");
 	let path = prePage();
-	if(path){
+	if (path) {
 		path = path.router;
-		if(path == undefined){
+		if (path == undefined) {
 			path = location.pathname + location.search;
 		}
-	}  
-		// #ifdef H5
-	else{
+	}
+	// #ifdef H5
+	else {
 		path = location.pathname + location.search;
-	} 
-		// #endif
-		
-	if(!pathLogin)
+	}
+	// #endif
+
+	if (!pathLogin)
 		pathLogin = '/page/users/login/index'
-	Cache.set('login_back_url',path);
+	Cache.set('login_back_url', path);
 	// #ifdef H5 || APP-PLUS
 	if (isWeixin()) {
 		auth.oAuth();
 	} else {
 		if (path !== pathLogin) {
-		 push ? uni.navigateTo({
-		 	url:'/pages/users/login/index'
-		 }) : uni.reLaunch({
-		 	url: '/pages/users/login/index'
-		 });
+			push ? uni.navigateTo({
+				url: '/pages/users/login/index'
+			}) : uni.reLaunch({
+				url: '/pages/users/login/index'
+			});
 		}
 	}
 	// #endif
-	
+
 	// #ifdef MP 
-	
-	
+
+
 	// #endif
 }
 
 
-export function checkLogin()
-{
+export function checkLogin() {
 	let token = Cache.get(LOGIN_STATUS);
 	let expiresTime = Cache.get(EXPIRES_TIME);
 	let newTime = Math.round(new Date() / 1000);
-	if (expiresTime < newTime || !token){
+	if (expiresTime < newTime || !token) {
 		Cache.clear(LOGIN_STATUS);
 		Cache.clear(EXPIRES_TIME);
 		Cache.clear(USER_INFO);
 		Cache.clear(STATE_R_KEY);
 		return false;
-	}else{
-		store.commit('UPDATE_LOGIN',token);
-		let userInfo = Cache.get(USER_INFO,true);
-		if(userInfo){
-			store.commit('UPDATE_USERINFO',userInfo);
+	} else {
+		store.commit('UPDATE_LOGIN', token);
+		let userInfo = Cache.get(USER_INFO, true);
+		if (userInfo) {
+			store.commit('UPDATE_USERINFO', userInfo);
 		}
 		return true;
 	}
 
-}
+}

+ 7 - 1
libs/routine.js

@@ -52,8 +52,14 @@ class Routine
 				});
 			uni.authorize({
 			    scope: that.scopeUserInfo,
-			    success() {
+			    success(res) {
+					// #ifdef MP-TOUTIAO
+					console.log(res);
+					resolve({islogin:false},res.data);
+					// #endif
+					// #ifdef MP-WEIXIN
 					resolve({islogin:false});
+					// #endif
 			    },
 				fail(res){
 					reject(res);

+ 1 - 0
pages/index/index.vue

@@ -699,6 +699,7 @@
 		methods: {
 			// 微信登录回调
 			onLoadFun: function(e) {
+				console.log('回调数据',e);
 				this.isShowAuth = false
 			},
 			setViewport(content) {

+ 2 - 2
utils/request.js

@@ -23,7 +23,7 @@ function baseRequest(url, method, data, {
 	if (!noAuth) {
 		//登录过期自动登录
 		if (!store.state.app.token && !checkLogin()) {
-			toLogin();
+			// toLogin();
 			return Promise.reject({
 				msg: '未登录'
 			});
@@ -44,7 +44,7 @@ function baseRequest(url, method, data, {
 				else if (res.data.status == 200)
 					reslove(res.data, res);
 				else if ([410000, 410001, 410002, 40000].indexOf(res.data.status) !== -1) {
-					toLogin();
+					// toLogin();
 					reject(res.data);
 				} else if (res.data.status == 501) {
 					uni.reLaunch({