create.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <template>
  2. <view class="container">
  3. <view class="form-card">
  4. <view class="form-title">{{ isNew ? '创建活动' : '编辑活动' }}</view>
  5. <!-- 活动标题 -->
  6. <view class="form-item">
  7. <text class="form-label">活动标题 *</text>
  8. <input class="form-input" v-model="form.title" placeholder="请输入活动标题" maxlength="50" />
  9. </view>
  10. <!-- 活动维度 -->
  11. <view class="form-item">
  12. <text class="form-label">所属维度 *</text>
  13. <picker class="form-picker" :value="dimIdx" :range="dimensionOptions" range-key="label" @change="onDimChange">
  14. <text class="picker-text">{{ form.dimensionCode ? dimLabel(form.dimensionCode) : '请选择维度' }}</text>
  15. </picker>
  16. </view>
  17. <!-- 活动类型 -->
  18. <view class="form-item">
  19. <text class="form-label">活动类型 *</text>
  20. <picker class="form-picker" :value="typeIdx" :range="typeOptions" range-key="label" @change="onTypeChange">
  21. <text class="picker-text">{{ form.activityType ? typeLabel(form.activityType) : '请选择类型' }}</text>
  22. </picker>
  23. </view>
  24. <!-- 开始时间 -->
  25. <view class="form-item">
  26. <text class="form-label">开始时间 *</text>
  27. <picker class="form-picker" mode="date" :value="datePart" @change="onStartDateChange">
  28. <text class="picker-text">{{ datePart || '选择日期' }}</text>
  29. </picker>
  30. <picker class="form-picker" mode="time" :value="timePart" @change="onStartTimeChange" style="margin-top: 16rpx;">
  31. <text class="picker-text">{{ timePart || '选择时间' }}</text>
  32. </picker>
  33. </view>
  34. <!-- 结束时间 -->
  35. <view class="form-item">
  36. <text class="form-label">结束时间</text>
  37. <picker class="form-picker" mode="date" :value="endDatePart" @change="onEndDateChange">
  38. <text class="picker-text">{{ endDatePart || '选择日期(可选)' }}</text>
  39. </picker>
  40. <picker class="form-picker" mode="time" :value="endTimePart" @change="onEndTimeChange" style="margin-top: 16rpx;">
  41. <text class="picker-text">{{ endTimePart || '选择时间(可选)' }}</text>
  42. </picker>
  43. </view>
  44. <!-- 报名截止时间 -->
  45. <view class="form-item">
  46. <text class="form-label">报名截止时间</text>
  47. <picker class="form-picker" mode="date" :value="deadlineDatePart" @change="onDeadlineDateChange">
  48. <text class="picker-text">{{ deadlineDatePart || '选择日期(可选)' }}</text>
  49. </picker>
  50. <picker class="form-picker" mode="time" :value="deadlineTimePart" @change="onDeadlineTimeChange" style="margin-top: 16rpx;">
  51. <text class="picker-text">{{ deadlineTimePart || '选择时间(可选)' }}</text>
  52. </picker>
  53. </view>
  54. <!-- 活动地点 -->
  55. <view class="form-item">
  56. <text class="form-label">活动地点</text>
  57. <input class="form-input" v-model="form.location" placeholder="请输入活动地点" maxlength="100" />
  58. </view>
  59. <!-- 人数 + 价格 -->
  60. <view class="form-item-row">
  61. <view class="form-item half">
  62. <text class="form-label">最大人数</text>
  63. <input class="form-input" type="number" v-model="maxParticipantsStr" placeholder="0=不限" />
  64. </view>
  65. <view class="form-item half">
  66. <text class="form-label">价格(元)</text>
  67. <input class="form-input" type="digit" v-model="priceYuan" placeholder="0=免费" />
  68. </view>
  69. </view>
  70. <!-- 封面图片 -->
  71. <view class="form-item">
  72. <text class="form-label">封面图片</text>
  73. <input class="form-input" v-model="form.coverImage" placeholder="输入图片URL(可选)" />
  74. <view class="cover-preview" v-if="form.coverImage">
  75. <image class="preview-img" :src="form.coverImage" mode="aspectFill" @error="onImgError" />
  76. </view>
  77. </view>
  78. <!-- 活动描述 -->
  79. <view class="form-item">
  80. <text class="form-label">活动描述</text>
  81. <textarea class="form-textarea" v-model="form.description" placeholder="请输入活动详细介绍" maxlength="500"
  82. auto-height />
  83. </view>
  84. <!-- 操作按钮 -->
  85. <view class="form-actions">
  86. <view class="btn-save" @click="handleSave">
  87. <text>保存为草稿</text>
  88. </view>
  89. <view class="btn-publish" @click="handlePublish">
  90. <text>保存并发布</text>
  91. </view>
  92. </view>
  93. </view>
  94. <view class="loading" v-if="loading">
  95. <view class="loading-spinner"></view>
  96. <text>加载中...</text>
  97. </view>
  98. </view>
  99. </template>
  100. <script>
  101. import { adminCreateActivity, adminUpdateActivity, adminPublishActivity, getActivityDetail } from '../../utils/api.js'
  102. import config from '../../config.js'
  103. var BASE_URL = config.api('')
  104. export default {
  105. data() {
  106. return {
  107. isNew: true,
  108. activityId: null,
  109. loading: false,
  110. childId: '',
  111. form: {
  112. title: '',
  113. description: '',
  114. coverImage: '',
  115. dimensionCode: '',
  116. activityType: '',
  117. status: 'draft',
  118. startTime: null,
  119. endTime: null,
  120. registrationDeadline: null,
  121. location: '',
  122. maxParticipants: 0,
  123. price: 0,
  124. memberPrice: 0,
  125. visibility: 'public',
  126. requireRegistration: 1,
  127. requireCheckinReview: 0,
  128. checkinPoints: 0
  129. },
  130. priceYuan: '0',
  131. maxParticipantsStr: '0',
  132. datePart: '',
  133. timePart: '',
  134. endDatePart: '',
  135. endTimePart: '',
  136. deadlineDatePart: '',
  137. deadlineTimePart: '',
  138. dimIdx: 0,
  139. typeIdx: 0,
  140. dimensionOptions: [
  141. { key: 'body', label: '身 · 主动健康' },
  142. { key: 'mind', label: '心 · 情感丰盈' },
  143. { key: 'wisdom', label: '智 · 赋能未来' },
  144. { key: 'action', label: '行 · 知行合一' },
  145. { key: 'wealth', label: '富 · 物质精神' }
  146. ],
  147. typeOptions: [
  148. { key: 'offline', label: '线下活动' },
  149. { key: 'online', label: '线上活动' },
  150. { key: 'campaign', label: '营销活动' }
  151. ]
  152. }
  153. },
  154. onLoad(options) {
  155. if (options.id) {
  156. this.activityId = parseInt(options.id)
  157. this.isNew = false
  158. uni.setNavigationBarTitle({ title: '编辑活动' })
  159. this.loadDetail()
  160. } else {
  161. uni.setNavigationBarTitle({ title: '创建活动' })
  162. }
  163. },
  164. methods: {
  165. loadDetail() {
  166. var self = this
  167. self.loading = true
  168. getActivityDetail(self.activityId).then(function(res) {
  169. self.loading = false
  170. if (res && res.code === 200 && res.data) {
  171. var d = res.data
  172. self.form.title = d.title || ''
  173. self.form.description = d.description || ''
  174. self.form.coverImage = d.coverImage || ''
  175. self.form.dimensionCode = d.dimensionCode || ''
  176. self.form.activityType = d.activityType || ''
  177. self.form.location = d.location || ''
  178. self.form.maxParticipants = d.maxParticipants || 0
  179. self.form.price = d.price || 0
  180. self.form.memberPrice = d.memberPrice || 0
  181. self.form.status = d.status || 'draft'
  182. self.form.visibility = d.visibility || 'public'
  183. self.maxParticipantsStr = String(self.form.maxParticipants)
  184. self.priceYuan = self.form.price ? (self.form.price / 100).toString() : '0'
  185. if (d.startTime) {
  186. var sd = new Date(d.startTime.replace ? d.startTime.replace(/-/g, '/') : d.startTime)
  187. self.datePart = self.fmtDate(sd)
  188. self.timePart = self.fmtTime(sd)
  189. self.form.startTime = sd.toISOString()
  190. }
  191. if (d.endTime) {
  192. var ed = new Date(d.endTime.replace ? d.endTime.replace(/-/g, '/') : d.endTime)
  193. self.endDatePart = self.fmtDate(ed)
  194. self.endTimePart = self.fmtTime(ed)
  195. self.form.endTime = ed.toISOString()
  196. }
  197. if (d.registrationDeadline) {
  198. var dl = new Date(d.registrationDeadline.replace ? d.registrationDeadline.replace(/-/g, '/') : d.registrationDeadline)
  199. self.deadlineDatePart = self.fmtDate(dl)
  200. self.deadlineTimePart = self.fmtTime(dl)
  201. self.form.registrationDeadline = dl.toISOString()
  202. }
  203. // sync pickers
  204. for (var i = 0; i < self.dimensionOptions.length; i++) {
  205. if (self.dimensionOptions[i].key === self.form.dimensionCode) self.dimIdx = i
  206. }
  207. for (var j = 0; j < self.typeOptions.length; j++) {
  208. if (self.typeOptions[j].key === self.form.activityType) self.typeIdx = j
  209. }
  210. }
  211. }).catch(function() { self.loading = false })
  212. },
  213. onDimChange(e) {
  214. this.dimIdx = e.detail.value
  215. this.form.dimensionCode = this.dimensionOptions[this.dimIdx].key
  216. },
  217. onTypeChange(e) {
  218. this.typeIdx = e.detail.value
  219. this.form.activityType = this.typeOptions[this.typeIdx].key
  220. },
  221. onStartDateChange(e) {
  222. this.datePart = e.detail.value
  223. this.syncStartTime()
  224. },
  225. onStartTimeChange(e) {
  226. this.timePart = e.detail.value
  227. this.syncStartTime()
  228. },
  229. onEndDateChange(e) {
  230. this.endDatePart = e.detail.value
  231. this.syncEndTime()
  232. },
  233. onEndTimeChange(e) {
  234. this.endTimePart = e.detail.value
  235. this.syncEndTime()
  236. },
  237. onDeadlineDateChange(e) {
  238. this.deadlineDatePart = e.detail.value
  239. this.syncDeadline()
  240. },
  241. onDeadlineTimeChange(e) {
  242. this.deadlineTimePart = e.detail.value
  243. this.syncDeadline()
  244. },
  245. syncStartTime() {
  246. if (this.datePart && this.timePart) {
  247. this.form.startTime = this.datePart + 'T' + this.timePart + ':00'
  248. }
  249. },
  250. syncEndTime() {
  251. if (this.endDatePart && this.endTimePart) {
  252. this.form.endTime = this.endDatePart + 'T' + this.endTimePart + ':00'
  253. } else {
  254. this.form.endTime = null
  255. }
  256. },
  257. syncDeadline() {
  258. if (this.deadlineDatePart && this.deadlineTimePart) {
  259. this.form.registrationDeadline = this.deadlineDatePart + 'T' + this.deadlineTimePart + ':00'
  260. } else {
  261. this.form.registrationDeadline = null
  262. }
  263. },
  264. chooseImage() {
  265. var self = this
  266. uni.chooseImage({
  267. count: 1,
  268. sizeType: ['compressed'],
  269. success: function(res) {
  270. self.uploadCover(res.tempFilePaths[0])
  271. }
  272. })
  273. },
  274. uploadCover(filePath) {
  275. var self = this
  276. uni.uploadFile({
  277. url: BASE_URL + '/api/admin/articles/upload/image',
  278. filePath: filePath,
  279. name: 'file',
  280. header: { 'Authorization': 'Bearer ' + (uni.getStorageSync('token') || '') },
  281. success: function(res) {
  282. try {
  283. var data = JSON.parse(res.data)
  284. if (data.code === 200 && data.data) {
  285. self.form.coverImage = data.data
  286. } else {
  287. uni.showToast({ title: '上传失败', icon: 'none' })
  288. }
  289. } catch (e) {
  290. uni.showToast({ title: '上传失败', icon: 'none' })
  291. }
  292. },
  293. fail: function() {
  294. uni.showToast({ title: '上传失败', icon: 'none' })
  295. }
  296. })
  297. },
  298. onImgError() {
  299. this.form.coverImage = ''
  300. },
  301. validate() {
  302. if (!this.form.title) {
  303. uni.showToast({ title: '请输入活动标题', icon: 'none' })
  304. return false
  305. }
  306. if (!this.form.dimensionCode) {
  307. uni.showToast({ title: '请选择所属维度', icon: 'none' })
  308. return false
  309. }
  310. if (!this.form.activityType) {
  311. uni.showToast({ title: '请选择活动类型', icon: 'none' })
  312. return false
  313. }
  314. if (!this.datePart || !this.timePart) {
  315. uni.showToast({ title: '请选择开始时间', icon: 'none' })
  316. return false
  317. }
  318. return true
  319. },
  320. buildPayload() {
  321. var payload = Object.assign({}, this.form)
  322. payload.maxParticipants = parseInt(this.maxParticipantsStr) || 0
  323. payload.price = Math.round((parseFloat(this.priceYuan) || 0) * 100)
  324. payload.memberPrice = payload.price // 会员价默认等于普通价
  325. if (this.activityId) payload.id = this.activityId
  326. return payload
  327. },
  328. handleSave() {
  329. if (!this.validate()) return
  330. this.submit(false)
  331. },
  332. handlePublish() {
  333. if (!this.validate()) return
  334. this.submit(true)
  335. },
  336. submit(publishAfter) {
  337. var self = this
  338. self.loading = true
  339. var payload = self.buildPayload()
  340. var doPublish = function(id) {
  341. adminPublishActivity(id).then(function(r) {
  342. self.loading = false
  343. if (r && r.code === 200) {
  344. uni.showToast({ title: '已发布', icon: 'success' })
  345. setTimeout(function() {
  346. uni.navigateBack()
  347. }, 1200)
  348. } else {
  349. uni.showToast({ title: (r && r.message) || '发布失败', icon: 'none' })
  350. }
  351. }).catch(function() {
  352. self.loading = false
  353. uni.showToast({ title: '发布失败', icon: 'none' })
  354. })
  355. }
  356. if (self.isNew) {
  357. adminCreateActivity(payload).then(function(res) {
  358. if (res && res.code === 200) {
  359. var newId = res.data.id || res.data
  360. if (publishAfter) {
  361. doPublish(newId)
  362. } else {
  363. self.loading = false
  364. uni.showToast({ title: '已保存草稿', icon: 'success' })
  365. setTimeout(function() { uni.navigateBack() }, 1200)
  366. }
  367. } else {
  368. self.loading = false
  369. uni.showToast({ title: (res && res.message) || '保存失败', icon: 'none' })
  370. }
  371. }).catch(function() {
  372. self.loading = false
  373. uni.showToast({ title: '网络异常', icon: 'none' })
  374. })
  375. } else {
  376. adminUpdateActivity(payload).then(function(res) {
  377. if (res && res.code === 200) {
  378. if (publishAfter && self.form.status === 'draft') {
  379. doPublish(self.activityId)
  380. } else {
  381. self.loading = false
  382. uni.showToast({ title: '已保存', icon: 'success' })
  383. setTimeout(function() { uni.navigateBack() }, 1200)
  384. }
  385. } else {
  386. self.loading = false
  387. uni.showToast({ title: (res && res.message) || '保存失败', icon: 'none' })
  388. }
  389. }).catch(function() {
  390. self.loading = false
  391. uni.showToast({ title: '网络异常', icon: 'none' })
  392. })
  393. }
  394. },
  395. dimLabel(key) {
  396. for (var i = 0; i < this.dimensionOptions.length; i++) {
  397. if (this.dimensionOptions[i].key === key) return this.dimensionOptions[i].label
  398. }
  399. return '请选择维度'
  400. },
  401. typeLabel(key) {
  402. for (var i = 0; i < this.typeOptions.length; i++) {
  403. if (this.typeOptions[i].key === key) return this.typeOptions[i].label
  404. }
  405. return '请选择类型'
  406. },
  407. fmtDate(d) {
  408. var y = d.getFullYear()
  409. var m = ('0' + (d.getMonth() + 1)).slice(-2)
  410. var day = ('0' + d.getDate()).slice(-2)
  411. return y + '-' + m + '-' + day
  412. },
  413. fmtTime(d) {
  414. var h = ('0' + d.getHours()).slice(-2)
  415. var mi = ('0' + d.getMinutes()).slice(-2)
  416. return h + ':' + mi
  417. }
  418. }
  419. }
  420. </script>
  421. <style scoped>
  422. .container {
  423. min-height: 100vh;
  424. background: #F5F7FA;
  425. padding: 24rpx;
  426. }
  427. .form-card {
  428. background: #fff;
  429. border-radius: 20rpx;
  430. padding: 32rpx 28rpx;
  431. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.04);
  432. }
  433. .form-title {
  434. font-size: 36rpx;
  435. font-weight: bold;
  436. color: #1E293B;
  437. margin-bottom: 24rpx;
  438. }
  439. .form-item {
  440. margin-bottom: 28rpx;
  441. }
  442. .form-item-row {
  443. display: flex;
  444. flex-direction: row;
  445. gap: 20rpx;
  446. margin-bottom: 28rpx;
  447. }
  448. .form-item.half {
  449. flex: 1;
  450. }
  451. .form-label {
  452. font-size: 26rpx;
  453. color: #475569;
  454. font-weight: 600;
  455. margin-bottom: 12rpx;
  456. display: block;
  457. }
  458. .form-input {
  459. width: 100%;
  460. height: 80rpx;
  461. border: 1rpx solid #E2E8F0;
  462. border-radius: 12rpx;
  463. padding: 0 20rpx;
  464. font-size: 28rpx;
  465. color: #1E293B;
  466. box-sizing: border-box;
  467. }
  468. .form-picker {
  469. width: 100%;
  470. height: 80rpx;
  471. border: 1rpx solid #E2E8F0;
  472. border-radius: 12rpx;
  473. padding: 0 20rpx;
  474. font-size: 28rpx;
  475. color: #1E293B;
  476. box-sizing: border-box;
  477. display: flex;
  478. align-items: center;
  479. }
  480. .picker-text {
  481. font-size: 28rpx;
  482. color: #1E293B;
  483. }
  484. .form-textarea {
  485. width: 100%;
  486. min-height: 160rpx;
  487. border: 1rpx solid #E2E8F0;
  488. border-radius: 12rpx;
  489. padding: 16rpx 20rpx;
  490. font-size: 28rpx;
  491. color: #1E293B;
  492. box-sizing: border-box;
  493. }
  494. .cover-upload {
  495. width: 100%;
  496. height: 320rpx;
  497. border: 2rpx dashed #CBD5E1;
  498. border-radius: 12rpx;
  499. display: flex;
  500. align-items: center;
  501. justify-content: center;
  502. overflow: hidden;
  503. }
  504. .preview-img {
  505. width: 100%;
  506. height: 100%;
  507. }
  508. .upload-placeholder {
  509. display: flex;
  510. flex-direction: column;
  511. align-items: center;
  512. }
  513. .upload-icon {
  514. font-size: 64rpx;
  515. color: #94A3B8;
  516. }
  517. .upload-text {
  518. font-size: 24rpx;
  519. color: #94A3B8;
  520. margin-top: 8rpx;
  521. }
  522. .form-actions {
  523. display: flex;
  524. flex-direction: row;
  525. gap: 20rpx;
  526. margin-top: 40rpx;
  527. }
  528. .btn-save,
  529. .btn-publish {
  530. flex: 1;
  531. height: 88rpx;
  532. border-radius: 44rpx;
  533. display: flex;
  534. align-items: center;
  535. justify-content: center;
  536. font-size: 30rpx;
  537. font-weight: 600;
  538. }
  539. .btn-save {
  540. background: #fff;
  541. color: #F97316;
  542. border: 2rpx solid #F97316;
  543. }
  544. .btn-publish {
  545. background: linear-gradient(135deg, #F97316, #FB923C);
  546. color: #fff;
  547. }
  548. .loading {
  549. position: fixed;
  550. top: 0;
  551. left: 0;
  552. right: 0;
  553. bottom: 0;
  554. background: rgba(255, 255, 255, 0.6);
  555. display: flex;
  556. flex-direction: column;
  557. align-items: center;
  558. justify-content: center;
  559. z-index: 999;
  560. }
  561. .loading-spinner {
  562. width: 60rpx;
  563. height: 60rpx;
  564. border: 4rpx solid #FED7AA;
  565. border-top: 4rpx solid #F97316;
  566. border-radius: 50%;
  567. animation: spin 0.8s linear infinite;
  568. margin-bottom: 16rpx;
  569. }
  570. @keyframes spin {
  571. to { transform: rotate(360deg); }
  572. }
  573. </style>