product-edit.vue 6.7 KB

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