publish.vue 17 KB

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