ActivityEdit.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <template>
  2. <div class="activity-edit admin-page">
  3. <el-card>
  4. <div slot="header" class="admin-page-header">
  5. <span class="admin-page-title">{{ readonly && activityStatus !== 'rejected' ? '活动详情' : (isEdit ? '编辑活动' : '创建活动') }}</span>
  6. <div class="admin-page-actions">
  7. <el-button @click="$router.push('/activities')">返回列表</el-button>
  8. <template v-if="!readonly && activityStatus !== 'rejected'">
  9. <el-button type="primary" @click="saveActivity(false)" :loading="saving">{{ activityStatus === 'published' ? '保存修改' : '保存草稿' }}</el-button>
  10. <el-button v-if="isEdit && activityStatus !== 'published'" type="success" @click="handleSubmitReview" :loading="reviewLoading">提交审核</el-button>
  11. </template>
  12. <template v-if="activityStatus === 'rejected'">
  13. <el-button type="primary" @click="handleReDraft" :loading="reDraftLoading">保存到草稿箱重新编辑</el-button>
  14. </template>
  15. <template v-if="readonly && activityStatus === 'draft' && !isEdit">
  16. <el-button type="primary" size="mini" @click="editFromReadonly">编辑</el-button>
  17. </template>
  18. </div>
  19. </div>
  20. <!-- 状态横幅 -->
  21. <el-alert
  22. v-if="statusLabel"
  23. :title="statusLabel"
  24. :type="statusType"
  25. :description="statusDescription"
  26. show-icon
  27. :closable="false"
  28. style="margin-bottom: 16px;"
  29. />
  30. <el-form label-width="120px" v-loading="loading" :model="form" :rules="formRules" ref="form">
  31. <el-form-item label="活动标题" prop="title">
  32. <el-input v-model="form.title" placeholder="请输入活动标题" :disabled="readonly" />
  33. </el-form-item>
  34. <el-form-item label="活动类型" prop="activityType">
  35. <el-select v-model="form.activityType" placeholder="请选择活动类型" style="width:100%" :disabled="readonly">
  36. <el-option label="线下活动" value="offline" />
  37. <el-option label="线上活动" value="online" />
  38. <el-option label="主题活动" value="campaign" />
  39. </el-select>
  40. </el-form-item>
  41. <el-form-item label="活动描述" prop="description">
  42. <div class="rich-editor" :class="{ 'is-disabled': readonly }">
  43. <Toolbar :editor="editorInstance" :defaultConfig="toolbarConfig" />
  44. <Editor :defaultConfig="editorConfig" v-model="form.description" @onCreated="handleEditorCreated" :disabled="readonly" style="min-height: 300px;" />
  45. </div>
  46. </el-form-item>
  47. <el-form-item label="封面图片">
  48. <template v-if="!readonly">
  49. <el-upload
  50. :action="uploadActionUrl"
  51. :headers="uploadHeaders"
  52. :on-success="handleUploadSuccess"
  53. :on-error="handleUploadError"
  54. :before-upload="beforeUpload"
  55. :show-file-list="false"
  56. accept="image/jpeg,image/png,image/gif,image/webp"
  57. >
  58. <img v-if="form.coverImage" :src="form.coverImage" class="cover-preview" />
  59. <el-button v-else type="primary" plain>+ 上传封面</el-button>
  60. </el-upload>
  61. <el-button v-if="form.coverImage" size="mini" type="text" style="margin-left:10px;" @click="form.coverImage = ''">清除</el-button>
  62. </template>
  63. <img v-else-if="form.coverImage" :src="form.coverImage" class="cover-preview" />
  64. <span v-else style="color:#999">无封面</span>
  65. </el-form-item>
  66. <el-form-item label="五维权重">
  67. <dimension-weight-picker v-model="form.dimensionWeights" :show-presets="true" :readonly="readonly" />
  68. </el-form-item>
  69. <el-form-item label="活动地点">
  70. <el-input v-model="form.location" placeholder="请输入活动地点" :disabled="readonly" />
  71. </el-form-item>
  72. <el-row :gutter="16">
  73. <el-col :span="12">
  74. <el-form-item label="最大人数">
  75. <el-input-number v-model="form.maxParticipants" :min="0" :max="99999" placeholder="0表示不限" style="width:100%" :disabled="readonly" />
  76. </el-form-item>
  77. </el-col>
  78. <el-col :span="12">
  79. <el-form-item label="价格(元)">
  80. <el-input-number v-model="form.priceYuan" :min="0" :precision="2" :step="0.01" placeholder="0表示免费" style="width:100%" :disabled="readonly" />
  81. </el-form-item>
  82. </el-col>
  83. </el-row>
  84. <el-row :gutter="16">
  85. <el-col :span="12">
  86. <el-form-item label="开始时间" prop="startTime">
  87. <el-date-picker v-model="form.startTime" type="datetime" placeholder="选择开始时间" style="width:100%" :disabled="readonly" />
  88. </el-form-item>
  89. </el-col>
  90. <el-col :span="12">
  91. <el-form-item label="结束时间" prop="endTime">
  92. <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择结束时间" style="width:100%" :disabled="readonly" />
  93. </el-form-item>
  94. </el-col>
  95. </el-row>
  96. <el-row :gutter="16">
  97. <el-col :span="12">
  98. <el-form-item label="可见范围">
  99. <el-select v-model="form.visibility" placeholder="请选择可见范围" style="width:100%" :disabled="readonly">
  100. <el-option label="公开" value="public" />
  101. <el-option label="受限" value="restricted" />
  102. <el-option label="全体" value="all" />
  103. <el-option label="会员" value="member" />
  104. </el-select>
  105. </el-form-item>
  106. </el-col>
  107. <el-col :span="12">
  108. <el-form-item label="可见对象">
  109. <el-select v-model="form.visibleScope" placeholder="请选择可见对象类型" style="width:100%" :disabled="readonly">
  110. <el-option label="不限" value="" />
  111. <el-option label="家庭" value="family" />
  112. <el-option label="角色" value="role" />
  113. <el-option label="用户" value="user" />
  114. </el-select>
  115. </el-form-item>
  116. </el-col>
  117. </el-row>
  118. <el-form-item label="可见对象ID" v-if="form.visibleScope">
  119. <el-input v-model="form.visibleTo" placeholder="多个用逗号分隔,如: family_1,family_2" :disabled="readonly" />
  120. </el-form-item>
  121. <el-form-item label="签到积分">
  122. <el-input-number v-model="form.checkinPoints" :min="0" :max="9999" placeholder="0使用系统默认" style="width:200px" :disabled="readonly" />
  123. <span style="margin-left:8px;color:#999;font-size:12px;">0表示使用系统默认积分</span>
  124. </el-form-item>
  125. </el-form>
  126. </el-card>
  127. <!-- 创建成功弹窗 -->
  128. <el-dialog title="创建成功" :visible.sync="successDialogVisible" width="420px" :close-on-click-modal="false" :show-close="false">
  129. <div style="text-align:center;padding:10px 0;">
  130. <i class="el-icon-circle-check" style="font-size:48px;color:#67C23A;"></i>
  131. <p style="font-size:16px;margin-top:12px;">活动「{{ form.title }}」已创建成功</p>
  132. </div>
  133. <div slot="footer" style="text-align:center;">
  134. <el-button @click="handleContinueAdd">继续添加</el-button>
  135. <el-button @click="$router.push('/activities')">返回列表</el-button>
  136. <el-button type="primary" @click="handlePublishAndBack" :loading="publishing">直接发布</el-button>
  137. </div>
  138. </el-dialog>
  139. </div>
  140. </template>
  141. <script>
  142. import DimensionWeightPicker from '@/components/DimensionWeightPicker.vue'
  143. import { getActivityDetail, createActivity, updateActivity, publishActivity, submitActivityReview, reDraftActivity } from '@/api/activity'
  144. import { saveDimensionWeights, getDimensionWeights } from '@/api/dimension-weight.js'
  145. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  146. import '@wangeditor/editor/dist/css/style.css'
  147. export default {
  148. name: 'ActivityEdit',
  149. components: { DimensionWeightPicker, Editor, Toolbar },
  150. data() {
  151. return {
  152. isEdit: false,
  153. loading: false,
  154. saving: false,
  155. publishing: false,
  156. readonly: false,
  157. activityStatus: '',
  158. auditReason: '',
  159. reviewLoading: false,
  160. reDraftLoading: false,
  161. successDialogVisible: false,
  162. createdActivityId: null,
  163. form: {
  164. title: '',
  165. coverImage: '',
  166. activityType: '',
  167. description: '',
  168. dimensionCode: '',
  169. dimensionWeights: [],
  170. location: '',
  171. maxParticipants: null,
  172. priceYuan: null,
  173. startTime: null,
  174. endTime: null,
  175. visibility: 'public',
  176. visibleScope: '',
  177. visibleTo: '',
  178. checkinPoints: 0
  179. },
  180. formRules: {
  181. title: [
  182. { required: true, message: '请填写活动标题', trigger: 'blur' }
  183. ],
  184. activityType: [
  185. { required: true, message: '请选择活动类型', trigger: 'change' }
  186. ],
  187. description: [
  188. { required: true, message: '请填写活动描述', trigger: 'blur' }
  189. ],
  190. startTime: [
  191. { required: true, message: '请选择开始时间', trigger: 'change' }
  192. ],
  193. endTime: [
  194. { required: true, message: '请选择结束时间', trigger: 'change' }
  195. ]
  196. },
  197. editorConfig: null,
  198. toolbarConfig: {},
  199. editorInstance: null
  200. }
  201. },
  202. computed: {
  203. statusLabel() {
  204. const map = { draft: '当前状态:草稿箱', pending: '当前状态:待审核(等待管理员审核)', rejected: '当前状态:已驳回', published: '当前状态:发布中', withdrawn: '当前状态:已撤回' }
  205. return map[this.activityStatus] || ''
  206. },
  207. statusType() {
  208. const map = { draft: 'info', pending: 'warning', rejected: 'error', published: 'success', withdrawn: 'info' }
  209. return map[this.activityStatus] || 'info'
  210. },
  211. statusDescription() {
  212. if (this.activityStatus === 'rejected' && this.auditReason) return '原因:' + this.auditReason
  213. return ''
  214. },
  215. uploadActionUrl() {
  216. const baseURL = process.env.VUE_APP_BASE_API || ''
  217. return baseURL + '/api/admin/articles/upload/image'
  218. },
  219. uploadHeaders() {
  220. const token = localStorage.getItem('token')
  221. return token ? { Authorization: 'Bearer ' + token } : {}
  222. }
  223. },
  224. created() {
  225. this.initEditorConfig()
  226. const id = this.$route.query.id
  227. this.readonly = this.$route.query.readonly === '1'
  228. if (id) {
  229. this.isEdit = true
  230. this.loadDetail(id)
  231. }
  232. },
  233. methods: {
  234. initEditorConfig() {
  235. const baseURL = process.env.VUE_APP_BASE_API || ''
  236. const token = localStorage.getItem('token')
  237. this.editorConfig = {
  238. MENU_CONF: {
  239. uploadImage: {
  240. server: baseURL + '/api/admin/articles/upload/image',
  241. headers: token ? { Authorization: 'Bearer ' + token } : {},
  242. maxNumberOfFiles: 1,
  243. fieldName: 'file',
  244. customAlert: (msg, type) => {
  245. this.$message && this.$message({ message: msg, type: type || 'error' })
  246. },
  247. onSuccess(res, insertFn) {
  248. if (res.code === 200) {
  249. const url = res.data.url || res.data
  250. insertFn(baseURL + url)
  251. }
  252. }
  253. }
  254. }
  255. }
  256. },
  257. handleEditorCreated(editor) {
  258. this.editorInstance = editor
  259. },
  260. async loadDetail(id) {
  261. this.loading = true
  262. try {
  263. const res = await getActivityDetail({ id })
  264. if (res.data) {
  265. const d = res.data
  266. this.activityStatus = d.status || ''
  267. this.auditReason = d.auditReason || ''
  268. // Set readonly based on workflow status
  269. if (d.status === 'pending' || d.status === 'rejected' || d.status === 'withdrawn') {
  270. this.readonly = true
  271. } else if (this.$route.query.readonly === '1') {
  272. this.readonly = true
  273. } else {
  274. this.readonly = false
  275. }
  276. this.form = {
  277. title: d.title || '',
  278. coverImage: d.coverImage || '',
  279. activityType: d.activityType || '',
  280. description: d.description || '',
  281. dimensionCode: d.dimensionCode || '',
  282. dimensionWeights: [],
  283. location: d.location || '',
  284. maxParticipants: d.maxParticipants || null,
  285. priceYuan: d.price ? d.price / 100 : null,
  286. startTime: d.startTime || null,
  287. endTime: d.endTime || null,
  288. visibility: d.visibility || 'public',
  289. visibleScope: d.visibleScope || '',
  290. visibleTo: d.visibleTo || '',
  291. checkinPoints: d.checkinPoints || 0
  292. }
  293. try {
  294. const dwRes = await getDimensionWeights('activity', d.id)
  295. if (dwRes.data) {
  296. this.form.dimensionWeights = dwRes.data
  297. }
  298. } catch (e) {
  299. }
  300. }
  301. } catch (e) {
  302. this.$message.error('加载活动详情失败')
  303. } finally {
  304. this.loading = false
  305. }
  306. },
  307. beforeUpload(file) {
  308. const isImage = file.type === 'image/jpeg' || file.type === 'image/png' ||
  309. file.type === 'image/gif' || file.type === 'image/webp'
  310. if (!isImage) {
  311. this.$message.error('只能上传 JPG/PNG/GIF/WebP 格式图片')
  312. return false
  313. }
  314. const isLt5M = file.size / 1024 / 1024 < 5
  315. if (!isLt5M) {
  316. this.$message.error('图片大小不能超过 5MB')
  317. return false
  318. }
  319. return true
  320. },
  321. handleUploadSuccess(resp) {
  322. if (resp.code === 0 || resp.code === 200) {
  323. this.form.coverImage = resp.data || resp
  324. this.$message.success('封面上传成功')
  325. } else {
  326. this.$message.error((resp && resp.message) || '封面上传失败')
  327. }
  328. },
  329. handleUploadError() {
  330. this.$message.error('封面上传失败,请重试')
  331. },
  332. async saveActivity(publishNow) {
  333. // 表单验证
  334. try {
  335. await this.$refs.form.validate()
  336. } catch (e) {
  337. return
  338. }
  339. if (publishNow) {
  340. this.publishing = true
  341. } else {
  342. this.saving = true
  343. }
  344. try {
  345. const payload = {
  346. title: this.form.title,
  347. coverImage: this.form.coverImage,
  348. activityType: this.form.activityType,
  349. description: this.form.description,
  350. dimensionCode: this.form.dimensionCode,
  351. location: this.form.location,
  352. maxParticipants: this.form.maxParticipants,
  353. price: this.form.priceYuan != null ? Math.round(this.form.priceYuan * 100) : 0,
  354. startTime: this.form.startTime,
  355. endTime: this.form.endTime,
  356. visibility: this.form.visibility,
  357. visibleScope: this.form.visibleScope,
  358. visibleTo: this.form.visibleTo,
  359. checkinPoints: this.form.checkinPoints || 0
  360. }
  361. if (this.isEdit) {
  362. // 编辑模式:保存后直接返回列表
  363. payload.id = this.form._activityId || this.$route.query.id
  364. await updateActivity(payload)
  365. if (this.form.dimensionWeights && this.form.dimensionWeights.length) {
  366. try {
  367. await saveDimensionWeights('activity', payload.id, this.form.dimensionWeights)
  368. } catch (e) {
  369. console.error('保存维度权重失败', e)
  370. }
  371. }
  372. this.$message.success('更新成功')
  373. this.$router.push('/activities')
  374. } else {
  375. // 创建模式:保存后弹窗
  376. const res = await createActivity(payload)
  377. if (res.data && res.data.id) {
  378. this.createdActivityId = res.data.id
  379. }
  380. this.isEdit = true
  381. this.saving = false
  382. this.successDialogVisible = true
  383. }
  384. } catch (e) {
  385. this.$message.error('保存失败')
  386. } finally {
  387. this.saving = false
  388. this.publishing = false
  389. }
  390. },
  391. handleContinueAdd() {
  392. this.successDialogVisible = false
  393. this.form = {
  394. title: '',
  395. coverImage: '',
  396. activityType: '',
  397. description: '',
  398. dimensionCode: '',
  399. dimensionWeights: [],
  400. location: '',
  401. maxParticipants: null,
  402. priceYuan: null,
  403. startTime: null,
  404. endTime: null,
  405. visibility: 'public',
  406. visibleScope: '',
  407. visibleTo: '',
  408. checkinPoints: 0
  409. }
  410. this.isEdit = false
  411. this.createdActivityId = null
  412. this.$refs.form && this.$refs.form.clearValidate()
  413. },
  414. async handlePublishAndBack() {
  415. this.successDialogVisible = false
  416. const activityId = this.createdActivityId
  417. if (!activityId) {
  418. this.$router.push('/activities')
  419. return
  420. }
  421. // 先保存维度权重
  422. if (this.form.dimensionWeights && this.form.dimensionWeights.length) {
  423. try {
  424. await saveDimensionWeights('activity', activityId, this.form.dimensionWeights)
  425. } catch (e) {
  426. console.error('保存维度权重失败', e)
  427. }
  428. }
  429. // 发布
  430. try {
  431. this.publishing = true
  432. await publishActivity(activityId)
  433. this.$message.success('发布成功')
  434. } catch (e) {
  435. this.$message.error('发布失败')
  436. } finally {
  437. this.publishing = false
  438. }
  439. this.$router.push('/activities')
  440. },
  441. editFromReadonly() {
  442. this.readonly = false
  443. },
  444. async handleSubmitReview() {
  445. this.reviewLoading = true
  446. try {
  447. // Save activity first
  448. const payload = {
  449. title: this.form.title,
  450. coverImage: this.form.coverImage,
  451. activityType: this.form.activityType,
  452. description: this.form.description,
  453. dimensionCode: this.form.dimensionCode,
  454. location: this.form.location,
  455. maxParticipants: this.form.maxParticipants,
  456. price: this.form.priceYuan != null ? Math.round(this.form.priceYuan * 100) : 0,
  457. startTime: this.form.startTime,
  458. endTime: this.form.endTime,
  459. visibility: this.form.visibility,
  460. visibleScope: this.form.visibleScope,
  461. visibleTo: this.form.visibleTo,
  462. checkinPoints: this.form.checkinPoints || 0
  463. }
  464. const activityId = this.form._activityId || this.$route.query.id
  465. if (
  466. activityId
  467. ) {
  468. payload.id = activityId
  469. await updateActivity(payload)
  470. } else {
  471. const res = await createActivity(payload)
  472. if (res.data && res.data.id) {
  473. payload.id = res.data.id
  474. this.form._activityId = res.data.id
  475. this.isEdit = true
  476. }
  477. }
  478. if (payload.id && this.form.dimensionWeights && this.form.dimensionWeights.length) {
  479. try { await saveDimensionWeights('activity', payload.id, this.form.dimensionWeights) } catch (e) {}
  480. }
  481. // Submit for review
  482. await submitActivityReview({ id: payload.id })
  483. this.$message.success('已提交审核')
  484. this.$router.push('/activities')
  485. } catch (e) {
  486. this.$message.error(e.message || '提交审核失败')
  487. } finally {
  488. this.reviewLoading = false
  489. }
  490. },
  491. async handleReDraft() {
  492. this.reDraftLoading = true
  493. try {
  494. await reDraftActivity({ id: this.$route.query.id })
  495. this.$message.success('已保存到草稿箱')
  496. this.activityStatus = 'draft'
  497. this.readonly = false
  498. } catch (e) {
  499. this.$message.error(e.message || '操作失败')
  500. } finally {
  501. this.reDraftLoading = false
  502. }
  503. }
  504. }
  505. }
  506. </script>
  507. <style scoped>
  508. .activity-edit {
  509. padding: 20px;
  510. display: block;
  511. min-height: auto;
  512. }
  513. .activity-edit > .el-card {
  514. display: block;
  515. min-height: auto;
  516. width: 100%;
  517. }
  518. .activity-edit > .el-card /deep/ .el-card__body {
  519. display: block;
  520. min-height: auto;
  521. overflow: visible;
  522. }
  523. .activity-edit /deep/ .el-form {
  524. width: 100%;
  525. }
  526. .activity-edit /deep/ .admin-page-header {
  527. display: flex;
  528. align-items: flex-start;
  529. justify-content: space-between;
  530. gap: 12px;
  531. flex-wrap: wrap;
  532. }
  533. .activity-edit /deep/ .admin-page-title {
  534. font-size: 18px;
  535. font-weight: 600;
  536. line-height: 32px;
  537. }
  538. .activity-edit /deep/ .admin-page-actions {
  539. display: flex;
  540. align-items: center;
  541. justify-content: flex-end;
  542. gap: 8px;
  543. flex-wrap: wrap;
  544. margin-left: auto;
  545. }
  546. .cover-preview {
  547. width: 200px;
  548. height: 120px;
  549. object-fit: cover;
  550. border-radius: 4px;
  551. border: 1px solid #ddd;
  552. }
  553. .rich-editor {
  554. border: 1px solid #dcdfe6;
  555. border-radius: 4px;
  556. }
  557. .rich-editor /deep/ .w-e-toolbar {
  558. flex-wrap: wrap;
  559. }
  560. .rich-editor /deep/ .w-e-text-container {
  561. min-height: 300px;
  562. }
  563. </style>