detail.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. },getImageUrl: function(path) {
  303. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  304. var s = String(path).trim()
  305. if (!s || s === 'null' || s === '/null') return ''
  306. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  307. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  308. }
  309. }
  310. }
  311. </script>
  312. <style scoped>
  313. .container {
  314. min-height: 100vh;
  315. background: #f5f7fa;
  316. padding-bottom: 60rpx;
  317. }
  318. /* ===== 编辑表单 ===== */
  319. .edit-section {
  320. padding: 20rpx 30rpx;
  321. }
  322. .form-card {
  323. background: #fff;
  324. border-radius: 20rpx;
  325. padding: 28rpx;
  326. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.04);
  327. }
  328. .form-title {
  329. font-size: 30rpx;
  330. font-weight: bold;
  331. color: #333;
  332. margin-bottom: 24rpx;
  333. padding-bottom: 16rpx;
  334. border-bottom: 1rpx solid #f0f0f0;
  335. }
  336. .form-item {
  337. margin-bottom: 24rpx;
  338. }
  339. .form-item:last-child {
  340. margin-bottom: 0;
  341. }
  342. .form-item-row {
  343. display: flex;
  344. flex-direction: row;
  345. gap: 16rpx;
  346. }
  347. .form-item.half {
  348. flex: 1;
  349. }
  350. .form-label {
  351. font-size: 26rpx;
  352. color: #888;
  353. display: block;
  354. margin-bottom: 8rpx;
  355. }
  356. .form-input {
  357. width: 100%;
  358. height: 80rpx;
  359. background: #f9fafb;
  360. border: 1rpx solid #e5e7eb;
  361. border-radius: 12rpx;
  362. padding: 0 20rpx;
  363. font-size: 28rpx;
  364. color: #333;
  365. box-sizing: border-box;
  366. }
  367. .form-picker, .form-date-picker {
  368. width: 100%;
  369. min-height: 80rpx;
  370. background: #f9fafb;
  371. border: 1rpx solid #e5e7eb;
  372. border-radius: 12rpx;
  373. padding: 20rpx;
  374. box-sizing: border-box;
  375. }
  376. .picker-text {
  377. font-size: 28rpx;
  378. color: #333;
  379. }
  380. .form-textarea {
  381. width: 100%;
  382. background: #f9fafb;
  383. border: 1rpx solid #e5e7eb;
  384. border-radius: 12rpx;
  385. padding: 16rpx 20rpx;
  386. font-size: 28rpx;
  387. color: #333;
  388. box-sizing: border-box;
  389. min-height: 160rpx;
  390. display: block;
  391. }
  392. .cover-preview {
  393. margin-top: 12rpx;
  394. border-radius: 12rpx;
  395. overflow: hidden;
  396. height: 200rpx;
  397. }
  398. .preview-img {
  399. width: 100%;
  400. height: 200rpx;
  401. }
  402. .form-actions {
  403. display: flex;
  404. flex-direction: row;
  405. gap: 16rpx;
  406. justify-content: center;
  407. margin-top: 32rpx;
  408. padding-top: 24rpx;
  409. border-top: 1rpx solid #f0f0f0;
  410. }
  411. .btn-save, .btn-publish {
  412. padding: 18rpx 48rpx;
  413. border-radius: 40rpx;
  414. font-size: 28rpx;
  415. font-weight: 500;
  416. }
  417. .btn-save {
  418. background: linear-gradient(135deg, #10B981, #059669);
  419. box-shadow: 0 4rpx 16rpx rgba(16, 185, 129, 0.3);
  420. }
  421. .btn-publish {
  422. background: #fff;
  423. border: 2rpx solid #10B981;
  424. color: #10B981;
  425. }
  426. .btn-save:active, .btn-publish:active { opacity: 0.8; }
  427. .btn-save text, .btn-publish text {
  428. font-weight: 500;
  429. }
  430. /* ===== 详情模式 ===== */
  431. .detail-section {
  432. padding: 0;
  433. }
  434. .detail-cover {
  435. width: 100%;
  436. height: 400rpx;
  437. background: linear-gradient(135deg, #10B981 0%, #059669 100%);
  438. display: flex;
  439. align-items: center;
  440. justify-content: center;
  441. }
  442. .cover-img {
  443. width: 100%;
  444. height: 100%;
  445. }
  446. .placeholder-icon {
  447. font-size: 120rpx;
  448. color: rgba(255,255,255,0.8);
  449. }
  450. .detail-card {
  451. background: #fff;
  452. margin: -40rpx 30rpx 0;
  453. border-radius: 24rpx;
  454. padding: 32rpx;
  455. box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.08);
  456. position: relative;
  457. }
  458. .detail-header {
  459. display: flex;
  460. flex-direction: row;
  461. justify-content: space-between;
  462. align-items: flex-start;
  463. margin-bottom: 20rpx;
  464. }
  465. .detail-title {
  466. font-size: 36rpx;
  467. font-weight: bold;
  468. color: #333;
  469. flex: 1;
  470. margin-right: 16rpx;
  471. }
  472. .detail-status {
  473. font-size: 24rpx;
  474. padding: 6rpx 18rpx;
  475. border-radius: 16rpx;
  476. flex-shrink: 0;
  477. }
  478. .status-draft { background: #f3f4f6; color: #666; }
  479. .status-published { background: #d1fae5; color: #059669; }
  480. .status-cancelled { background: #fee2e2; color: #dc2626; }
  481. .status-completed { background: #dbeafe; color: #2563eb; }
  482. .detail-meta-row {
  483. display: flex;
  484. flex-direction: row;
  485. gap: 24rpx;
  486. margin-bottom: 24rpx;
  487. padding-bottom: 20rpx;
  488. border-bottom: 1rpx solid #f0f0f0;
  489. }
  490. .meta-item {
  491. display: flex;
  492. flex-direction: row;
  493. align-items: center;
  494. gap: 6rpx;
  495. }
  496. .meta-icon {
  497. font-size: 26rpx;
  498. }
  499. .meta-text {
  500. font-size: 26rpx;
  501. color: #666;
  502. }
  503. .detail-stats {
  504. display: flex;
  505. flex-direction: row;
  506. gap: 20rpx;
  507. margin-bottom: 24rpx;
  508. padding-bottom: 20rpx;
  509. border-bottom: 1rpx solid #f0f0f0;
  510. }
  511. .stat-box {
  512. flex: 1;
  513. background: #f9fafb;
  514. border-radius: 16rpx;
  515. padding: 20rpx;
  516. text-align: center;
  517. }
  518. .stat-val {
  519. font-size: 36rpx;
  520. font-weight: bold;
  521. color: #10B981;
  522. display: block;
  523. }
  524. .stat-label {
  525. font-size: 22rpx;
  526. color: #888;
  527. display: block;
  528. margin-top: 4rpx;
  529. }
  530. .section-title {
  531. font-size: 28rpx;
  532. font-weight: bold;
  533. color: #333;
  534. display: block;
  535. margin-bottom: 12rpx;
  536. }
  537. .desc-text {
  538. font-size: 28rpx;
  539. color: #666;
  540. line-height: 1.7;
  541. display: block;
  542. }
  543. .detail-desc {
  544. margin-bottom: 24rpx;
  545. }
  546. .detail-actions {
  547. display: flex;
  548. flex-direction: row;
  549. justify-content: center;
  550. margin-top: 24rpx;
  551. }
  552. .btn-edit {
  553. background: linear-gradient(135deg, #10B981, #059669);
  554. padding: 18rpx 64rpx;
  555. border-radius: 40rpx;
  556. box-shadow: 0 4rpx 16rpx rgba(16, 185, 129, 0.3);
  557. }
  558. .btn-edit:active { opacity: 0.8; }
  559. .btn-edit text {
  560. color: #fff;
  561. font-size: 28rpx;
  562. font-weight: bold;
  563. }
  564. .loading {
  565. display: flex;
  566. align-items: center;
  567. justify-content: center;
  568. padding: 80rpx;
  569. }
  570. .loading-text {
  571. font-size: 28rpx;
  572. color: #999;
  573. }
  574. </style>