detail.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <template>
  2. <view class="container">
  3. <!-- 编辑模式 -->
  4. <view class="edit-section" v-if="!readonly">
  5. <view class="form-card">
  6. <view class="form-title">活动信息</view>
  7. <view class="form-item">
  8. <text class="form-label">活动标题 *</text>
  9. <input class="form-input" v-model="form.title" placeholder="请输入活动标题" :disabled="readonly" />
  10. </view>
  11. <view class="form-item">
  12. <text class="form-label">活动分类</text>
  13. <picker class="form-picker" :value="categoryIdx" :range="categoryOptions" range-key="label"
  14. @change="onCategoryChange">
  15. <text class="picker-text">{{ form.category ? categoryLabel(form.category) : '请选择分类' }}</text>
  16. </picker>
  17. </view>
  18. <view class="form-item">
  19. <text class="form-label">活动日期时间 *</text>
  20. <view class="form-date-picker" @click="showDatePicker">
  21. <text class="picker-text">{{ form.activityDate ? formatDate(form.activityDate) : '请选择日期和时间' }}</text>
  22. </view>
  23. </view>
  24. <view class="form-item">
  25. <text class="form-label">活动地点</text>
  26. <input class="form-input" v-model="form.location" placeholder="请输入活动地点" />
  27. </view>
  28. <view class="form-item-row">
  29. <view class="form-item half">
  30. <text class="form-label">最大人数</text>
  31. <input class="form-input" type="number" v-model="form.maxParticipants" placeholder="20" />
  32. </view>
  33. <view class="form-item half">
  34. <text class="form-label">价格(元)</text>
  35. <input class="form-input" type="digit" v-model="priceYuan" placeholder="0表示免费" />
  36. </view>
  37. </view>
  38. <view class="form-item">
  39. <text class="form-label">封面图片URL</text>
  40. <input class="form-input" v-model="form.coverImage" placeholder="输入图片URL" />
  41. <view class="cover-preview" v-if="form.coverImage">
  42. <image class="preview-img" :src="getImageUrl(form.coverImage)" mode="aspectFill" @error="onImgError" />
  43. </view>
  44. </view>
  45. <view class="form-item">
  46. <text class="form-label">活动描述</text>
  47. <textarea class="form-textarea" v-model="form.description" placeholder="请输入活动详细介绍" />
  48. </view>
  49. <view class="form-actions">
  50. <view class="btn-save" @click="handleSave">
  51. <text>保存</text>
  52. </view>
  53. <view class="btn-publish" v-if="form.status === 'draft'" @click="handlePublish">
  54. <text>保存并发布</text>
  55. </view>
  56. </view>
  57. </view>
  58. </view>
  59. <!-- 只读模式(已发布/已取消/已完成) -->
  60. <view class="detail-section" v-else>
  61. <view class="detail-cover" v-if="form.coverImage">
  62. <image class="cover-img" :src="getImageUrl(form.coverImage)" mode="aspectFill" />
  63. </view>
  64. <view class="detail-cover placeholder" v-else>
  65. <text class="placeholder-icon">{{ categoryIcon(form.category) }}</text>
  66. </view>
  67. <view class="detail-card">
  68. <view class="detail-header">
  69. <text class="detail-title">{{ form.title }}</text>
  70. <text class="detail-status" :class="'status-' + form.status">{{ statusText(form.status) }}</text>
  71. </view>
  72. <view class="detail-meta-row">
  73. <view class="meta-item">
  74. <text class="meta-icon">&#x1F4C5;</text>
  75. <text class="meta-text">{{ form.activityDate ? formatDate(form.activityDate) : '待定' }}</text>
  76. </view>
  77. <view class="meta-item" v-if="form.location">
  78. <text class="meta-icon">&#x1F4CD;</text>
  79. <text class="meta-text">{{ form.location }}</text>
  80. </view>
  81. </view>
  82. <view class="detail-stats">
  83. <view class="stat-box">
  84. <text class="stat-val">{{ form.currentParticipants || 0 }}</text>
  85. <text class="stat-label">已报名</text>
  86. </view>
  87. <view class="stat-box">
  88. <text class="stat-val">{{ form.maxParticipants || 20 }}</text>
  89. <text class="stat-label">最大人数</text>
  90. </view>
  91. <view class="stat-box">
  92. <text class="stat-val">{{ form.price > 0 ? (form.price / 100 + '元') : '免费' }}</text>
  93. <text class="stat-label">价格</text>
  94. </view>
  95. </view>
  96. <view class="detail-desc" v-if="form.description">
  97. <text class="section-title">活动介绍</text>
  98. <text class="desc-text">{{ form.description }}</text>
  99. </view>
  100. <view class="detail-actions" v-if="form.status === 'draft'">
  101. <view class="btn-edit" @click="readonly = false">
  102. <text>编辑</text>
  103. </view>
  104. </view>
  105. </view>
  106. </view>
  107. <view class="loading" v-if="loading"><text class="loading-text">加载中...</text></view>
  108. </view>
  109. </template>
  110. <script>
  111. import { getGuideActivityDetail, createActivity, updateActivity, publishActivity } from '../../../utils/api.js'
  112. import config from '@/config.js'
  113. import { parseDate } from '../../../utils/format.js'
  114. export default {
  115. data() {
  116. return {
  117. activityId: null,
  118. isNew: false,
  119. readonly: false,
  120. loading: false,
  121. form: {
  122. title: '',
  123. description: '',
  124. coverImage: '',
  125. category: '',
  126. location: '',
  127. activityDate: null,
  128. maxParticipants: 20,
  129. price: 0,
  130. status: 'draft'
  131. },
  132. priceYuan: '0',
  133. categoryIdx: 0,
  134. categoryOptions: [
  135. { key: 'outdoor', label: '&#x26F5; 户外运动' },
  136. { key: 'nature', label: '&#x1F33F; 自然探索' },
  137. { key: 'culture', label: '&#x1F3A8; 文化体验' },
  138. { key: 'sports', label: '&#x26BD; 体育竞技' },
  139. { key: 'other', label: '&#x1F3E0; 其他' }
  140. ]
  141. }
  142. },
  143. onLoad(options) {
  144. this.activityId = options.activityId ? parseInt(options.activityId) : null
  145. if (!this.activityId || this.activityId === 0) {
  146. this.isNew = true
  147. this.readonly = false
  148. uni.setNavigationBarTitle({ title: '创建活动' })
  149. } else {
  150. this.loadDetail()
  151. }
  152. },
  153. methods: {
  154. loadDetail() {
  155. var self = this
  156. self.loading = true
  157. getGuideActivityDetail(self.activityId).then(function(res) {
  158. self.loading = false
  159. if (res.code === 200 && res.data) {
  160. self.form = res.data
  161. self.priceYuan = self.form.price ? (self.form.price / 100).toString() : '0'
  162. self.readonly = self.form.status !== 'draft'
  163. uni.setNavigationBarTitle({ title: self.readonly ? '活动详情' : '编辑活动' })
  164. }
  165. }).catch(function() { self.loading = false })
  166. },
  167. onCategoryChange(e) {
  168. this.categoryIdx = e.detail.value
  169. this.form.category = this.categoryOptions[this.categoryIdx].key
  170. },
  171. showDatePicker() {
  172. var self = this
  173. uni.showDatePicker({
  174. mode: 'date',
  175. success: function(res) {
  176. var date = res.value
  177. uni.showTimePicker({
  178. mode: 'time',
  179. success: function(res2) {
  180. var time = res2.value
  181. var d = parseDate(date)
  182. if (!d) return
  183. var parts = time.split(':')
  184. d.setHours(parseInt(parts[0]), parseInt(parts[1]))
  185. self.form.activityDate = d
  186. }
  187. })
  188. }
  189. })
  190. },
  191. handleSave() {
  192. if (!this.form.title) {
  193. uni.showToast({ title: '请输入活动标题', icon: 'none' })
  194. return
  195. }
  196. this.save(false)
  197. },
  198. handlePublish() {
  199. if (!this.form.title) {
  200. uni.showToast({ title: '请输入活动标题', icon: 'none' })
  201. return
  202. }
  203. this.save(true)
  204. },
  205. save(publishAfterSave) {
  206. var self = this
  207. var payload = Object.assign({}, self.form)
  208. if (self.priceYuan) {
  209. payload.price = Math.round(parseFloat(self.priceYuan) * 100)
  210. }
  211. var done = function() {
  212. if (publishAfterSave && self.form.status === 'draft') {
  213. publishActivity(self.form.id || self.activityId).then(function(r) {
  214. if (r.code === 200) {
  215. uni.showToast({ title: '已发布', icon: 'success' })
  216. setTimeout(function() {
  217. uni.redirectTo({ url: '/pages/guide/activities/index' })
  218. }, 1000)
  219. } else {
  220. uni.showToast({ title: '发布失败', icon: 'none' })
  221. }
  222. })
  223. } else {
  224. uni.showToast({ title: '保存成功', icon: 'success' })
  225. setTimeout(function() {
  226. uni.navigateBack()
  227. }, 1000)
  228. }
  229. }
  230. if (self.isNew || !self.form.id) {
  231. createActivity(payload).then(function(res) {
  232. if (res.code === 200) {
  233. self.form.id = res.data
  234. self.isNew = false
  235. if (publishAfterSave) {
  236. publishActivity(res.data).then(function(r) {
  237. if (r.code === 200) {
  238. uni.showToast({ title: '已发布', icon: 'success' })
  239. setTimeout(function() {
  240. uni.redirectTo({ url: '/pages/guide/activities/index' })
  241. }, 1000)
  242. }
  243. })
  244. } else {
  245. done()
  246. }
  247. } else {
  248. uni.showToast({ title: res.message || '保存失败', icon: 'none' })
  249. }
  250. }).catch(function() {
  251. uni.showToast({ title: '网络异常', icon: 'none' })
  252. })
  253. } else {
  254. payload.id = self.form.id || self.activityId
  255. updateActivity(payload).then(function(res) {
  256. if (res.code === 200) {
  257. if (publishAfterSave) {
  258. publishActivity(payload.id).then(function(r) {
  259. if (r.code === 200) {
  260. uni.showToast({ title: '已发布', icon: 'success' })
  261. setTimeout(function() {
  262. uni.redirectTo({ url: '/pages/guide/activities/index' })
  263. }, 1000)
  264. }
  265. })
  266. } else {
  267. done()
  268. }
  269. } else {
  270. uni.showToast({ title: res.message || '保存失败', icon: 'none' })
  271. }
  272. }).catch(function() {
  273. uni.showToast({ title: '网络异常', icon: 'none' })
  274. })
  275. }
  276. },
  277. onImgError() {
  278. this.form.coverImage = ''
  279. },
  280. categoryLabel(key) {
  281. var map = { outdoor: '户外运动', nature: '自然探索', culture: '文化体验', sports: '体育竞技', other: '其他' }
  282. return map[key] || '其他'
  283. },
  284. categoryIcon(category) {
  285. var map = { outdoor: '&#x26F5;', nature: '&#x1F33F;', culture: '&#x1F3A8;', sports: '&#x26BD;', other: '&#x1F3E0;' }
  286. return map[category] || '&#x1F3E0;'
  287. },
  288. statusText(status) {
  289. var map = { draft: '草稿', published: '已发布', cancelled: '已取消', completed: '已完成' }
  290. return map[status] || status || '草稿'
  291. },
  292. formatDate(dateStr) {
  293. if (!dateStr) return ''
  294. var d = parseDate(dateStr)
  295. if (!d) return ''
  296. var y = d.getFullYear()
  297. var m = ('0' + (d.getMonth() + 1)).slice(-2)
  298. var day = ('0' + d.getDate()).slice(-2)
  299. var h = ('0' + d.getHours()).slice(-2)
  300. var mi = ('0' + d.getMinutes()).slice(-2)
  301. return y + '-' + m + '-' + day + ' ' + h + ':' + mi
  302. },
  303. getImageUrl: function(path) {
  304. return config.API_BASE_URL + path
  305. }
  306. }
  307. }
  308. </script>
  309. <style scoped>
  310. .container {
  311. min-height: 100vh;
  312. background: #f5f7fa;
  313. padding-bottom: 60rpx;
  314. }
  315. /* ===== 编辑表单 ===== */
  316. .edit-section {
  317. padding: 20rpx 30rpx;
  318. }
  319. .form-card {
  320. background: #fff;
  321. border-radius: 20rpx;
  322. padding: 28rpx;
  323. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.04);
  324. }
  325. .form-title {
  326. font-size: 30rpx;
  327. font-weight: bold;
  328. color: #333;
  329. margin-bottom: 24rpx;
  330. padding-bottom: 16rpx;
  331. border-bottom: 1rpx solid #f0f0f0;
  332. }
  333. .form-item {
  334. margin-bottom: 24rpx;
  335. }
  336. .form-item:last-child {
  337. margin-bottom: 0;
  338. }
  339. .form-item-row {
  340. display: flex;
  341. flex-direction: row;
  342. gap: 16rpx;
  343. }
  344. .form-item.half {
  345. flex: 1;
  346. }
  347. .form-label {
  348. font-size: 26rpx;
  349. color: #888;
  350. display: block;
  351. margin-bottom: 8rpx;
  352. }
  353. .form-input {
  354. width: 100%;
  355. height: 80rpx;
  356. background: #f9fafb;
  357. border: 1rpx solid #e5e7eb;
  358. border-radius: 12rpx;
  359. padding: 0 20rpx;
  360. font-size: 28rpx;
  361. color: #333;
  362. box-sizing: border-box;
  363. }
  364. .form-picker, .form-date-picker {
  365. width: 100%;
  366. min-height: 80rpx;
  367. background: #f9fafb;
  368. border: 1rpx solid #e5e7eb;
  369. border-radius: 12rpx;
  370. padding: 20rpx;
  371. box-sizing: border-box;
  372. }
  373. .picker-text {
  374. font-size: 28rpx;
  375. color: #333;
  376. }
  377. .form-textarea {
  378. width: 100%;
  379. background: #f9fafb;
  380. border: 1rpx solid #e5e7eb;
  381. border-radius: 12rpx;
  382. padding: 16rpx 20rpx;
  383. font-size: 28rpx;
  384. color: #333;
  385. box-sizing: border-box;
  386. min-height: 160rpx;
  387. display: block;
  388. }
  389. .cover-preview {
  390. margin-top: 12rpx;
  391. border-radius: 12rpx;
  392. overflow: hidden;
  393. height: 200rpx;
  394. }
  395. .preview-img {
  396. width: 100%;
  397. height: 200rpx;
  398. }
  399. .form-actions {
  400. display: flex;
  401. flex-direction: row;
  402. gap: 16rpx;
  403. justify-content: center;
  404. margin-top: 32rpx;
  405. padding-top: 24rpx;
  406. border-top: 1rpx solid #f0f0f0;
  407. }
  408. .btn-save, .btn-publish {
  409. padding: 18rpx 48rpx;
  410. border-radius: 40rpx;
  411. font-size: 28rpx;
  412. font-weight: 500;
  413. }
  414. .btn-save {
  415. background: linear-gradient(135deg, #10B981, #059669);
  416. box-shadow: 0 4rpx 16rpx rgba(16, 185, 129, 0.3);
  417. }
  418. .btn-publish {
  419. background: #fff;
  420. border: 2rpx solid #10B981;
  421. color: #10B981;
  422. }
  423. .btn-save:active, .btn-publish:active { opacity: 0.8; }
  424. .btn-save text, .btn-publish text {
  425. font-weight: 500;
  426. }
  427. /* ===== 详情模式 ===== */
  428. .detail-section {
  429. padding: 0;
  430. }
  431. .detail-cover {
  432. width: 100%;
  433. height: 400rpx;
  434. background: linear-gradient(135deg, #10B981 0%, #059669 100%);
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. }
  439. .cover-img {
  440. width: 100%;
  441. height: 100%;
  442. }
  443. .placeholder-icon {
  444. font-size: 120rpx;
  445. color: rgba(255,255,255,0.8);
  446. }
  447. .detail-card {
  448. background: #fff;
  449. margin: -40rpx 30rpx 0;
  450. border-radius: 24rpx;
  451. padding: 32rpx;
  452. box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.08);
  453. position: relative;
  454. }
  455. .detail-header {
  456. display: flex;
  457. flex-direction: row;
  458. justify-content: space-between;
  459. align-items: flex-start;
  460. margin-bottom: 20rpx;
  461. }
  462. .detail-title {
  463. font-size: 36rpx;
  464. font-weight: bold;
  465. color: #333;
  466. flex: 1;
  467. margin-right: 16rpx;
  468. }
  469. .detail-status {
  470. font-size: 24rpx;
  471. padding: 6rpx 18rpx;
  472. border-radius: 16rpx;
  473. flex-shrink: 0;
  474. }
  475. .status-draft { background: #f3f4f6; color: #666; }
  476. .status-published { background: #d1fae5; color: #059669; }
  477. .status-cancelled { background: #fee2e2; color: #dc2626; }
  478. .status-completed { background: #dbeafe; color: #2563eb; }
  479. .detail-meta-row {
  480. display: flex;
  481. flex-direction: row;
  482. gap: 24rpx;
  483. margin-bottom: 24rpx;
  484. padding-bottom: 20rpx;
  485. border-bottom: 1rpx solid #f0f0f0;
  486. }
  487. .meta-item {
  488. display: flex;
  489. flex-direction: row;
  490. align-items: center;
  491. gap: 6rpx;
  492. }
  493. .meta-icon {
  494. font-size: 26rpx;
  495. }
  496. .meta-text {
  497. font-size: 26rpx;
  498. color: #666;
  499. }
  500. .detail-stats {
  501. display: flex;
  502. flex-direction: row;
  503. gap: 20rpx;
  504. margin-bottom: 24rpx;
  505. padding-bottom: 20rpx;
  506. border-bottom: 1rpx solid #f0f0f0;
  507. }
  508. .stat-box {
  509. flex: 1;
  510. background: #f9fafb;
  511. border-radius: 16rpx;
  512. padding: 20rpx;
  513. text-align: center;
  514. }
  515. .stat-val {
  516. font-size: 36rpx;
  517. font-weight: bold;
  518. color: #10B981;
  519. display: block;
  520. }
  521. .stat-label {
  522. font-size: 22rpx;
  523. color: #888;
  524. display: block;
  525. margin-top: 4rpx;
  526. }
  527. .section-title {
  528. font-size: 28rpx;
  529. font-weight: bold;
  530. color: #333;
  531. display: block;
  532. margin-bottom: 12rpx;
  533. }
  534. .desc-text {
  535. font-size: 28rpx;
  536. color: #666;
  537. line-height: 1.7;
  538. display: block;
  539. }
  540. .detail-desc {
  541. margin-bottom: 24rpx;
  542. }
  543. .detail-actions {
  544. display: flex;
  545. flex-direction: row;
  546. justify-content: center;
  547. margin-top: 24rpx;
  548. }
  549. .btn-edit {
  550. background: linear-gradient(135deg, #10B981, #059669);
  551. padding: 18rpx 64rpx;
  552. border-radius: 40rpx;
  553. box-shadow: 0 4rpx 16rpx rgba(16, 185, 129, 0.3);
  554. }
  555. .btn-edit:active { opacity: 0.8; }
  556. .btn-edit text {
  557. color: #fff;
  558. font-size: 28rpx;
  559. font-weight: bold;
  560. }
  561. .loading {
  562. display: flex;
  563. align-items: center;
  564. justify-content: center;
  565. padding: 80rpx;
  566. }
  567. .loading-text {
  568. font-size: 28rpx;
  569. color: #999;
  570. }
  571. </style>