123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- <template>
- <view style="overflow: hidden;">
- <view class="block input-item inline-form">
- <label class="label" for="">商品标题</label>
- <input class="input" type="text" v-model="form.name">
- </view>
- <view class="images-upload">
- <label class="label" for="">商品主图(需要上传5张图片)</label>
- <view class="block">
- <view class="images-wrapper">
- <view class="image-item" v-for="(image,i) in mainImages">
- <view class="del" @tap="mainImages.splice(i,1)">删除</view>
- <image class="image" :src="image|imagesFilter" mode="aspectFill" @tap="replaceImage(i)"></image>
- </view>
- <view class="image-item" v-if="mainImagesLimit>mainImages.length">
- <view class="add" @tap="addImage">
- <uni-icons type="plusempty" size="50" color="#EEEEEE"></uni-icons>
- </view>
- </view>
- </view>
- </view>
- </view>
- <view class="block spec" v-for="(spec,i) in specs">
- <view class="input-item inline-form">
- <label class="label" for="">颜色{{i+1}}</label>
- <view class="add" @tap="specImage(i)">
- <image v-if="spec.image" class="image" :src="spec.image|imagesFilter" mode="aspectFill"></image>
- <uni-icons v-else type="plusempty" size="24" color="#eeeeee"></uni-icons>
- </view>
- <view v-if="specs.length>1" class="remove" @tap="specs.splice(i,1)">移除</view>
- </view>
- <view class="input-item inline-form">
- <label class="label" for="">填写颜色</label>
- <input class="input" type="text" v-model="spec.name">
- </view>
- <view class="input-item inline-form">
- <label class="label" for="">库存</label>
- <input class="input" type="number" v-model="spec.stock">
- </view>
- <view class="input-item inline-form">
- <label class="label" for="">正常售价</label>
- <input class="input" type="digit" v-model="spec.org_price">
- </view>
- <view class="input-item inline-form">
- <label class="label" for="">直销价</label>
- <input class="input" type="digit" v-model="spec.price">
- </view>
- </view>
- <view class="">
- <button class="btn spec-btn" type="default" @tap="addSpec()">新增颜色分类</button>
- </view>
- <view class="details">
- <label class="label" for="">商品详情</label>
- <view class="" style="position: relative;background: white;margin: 0 20upx;">
- <robin-editor :imageUploader="imageUploader" v-model="form.details"></robin-editor>
- </view>
- </view>
- <view class="">
- <button class="btn submit-btn" type="default" @tap="submit()">立即发布</button>
- </view>
- </view>
- </template>
- <script>
- import robinEditor from '@/uni_modules/robin-editor/components/robin-editor/robin-editor'
- const specTpl = {
- image: '',
- name: '',
- stock: undefined,
- org_price: undefined,
- price: undefined
- }
- export default {
- data() {
- return {
- mainImages: [],
- mainImagesLimit: 5,
- form: {},
- specs: [{
- ...specTpl
- }]
- }
- },
- components: {
- robinEditor
- },
- onLoad(option) {
- console.log(option)
- if (option.id) {
- this.$http.get({
- url: "/product",
- data: {
- id: option.id
- },
- success: (res) => {
- this.mainImages = res.data.data.images.split(",")
- this.form.name = res.data.data.name
- this.form.details = res.data.data.details
- this.specs = JSON.parse(res.data.data.specs)
- this.form.id = res.data.data.id
- }
- })
- }
- },
- methods: {
- imageUploader(path, callback) {
- this.$http.upload({
- filePath: path,
- name: 'file',
- success: (res) => {
- console.log(res.data.data)
- callback(res.data.data.fullurl)
- }
- })
- },
- specImage(index) {
- this.chooseImage().then((res) => {
- console.log(res)
- this.$http.upload({
- file: res.tempFiles[0],
- filePath: res.tempFilePaths[0],
- name: 'file',
- success: (res) => {
- console.log(res.data.data)
- this.$set(this.specs[index], 'image', res.data.data.url)
- }
- })
- })
- },
- addSpec() {
- this.specs.push({
- ...specTpl
- })
- },
- addImage() {
- this.chooseImage().then((res) => {
- console.log(res)
- for (let i in res.tempFilePaths) {
- this.$http.upload({
- file: res.tempFiles[i],
- filePath: res.tempFilePaths[i],
- name: 'file',
- success: (res) => {
- console.log(res.data.data)
- this.mainImages.push(res.data.data.url)
- }
- })
- }
- });
- },
- replaceImage(index) {
- console.log(index)
- this.chooseImage().then((res) => {
- this.$http.upload({
- file: res.tempFiles[0],
- filePath: res.tempFilePaths[0],
- name: 'file',
- success: (res) => {
- // this.mainImages[index] = res.data.data.url
- this.$set(this.mainImages, index, res.data.data.url)
- }
- })
- });
- },
- chooseImage() {
- return new Promise((resolve, reject) => {
- uni.chooseImage({
- count: this.mainImagesLimit - this.mainImages.length,
- extension: ['jpg', 'png', 'bmp', 'jpeg'],
- success(res) {
- resolve(res)
- },
- fail(err) {
- reject(err)
- }
- })
- })
- },
- submit() {
- this.form.org_price = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
- return Math.min(accumulator, currentValue.org_price)
- }, this.specs[0].org_price)
- this.form.price = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
- return Math.min(accumulator, currentValue.price)
- }, this.specs[0].price)
- this.form.stock = this.specs.reduce((accumulator, currentValue, currentIndex, array) => {
- return parseInt(accumulator) + parseInt(currentValue.stock)
- }, 0)
- this.form.images = this.mainImages.join(",");
- this.form.specs = JSON.stringify(this.specs);
- console.log(this.form)
- for (var i = 0; i < this.specs.length; i++) {
- if (!this.specs[i].image) {
- uni.showToast({
- icon: "none",
- title: `请上传颜色${i+1}的图片`
- })
- return;
- }
- if (!this.specs[i].name) {
- uni.showToast({
- icon: "none",
- title: `请填写颜色${i+1}的名称`
- })
- return;
- }
- if (!this.specs[i].org_price) {
- uni.showToast({
- icon: "none",
- title: `请填写颜色${i+1}的正常售价`
- })
- return;
- }
- if (!this.specs[i].price) {
- uni.showToast({
- icon: "none",
- title: `请填写颜色${i+1}的直销价`
- })
- return;
- }
- if (!this.specs[i].stock) {
- uni.showToast({
- icon: "none",
- title: `请填写颜色${i+1}的库存`
- })
- return;
- }
- }
- this.$http.post({
- url: "/product/create",
- data: this.form,
- success: (res) => {
- console.log(res)
- uni.showModal({
- title: "商品发布",
- content: "发布成功",
- success: (res) => {
- if (res.confirm) {
- uni.navigateBack()
- }
- }
- })
- }
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .block {
- background: white;
- margin: 20upx;
- border-radius: 10upx;
- }
- .input-item.inline-form {
- display: flex;
- align-items: center;
- height: 80upx;
- font-size: 28upx;
- padding: 0 20upx;
- .label {
- width: 160upx;
- // flex: 1 1 160upx;
- }
- .input {
- flex-grow: 1;
- font-size: 28upx;
- }
- }
- .images-upload {
- .label {
- margin: 20upx;
- padding: 20upx;
- font-size: 28upx;
- }
- .images-wrapper {
- padding: 0;
- padding-top: 40upx;
- display: flex;
- flex-wrap: wrap;
- // justify-content: space-between;
- .image-item {
- position: relative;
- width: 33.33%;
- text-align: center;
- margin-bottom: 40upx;
- .del {
- position: absolute;
- top: 0;
- right: 42upx;
- z-index: 1;
- font-size: 24upx;
- background-color: $primary-color;
- color: white;
- padding: 2upx 15upx;
- }
- }
- .image {
- width: 150upx;
- height: 150upx;
- background: #EEEEEE;
- flex: 0 0 150upx;
- }
- .add {
- width: 150upx;
- height: 150upx;
- display: inline-block;
- line-height: 140upx;
- border: 4upx #EEEEEE dashed;
- box-sizing: border-box;
- }
- }
- }
- .details {
- .label {
- height: 80upx;
- font-size: 28upx;
- padding: 0 40upx;
- display: block;
- line-height: 80upx;
- }
- }
- .btn {
- margin: 0 20upx;
- line-height: 80upx;
- font-size: 30upx;
- color: white;
- }
- .btn.spec-btn {
- background: #007AFF;
- }
- .btn.submit-btn {
- background: $primary-color;
- margin: 20upx;
- }
- .spec.block {
- .add {
- border: 4upx dashed #eeeeee;
- width: 50upx;
- height: 50upx;
- .image {
- width: 50upx;
- height: 50upx;
- }
- }
- .remove {
- margin-left: 380upx;
- color: #007AFF;
- }
- }
- </style>
|