detail.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <view class="container">
  3. <!-- 任务模板信息 -->
  4. <view class="package-info">
  5. <view class="info-header">
  6. <text class="name">{{ pkg.name }}</text>
  7. <text class="status" :class="pkg.status">{{ getStatusText(pkg.status) }}</text>
  8. </view>
  9. <view class="info-row">
  10. <text>周期: {{ pkg.cycleDays }}天</text>
  11. <text>价格: {{ formatPriceWithSymbol(pkg.price) }}</text>
  12. </view>
  13. <view class="info-desc">{{ pkg.description }}</view>
  14. <view class="share-code" v-if="pkg.shareCode">
  15. <text>分享码: {{ pkg.shareCode }}</text>
  16. <text class="copy" @click="copyCode">复制</text>
  17. </view>
  18. </view>
  19. <!-- 任务项列表 -->
  20. <view class="section">
  21. <view class="section-header">
  22. <text class="section-title">任务项</text>
  23. <button class="btn-add" @click="showAddItem">添加</button>
  24. </view>
  25. <view class="item-list">
  26. <view
  27. class="item-card"
  28. v-for="item in items"
  29. :key="item.id"
  30. >
  31. <view class="item-header">
  32. <text class="item-name">{{ item.taskName }}</text>
  33. <text class="item-required" v-if="item.isRequired">必选</text>
  34. </view>
  35. <view class="item-info">
  36. <text>积分: {{ item.points }}</text>
  37. <text>频率: {{ item.frequency }}</text>
  38. <text>时长: {{ item.duration }}分钟</text>
  39. </view>
  40. <view class="item-actions">
  41. <text class="action-edit" @click="editItem(item)">编辑</text>
  42. <text class="action-delete" @click="deleteItem(item.id)">删除</text>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="empty-items" v-if="items.length === 0">
  47. <text>暂无任务项,请添加</text>
  48. </view>
  49. </view>
  50. <!-- 添加/编辑任务项弹窗 -->
  51. <uni-popup ref="itemPopup" type="bottom">
  52. <view class="popup-content">
  53. <view class="popup-header">
  54. <text>{{ editingItem ? '编辑任务项' : '添加任务项' }}</text>
  55. <text class="close" @click="closePopup">×</text>
  56. </view>
  57. <view class="popup-form">
  58. <view class="form-item">
  59. <text class="label">任务名称</text>
  60. <input class="input" v-model="itemForm.taskName" placeholder="任务名称" />
  61. </view>
  62. <view class="form-item">
  63. <text class="label">描述</text>
  64. <textarea class="textarea" v-model="itemForm.description" placeholder="任务描述" />
  65. </view>
  66. <view class="form-item">
  67. <text class="label">积分</text>
  68. <input class="input" type="number" v-model="itemForm.points" placeholder="积分" />
  69. </view>
  70. <view class="form-item">
  71. <text class="label">频率</text>
  72. <picker :range="frequencies" @change="onFreqChange">
  73. <view class="picker">{{ itemForm.frequency || '请选择' }}</view>
  74. </picker>
  75. </view>
  76. <view class="form-item">
  77. <text class="label">预计时长(分钟)</text>
  78. <input class="input" type="number" v-model="itemForm.duration" />
  79. </view>
  80. <view class="form-item">
  81. <text class="label">必选</text>
  82. <switch v-model="itemForm.isRequired" />
  83. </view>
  84. <button class="btn-save" @click="saveItem">保存</button>
  85. </view>
  86. </view>
  87. </uni-popup>
  88. </view>
  89. </template>
  90. <script>
  91. import {
  92. getPackage,
  93. getPackageItems,
  94. addPackageItem,
  95. updatePackageItem,
  96. deletePackageItem
  97. } from '@/utils/api.js'
  98. export default {
  99. data() {
  100. return {
  101. pkgId: null,
  102. pkg: {},
  103. items: [],
  104. editingItem: null,
  105. itemForm: {
  106. taskName: '',
  107. description: '',
  108. points: 10,
  109. frequency: 'daily',
  110. duration: 30,
  111. isRequired: false
  112. },
  113. frequencies: ['daily', 'weekly', 'custom']
  114. }
  115. },
  116. onLoad(options) {
  117. this.pkgId = options.id
  118. this.loadPackage()
  119. this.loadItems()
  120. },
  121. methods: {
  122. async loadPackage() {
  123. try {
  124. const res = await getPackage(this.pkgId)
  125. this.pkg = res.data || {}
  126. } catch (e) {
  127. console.error(e)
  128. }
  129. },
  130. async loadItems() {
  131. try {
  132. const res = await getPackageItems(this.pkgId)
  133. this.items = res.data || []
  134. } catch (e) {
  135. console.error(e)
  136. }
  137. },
  138. getStatusText(status) {
  139. const map = { 'draft': '草稿', 'pending': '待审核', 'approved': '已发布', 'rejected': '已拒绝' }
  140. return map[status] || status
  141. },
  142. copyCode() {
  143. uni.setClipboardData({ data: this.pkg.shareCode })
  144. uni.showToast({ title: '已复制' })
  145. },
  146. showAddItem() {
  147. this.editingItem = null
  148. this.itemForm = { taskName: '', description: '', points: 10, frequency: 'daily', duration: 30, isRequired: false }
  149. this.$refs.itemPopup.open()
  150. },
  151. editItem(item) {
  152. this.editingItem = item
  153. this.itemForm = { ...item, isRequired: !!item.isRequired }
  154. this.$refs.itemPopup.open()
  155. },
  156. onFreqChange(e) {
  157. this.itemForm.frequency = this.frequencies[e.detail.value]
  158. },
  159. closePopup() {
  160. this.$refs.itemPopup.close()
  161. },
  162. async saveItem() {
  163. if (!this.itemForm.taskName) {
  164. uni.showToast({ title: '请输入任务名称', icon: 'none' })
  165. return
  166. }
  167. try {
  168. const data = { ...this.itemForm, packageId: this.pkgId, isRequired: this.itemForm.isRequired ? 1 : 0 }
  169. if (this.editingItem) {
  170. await updatePackageItem(this.editingItem.id, data)
  171. } else {
  172. await addPackageItem(this.pkgId, data)
  173. }
  174. uni.showToast({ title: '保存成功' })
  175. this.closePopup()
  176. this.loadItems()
  177. } catch (e) {
  178. uni.showToast({ title: '保存失败', icon: 'none' })
  179. }
  180. },
  181. async deleteItem(id) {
  182. uni.showModal({
  183. title: '确认删除',
  184. content: '确定要删除这个任务项吗?',
  185. success: async (res) => {
  186. if (res.confirm) {
  187. try {
  188. await deletePackageItem(id)
  189. uni.showToast({ title: '已删除' })
  190. this.loadItems()
  191. } catch (e) {
  192. uni.showToast({ title: '删除失败', icon: 'none' })
  193. }
  194. }
  195. }
  196. })
  197. }
  198. }
  199. }
  200. </script>
  201. <style scoped>
  202. .container { min-height: 100vh; background: #f5f5f5; }
  203. .package-info { background: #fff; padding: 30rpx; margin-bottom: 20rpx; }
  204. .info-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16rpx; }
  205. .name { font-size: 34rpx; font-weight: bold; color: #333; }
  206. .status { font-size: 24rpx; padding: 8rpx 16rpx; border-radius: 20rpx; background: #f0f0f0; color: #666; }
  207. .info-row { display: flex; gap: 30rpx; font-size: 26rpx; color: #666; margin-bottom: 16rpx; }
  208. .info-desc { font-size: 26rpx; color: #999; margin-bottom: 16rpx; }
  209. .share-code { display: flex; justify-content: space-between; font-size: 26rpx; color: #4CAF50; }
  210. .copy { color: #4CAF50; }
  211. .section { background: #fff; padding: 30rpx; }
  212. .section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20rpx; }
  213. .section-title { font-size: 30rpx; font-weight: bold; }
  214. .btn-add { background: #4CAF50; color: #fff; font-size: 24rpx; padding: 12rpx 24rpx; border-radius: 24rpx; }
  215. .item-list { display: flex; flex-direction: column; gap: 16rpx; }
  216. .item-card { background: #f9f9f9; padding: 20rpx; border-radius: 12rpx; }
  217. .item-header { display: flex; justify-content: space-between; margin-bottom: 12rpx; }
  218. .item-name { font-size: 28rpx; font-weight: bold; }
  219. .item-required { font-size: 22rpx; color: #ff9800; }
  220. .item-info { display: flex; gap: 20rpx; font-size: 24rpx; color: #666; margin-bottom: 12rpx; }
  221. .item-actions { display: flex; gap: 20rpx; justify-content: flex-end; }
  222. .action-edit { color: #4CAF50; font-size: 26rpx; }
  223. .action-delete { color: #f44336; font-size: 26rpx; }
  224. .empty-items { text-align: center; padding: 40rpx; color: #999; }
  225. .popup-content { background: #fff; border-radius: 24rpx 24rpx 0 0; padding: 30rpx; max-height: 80vh; overflow-y: auto; }
  226. .popup-header { display: flex; justify-content: space-between; font-size: 32rpx; font-weight: bold; margin-bottom: 30rpx; }
  227. .close { font-size: 48rpx; color: #999; }
  228. .popup-form .form-item { margin-bottom: 24rpx; }
  229. .popup-form .label { display: block; font-size: 26rpx; color: #333; margin-bottom: 12rpx; }
  230. .popup-form .input, .popup-form .textarea { width: 100%; padding: 16rpx; background: #f5f5f5; border-radius: 8rpx; }
  231. .popup-form .textarea { height: 100rpx; }
  232. .popup-form .picker { padding: 16rpx; background: #f5f5f5; border-radius: 8rpx; }
  233. .btn-save { background: #4CAF50; color: #fff; border-radius: 40rpx; margin-top: 20rpx; }
  234. </style>