publish.vue 17 KB

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