diff --git a/src/pages/plan/product/add.js b/src/pages/plan/product/add.js index 01d18248..f889944b 100644 --- a/src/pages/plan/product/add.js +++ b/src/pages/plan/product/add.js @@ -1,10 +1,10 @@ /* 新增商品 */ -import ReactDOM from "react-dom"; -import React from "react"; -import "./add.less"; -import Ipt from "../../../components/input/main"; -import Form from "../../../components/form/main"; -import FormItem from "../../../components/form-item/main"; +import ReactDOM from "react-dom" +import React from "react" +import "./add.less" +import Ipt from "../../../components/input/main" +import Form from "../../../components/form/main" +import FormItem from "../../../components/form-item/main" import { Select, Button, @@ -13,28 +13,28 @@ import { Sweetalert, RadioButton, RadioGroup, - Notify, -} from "zent"; -import Bus from "../../../assets/eventBus.js"; -import { subNum } from "@/tools/number"; + Notify +} from "zent" +import Bus from "../../../assets/eventBus.js" +import { subNum } from "@/tools/number" import { getProductInfoSelect, handelResponse, - uploadImg, -} from "../../../assets/api.js"; -import _ from "lodash"; -import Swiper from "swiper/dist/js/swiper.js"; -import "swiper/dist/css/swiper.min.css"; -var timer = null; -var mySwiper = 0; + uploadImg +} from "../../../assets/api.js" +import _ from "lodash" +import Swiper from "swiper/dist/js/swiper.js" +import "swiper/dist/css/swiper.min.css" +var timer = null +var mySwiper = 0 export default class adduserinfo extends React.Component { constructor(props) { - super(props); + super(props) this.state = { model: { //数据模型不可少 product_id: "", - product_type: "1", + product_type: 1, contract_price: "", official_price: "", cost_price: "", @@ -46,7 +46,7 @@ export default class adduserinfo extends React.Component { map_product_name: "", detail_url: "", account_type: "", - type: 1, + type: 1 }, form_data: {}, cur_product: {}, @@ -55,8 +55,8 @@ export default class adduserinfo extends React.Component { show_url: "", describe_url: "", loadshow: false, - productpic: [], - }; + productpic: [] + } } componentDidMount() { @@ -69,433 +69,432 @@ export default class adduserinfo extends React.Component { observer: true, // 修改swiper自己或子元素时,自动初始化swiper observeParents: false, // 修改swiper的父元素时,自动初始化swiper pagination: ".swiper-pagination", - paginationClickable: true, - }); + paginationClickable: true + }) } componentWillMount(e) { if (this.props.data) { - console.log("商品数据 =>", this.props.data); - let model_temp = this.props.data; - let model = this.state.model; - model.product_id = model_temp.product_id; - model.product_type = Number(model_temp.product_type); - model.contract_price = model_temp.contract_price; - model.official_price = model_temp.official_price; - model.cost_price = model_temp.cost_price; - model.quantity = model_temp.quantity; - model.product_name = model_temp.product_name; - model.show_url = model_temp.show_url; - model.weight = String(model_temp.weight); - model.detail_url = model_temp.detail_url; - model.account_type = model_temp.account_type; - model.code_batch_id = model_temp.code_batch_id; - let defaultPic = []; - let temp_productpic = this.state.productpic; - let pics = ""; + console.log("商品数据 =>", this.props.data) + let model_temp = this.props.data + let model = this.state.model + model.product_id = model_temp.product_id + model.product_type = Number(model_temp.product_type) + model.contract_price = model_temp.contract_price + model.official_price = model_temp.official_price + model.cost_price = model_temp.cost_price + model.quantity = model_temp.quantity + model.product_name = model_temp.product_name + model.show_url = model_temp.show_url + model.weight = String(model_temp.weight) + model.detail_url = model_temp.detail_url + model.account_type = model_temp.account_type + model.code_batch_id = model_temp.code_batch_id + let defaultPic = [] + let temp_productpic = this.state.productpic + let pics = "" if (Array.isArray(model_temp.describe_url)) { - pics = model_temp.describe_url; + pics = model_temp.describe_url } else { if ( model_temp.describe_url != "" && model_temp.describe_url != undefined ) { - pics = model_temp.describe_url.split(","); + pics = model_temp.describe_url.split(",") } else { - pics = []; + pics = [] } } for (let i = 0; i < pics.length; i++) { - let obj = {}; - let nameArr = pics[i].split("/"); - obj.name = nameArr[5] + i || nameArr[2] + i; - obj.src = pics[i]; - defaultPic.push(obj); + let obj = {} + let nameArr = pics[i].split("/") + obj.name = nameArr[5] + i || nameArr[2] + i + obj.src = pics[i] + defaultPic.push(obj) let obj2 = { name: nameArr[5] + i || nameArr[2] + i, - url: pics[i], - }; - temp_productpic.push(obj2); + url: pics[i] + } + temp_productpic.push(obj2) } - this.setState({ temp_productpic }); - model.describe_url = defaultPic == [] ? "" : defaultPic; - model.stock = model_temp.stock; - model.usage = model_temp.usage; - model.map_product_name = model_temp.map_product_name; - model.account_type = model_temp.account_type; - this.setState({ model: model }); + this.setState({ temp_productpic }) + model.describe_url = defaultPic == [] ? "" : defaultPic + model.stock = model_temp.stock + model.usage = model_temp.usage + model.map_product_name = model_temp.map_product_name + model.account_type = model_temp.account_type + this.setState({ model: model }) this.setState({ - show_url: [{ src: model_temp.show_url, id: new Date().getTime() }], - }); + show_url: [{ src: model_temp.show_url, id: new Date().getTime() }] + }) } - this.getProductInfo(); + this.getProductInfo() } componentWillReceiveProps() { - mySwiper.update(); + mySwiper.update() } //获取直连天下商品映射信息 getProductInfo() { - let productList = sessionStorage.getItem("productsList"); - productList = JSON.parse(productList); - let product_data = []; + let productList = sessionStorage.getItem("productsList") + productList = JSON.parse(productList) + let product_data = [] if (sessionStorage.getItem("productData")) { - product_data = JSON.parse(sessionStorage.getItem("productData")); + product_data = JSON.parse(sessionStorage.getItem("productData")) } - let checked_product = product_data ? product_data : []; - let arr = []; - this.setState({ products: productList }); + let checked_product = product_data ? product_data : [] + let arr = [] + this.setState({ products: productList }) _.map(productList, (res) => { - let obj = {}; - obj.key = res.id; - obj.text = res.title; + let obj = {} + obj.key = res.id + obj.text = res.title obj.disabled = checked_product.findIndex((item) => { - return item.product_id == res.id; - }) > -1; + return item.product_id == res.id + }) > -1 if (obj.disabled) { } - arr.push(obj); - return obj; - }); + arr.push(obj) + return obj + }) - this.setState({ productOption: arr }); + this.setState({ productOption: arr }) if (this.state.model.product_id) { let cur_product = arr.find((item) => { - return item.key == this.state.model.product_id; - }); - this.setState({ cur_product: cur_product }); + return item.key == this.state.model.product_id + }) + this.setState({ cur_product: cur_product }) } } async submit() { if (this.refs.form1.validator()) { if (this.props.data) { - let temp = []; + let temp = [] if (sessionStorage.getItem("productData")) { - temp = JSON.parse(sessionStorage.getItem("productData")); + temp = JSON.parse(sessionStorage.getItem("productData")) let index = temp.findIndex((item) => { - return item.product_id == this.props.data.product_id; - }); + return item.product_id == this.props.data.product_id + }) - temp[index].product_name = this.state.model.product_name; - temp[index].weight = this.state.model.weight; - temp[index].account_type = this.state.model.account_type; - temp[index].product_id = this.state.model.product_id; - temp[index].product_type = this.state.model.product_type; - temp[index].contract_price = this.state.model.contract_price; - temp[index].official_price = this.state.model.official_price; - temp[index].cost_price = this.state.model.cost_price; - temp[index].quantity = this.state.model.quantity; - temp[index].stock = this.state.model.quantity; - temp[index].usage = this.state.model.usage; - temp[index].show_url = this.state.model.show_url; - temp[index].detail_url = this.state.model.detail_url; + temp[index].product_name = this.state.model.product_name + temp[index].weight = this.state.model.weight + temp[index].account_type = this.state.model.account_type + temp[index].product_id = this.state.model.product_id + temp[index].product_type = this.state.model.product_type + temp[index].contract_price = this.state.model.contract_price + temp[index].official_price = this.state.model.official_price + temp[index].cost_price = this.state.model.cost_price + temp[index].quantity = this.state.model.quantity + temp[index].stock = this.state.model.quantity + temp[index].usage = this.state.model.usage + temp[index].show_url = this.state.model.show_url + temp[index].detail_url = this.state.model.detail_url let urls = _.map(this.state.productpic, (o) => { - return o.url; - }); + return o.url + }) if (urls.length <= 0) { - Notify.error(`商品图至少请上传一张`); - return; + Notify.error(`商品图至少请上传一张`) + return } - temp[index].describe_url = urls; - temp[index].map_product_name = this.state.model.map_product_name; - this.setState({ form_data: temp[index] }); - sessionStorage.setItem("productData", JSON.stringify(temp)); - return true; + temp[index].describe_url = urls + temp[index].map_product_name = this.state.model.map_product_name + this.setState({ form_data: temp[index] }) + sessionStorage.setItem("productData", JSON.stringify(temp)) + return true } } else { - let model = this.state.model; + let model = this.state.model let urls = _.map(this.state.productpic, (o) => { - return o.url; - }); + return o.url + }) if (urls.length <= 0) { - Notify.error(`商品图至少请上传一张`); - return; + Notify.error(`商品图至少请上传一张`) + return } - model.describe_url = urls; - this.setState({ model: model }); + model.describe_url = urls + this.setState({ model: model }) if (this.state.model.show_url == "") { - Notify.error(`请上传商品Logo`); - return; + Notify.error(`请上传商品Logo`) + return } - let temp = []; + let temp = [] if (sessionStorage.getItem("productData")) { - temp = JSON.parse(sessionStorage.getItem("productData")); + temp = JSON.parse(sessionStorage.getItem("productData")) } - let data = temp ? temp : []; - data.push(this.state.model); - sessionStorage.setItem("productData", JSON.stringify(data)); - this.setState({ form_data: model }); - return true; + let data = temp ? temp : [] + data.push(this.state.model) + sessionStorage.setItem("productData", JSON.stringify(data)) + this.setState({ form_data: model }) + return true } } } onProductChange(e) { - console.log("e ==>", e); - this.setState({ cur_product: e }); + console.log("e ==>", e) + this.setState({ cur_product: e }) let cur_product = this.state.products.find((item) => { - return item.id == e.key; - }); + return item.id == e.key + }) let picItem = window.goods.find((item) => { - return item.id == cur_product.product_category_id; - }); - console.log("picItem ==>", picItem); - let model2 = this.state.model; - model2.product_id = cur_product.id; - model2.product_type = cur_product.type; - model2.contract_price = this.state.model.contract_price; - model2.official_price = cur_product.official_price; - model2.cost_price = cur_product.cost_price; - model2.quantity = this.state.model.quantity; - model2.product_name = cur_product.title; - model2.account_type = cur_product.account_type; - model2.show_url = picItem ? picItem.pic : ""; - const obj1 = []; - const obj2 = []; + return item.id == cur_product.product_category_id + }) + let model2 = this.state.model + model2.product_id = cur_product.id + model2.product_type = Number(cur_product.type) + model2.contract_price = this.state.model.contract_price + model2.official_price = cur_product.official_price + model2.cost_price = cur_product.cost_price + model2.quantity = this.state.model.quantity + model2.product_name = cur_product.title + model2.account_type = cur_product.account_type + model2.show_url = picItem ? picItem.pic : "" + const obj1 = [] + const obj2 = [] const productItem = picItem?.products.find( (item) => item.id == cur_product.id - ); + ) productItem?.pic.map((item, index) => { obj1.push({ src: item, id: item + index, - name: productItem.title + index, - }); + name: productItem.title + index + }) obj2.push({ url: item, id: item + index, - name: productItem.title + index, - }); - }); - model2.describe_url = obj1; - model2.map_product_name = cur_product.title; - model2.detail_url = ""; + name: productItem.title + index + }) + }) + model2.describe_url = obj1 + model2.map_product_name = cur_product.title + model2.detail_url = "" this.setState({ model: model2, show_url: picItem ? [{ src: picItem.pic, id: new Date().getTime() }] : [], - productpic: obj2, - }); + productpic: obj2 + }) } onTypeChange(e) { - let model2 = this.state.model; - model2.product_type = e.target.value; - this.setState({ model: model2 }); + let model2 = this.state.model + model2.product_type = e.target.value + this.setState({ model: model2 }) } onUpload = (file, report) => { - return new Promise((resolve, reject) => {}); - }; + return new Promise((resolve, reject) => {}) + } onUploadChange(files) { - console.log("files -1", files); + console.log("files -1", files) if (files.length > 0) { - let formdata = new FormData(); - formdata.append("file", files[0].file); - formdata.append("path", "common_image"); + let formdata = new FormData() + formdata.append("file", files[0].file) + formdata.append("path", "common_image") uploadImg(formdata).then((res) => { handelResponse( res, (req, msg) => { - let path = req.path; - let model = this.state.model; - model.show_url = path; - this.setState({ model: model }); + let path = req.path + let model = this.state.model + model.show_url = path + this.setState({ model: model }) this.setState({ - show_url: [{ src: req.path, id: new Date().getTime() }], - }); + show_url: [{ src: req.path, id: new Date().getTime() }] + }) }, (err) => {} - ); - }); + ) + }) } else { - let model = this.state.model; - model.show_url = ""; - this.setState({ model: model }); - this.setState({ show_url: [] }); + let model = this.state.model + model.show_url = "" + this.setState({ model: model }) + this.setState({ show_url: [] }) } } onUploadChange1(files) { if (files.length > 0) { - let formdata = new FormData(); - formdata.append("file", files[0].file); - formdata.append("path", "common_image"); + let formdata = new FormData() + formdata.append("file", files[0].file) + formdata.append("path", "common_image") uploadImg(formdata).then((res) => { handelResponse( res, (req, msg) => { - let path = req.path; - let model = this.state.model; - model.describe_url = path; - this.setState({ model: model }); + let path = req.path + let model = this.state.model + model.describe_url = path + this.setState({ model: model }) }, (err) => {} - ); - }); + ) + }) } else { - let model = this.state.model; - model.describe_url = ""; - this.setState({ model: model }); + let model = this.state.model + model.describe_url = "" + this.setState({ model: model }) } } onUploadError = (type, data) => { if (type === "overMaxAmount") { - Notify.error(`最多可上传 ${data.maxAmount} 张图片`); + Notify.error(`最多可上传 ${data.maxAmount} 张图片`) } else if (type === "overMaxSize") { - Notify.error(`图片大小不能超过 ${data.formattedMaxSize}`); + Notify.error(`图片大小不能超过 ${data.formattedMaxSize}`) } - }; + } onUploadChange3(files, e) { let arr = _.map(files, (o) => { - return o.name; - }); + return o.name + }) if (mySwiper !== 0) { - mySwiper.destroy(); + mySwiper.destroy() } mySwiper = new Swiper(".swiper-container", { observer: true, //修改swiper自己或子元素时,自动初始化swiper observeParents: true, //修改swiper的父元素时,自动初始化swiper pagination: ".swiper-pagination", paginationClickable: true, - autoplay: 3000, - }); + autoplay: 3000 + }) - clearTimeout(timer); + clearTimeout(timer) timer = setTimeout(() => { if (arr.length > this.state.productpic.length) { //新增 for (let i = 0; i < arr.length; i++) { let index = this.state.productpic.findIndex((o) => { - return o.name == arr[i]; - }); + return o.name == arr[i] + }) if (index < 0) { - let path = ""; - let formdata = new FormData(); - formdata.append("file", files[i].file); - formdata.append("path", "common_image"); + let path = "" + let formdata = new FormData() + formdata.append("file", files[i].file) + formdata.append("path", "common_image") uploadImg(formdata).then((res) => { handelResponse( res, (req, msg) => { - path = req.path; + path = req.path let obj = { id: files[i].id, name: files[i].name, - url: path, - }; + url: path + } let obj1 = { name: files[i].name, src: path, - id: files[i].id, - }; - const model = this.state.model; - model.describe_url.push(obj1); - let arr = this.state.productpic; - arr.push(obj); - this.setState({ productpic: arr, model }); + id: files[i].id + } + const model = this.state.model + model.describe_url.push(obj1) + let arr = this.state.productpic + arr.push(obj) + this.setState({ productpic: arr, model }) }, (err) => {} - ); - }); + ) + }) } } } if (arr.length < this.state.productpic.length) { - let temp_arr = Object.assign([], this.state.productpic); - this.setState({ productpic: [] }); - this.state.productpic = []; - let temp_arr2 = this.state.productpic; + let temp_arr = Object.assign([], this.state.productpic) + this.setState({ productpic: [] }) + this.state.productpic = [] + let temp_arr2 = this.state.productpic for (let i = 0; i < temp_arr.length; i++) { - let temp = temp_arr[i]; + let temp = temp_arr[i] if (arr.indexOf(temp.name) > -1) { - temp_arr2.push(temp); + temp_arr2.push(temp) } } - this.setState({ temp_arr2 }); + this.setState({ temp_arr2 }) } // //调整位置 if (arr.length === this.state.productpic.length) { - let temp_arr = []; - let pic_arr = this.state.productpic; + let temp_arr = [] + let pic_arr = this.state.productpic for (let i = 0; i < arr.length; i++) { let temp = pic_arr.find((o) => { - return o.name == arr[i]; - }); - temp_arr[i] = temp; + return o.name == arr[i] + }) + temp_arr[i] = temp } let left_temp_arr = files.map((item) => { return { ...item, - url: item.src, - }; - }); - let models = this.state.model; - models.describe_url = left_temp_arr; - this.setState({ model: models, productpic: temp_arr }); + url: item.src + } + }) + let models = this.state.model + models.describe_url = left_temp_arr + this.setState({ model: models, productpic: temp_arr }) } - }, 500); + }, 500) } onUploadChange2(files) { if (files.length > 0) { - let formdata = new FormData(); - formdata.append("file", files[0].file); - formdata.append("path", "common_image"); + let formdata = new FormData() + formdata.append("file", files[0].file) + formdata.append("path", "common_image") uploadImg(formdata).then((res) => { handelResponse( res, (req, msg) => { - let path = req.path; - let model = this.state.model; - model.detail_url = path; + let path = req.path + let model = this.state.model + model.detail_url = path - this.setState({ model: model }); + this.setState({ model: model }) }, (err) => {} - ); - }); + ) + }) } else { - let model = this.state.model; - model.detail_url = ""; - this.setState({ model: model }); + let model = this.state.model + model.detail_url = "" + this.setState({ model: model }) } } isReduce() { - let num = this.props.full; + let num = this.props.full if (num !== "" && num) { - num = Number(num); + num = Number(num) } if (Number(this.state.model.official_price) >= num) { return (
+
¥{" "} {subNum(this.state.model.official_price, this.props.reduce)}
-共省¥ {this.props.reduce}
+共省¥ {this.props.reduce}
+
¥{" "} {this.state.model.official_price}
- ); + ) } } @@ -503,41 +502,41 @@ export default class adduserinfo extends React.Component { couponH5() { return (+
¥{" "} {this.state.model.official_price}
-{this.state.model.product_name}
- - +{this.state.model.product_name}
+ +