product-edit.vue 8.4 KB

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