product-edit.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. var idx = Number(e.detail.value)
  85. this.typeIndex = idx
  86. this.form.productType = this.typeOptions[idx].value
  87. },
  88. loadProduct(id) {
  89. uni.showLoading({ title: '加载中...' })
  90. productDetail({ id: Number(id) }).then(function(res) {
  91. uni.hideLoading()
  92. if (res.code === 200 && res.data) {
  93. var p = res.data
  94. this.form.name = p.name || ''
  95. this.form.intro = p.intro || ''
  96. this.form.description = p.description || ''
  97. this.form.coverImage = p.coverImage || ''
  98. this.form.price = p.price || 0
  99. this.form.stock = p.stock || 0
  100. this.form.productType = p.productType || 'physical'
  101. // sync picker index
  102. var self = this
  103. var idx = this.typeOptions.findIndex(function(t) { return t.value === self.form.productType })
  104. if (idx >= 0) this.typeIndex = idx
  105. } else {
  106. uni.showToast({ title: res.message || '加载失败', icon: 'none' })
  107. }
  108. }.bind(this)).catch(function() {
  109. uni.hideLoading()
  110. uni.showToast({ title: '加载失败', icon: 'none' })
  111. })
  112. },
  113. onSubmit() {
  114. if (!this.form.name || !this.form.name.trim()) {
  115. uni.showToast({ title: '请输入商品名称', icon: 'none' })
  116. return
  117. }
  118. if (this.form.price === '' || this.form.price === null) {
  119. uni.showToast({ title: '请输入价格', icon: 'none' })
  120. return
  121. }
  122. if (this.form.stock === '' || this.form.stock === null) {
  123. uni.showToast({ title: '请输入库存', icon: 'none' })
  124. return
  125. }
  126. this.submitting = true
  127. var data = {
  128. name: this.form.name.trim(),
  129. productType: this.form.productType,
  130. price: Number(this.form.price),
  131. stock: Number(this.form.stock),
  132. coverImage: this.form.coverImage || '',
  133. intro: this.form.intro || '',
  134. description: this.form.description || ''
  135. }
  136. var apiCall = this.isEdit
  137. ? productUpdate(Object.assign({ id: this.productId }, data))
  138. : productCreate(data)
  139. apiCall.then(function(res) {
  140. this.submitting = false
  141. if (res.code === 200) {
  142. uni.showToast({ title: this.isEdit ? '修改成功' : '发布成功', icon: 'success' })
  143. var _self = this
  144. setTimeout(function() {
  145. uni.navigateBack()
  146. }, 1500)
  147. } else {
  148. uni.showToast({ title: res.message || '操作失败', icon: 'none' })
  149. }
  150. }.bind(this)).catch(function() {
  151. this.submitting = false
  152. uni.showToast({ title: '网络错误', icon: 'none' })
  153. }.bind(this))
  154. }
  155. }
  156. }
  157. </script>
  158. <style scoped>
  159. .container {
  160. min-height: 100vh;
  161. background: #f5f5f5;
  162. padding: 24rpx;
  163. }
  164. .form-section {
  165. background: #fff;
  166. border-radius: 16rpx;
  167. padding: 24rpx;
  168. margin-bottom: 24rpx;
  169. }
  170. .form-item {
  171. margin-bottom: 32rpx;
  172. }
  173. .form-item:last-child {
  174. margin-bottom: 0;
  175. }
  176. .form-label {
  177. display: block;
  178. font-size: 28rpx;
  179. color: #333;
  180. font-weight: 500;
  181. margin-bottom: 12rpx;
  182. }
  183. .form-input {
  184. width: 100%;
  185. box-sizing: border-box;
  186. height: 80rpx;
  187. padding: 0 24rpx;
  188. border: 2rpx solid #e5e5e5;
  189. border-radius: 12rpx;
  190. font-size: 28rpx;
  191. background: #fafafa;
  192. }
  193. .form-textarea {
  194. width: 100%;
  195. box-sizing: border-box;
  196. height: 160rpx;
  197. padding: 16rpx 24rpx;
  198. border: 2rpx solid #e5e5e5;
  199. border-radius: 12rpx;
  200. font-size: 28rpx;
  201. background: #fafafa;
  202. resize: none;
  203. }
  204. .form-hint {
  205. display: block;
  206. font-size: 22rpx;
  207. color: #999;
  208. margin-top: 8rpx;
  209. line-height: 1.5;
  210. }
  211. .form-item switch {
  212. float: right;
  213. margin-top: 6rpx;
  214. }
  215. .form-picker {
  216. height: 80rpx;
  217. padding: 0 24rpx;
  218. border: 2rpx solid #e5e5e5;
  219. border-radius: 12rpx;
  220. background: #fafafa;
  221. display: flex;
  222. align-items: center;
  223. }
  224. .picker-value {
  225. font-size: 28rpx;
  226. color: #333;
  227. }
  228. .cover-preview {
  229. margin-top: 12rpx;
  230. }
  231. .cover-thumb {
  232. width: 200rpx;
  233. height: 160rpx;
  234. border-radius: 8rpx;
  235. background: #f0f0f0;
  236. }
  237. .form-actions {
  238. padding: 0 24rpx;
  239. }
  240. .btn-submit {
  241. width: 100%;
  242. height: 88rpx;
  243. background: #F97316;
  244. color: #fff;
  245. font-size: 32rpx;
  246. font-weight: 700;
  247. border-radius: 44rpx;
  248. display: flex;
  249. align-items: center;
  250. justify-content: center;
  251. border: none;
  252. }
  253. .btn-submit:active {
  254. opacity: 0.85;
  255. }
  256. .btn-submit[disabled] {
  257. background: #ccc;
  258. }
  259. </style>