product-edit.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="container">
  3. <view class="form-section">
  4. <view class="form-item">
  5. <text class="form-label">商品名称 *</text>
  6. <input class="form-input" v-model="form.name" placeholder="请输入商品名称" />
  7. </view>
  8. <view class="form-item">
  9. <text class="form-label">商品类型 *</text>
  10. <picker class="form-picker" :value="typeIndex" :range="typeOptions" range-key="label" @change="onTypeChange">
  11. <view class="picker-value">{{ typeOptions[typeIndex].label }}</view>
  12. </picker>
  13. </view>
  14. <view class="form-item">
  15. <text class="form-label">价格(分)*</text>
  16. <input class="form-input" type="number" v-model="form.price" placeholder="价格,单位:分(0=免费)" />
  17. </view>
  18. <view class="form-item">
  19. <text class="form-label">库存 *</text>
  20. <input class="form-input" type="number" v-model="form.stock" placeholder="库存数量" />
  21. </view>
  22. <view class="form-item">
  23. <text class="form-label">封面图URL</text>
  24. <input class="form-input" v-model="form.coverImage" placeholder="图片地址,留空使用默认图" />
  25. <view v-if="form.coverImage" class="cover-preview">
  26. <image :src="form.coverImage" mode="aspectFill" class="cover-thumb" />
  27. </view>
  28. </view>
  29. <view class="form-item">
  30. <text class="form-label">商品简介</text>
  31. <textarea class="form-textarea" v-model="form.intro" placeholder="商品简介(可选,简短介绍)" />
  32. </view>
  33. <view class="form-item">
  34. <text class="form-label">商品描述</text>
  35. <textarea class="form-textarea" v-model="form.description" placeholder="商品描述(可选,详细内容)" />
  36. </view>
  37. </view>
  38. <view class="form-actions">
  39. <button class="btn-submit" :disabled="submitting" @click="onSubmit">
  40. {{ submitting ? '提交中...' : (isEdit ? '保存修改' : '立即发布') }}
  41. </button>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. import { productCreate, productUpdate, productDetail } from '@/utils/api.js'
  47. export default {
  48. data() {
  49. return {
  50. isEdit: false,
  51. productId: null,
  52. submitting: false,
  53. typeIndex: 0,
  54. typeOptions: [
  55. { label: '实物商品', value: 'physical' },
  56. { label: '数字商品', value: 'digital' },
  57. { label: '服务', value: 'service' },
  58. { label: '课程', value: 'course' },
  59. { label: '活动', value: 'activity' }
  60. ],
  61. form: {
  62. name: '',
  63. productType: 'physical',
  64. price: 0,
  65. stock: 0,
  66. coverImage: '',
  67. intro: '',
  68. description: ''
  69. }
  70. }
  71. },
  72. onLoad(options) {
  73. if (options.id) {
  74. this.isEdit = true
  75. this.productId = options.id
  76. uni.setNavigationBarTitle({ title: '编辑商品' })
  77. this.loadProduct(options.id)
  78. } else {
  79. uni.setNavigationBarTitle({ title: '发布商品' })
  80. }
  81. },
  82. methods: {
  83. onTypeChange(e) {
  84. this.typeIndex = e.detail.value
  85. this.form.productType = this.typeOptions[e.detail.value].value
  86. },
  87. loadProduct(id) {
  88. uni.showLoading({ title: '加载中...' })
  89. productDetail({ id: Number(id) }).then(res => {
  90. uni.hideLoading()
  91. if (res.code === 200 && res.data) {
  92. const p = res.data
  93. this.form.name = p.name || ''
  94. this.form.intro = p.intro || ''
  95. this.form.description = p.description || ''
  96. this.form.coverImage = p.coverImage || ''
  97. this.form.price = p.price || 0
  98. this.form.stock = p.stock || 0
  99. this.form.productType = p.productType || 'physical'
  100. // sync picker index
  101. var idx = this.typeOptions.findIndex(function(t) { return t.value === this.form.productType }, this)
  102. if (idx >= 0) this.typeIndex = idx
  103. } else {
  104. uni.showToast({ title: res.message || '加载失败', icon: 'none' })
  105. }
  106. }).catch(function() {
  107. uni.hideLoading()
  108. uni.showToast({ title: '加载失败', icon: 'none' })
  109. })
  110. },
  111. onSubmit() {
  112. if (!this.form.name || !this.form.name.trim()) {
  113. uni.showToast({ title: '请输入商品名称', icon: 'none' })
  114. return
  115. }
  116. if (this.form.price === '' || this.form.price === null) {
  117. uni.showToast({ title: '请输入价格', icon: 'none' })
  118. return
  119. }
  120. if (this.form.stock === '' || this.form.stock === null) {
  121. uni.showToast({ title: '请输入库存', icon: 'none' })
  122. return
  123. }
  124. this.submitting = true
  125. var data = {
  126. name: this.form.name.trim(),
  127. productType: this.form.productType,
  128. price: Number(this.form.price),
  129. stock: Number(this.form.stock),
  130. coverImage: this.form.coverImage || '',
  131. intro: this.form.intro || '',
  132. description: this.form.description || ''
  133. }
  134. var apiCall = this.isEdit
  135. ? productUpdate(Object.assign({ id: this.productId }, data))
  136. : productCreate(data)
  137. apiCall.then(function(res) {
  138. this.submitting = false
  139. if (res.code === 200) {
  140. uni.showToast({ title: this.isEdit ? '修改成功' : '发布成功', icon: 'success' })
  141. var _self = this
  142. setTimeout(function() {
  143. uni.navigateBack()
  144. }, 1500)
  145. } else {
  146. uni.showToast({ title: res.message || '操作失败', icon: 'none' })
  147. }
  148. }.bind(this)).catch(function() {
  149. this.submitting = false
  150. uni.showToast({ title: '网络错误', icon: 'none' })
  151. }.bind(this))
  152. }
  153. }
  154. }
  155. </script>
  156. <style scoped>
  157. .container {
  158. min-height: 100vh;
  159. background: #f5f5f5;
  160. padding: 24rpx;
  161. }
  162. .form-section {
  163. background: #fff;
  164. border-radius: 16rpx;
  165. padding: 24rpx;
  166. margin-bottom: 24rpx;
  167. }
  168. .form-item {
  169. margin-bottom: 32rpx;
  170. }
  171. .form-item:last-child {
  172. margin-bottom: 0;
  173. }
  174. .form-label {
  175. display: block;
  176. font-size: 28rpx;
  177. color: #333;
  178. font-weight: 500;
  179. margin-bottom: 12rpx;
  180. }
  181. .form-input {
  182. width: 100%;
  183. box-sizing: border-box;
  184. height: 80rpx;
  185. padding: 0 24rpx;
  186. border: 2rpx solid #e5e5e5;
  187. border-radius: 12rpx;
  188. font-size: 28rpx;
  189. background: #fafafa;
  190. }
  191. .form-textarea {
  192. width: 100%;
  193. box-sizing: border-box;
  194. height: 160rpx;
  195. padding: 16rpx 24rpx;
  196. border: 2rpx solid #e5e5e5;
  197. border-radius: 12rpx;
  198. font-size: 28rpx;
  199. background: #fafafa;
  200. resize: none;
  201. }
  202. .form-picker {
  203. height: 80rpx;
  204. padding: 0 24rpx;
  205. border: 2rpx solid #e5e5e5;
  206. border-radius: 12rpx;
  207. background: #fafafa;
  208. display: flex;
  209. align-items: center;
  210. }
  211. .picker-value {
  212. font-size: 28rpx;
  213. color: #333;
  214. }
  215. .cover-preview {
  216. margin-top: 12rpx;
  217. }
  218. .cover-thumb {
  219. width: 200rpx;
  220. height: 160rpx;
  221. border-radius: 8rpx;
  222. background: #f0f0f0;
  223. }
  224. .form-actions {
  225. padding: 0 24rpx;
  226. }
  227. .btn-submit {
  228. width: 100%;
  229. height: 88rpx;
  230. background: #F97316;
  231. color: #fff;
  232. font-size: 32rpx;
  233. font-weight: 700;
  234. border-radius: 44rpx;
  235. display: flex;
  236. align-items: center;
  237. justify-content: center;
  238. border: none;
  239. }
  240. .btn-submit:active {
  241. opacity: 0.85;
  242. }
  243. .btn-submit[disabled] {
  244. background: #ccc;
  245. }
  246. </style>