product-edit.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. mainImages: [],
  76. mainImagesLimit: 5,
  77. form: {},
  78. specs: [{
  79. ...specTpl
  80. }]
  81. }
  82. },
  83. components: {
  84. robinEditor
  85. },
  86. onLoad(option) {
  87. console.log(option)
  88. if (option.id) {
  89. this.$http.get({
  90. url: "/product",
  91. data: {
  92. id: option.id
  93. },
  94. success: (res) => {
  95. this.mainImages = res.data.data.images.split(",")
  96. this.form.name = res.data.data.name
  97. this.form.details = res.data.data.details
  98. this.specs = JSON.parse(res.data.data.specs)
  99. this.form.id = res.data.data.id
  100. }
  101. })
  102. }
  103. },
  104. methods: {
  105. imageUploader(path, callback) {
  106. this.$http.upload({
  107. filePath: path,
  108. name: 'file',
  109. success: (res) => {
  110. console.log(res.data.data)
  111. callback(res.data.data.fullurl)
  112. }
  113. })
  114. },
  115. specImage(index) {
  116. this.chooseImage().then((res) => {
  117. console.log(res)
  118. this.$http.upload({
  119. file: res.tempFiles[0],
  120. filePath: res.tempFilePaths[0],
  121. name: 'file',
  122. success: (res) => {
  123. console.log(res.data.data)
  124. this.$set(this.specs[index], 'image', res.data.data.url)
  125. }
  126. })
  127. })
  128. },
  129. addSpec() {
  130. this.specs.push({
  131. ...specTpl
  132. })
  133. },
  134. addImage() {
  135. this.chooseImage().then((res) => {
  136. console.log(res)
  137. for (let i in res.tempFilePaths) {
  138. this.$http.upload({
  139. file: res.tempFiles[i],
  140. filePath: res.tempFilePaths[i],
  141. name: 'file',
  142. success: (res) => {
  143. console.log(res.data.data)
  144. this.mainImages.push(res.data.data.url)
  145. }
  146. })
  147. }
  148. });
  149. },
  150. replaceImage(index) {
  151. console.log(index)
  152. this.chooseImage().then((res) => {
  153. this.$http.upload({
  154. file: res.tempFiles[0],
  155. filePath: res.tempFilePaths[0],
  156. name: 'file',
  157. success: (res) => {
  158. // this.mainImages[index] = res.data.data.url
  159. this.$set(this.mainImages, index, res.data.data.url)
  160. }
  161. })
  162. });
  163. },
  164. chooseImage() {
  165. return new Promise((resolve, reject) => {
  166. uni.chooseImage({
  167. count: this.mainImagesLimit - this.mainImages.length,
  168. extension: ['jpg', 'png', 'bmp', 'jpeg'],
  169. success(res) {
  170. resolve(res)
  171. },
  172. fail(err) {
  173. reject(err)
  174. }
  175. })
  176. })
  177. },
  178. submit() {
  179. this.form.org_price = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
  180. return Math.min(accumulator, currentValue.org_price)
  181. }, this.specs[0].org_price)
  182. this.form.price = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
  183. return Math.min(accumulator, currentValue.price)
  184. }, this.specs[0].price)
  185. this.form.stock = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
  186. return parseInt(accumulator) + parseInt(currentValue.stock)
  187. }, 0)
  188. this.form.images = this.mainImages.join(",");
  189. this.form.specs = JSON.stringify(this.specs);
  190. console.log(this.form)
  191. for (var i = 0; i < this.specs.length; i++) {
  192. if (!this.specs[i].image) {
  193. uni.showToast({
  194. icon: "none",
  195. title: `请上传颜色${i+1}的图片`
  196. })
  197. return;
  198. }
  199. if (!this.specs[i].name) {
  200. uni.showToast({
  201. icon: "none",
  202. title: `请填写颜色${i+1}的名称`
  203. })
  204. return;
  205. }
  206. if (!this.specs[i].org_price) {
  207. uni.showToast({
  208. icon: "none",
  209. title: `请填写颜色${i+1}的正常售价`
  210. })
  211. return;
  212. }
  213. if (!this.specs[i].price) {
  214. uni.showToast({
  215. icon: "none",
  216. title: `请填写颜色${i+1}的直销价`
  217. })
  218. return;
  219. }
  220. if (!this.specs[i].stock) {
  221. uni.showToast({
  222. icon: "none",
  223. title: `请填写颜色${i+1}的库存`
  224. })
  225. return;
  226. }
  227. }
  228. this.$http.post({
  229. url: "/product/create",
  230. data: this.form,
  231. success: (res) => {
  232. console.log(res)
  233. uni.showModal({
  234. title: "商品发布",
  235. content: "发布成功",
  236. success: (res) => {
  237. if (res.confirm) {
  238. uni.navigateBack()
  239. }
  240. }
  241. })
  242. }
  243. })
  244. }
  245. }
  246. }
  247. </script>
  248. <style lang="scss" scoped>
  249. .block {
  250. background: white;
  251. margin: 20upx;
  252. border-radius: 10upx;
  253. }
  254. .input-item.inline-form {
  255. display: flex;
  256. align-items: center;
  257. height: 80upx;
  258. font-size: 28upx;
  259. padding: 0 20upx;
  260. .label {
  261. width: 160upx;
  262. // flex: 1 1 160upx;
  263. }
  264. .input {
  265. flex-grow: 1;
  266. font-size: 28upx;
  267. }
  268. }
  269. .images-upload {
  270. .label {
  271. margin: 20upx;
  272. padding: 20upx;
  273. font-size: 28upx;
  274. }
  275. .images-wrapper {
  276. padding: 0;
  277. padding-top: 40upx;
  278. display: flex;
  279. flex-wrap: wrap;
  280. // justify-content: space-between;
  281. .image-item {
  282. position: relative;
  283. width: 33.33%;
  284. text-align: center;
  285. margin-bottom: 40upx;
  286. .del {
  287. position: absolute;
  288. top: 0;
  289. right: 42upx;
  290. z-index: 1;
  291. font-size: 24upx;
  292. background-color: $primary-color;
  293. color: white;
  294. padding: 2upx 15upx;
  295. }
  296. }
  297. .image {
  298. width: 150upx;
  299. height: 150upx;
  300. background: #EEEEEE;
  301. flex: 0 0 150upx;
  302. }
  303. .add {
  304. width: 150upx;
  305. height: 150upx;
  306. display: inline-block;
  307. line-height: 140upx;
  308. border: 4upx #EEEEEE dashed;
  309. box-sizing: border-box;
  310. }
  311. }
  312. }
  313. .details {
  314. .label {
  315. height: 80upx;
  316. font-size: 28upx;
  317. padding: 0 40upx;
  318. display: block;
  319. line-height: 80upx;
  320. }
  321. }
  322. .btn {
  323. margin: 0 20upx;
  324. line-height: 80upx;
  325. font-size: 30upx;
  326. color: white;
  327. }
  328. .btn.spec-btn {
  329. background: #007AFF;
  330. }
  331. .btn.submit-btn {
  332. background: $primary-color;
  333. margin: 20upx;
  334. }
  335. .spec.block {
  336. .add {
  337. border: 4upx dashed #eeeeee;
  338. width: 50upx;
  339. height: 50upx;
  340. .image {
  341. width: 50upx;
  342. height: 50upx;
  343. }
  344. }
  345. .remove {
  346. margin-left: 380upx;
  347. color: #007AFF;
  348. }
  349. }
  350. </style>