| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 | 
							- <template>
 
- 	<view class="container">
 
- 		<!-- 加载提示 -->
 
- 		<view class="loading" v-if="loading">
 
- 			<text>加载中...</text>
 
- 		</view>
 
- 		<!-- web-view组件 -->
 
- 		<web-view :src="webviewUrl" @message="handleMessage" @load="onLoads" @error="onError"></web-view>
 
- 	</view>
 
- </template>
 
- <script>
 
- 	import {loginByMpH5} from '@/api/course.js'
 
- 	export default {
 
- 		data() {
 
- 			return {
 
- 				loading: true,
 
- 				webviewUrl: 'https://h5api.his.cdwjyyh.com/weixinOauth',
 
- 				userInfo: null
 
- 			}
 
- 		},
 
- 		onLoad(options) {
 
- 			console.log("webview",options)
 
- 			if (options.code) {
 
- 				// uni.$emit('usercode', {  code: options.code });
 
- 				this.loginweixin(options.code)
 
- 			}
 
- 			// 生成带参的H5授权页面URL
 
- 			// this.webviewUrl = this.generateAuthUrl()
 
- 		},
 
- 		methods: {
 
- 			loginweixin(datas) {
 
- 				var data = {
 
- 					code: datas,
 
- 				}
 
- 				loginByMpH5(data).then(res => {
 
- 						this.res = res
 
- 						uni.hideLoading();
 
- 						if (res.code == 200) {
 
- 							console.log(res)
 
- 							uni.hideLoading();
 
- 							uni.showToast({
 
- 								icon: 'none',
 
- 								title: "成功获取用户信息",
 
- 							});
 
- 							uni.setStorageSync('userInfos', JSON.stringify(res.user));
 
- 							this.userInfo = res.user;
 
- 							setTimeout(() => {
 
- 								uni.navigateBack({
 
- 									delta: 1
 
- 								});
 
- 							}, 200)
 
- 						} else {
 
- 							uni.hideLoading();
 
- 							uni.showToast({
 
- 								title: res.msg || '获取用户信息失败',
 
- 								icon: 'none'
 
- 							})
 
- 						}
 
- 					},
 
- 					err => {}
 
- 				).catch(err => {
 
- 					uni.hideLoading();
 
- 					uni.showToast({
 
- 						icon: 'none',
 
- 						title: "授权登录失败,请重新登录",
 
- 					});
 
- 				});
 
- 			},
 
- 			// 生成授权页面URL,附带小程序传递的参数
 
- 			generateAuthUrl() {
 
- 				// 获取当前小程序的场景值,用于后续业务处理
 
- 				const scene = uni.getLaunchOptionsSync().scene
 
- 				// 这里替换为你的uniapp H5项目域名
 
- 				// 拼接参数,可包含小程序特有的信息
 
- 				const params = {
 
- 					scene,
 
- 					appid: 'wx961fadab9bcb792b', // 公众号AppID
 
- 					redirect_uri: encodeURIComponent('https://h5api.his.cdwjyyh.com/weixinOauth'),
 
- 					scope: 'snsapi_userinfo',
 
- 					state: 'wechat_redirect'
 
- 				}
 
- 				// 微信公众号授权URL
 
- 				return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${params.appid}&redirect_uri=${params.redirect_uri}&response_type=code&scope=${params.scope}&state=${params.state}#wechat_redirect`
 
- 			},
 
- 			// 处理web-view向小程序发送的消息
 
- 			handleMessage(e) {
 
- 				console.log('收到web-view消息:', e.detail)
 
- 				console.log('收到web-view消息:', e)
 
- 				// 获取H5页面传递过来的用户信息
 
- 				if (e.detail && e.detail.type === 'user_info') {
 
- 					this.userInfo = e.detail.data
 
- 					this.token = e.detail.token
 
- 					// 存储用户信息到本地
 
- 					uni.setStorageSync('userInfo', this.userInfo)
 
- 					uni.setStorageSync('TOKEN_WEXIN', this.userInfo)
 
- 					// 返回上一页或跳转到首页
 
- 					uni.showToast({
 
- 						title: '登录成功',
 
- 						icon: 'success'
 
- 					})
 
- 					setTimeout(() => {
 
- 						uni.navigateBack()
 
- 					}, 1500)
 
- 				}
 
- 			},
 
- 			// web-view加载完成
 
- 			onLoads() {
 
- 				this.loading = false
 
- 				console.log('web-view加载完成')
 
- 			},
 
- 			// web-view加载失败
 
- 			onError(e) {
 
- 				this.loading = false
 
- 				console.error('web-view加载失败:', e)
 
- 				uni.showToast({
 
- 					title: '页面加载失败',
 
- 					icon: 'none'
 
- 				})
 
- 			}
 
- 		}
 
- 	}
 
- </script>
 
- <style>
 
- 	.container {
 
- 		width: 100%;
 
- 		height: 100%;
 
- 		position: relative;
 
- 	}
 
- 	.loading {
 
- 		position: absolute;
 
- 		top: 0;
 
- 		left: 0;
 
- 		width: 100%;
 
- 		height: 100%;
 
- 		display: flex;
 
- 		justify-content: center;
 
- 		align-items: center;
 
- 		background-color: #fff;
 
- 		z-index: 100;
 
- 	}
 
- 	web-view {
 
- 		width: 100%;
 
- 		height: 100%;
 
- 	}
 
- </style>
 
 
  |