| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <template>
- <view class="container">
- <!-- 顶部导航栏 -->
- <view class="header">
- <text class="header-title">写书评</text>
- </view>
-
- <!-- 评分选择 -->
- <view class="rating-section">
- <view
- class="rating-btn"
- v-for="(rating, index) in ratingOptions"
- :key="index"
- :class="{ active: selectedRating === rating.value }"
- @click="selectRating(rating.value)"
- >
- <text class="rating-text">{{ rating.label }}</text>
- </view>
- </view>
-
- <!-- 书评输入区域 -->
- <view class="review-input-section">
- <textarea
- class="review-input"
- v-model="reviewContent"
- placeholder="写书评..."
- maxlength="500"
- :auto-focus="true"
- @input="handleInput"
- ></textarea>
- <view class="char-count">
- <text class="char-count-text">{{ reviewContent.length }}/500</text>
- </view>
- </view>
-
- <!-- 底部操作栏 -->
- <view class="action-bar">
- <view class="cancel-btn" @click="handleCancel">
- <text class="action-text">取消</text>
- </view>
- <view class="publish-btn" :class="{ disabled: !canPublish }" @click="handlePublish">
- <text class="action-text">发表</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- import { addComment } from '../../utils/api.js'
-
- export default {
- data() {
- return {
- bookInfo: {
- id: null,
- title: ''
- },
- selectedRating: 'recommend', // recommend, general, bad
- ratingOptions: [
- { label: '推荐', value: 'recommend' },
- { label: '一般', value: 'general' },
- { label: '不好', value: 'bad' }
- ],
- reviewContent: '',
- userInfo: null,
- isSubmitting: false
- }
- },
- computed: {
- canPublish() {
- return this.reviewContent.trim().length > 0 && !this.isSubmitting
- }
- },
- onLoad(options) {
- // 获取用户信息
- try {
- const userInfo = uni.getStorageSync('userInfo')
- if (userInfo && userInfo.id) {
- this.userInfo = userInfo
- }
- } catch (e) {
- console.error('获取用户信息失败', e)
- }
-
- if (options.bookId) {
- this.bookInfo.id = parseInt(options.bookId)
- }
- if (options.title) {
- this.bookInfo.title = decodeURIComponent(options.title)
- }
- },
- methods: {
- selectRating(value) {
- this.selectedRating = value
- },
- handleInput(e) {
- this.reviewContent = e.detail.value
- },
- handleCancel() {
- uni.showModal({
- title: '提示',
- content: '确定要取消吗?未保存的内容将丢失。',
- success: (res) => {
- if (res.confirm) {
- uni.navigateBack()
- }
- }
- })
- },
- async handlePublish() {
- if (!this.canPublish) {
- uni.showToast({
- title: '请输入书评内容',
- icon: 'none'
- })
- return
- }
-
- // 检查用户是否登录
- if (!this.userInfo || !this.userInfo.id) {
- uni.showModal({
- title: '提示',
- content: '请先登录',
- showCancel: true,
- cancelText: '取消',
- confirmText: '去登录',
- success: (res) => {
- if (res.confirm) {
- uni.navigateTo({
- url: '/pages/login/login'
- })
- }
- }
- })
- return
- }
-
- // 检查书籍ID
- if (!this.bookInfo.id) {
- uni.showToast({
- title: '书籍信息不完整',
- icon: 'none'
- })
- return
- }
-
- try {
- this.isSubmitting = true
- uni.showLoading({
- title: '发表中...',
- mask: true
- })
-
- const res = await addComment(
- this.userInfo.id,
- this.bookInfo.id,
- this.reviewContent.trim()
- )
-
- uni.hideLoading()
- this.isSubmitting = false
-
- if (res && res.code === 200) {
- uni.showToast({
- title: '发表成功',
- icon: 'success',
- duration: 2000
- })
-
- setTimeout(() => {
- uni.navigateBack()
- }, 2000)
- } else {
- uni.showToast({
- title: res.message || '发表失败',
- icon: 'none'
- })
- }
- } catch (e) {
- uni.hideLoading()
- this.isSubmitting = false
- console.error('发表评论失败', e)
- uni.showToast({
- title: e.message || '发表失败,请重试',
- icon: 'none'
- })
- }
- }
- }
- }
- </script>
- <style scoped>
- .container {
- width: 100%;
- height: 100vh;
- background-color: #FFFFFF;
- display: flex;
- flex-direction: column;
- padding-top: 30px;
- box-sizing: border-box;
- }
-
- .header {
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 20rpx 30rpx;
- padding-top: calc(20rpx + env(safe-area-inset-top));
- background-color: #F5F5F5;
- border-bottom: 1rpx solid #E0E0E0;
- position: relative;
- }
-
- .header-title {
- font-size: 36rpx;
- font-weight: bold;
- color: #333333;
- }
-
- .rating-section {
- display: flex;
- justify-content: center;
- align-items: center;
- padding: 30rpx;
- gap: 20rpx;
- background-color: #F5F5F5;
- border-bottom: 1rpx solid #E0E0E0;
- }
-
- .rating-btn {
- flex: 1;
- height: 70rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- background-color: #E0E0E0;
- border-radius: 8rpx;
- border: 2rpx solid #E0E0E0;
- transition: all 0.3s;
- }
-
- .rating-btn.active {
- background-color: #4CAF50;
- border-color: #4CAF50;
- }
-
- .rating-btn.active .rating-text {
- color: #FFFFFF;
- }
-
- .rating-text {
- font-size: 28rpx;
- color: #666666;
- font-weight: 500;
- }
-
- .review-input-section {
- flex: 1;
- display: flex;
- flex-direction: column;
- padding: 30rpx;
- background-color: #FFFFFF;
- }
-
- .review-input {
- flex: 1;
- width: 100%;
- min-height: 400rpx;
- padding: 20rpx;
- background-color: #FFFFFF;
- border: 1rpx solid #E0E0E0;
- border-radius: 8rpx;
- font-size: 30rpx;
- color: #333333;
- line-height: 1.8;
- box-sizing: border-box;
- }
-
- .review-input::placeholder {
- color: #CCCCCC;
- }
-
- .char-count {
- display: flex;
- justify-content: flex-end;
- margin-top: 20rpx;
- }
-
- .char-count-text {
- font-size: 24rpx;
- color: #999999;
- }
-
- .action-bar {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 20rpx 30rpx;
- background-color: #F5F5F5;
- border-top: 1rpx solid #E0E0E0;
- padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
- }
-
- .cancel-btn,
- .publish-btn {
- flex: 1;
- height: 80rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- border-radius: 8rpx;
- transition: all 0.3s;
- }
-
- .cancel-btn {
- margin-right: 20rpx;
- background-color: #FFFFFF;
- border: 1rpx solid #E0E0E0;
- }
-
- .publish-btn {
- background-color: #4FC3F7;
- border: none;
- }
-
- .publish-btn.disabled {
- background-color: #CCCCCC;
- opacity: 0.6;
- }
-
- .action-text {
- font-size: 32rpx;
- color: #333333;
- font-weight: 500;
- }
-
- .publish-btn .action-text {
- color: #FFFFFF;
- }
-
- .publish-btn.disabled .action-text {
- color: #FFFFFF;
- }
- </style>
|