publish.vue 17 KB

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