publish.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <template>
  2. <view class="publish-page">
  3. <!-- 自定义导航栏 -->
  4. <view class="custom-navbar">
  5. <view class="navbar-left" @click="goBack">
  6. <text class="back-icon">← 返回</text>
  7. </view>
  8. <view class="navbar-title">发布动态</view>
  9. <view class="navbar-right"></view>
  10. </view>
  11. <!-- 内容区域 -->
  12. <scroll-view class="content-scroll" scroll-y>
  13. <view class="form-container">
  14. <!-- 内容输入 -->
  15. <view class="content-section">
  16. <textarea
  17. v-model="content"
  18. class="content-input"
  19. placeholder="分享你的心动瞬间"
  20. maxlength="1000"
  21. :show-count="true"
  22. auto-height
  23. />
  24. </view>
  25. <!-- 图片上传区域 -->
  26. <view class="media-section">
  27. <view class="section-title">📷 图片</view>
  28. <view class="media-grid">
  29. <view v-for="(m,idx) in mediaList" :key="idx" class="media-item">
  30. <image :src="m" mode="aspectFill" class="media-image"/>
  31. <text class="remove-btn" @click="removeMedia(idx)">×</text>
  32. </view>
  33. <view v-if="mediaList.length < 9" class="add-media" @click="chooseImages">
  34. <text class="add-icon">+</text>
  35. <text class="add-text">添加图片</text>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </scroll-view>
  41. <!-- 底部发布栏 -->
  42. <view class="publish-bottom-bar">
  43. <button
  44. class="publish-btn"
  45. :disabled="uploading || !content.trim()"
  46. :class="{ disabled: uploading || !content.trim() }"
  47. @click="submit">
  48. <text class="btn-icon">💕</text>
  49. <text class="btn-text">发布</text>
  50. </button>
  51. </view>
  52. </view>
  53. </template>
  54. <script>
  55. import api from '@/utils/api.js'
  56. export default {
  57. data() {
  58. return {
  59. content: '',
  60. mediaList: [],
  61. uploading: false
  62. }
  63. },
  64. methods: {
  65. // 返回上一页
  66. goBack() {
  67. uni.navigateBack()
  68. },
  69. isImage(url) {
  70. if (!url || typeof url !== 'string') return false
  71. console.log('检查文件类型:', url)
  72. // 1. 检查文件扩展名
  73. const hasImageExtension = /(\.png|\.jpg|\.jpeg|\.gif|\.webp|\.bmp)$/i.test(url)
  74. // 2. 检查是否是微信小程序的临时图片路径
  75. const isTempImagePath = url.includes('tmp_') ||
  76. url.includes('wxfile://tmp_') ||
  77. url.includes('tempFilePath') ||
  78. url.startsWith('http://tmp/') ||
  79. /\/tmp_[a-zA-Z0-9_]+\.(png|jpg|jpeg|gif|webp|bmp)$/i.test(url)
  80. // 3. 检查是否是MinIO的图片URL
  81. const isMinioImageUrl = url.includes('dynamic-comments/dynamics/') &&
  82. /(\.png|\.jpg|\.jpeg|\.gif|\.webp|\.bmp)(\?|$)/i.test(url)
  83. const result = hasImageExtension || isTempImagePath || isMinioImageUrl
  84. console.log('文件类型检查结果:', {
  85. url: url,
  86. hasImageExtension,
  87. isTempImagePath,
  88. isMinioImageUrl,
  89. result
  90. })
  91. return result
  92. },
  93. removeMedia(i) { this.mediaList.splice(i,1) },
  94. async chooseImages() {
  95. const maxCount = Math.max(0, 9 - this.mediaList.length)
  96. if (maxCount <= 0) {
  97. uni.showToast({ title: '最多选择9张图片', icon: 'none' })
  98. return
  99. }
  100. uni.chooseImage({
  101. count: maxCount,
  102. sizeType: ['compressed'],
  103. success: async (res) => {
  104. const filePaths = res.tempFilePaths || []
  105. if (filePaths.length === 0) return
  106. // 立即预览
  107. this.mediaList.push(...filePaths)
  108. this.uploading = true
  109. uni.showLoading({ title: `正在上传 ${filePaths.length} 张图片...` })
  110. try {
  111. for (const filePath of filePaths) {
  112. try {
  113. const url = await api.dynamic.uploadSingle(filePath)
  114. const idx = this.mediaList.indexOf(filePath)
  115. if (idx !== -1) this.mediaList.splice(idx, 1, url)
  116. } catch (e) {
  117. const idx = this.mediaList.indexOf(filePath)
  118. if (idx !== -1) this.mediaList.splice(idx, 1)
  119. }
  120. }
  121. } finally {
  122. this.uploading = false
  123. uni.hideLoading()
  124. }
  125. },
  126. fail: () => {
  127. uni.showToast({ title: '选择图片失败', icon: 'none' })
  128. }
  129. })
  130. },
  131. async submit() {
  132. if (!this.content.trim()) {
  133. uni.showToast({ title: '请分享你的心动瞬间吧~', icon: 'none' })
  134. return
  135. }
  136. if (this.content.length > 1000) {
  137. uni.showToast({ title: '内容过长,请精简到1000字以内', icon: 'none' })
  138. return
  139. }
  140. if (this.uploading) {
  141. uni.showToast({ title: '图片还在上传中,请稍等', icon: 'none' })
  142. return
  143. }
  144. // 简单敏感词检测
  145. const badWords = ['傻', '坏词', '违规']
  146. if (badWords.some(w => this.content.includes(w))) {
  147. uni.showToast({ title: '内容包含敏感词,请修改后发布', icon: 'none' })
  148. return
  149. }
  150. uni.showLoading({ title: '正在发布...' })
  151. try {
  152. const payload = {
  153. userId: 1,
  154. content: this.content,
  155. mediaList: this.mediaList,
  156. mediaType: this.mediaList.length > 0 ? 2 : 1, // 自动检测:有图片为2,纯文本为1
  157. visibility: 1 // 默认公开
  158. }
  159. const dynamicId = await api.dynamic.publish(payload)
  160. uni.hideLoading()
  161. uni.showToast({ title: '发布成功!开始你的缘分旅程~', icon: 'success' })
  162. // 直接插入到广场页数据头部,避免全量刷新
  163. uni.$emit('dynamic-insert', {
  164. dynamicId,
  165. userId: 1,
  166. content: this.content,
  167. mediaUrls: this.mediaList,
  168. mediaType: this.mediaList.length > 0 ? 2 : 1,
  169. visibility: 1,
  170. likeCount: 0,
  171. commentCount: 0,
  172. favoriteCount: 0,
  173. shareCount: 0,
  174. viewCount: 0,
  175. isLiked: false,
  176. isFavorited: false,
  177. createdAt: new Date().toISOString(),
  178. user: { userId: 1, nickname: '我', avatarUrl: '' }
  179. })
  180. setTimeout(() => {
  181. // 发布成功后回到动态列表页面
  182. uni.switchTab({
  183. url: '/pages/plaza/index',
  184. fail: () => {
  185. uni.redirectTo({ url: '/pages/plaza/index' })
  186. }
  187. })
  188. }, 1500)
  189. } catch (e) {
  190. uni.hideLoading()
  191. uni.showToast({ title: '发布失败,请重试', icon: 'none' })
  192. console.error('发布失败:', e)
  193. }
  194. }
  195. }
  196. }
  197. </script>
  198. <style scoped lang="scss">
  199. .publish-page {
  200. min-height: 100vh;
  201. background: #FFF0F5; // 与广场一致淡粉背景
  202. // 自定义导航栏
  203. .custom-navbar {
  204. position: fixed;
  205. top: 0;
  206. left: 0;
  207. right: 0;
  208. height: 96rpx; // 提高高度以便垂直居中
  209. background: transparent; // 透明,标题用粉色
  210. display: flex;
  211. align-items: center;
  212. justify-content: space-between;
  213. padding: 0 32rpx;
  214. padding-top: constant(safe-area-inset-top);
  215. padding-top: env(safe-area-inset-top);
  216. z-index: 1000;
  217. box-shadow: none;
  218. .navbar-left,
  219. .navbar-right {
  220. width: 120rpx;
  221. height: 96rpx; // 与导航高度一致
  222. display: flex;
  223. align-items: center;
  224. }
  225. .back-icon {
  226. color: #E91E63;
  227. font-size: 36rpx;
  228. font-weight: bold;
  229. }
  230. .navbar-title {
  231. flex: 1;
  232. text-align: center;
  233. color: #D81B60;
  234. font-size: 32rpx;
  235. font-weight: 600;
  236. }
  237. .publish-text {
  238. color: #FFFFFF;
  239. font-size: 28rpx;
  240. font-weight: 500;
  241. background: linear-gradient(135deg, #E91E63, #FF6B9D);
  242. padding: 12rpx 24rpx;
  243. border-radius: 24rpx;
  244. border: none;
  245. }
  246. }
  247. // 内容滚动区域
  248. .content-scroll {
  249. padding-top: calc(96rpx + constant(safe-area-inset-top));
  250. padding-top: calc(96rpx + env(safe-area-inset-top));
  251. height: calc(100vh - 100rpx); // 预留底部发布栏空间
  252. padding-bottom: 0;
  253. }
  254. .form-container {
  255. padding: 32rpx;
  256. }
  257. // 底部发布栏
  258. .publish-bottom-bar {
  259. position: fixed;
  260. bottom: 0;
  261. left: 0;
  262. right: 0;
  263. background: #FFFFFF;
  264. padding: 20rpx 24rpx;
  265. box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.06);
  266. padding-bottom: constant(safe-area-inset-bottom);
  267. padding-bottom: env(safe-area-inset-bottom);
  268. z-index: 1001;
  269. .publish-btn {
  270. width: 100%;
  271. display: flex;
  272. align-items: center;
  273. justify-content: center;
  274. gap: 10rpx;
  275. padding: 18rpx 28rpx;
  276. border-radius: 999rpx;
  277. background: linear-gradient(135deg, #E91E63, #FF6B9D);
  278. color: #FFFFFF;
  279. border: none;
  280. &.disabled {
  281. background: #E0E0E0;
  282. color: #AAAAAA;
  283. }
  284. .btn-icon { font-size: 30rpx; }
  285. .btn-text { font-size: 28rpx; font-weight: 600; }
  286. }
  287. }
  288. // 内容输入区域
  289. .content-section {
  290. background: #FFFFFF;
  291. border-radius: 20rpx;
  292. padding: 32rpx;
  293. margin-bottom: 24rpx;
  294. box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.06);
  295. .content-input {
  296. width: 100%;
  297. min-height: 240rpx;
  298. font-size: 28rpx;
  299. line-height: 1.6;
  300. color: #333333;
  301. background: transparent;
  302. border: none;
  303. outline: none;
  304. }
  305. }
  306. // 媒体上传区域
  307. .media-section {
  308. background: #FFFFFF;
  309. border-radius: 20rpx;
  310. padding: 24rpx;
  311. margin-bottom: 24rpx;
  312. box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.06);
  313. .section-title {
  314. font-size: 30rpx;
  315. font-weight: 600;
  316. color: #333333;
  317. margin-bottom: 24rpx;
  318. }
  319. .media-grid {
  320. display: grid;
  321. grid-template-columns: repeat(3, 1fr);
  322. gap: 20rpx;
  323. }
  324. .media-item {
  325. position: relative;
  326. width: 100%;
  327. height: 200rpx;
  328. border-radius: 16rpx;
  329. overflow: hidden;
  330. .media-image {
  331. width: 100%;
  332. height: 100%;
  333. }
  334. .video-placeholder {
  335. width: 100%;
  336. height: 100%;
  337. background: #F5F5F5;
  338. display: flex;
  339. flex-direction: column;
  340. align-items: center;
  341. justify-content: center;
  342. .video-icon {
  343. font-size: 32rpx;
  344. margin-bottom: 8rpx;
  345. }
  346. .video-text {
  347. font-size: 24rpx;
  348. color: #666666;
  349. }
  350. }
  351. .remove-btn {
  352. position: absolute;
  353. top: 8rpx;
  354. right: 8rpx;
  355. width: 36rpx;
  356. height: 36rpx;
  357. background: rgba(0,0,0,0.7);
  358. color: #FFFFFF;
  359. border-radius: 50%;
  360. display: flex;
  361. align-items: center;
  362. justify-content: center;
  363. font-size: 24rpx;
  364. font-weight: bold;
  365. }
  366. }
  367. .add-media {
  368. width: 100%;
  369. height: 200rpx;
  370. background: #F8F9FA;
  371. border: 2rpx dashed #D0D7DE;
  372. border-radius: 16rpx;
  373. display: flex;
  374. flex-direction: column;
  375. align-items: center;
  376. justify-content: center;
  377. transition: all 0.3s ease;
  378. &:active {
  379. background: #F0F0F0;
  380. border-color: #E91E63;
  381. }
  382. .add-icon {
  383. font-size: 48rpx;
  384. color: #8C8C8C;
  385. margin-bottom: 8rpx;
  386. }
  387. .add-text {
  388. font-size: 24rpx;
  389. color: #8C8C8C;
  390. }
  391. }
  392. }
  393. // 设置区域
  394. .settings-section {
  395. background: #FFFFFF;
  396. border-radius: 20rpx;
  397. padding: 32rpx;
  398. margin-bottom: 24rpx;
  399. box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.06);
  400. .section-title {
  401. font-size: 30rpx;
  402. font-weight: 600;
  403. color: #333333;
  404. margin-bottom: 24rpx;
  405. }
  406. .setting-item {
  407. display: flex;
  408. align-items: center;
  409. justify-content: space-between;
  410. padding: 20rpx 0;
  411. border-bottom: 1rpx solid #F0F0F0;
  412. &:last-child {
  413. border-bottom: none;
  414. }
  415. .setting-label {
  416. font-size: 28rpx;
  417. color: #333333;
  418. }
  419. .setting-value {
  420. display: flex;
  421. align-items: center;
  422. font-size: 26rpx;
  423. color: #666666;
  424. .arrow {
  425. margin-left: 12rpx;
  426. color: #CCCCCC;
  427. }
  428. }
  429. }
  430. }
  431. // 上传方式弹窗
  432. .upload-modal {
  433. position: fixed;
  434. top: 0;
  435. left: 0;
  436. right: 0;
  437. bottom: 0;
  438. z-index: 9999;
  439. .modal-mask {
  440. position: absolute;
  441. top: 0;
  442. left: 0;
  443. right: 0;
  444. bottom: 0;
  445. background: rgba(0,0,0,0.5);
  446. }
  447. .modal-content {
  448. position: absolute;
  449. bottom: 0;
  450. left: 0;
  451. right: 0;
  452. background: #FFFFFF;
  453. border-radius: 32rpx 32rpx 0 0;
  454. padding: 40rpx;
  455. padding-bottom: calc(40rpx + constant(safe-area-inset-bottom));
  456. padding-bottom: calc(40rpx + env(safe-area-inset-bottom));
  457. .modal-title {
  458. text-align: center;
  459. font-size: 32rpx;
  460. font-weight: 600;
  461. color: #333333;
  462. margin-bottom: 32rpx;
  463. }
  464. .upload-options {
  465. display: flex;
  466. gap: 24rpx;
  467. }
  468. .upload-option {
  469. flex: 1;
  470. background: #F8F9FA;
  471. border-radius: 20rpx;
  472. padding: 40rpx 20rpx;
  473. display: flex;
  474. flex-direction: column;
  475. align-items: center;
  476. transition: all 0.3s ease;
  477. &:active {
  478. background: #E91E63;
  479. transform: scale(0.95);
  480. .option-icon,
  481. .option-text {
  482. color: #FFFFFF;
  483. }
  484. }
  485. .option-icon {
  486. font-size: 48rpx;
  487. margin-bottom: 16rpx;
  488. }
  489. .option-text {
  490. font-size: 28rpx;
  491. color: #333333;
  492. font-weight: 500;
  493. }
  494. }
  495. }
  496. }
  497. // 温馨提示区域
  498. .tips-section {
  499. background: linear-gradient(145deg, #FFF5F7 0%, #FFE8EC 100%);
  500. border-radius: 24rpx;
  501. padding: 32rpx;
  502. margin-bottom: 24rpx;
  503. box-shadow: 0 8rpx 24rpx rgba(233, 30, 99, 0.08);
  504. border: 2rpx solid rgba(255, 182, 193, 0.15);
  505. position: relative;
  506. overflow: hidden;
  507. &::before {
  508. content: '';
  509. position: absolute;
  510. top: 0;
  511. left: 0;
  512. right: 0;
  513. height: 4rpx;
  514. background: linear-gradient(90deg, #FFB3BA, #FF8A95, #FFC0CB);
  515. }
  516. &::after {
  517. content: '💕';
  518. position: absolute;
  519. top: 20rpx;
  520. right: 20rpx;
  521. font-size: 40rpx;
  522. opacity: 0.1;
  523. animation: pulse-love 3s ease-in-out infinite;
  524. }
  525. .section-header {
  526. text-align: center;
  527. margin-bottom: 24rpx;
  528. .header-decoration {
  529. margin-bottom: 12rpx;
  530. .feather-tiny {
  531. font-size: 24rpx;
  532. opacity: 0.6;
  533. margin: 0 8rpx;
  534. animation: float 3s ease-in-out infinite;
  535. &:first-child {
  536. animation-delay: 0s;
  537. }
  538. &:last-child {
  539. animation-delay: 1.5s;
  540. }
  541. }
  542. .section-icon {
  543. font-size: 32rpx;
  544. margin: 0 12rpx;
  545. }
  546. }
  547. .section-title {
  548. font-size: 32rpx;
  549. font-weight: 600;
  550. color: #2D1B69;
  551. margin-bottom: 8rpx;
  552. }
  553. .section-desc {
  554. font-size: 24rpx;
  555. color: #8B5A96;
  556. opacity: 0.8;
  557. }
  558. }
  559. .tips-content {
  560. .tip-item {
  561. display: flex;
  562. align-items: center;
  563. margin-bottom: 16rpx;
  564. padding: 16rpx 20rpx;
  565. background: rgba(255, 255, 255, 0.6);
  566. border-radius: 16rpx;
  567. border: 1rpx solid rgba(255, 182, 193, 0.2);
  568. transition: all 0.3s ease;
  569. &:last-child {
  570. margin-bottom: 0;
  571. }
  572. .tip-icon {
  573. font-size: 28rpx;
  574. margin-right: 16rpx;
  575. animation: float 2s ease-in-out infinite;
  576. }
  577. .tip-text {
  578. flex: 1;
  579. font-size: 26rpx;
  580. color: #2D1B69;
  581. line-height: 1.4;
  582. font-weight: 500;
  583. }
  584. }
  585. }
  586. }
  587. }
  588. </style>