Browse Source

研究报告详情:如果没有登录显示登录按钮,如果没有购买显示购买按钮

littleblue55 1 month ago
parent
commit
63ebc0f13a
2 changed files with 181 additions and 68 deletions
  1. 72 48
      pages/login/login.vue
  2. 109 20
      pages/reportDetail/reportDetail.vue

+ 72 - 48
pages/login/login.vue

@@ -127,6 +127,9 @@
 	import {
 		encryptAESCBC
 	} from "@/utils/crypto.js"
+	import {
+		me
+	} from '@/api/user.js'
 	import configService from '@/utils/baseurl.js'
 	export default {
 		data() {
@@ -158,6 +161,7 @@
 					password: "123456"
 				},
 				userBook: false, //协议
+				redirect: null
 			}
 		},
 		methods: {
@@ -222,19 +226,26 @@
 				if (res.data.token) {
 					msgSuccess("登录成功!")
 					this.useAuthStore.setAuthToken(res.data.token)
-					const loginRes = await wx.login();
-					const code = loginRes.code;
-					wxLoginOpenid({code}).then(resp=>{
-						if(resp?.data){
-							const { openid, sessionKey } = resp.data
-							this.useAuthStore.setOpenid(openid)
-						}
-						setTimeout(() => {
-							uni.switchTab({
-								url: "/pages/index/index"
-							})
-						}, 1000)
-					})
+					this.initUser()
+					// const loginRes = await wx.login();
+					// const code = loginRes.code;
+					// wxLoginOpenid({code}).then(resp=>{
+					// 	if(resp?.data){
+					// 		const { openid, sessionKey } = resp.data
+					// 		this.useAuthStore.setOpenid(openid)
+					// 	}
+					// 	setTimeout(() => {
+					// 		if(this.redirect){
+					// 			uni.reLaunch({
+					// 				url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
+					// 			})
+					// 		}else{
+					// 			uni.switchTab({
+					// 				url: "/pages/index/index"
+					// 			})
+					// 		}
+					// 	}, 1000)
+					// })
 				}
 			},
 			// 账号登录
@@ -259,24 +270,7 @@
 				if (res.data.token) {
 					msgSuccess("登录成功!")
 					this.useAuthStore.setAuthToken(res.data.token)
-					const loginRes = await wx.login();
-					const code = loginRes.code;
-					wxLoginOpenid({code}).then(resp=>{
-						if(resp?.data){
-							const { openid, sessionKey } = resp.data
-							this.useAuthStore.setOpenid(openid)
-						}
-						setTimeout(() => {
-							uni.switchTab({
-								url: "/pages/index/index"
-							})
-						}, 1000)
-					})
-					// setTimeout(() => {
-					// 	uni.switchTab({
-					// 		url: "/pages/index/index"
-					// 	})
-					// }, 1000)
+					this.initUser()
 				}
 			},
 			toPage(url) {
@@ -284,6 +278,33 @@
 					url: url
 				})
 			},
+			async initUser(){
+				const loginRes = await wx.login();
+				const code = loginRes.code;
+				let userRes = await me();
+				let wxRes = await wxLoginOpenid({code})
+				// 加载用户信息
+				if(userRes && userRes?.code===0){
+					this.useAuthStore.setUserInfo(userRes.data)
+				}
+				// 加载openid
+				if(wxRes && wxRes?.data){
+					const { openid, sessionKey } = wxRes.data
+					this.useAuthStore.setOpenid(openid)
+				}
+				setTimeout(() => {
+					if(this.redirect){
+						uni.removeStorageSync("redirect")
+						uni.reLaunch({
+							url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
+						})
+					}else{
+						uni.switchTab({
+							url: "/pages/index/index"
+						})
+					}
+				}, 1000)
+			},
 			// --------验证码 start---------
 			codeChange(text) {
 				this.tips = text;
@@ -380,18 +401,6 @@
 						const { openid, sessionKey } = res.data
 					}
 				})
-
-
-				// try {
-				// 	// 获取code
-				// 	const code = loginRes.code;
-				// 	console.log(code)
-				// 	wxLoginOpenid({code}).then(res=>{
-				// 		console.log("weixinlogin", res)
-				// 	})
-				// } catch (error) {
-				// 	console.log('微信登录失败:', error);
-				// }
 			},
 			async checkBook(){
 				let checkUserBook = await this.checkWeixinUserBook()
@@ -428,10 +437,20 @@
 							}else{
 								// 老用户,跳转至:首页
 								msgSuccess("授权成功!")
+								let userRes = await me()
+								if(userRes && userRes?.code===0){
+									this.useAuthStore.setUserInfo(userRes.data)
+								}
 								setTimeout(()=>{
-									uni.switchTab({
-										url: "/pages/index/index"
-									})
+									if(this.redirect){
+										uni.reLaunch({
+											url: this.redirect.url+'?id='+this.redirect.id+'&title='+this.redirect.title
+										})
+									}else{
+										uni.switchTab({
+											url: "/pages/index/index"
+										})
+									}
 								}, 1000)
 							}
 						}
@@ -483,12 +502,17 @@
 				return useAuthStore(); // 获取 store 实例
 			}
 		},
-		onLoad() {
+		onLoad(option) {
 			const that = this;
 			if (uni.getUserProfile) {
-				console.log(1)
+				// console.log(1)
 				that.canIUseGetUserProfile = true
 			}
+			if(option.redirect){
+				console.log("重定向", JSON.parse(option.redirect))
+				uni.setStorageSync("redirect", JSON.parse(option.redirect))
+				this.redirect = JSON.parse(option.redirect)
+			}
 		}
 	}
 </script>

+ 109 - 20
pages/reportDetail/reportDetail.vue

@@ -1,16 +1,11 @@
 <template>
-	<view class="container">
-		<view class="pay-wall">
+	<view class="container" :class="{noscroll: buyShow}">
+		<view class="pay-wall" v-show="loginShow">
 			<view class="title">
 				{{ report.title }}
 			</view>
-			<button @click="toBuy">点击购买</button>
 		</view>
-		<!-- <web-view  id="webview" :src="report.sourceFile">
-			<cover-view class="close-view">
-				<button>22222</button>
-			</cover-view>
-		</web-view> -->
+		<u-parse :html="report.content"></u-parse>
 		<view class="bottom-box">
 			<view class="menu-box">
 				<button class="menu-item" @click="changeApplaud">
@@ -30,6 +25,35 @@
 				</button>
 			</view>
 		</view>
+		<u-popup v-model="buyShow" 
+		:mask="false" :closeable='false' 
+		mode="bottom" :mask-close-able='false'
+		safe-area-inset-bottom>
+			<view style="height: 70vh;padding: 40rpx;position: relative;">
+				<view style="text-align: center;color: #cccccc;margin: 50rpx 0;">正文需付费后才可阅读</view>
+				<view style="text-align: center;">
+					<u-button size="medium" type="error" @click="toBuy">¥{{ payForm.amount }} 阅读全部</u-button>
+				</view>
+				<view style="text-align: center;
+					color: #ff0000;
+					position: absolute;
+					bottom: 20%;
+					left: 50%;
+					width: 80%;
+					transform: translateX(-50%);" @click="toJoin">个人会员或单位会员免费,点击现在入会></view>
+			</view>
+		</u-popup>
+		<u-popup v-model="loginShow"
+		:mask="false" :closeable='false' 
+		mode="bottom" :mask-close-able='false'
+		safe-area-inset-bottom>
+			<view style="height: 70vh;padding: 40rpx;position: relative;">
+				<!-- <view style="text-align: center;color: #cccccc;margin: 50rpx 0;">正文需付费后才可阅读</view> -->
+				<view style="text-align: center;margin: 70rpx 0;">
+					<u-button size="medium" type="error" @click="toLogin">登录阅读原文</u-button>
+				</view>
+			</view>
+		</u-popup>
 	</view>
 </template>
 
@@ -63,14 +87,17 @@
 		useAuthStore
 	} from '@/store/authStore'
 	const authStore = useAuthStore();
-	const isMember = computed(()=>{
+	const isMember = computed(() => {
 		authStore.loadUserInfo()
 		return authStore.userInfo.isMember == '0' ? false : true
 	})
-	const openid = computed(()=>{
+	const openid = computed(() => {
 		authStore.loadOpenid()
+		console.log(authStore.openid, 1111)
 		return authStore.openid
 	})
+	const buyShow = ref(false)
+	const loginShow = ref(true)
 	const webview = ref();
 	const id = ref()
 	const title = ref()
@@ -138,6 +165,12 @@
 		"openid": ""
 	})
 
+	function parseLoaded() {
+		setTimeout(() => {
+			this.$refs.uReadMore1.init();
+		}, 10);
+	}
+
 	function changeFavi() {
 		if (report.value.hasFavi) {
 			cancelReportFavi(id.value).then(res => {
@@ -171,12 +204,12 @@
 	}
 
 	function toBuy() {
+		// console.log("点击购买")
 		// 修改一下,如果是会员,用会员价格,
 		// 如果不是会员就用普通价格
-		payForm.value.amount = isMember.value? report.value.priceMember : report.value.price
 		payForm.value.openid = openid.value
-		payReport(payForm.value).then(res=>{
-			if(res && res.code===0){
+		payReport(payForm.value).then(res => {
+			if (res && res.code === 0) {
 				const params = res.data
 				wx.requestPayment({
 					nonceStr: params.nonceStr,
@@ -184,25 +217,59 @@
 					paySign: params.paySign,
 					signType: params.signType,
 					timeStamp: params.timeStamp,
-					success(res){
+					success(res) {
+						buyShow.value = false
 						msgSuccess("支付成功")
 					},
-					fail(res){
+					fail(res) {
+						buyShow.value = true
 						msgError("支付失败")
 					}
 				})
 			}
 		})
 	}
+	function toLogin(){
+		let url = {
+			url: '/pages/reportDetail/reportDetail',
+			id: id.value,
+			title: title.value
+		}
+		uni.navigateTo({
+			url:`/pages/login/login?redirect=${JSON.stringify(url)}`
+		})
+	}
+	
+	const toJoin = () =>{
+		uni.navigateTo({
+			url: '/pages/joinClub/joinClub'
+		})
+	}
 
 	function init(id) {
+		// 测试图片
 		getReportDetail(id).then(res => {
 			if (res.code === 0) {
 				report.value = res.data
 				payForm.value.id = res.data.id
 				payForm.value.title = res.data.title
 				payForm.value.desc = res.data.title
-				
+				payForm.value.amount = isMember.value ? report.value.priceMember : report.value.price
+				report.value.content = ""
+				if(!report.value.hasBuy){
+					buyShow.value = true
+				}
+				if(res.data.imgData){
+					const path = res.data.imgData.path
+					const type = res.data.imgData.type
+					const size = res.data.imgData.size
+					for(let i=1;i <= size;i++){
+						let image = `${path}/${i}.${type}`
+						report.value.content = report.value.content + `<img src='${image}'></img>`
+					}
+				}else{
+					report.value.content = ''
+				}
 			}
 		})
 	}
@@ -210,7 +277,14 @@
 		if (load.id) {
 			id.value = load.id
 			title.value = load.title
-			init(id.value)
+			// console.log(openid.value, 2222)
+			if(openid.value){
+				init(id.value)
+				loginShow.value = false
+			}else{
+				loginShow.value = true
+			}
+			// loginShow.value = true
 		}
 		uni.setNavigationBarTitle({
 			title: title.value
@@ -227,7 +301,7 @@
 		}
 		return {
 			title: '研究报告',
-			path: `/pages/reportDetail/reportDetail?id=${id.value}`,
+			path: `/pages/reportDetail/reportDetail?id=${id.value}&title=${title.value}`,
 			imageUrl: imgurl.value
 		};
 	})
@@ -235,12 +309,27 @@
 	onShareTimeline(() => {
 		return {
 			title: '朋友圈标题-研究报告',
-			query: `id=${id.value}`,
+			query: `id=${id.value}&title=${title.value}`,
 			imageUrl: imgurl.value
 		};
 	})
 </script>
-
+<style>
+/* 新增样式 */
+.noscroll {
+  height: 100vh;
+  overflow: hidden;
+  position: fixed;
+  width: 100%;
+}
+
+/* 兼容H5和小程序 */
+/* #ifdef H5 */
+.noscroll {
+  touch-action: none;
+}
+/* #endif */
+</style>
 <style lang="scss" scoped>
 	.container {
 		height: 100vh;