product-edit.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view style="overflow: hidden;">
  3. <view class="block input-item inline-form">
  4. <label class="label" for="">商品标题</label>
  5. <input class="input" type="text" v-model="form.name">
  6. </view>
  7. <view class="images-upload">
  8. <label class="label" for="">商品主图(需要上传5张图片)</label>
  9. <view class="block">
  10. <view class="images-wrapper">
  11. <view class="image-item" v-for="(image,i) in mainImages">
  12. <view class="del" @tap="mainImages.splice(i,1)">删除</view>
  13. <image class="image" :src="image|imagesFilter" mode="aspectFill" @tap="replaceImage(i)"></image>
  14. </view>
  15. <view class="image-item" v-if="mainImagesLimit>mainImages.length">
  16. <view class="add" @tap="addImage">
  17. <uni-icons type="plusempty" size="50" color="#EEEEEE"></uni-icons>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. </view>
  23. <view class="block spec" v-for="(spec,i) in specs">
  24. <view class="input-item inline-form">
  25. <label class="label" for="">颜色{{i+1}}</label>
  26. <view class="add" @tap="specImage(i)">
  27. <image v-if="spec.image" class="image" :src="spec.image|imagesFilter" mode="aspectFill"></image>
  28. <uni-icons v-else type="plusempty" size="24" color="#eeeeee"></uni-icons>
  29. </view>
  30. <view v-if="specs.length>1" class="remove" @tap="specs.splice(i,1)">移除</view>
  31. </view>
  32. <view class="input-item inline-form">
  33. <label class="label" for="">填写颜色</label>
  34. <input class="input" type="text" v-model="spec.name">
  35. </view>
  36. <view class="input-item inline-form">
  37. <label class="label" for="">库存</label>
  38. <input class="input" type="number" v-model="spec.stock">
  39. </view>
  40. <view class="input-item inline-form">
  41. <label class="label" for="">正常售价</label>
  42. <input class="input" type="digit" v-model="spec.org_price">
  43. </view>
  44. <view class="input-item inline-form">
  45. <label class="label" for="">直销价</label>
  46. <input class="input" type="digit" v-model="spec.price">
  47. </view>
  48. </view>
  49. <view class="">
  50. <button class="btn spec-btn" type="default" @tap="addSpec()">新增颜色分类</button>
  51. </view>
  52. <view class="details">
  53. <label class="label" for="">商品详情</label>
  54. <view class="" style="position: relative;background: white;margin: 0 20upx;">
  55. <robin-editor :imageUploader="imageUploader" v-model="form.details"></robin-editor>
  56. </view>
  57. </view>
  58. <view class="">
  59. <button class="btn submit-btn" type="default" @tap="submit()">立即发布</button>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. import robinEditor from '@/uni_modules/robin-editor/components/robin-editor/robin-editor'
  65. const specTpl = {
  66. image: '',
  67. name: '',
  68. stock: undefined,
  69. org_price: undefined,
  70. price: undefined
  71. }
  72. export default {
  73. data() {
  74. return {
  75. html: "333",
  76. mainImages: [],
  77. mainImagesLimit: 5,
  78. form: {},
  79. specs: [{
  80. ...specTpl
  81. }]
  82. }
  83. },
  84. components: {
  85. robinEditor
  86. },
  87. methods: {
  88. imageUploader(path, callback) {
  89. this.$http.upload({
  90. filePath: path,
  91. name: 'file',
  92. success: (res) => {
  93. console.log(res.data.data)
  94. callback(res.data.data.fullurl)
  95. }
  96. })
  97. },
  98. specImage(index) {
  99. this.chooseImage().then((res) => {
  100. console.log(res)
  101. this.$http.upload({
  102. file: res.tempFiles[0],
  103. filePath: res.tempFilePaths[0],
  104. name: 'file',
  105. success: (res) => {
  106. console.log(res.data.data)
  107. this.$set(this.specs[index], 'image', res.data.data.url)
  108. }
  109. })
  110. })
  111. },
  112. addSpec() {
  113. this.specs.push({
  114. ...specTpl
  115. })
  116. },
  117. addImage() {
  118. this.chooseImage().then((res) => {
  119. console.log(res)
  120. for (let i in res.tempFilePaths) {
  121. this.$http.upload({
  122. file: res.tempFiles[i],
  123. filePath: res.tempFilePaths[i],
  124. name: 'file',
  125. success: (res) => {
  126. console.log(res.data.data)
  127. this.mainImages.push(res.data.data.url)
  128. }
  129. })
  130. }
  131. });
  132. },
  133. replaceImage(index) {
  134. console.log(index)
  135. this.chooseImage().then((res) => {
  136. this.$http.upload({
  137. file: res.tempFiles[0],
  138. filePath: res.tempFilePaths[0],
  139. name: 'file',
  140. success: (res) => {
  141. // this.mainImages[index] = res.data.data.url
  142. this.$set(this.mainImages, index, res.data.data.url)
  143. }
  144. })
  145. });
  146. },
  147. chooseImage() {
  148. return new Promise((resolve, reject) => {
  149. uni.chooseImage({
  150. count: this.mainImagesLimit - this.mainImages.length,
  151. extension: ['jpg', 'png', 'bmp', 'jpeg'],
  152. success(res) {
  153. resolve(res)
  154. },
  155. fail(err) {
  156. reject(err)
  157. }
  158. })
  159. })
  160. },
  161. submit() {
  162. this.form.org_price = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
  163. return Math.min(accumulator, currentValue.org_price)
  164. }, this.specs[0].org_price)
  165. this.form.price = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
  166. return Math.min(accumulator, currentValue.price)
  167. }, this.specs[0].price)
  168. this.form.stock = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
  169. return parseInt(accumulator) + parseInt(currentValue.stock)
  170. }, 0)
  171. this.form.images = this.mainImages.join(",");
  172. this.form.specs = JSON.stringify(this.specs);
  173. console.log(this.form)
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped>
  179. .block {
  180. background: white;
  181. margin: 20upx;
  182. border-radius: 10upx;
  183. }
  184. .input-item.inline-form {
  185. display: flex;
  186. align-items: center;
  187. height: 80upx;
  188. font-size: 28upx;
  189. padding: 0 20upx;
  190. .label {
  191. width: 160upx;
  192. // flex: 1 1 160upx;
  193. }
  194. .input {
  195. flex-grow: 1;
  196. }
  197. }
  198. .images-upload {
  199. .label {
  200. margin: 20upx;
  201. padding: 20upx;
  202. font-size: 28upx;
  203. }
  204. .images-wrapper {
  205. padding: 0;
  206. padding-top: 40upx;
  207. display: flex;
  208. flex-wrap: wrap;
  209. // justify-content: space-between;
  210. .image-item {
  211. position: relative;
  212. width: 33.33%;
  213. text-align: center;
  214. margin-bottom: 40upx;
  215. .del {
  216. position: absolute;
  217. top: 0;
  218. right: 42upx;
  219. z-index: 1;
  220. font-size: 24upx;
  221. background-color: $primary-color;
  222. color: white;
  223. padding: 2upx 15upx;
  224. }
  225. }
  226. .image {
  227. width: 150upx;
  228. height: 150upx;
  229. background: #EEEEEE;
  230. flex: 0 0 150upx;
  231. }
  232. .add {
  233. width: 150upx;
  234. height: 150upx;
  235. display: inline-block;
  236. line-height: 140upx;
  237. border: 4upx #EEEEEE dashed;
  238. box-sizing: border-box;
  239. }
  240. }
  241. }
  242. .details {
  243. .label {
  244. height: 80upx;
  245. font-size: 28upx;
  246. padding: 0 40upx;
  247. display: block;
  248. line-height: 80upx;
  249. }
  250. }
  251. .btn {
  252. margin: 0 20upx;
  253. line-height: 80upx;
  254. font-size: 30upx;
  255. color: white;
  256. }
  257. .btn.spec-btn {
  258. background: #007AFF;
  259. }
  260. .btn.submit-btn {
  261. background: $primary-color;
  262. margin: 20upx;
  263. }
  264. .spec.block {
  265. .add {
  266. border: 4upx dashed #eeeeee;
  267. width: 50upx;
  268. height: 50upx;
  269. .image {
  270. width: 50upx;
  271. height: 50upx;
  272. }
  273. }
  274. .remove {
  275. margin-left: 380upx;
  276. color: #007AFF;
  277. }
  278. }
  279. </style>