hwq преди 1 година
родител
ревизия
a49ef5f4ba
променени са 3 файла, в които са добавени 132 реда и са изтрити 108 реда
  1. 2 2
      pages/public/wxLogin.vue
  2. 127 105
      pages/redirect/redirect.vue
  3. 3 1
      utils/wxAuthorized.js

+ 2 - 2
pages/public/wxLogin.vue

@@ -75,7 +75,8 @@
 				isclick: false, //是否点击了
 				isclick: false, //是否点击了
 				iv1: '',
 				iv1: '',
 				encryptedData1: '',
 				encryptedData1: '',
-				hasphone: false
+				hasphone: false,
+				spid: ''
 			};
 			};
 		},
 		},
 		onLoad() {
 		onLoad() {
@@ -175,7 +176,6 @@
 				// #ifdef MP
 				// #ifdef MP
 				let spread_code = uni.getStorageSync('spread_code') || '';
 				let spread_code = uni.getStorageSync('spread_code') || '';
 				// #endif
 				// #endif
-
 				wechatMpAuth({
 				wechatMpAuth({
 						code: obj.code,
 						code: obj.code,
 						iv: user.iv,
 						iv: user.iv,

+ 127 - 105
pages/redirect/redirect.vue

@@ -3,120 +3,142 @@
 	</view>
 	</view>
 </template>
 </template>
 <script>
 <script>
-import { getUserInfo } from '@/api/user.js';
-import { mapMutations,mapState } from 'vuex';
-// #ifdef H5
-import { wechatAuth } from '@/api/wx';
-// #endif
-// #ifdef MP-WEIXIN
-import { wechatMpAuth } from '@/api/wx';
-// #endif
-export default {
-	computed: {
-		...mapState(['urlFile'])
-	},
-	onLoad(option) {
-		let obj = this;
-		// 判断是否需要保存定向地址
-		// #ifdef H5
-		this.loadH5()
-		// #endif
-		// #ifdef MP-WEIXIN
-		this.loadMp(option)
-		// #endif
-	},
-	methods: {
-		...mapMutations('user',['login', 'setUserInfo']),
-		// #ifdef H5
-		loadH5() {
-			let obj = this;
-			let url = window.location.href;
-			let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
-			let spread = uni.getStorageSync('spread')||'';
-			wechatAuth({
-				code: code,
-				spread:spread,
-			}).then(({ data }) => {
-				obj.wchatAuth(data);
-			}).catch((e) => {
-				uni.showModal({
-					title: '错误',
-					content: JSON.stringify(e),
-					showCancel: false,
-				});
-			});;
+	import {
+		getUserInfo
+	} from '@/api/user.js';
+	import {
+		mapMutations,
+		mapState
+	} from 'vuex';
+	// #ifdef H5
+	import {
+		wechatAuth
+	} from '@/api/wx';
+	// #endif
+	// #ifdef MP-WEIXIN
+	import {
+		wechatMpAuth
+	} from '@/api/wx';
+	// #endif
+	export default {
+		computed: {
+			...mapState(['urlFile'])
+		},
+		data() {
+			return {
+				spid: '',
+			}
 		},
 		},
-		// #endif
-		// #ifdef MP-WEIXIN
-		loadMp(option) {
+		onLoad(option) {
 			let obj = this;
 			let obj = this;
-			// 获取登录授权页数据
-			let user = obj.$api.prePage().userInfo;
-			// #ifndef MP
-			// 获取推广人id
-			let spread_spid = uni.getStorageSync('spread') || '';
+			// 判断是否需要保存定向地址
+			// #ifdef H5
+			if (option.spid) {
+				this.spid = option.spid
+			}
+			this.loadH5()
 			// #endif
 			// #endif
-			// #ifdef MP
-			// 小程序推广人
-			let spread_code = uni.getStorageSync('spread_code') || '';
+			// #ifdef MP-WEIXIN
+			this.loadMp(option)
+			// #endif
+		},
+		methods: {
+			...mapMutations('user', ['login', 'setUserInfo']),
+			// #ifdef H5
+			loadH5() {
+				let obj = this;
+				let url = window.location.href;
+				let code = url.match(/code=([0-9]|[a-z]|[A-Z])*/g)[0].replace('code=', '');
+				let spread = this.spid != '' ? this.spid : uni.getStorageSync('spread') ? uni.getStorageSync('spread') :
+					'';
+				wechatAuth({
+					code: code,
+					spread: spread,
+				}).then(({
+					data
+				}) => {
+					obj.wchatAuth(data);
+				}).catch((e) => {
+					uni.showModal({
+						title: '错误',
+						content: JSON.stringify(e),
+						showCancel: false,
+					});
+				});;
+			},
 			// #endif
 			// #endif
-			wechatMpAuth({
-				code: option.code,
-				iv: user.target.iv,
-				encryptedData: user.target.encryptedData,
+			// #ifdef MP-WEIXIN
+			loadMp(option) {
+				let obj = this;
+				// 获取登录授权页数据
+				let user = obj.$api.prePage().userInfo;
 				// #ifndef MP
 				// #ifndef MP
-				spread_spid: spread_spid,
+				// 获取推广人id
+				let spread_spid = uni.getStorageSync('spread') || '';
 				// #endif
 				// #endif
 				// #ifdef MP
 				// #ifdef MP
-				spread_code:spread_code
+				// 小程序推广人
+				let spread_code = uni.getStorageSync('spread_code') || '';
 				// #endif
 				// #endif
-			}).then(({ data }) => {
-				obj.wchatAuth(data);
-			}).catch((e) => {
-				uni.showModal({
-					title: '错误',
-					content: JSON.stringify(e),
-					showCancel: false,
+				wechatMpAuth({
+					code: option.code,
+					iv: user.target.iv,
+					encryptedData: user.target.encryptedData,
+					// #ifndef MP
+					spread_spid: spread_spid,
+					// #endif
+					// #ifdef MP
+					spread_code: spread_code
+					// #endif
+				}).then(({
+					data
+				}) => {
+					obj.wchatAuth(data);
+				}).catch((e) => {
+					uni.showModal({
+						title: '错误',
+						content: JSON.stringify(e),
+						showCancel: false,
+					});
 				});
 				});
-			});
-		},
-		// #endif
-		wchatAuth(data) {
-			let obj = this;
-			// 保存token
-			uni.setStorageSync('token', data.token);
-			// 获取用户基础信息
-			getUserInfo({}).then(e => {
-				obj.login();
-				// 保存返回用户数据
-				obj.setUserInfo(e.data);
-				let ur = uni.getStorageSync('present')|| '/pages/index/index';
-				// 用于处理缓存bug
-				if (ur=='pages/product/product') {
-					ur = '/pages/index/index'
-				}
-				uni.switchTab({
-					url: ur,
-					fail(e) {
-						uni.navigateTo({
-							url: ur,
-							fail(e) {
-								uni.navigateTo({
-									url: '/pages/index/index',
-								});
-							}
-						});
+			},
+			// #endif
+			wchatAuth(data) {
+				let obj = this;
+				// 保存token
+				uni.setStorageSync('token', data.token);
+				// 获取用户基础信息
+				getUserInfo({}).then(e => {
+					obj.login();
+					// 保存返回用户数据
+					obj.setUserInfo(e.data);
+					let ur = uni.getStorageSync('present') || '/pages/index/index';
+					// 用于处理缓存bug
+					if (ur == 'pages/product/product') {
+						ur = '/pages/index/index'
 					}
 					}
-				});
-			}).catch((e) => {
-				uni.showModal({
-					title: '错误',
-					content: JSON.stringify(e),
-					showCancel: false,
-				});
-			});;
+					uni.switchTab({
+						url: ur,
+						fail(e) {
+							uni.navigateTo({
+								url: ur,
+								fail(e) {
+									uni.navigateTo({
+										url: '/pages/index/index',
+									});
+								}
+							});
+						}
+					});
+				}).catch((e) => {
+					uni.showModal({
+						title: '错误',
+						content: JSON.stringify(e),
+						showCancel: false,
+					});
+				});;
+			}
 		}
 		}
-	}
-};
+	};
 </script>
 </script>
-<style></style>
+<style></style>

+ 3 - 1
utils/wxAuthorized.js

@@ -54,9 +54,11 @@ export async function loginWinxin() {
 function weixinSq() {
 function weixinSq() {
 	// 微信授权后跳转页面
 	// 微信授权后跳转页面
 	try {
 	try {
+		let spread = uni.getStorageSync('spread');
 		// 判断是否真实路由模式
 		// 判断是否真实路由模式
 		const type = router.mode === "history" ? '' : '/#'
 		const type = router.mode === "history" ? '' : '/#'
-		let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + type + '/pages/redirect/redirect');
+		let ul = encodeURIComponent(store.state.baseURL + store.state.urlFile + type +
+			'/pages/redirect/redirect?spid=' + spread);
 		// 打开微信授权页面
 		// 打开微信授权页面
 		let url =
 		let url =
 			'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
 			'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +