123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763 |
- const app = getApp();
- const api = require('../../api/api');
- const storageManager = require("../../utils/storage-manager.js");
- Page({
-
- data: {
- navbarData: {
- showCapsule: 1,
- title: '添加商品',
- capsuleMode: 'navBack',
- },
- enterType: '',
- arealength: 0,
- showLadderPrice: false,
- showCategory: false,
- showSpecification: false,
- specificationText: '统一规格',
-
- goodName: '',
- goodIntro: '',
- imageList: [],
- scribePrice: '',
- specifications: [],
- price: '',
- stock: '',
- categorys: [],
- categoryIndex: 0,
- isAddToGoodsBank: 1,
- startingPurchase: '',
- purchaseRestrictionsNumber: '',
- purchaseRestrictionsSecond: '',
- startingPurchaseNumber: '',
- prices: [],
- },
-
- onLoad(options) {
- let that = this;
- that.setData({
- enterType: options.enterType
- })
-
- wx.showLoading({
- title: '加载中',
- mask: true
- })
- wx.request({
- url: api.Classification_list,
- header: {
- 'Authorization': wx.getStorageSync('token')
- },
- method: 'POST',
- success(res) {
- console.log(res);
- if (res.data.code === 1) {
- that.setData({
- categorys: res.data.data
- })
- } else {
- wx.showToast({
- title: res.data.msg,
- mask: true,
- icon: 'none'
- })
- }
- },
- fail(err) {
- wx.showToast({
- title: '发起网络请求失败',
- icon: 'none',
- mask: true
- })
- },
- complete() {
- wx.hideLoading()
- }
- })
- },
-
- onReady() {
- },
-
- onShow() {
- },
-
- onHide() {
- },
-
- onUnload() {
- },
-
- onPullDownRefresh() {
- },
-
- onReachBottom() {
- },
-
- onShareAppMessage() {
- },
-
- inputGoodName(e) {
- this.setData({
- goodName: e.detail.value
- })
- },
-
- inputGoodIntro(e) {
- this.setData({
- goodIntro: e.detail.value,
- arealength: e.detail.value.length
- })
- },
-
- uploadSmallImage() {
- let that = this;
- if (that.data.imageList.length < 5) {
-
-
-
-
- wx.request({
- url: api.getSignedUrl,
- data: {
- type: 'image'
- },
- success: (oss) => {
- console.log(oss);
- if (oss.statusCode === 200) {
- wx.chooseMedia({
- count: 1,
- mediaType: ['image'],
- success: (img) => {
- console.log(img);
- let key = "image/" + new Date().getTime() + Math.floor(Math.random() * 150) + '.png';
- wx.showLoading({
- title: '文件上传中',
- mask: true
- })
- wx.uploadFile({
- filePath: img.tempFiles[0].tempFilePath,
- name: 'file',
- url: 'https://' + oss.data.data.host,
- formData: {
- key: key,
- policy: oss.data.data.policy,
- OSSAccessKeyId: oss.data.data.OSSAccessKeyId,
- success_action_status: '200',
- signature: oss.data.data.Signature,
- },
- success: (res) => {
- if (res.statusCode == 200) {
- let src = 'https://' + oss.data.data.host + '/' + key;
- console.log(src);
- let imageList = that.data.imageList;;
- imageList.push(src);
- that.setData({
- imageList
- })
- }
- },
- fail: (err) => {
- console.log(err);
- },
- complete: () => {
- wx.hideLoading()
- }
- })
- },
- fail: (err) => {
- wx.hideLoading()
- }
- })
- } else {
- wx.showToast({
- title: '服务器出现错误',
- icon: 'none'
- })
- }
- },
- fail: (err) => {
- console.log(err);
- }
- })
- }
- },
-
- deleteSmallImage(e) {
- let that = this;
- let index = e.currentTarget.dataset.index;
- let imageList = that.data.imageList;
- imageList.splice(index, 1);
- this.setData({
- imageList
- })
- },
-
- inputScribePrice(e) {
- this.setData({
- scribePrice: e.detail.value
- })
- },
-
- showSpecificationPopup() {
- let that = this;
- if (that.data.prices.length == 0) {
- if (that.data.specifications.length == 0) {
- that.setData({
- showSpecification: true,
- specifications: [{
- title: '',
- price: ''
- },
- {
- title: '',
- price: ''
- }
- ]
- })
- } else {
- that.setData({
- showSpecification: true
- })
- }
- } else {
- wx.showModal({
- title: '提示',
- content: '规格和阶梯价不可同时设置,如需设置规格,则会清除阶梯价内所有设置',
- success(res) {
- if (res.confirm) {
- if (that.data.specifications.length == 0) {
- that.setData({
- prices: [],
- showSpecification: true,
- specifications: [{
- title: '',
- price: ''
- },
- {
- title: '',
- price: ''
- }
- ]
- })
- } else {
- that.setData({
- prices: [],
- showSpecification: true
- })
- }
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- }
- },
-
- closeSpecificationPopup() {
- let that = this;
- let isOK = that.judgeNoyNullSpecifications(that.data.specifications);
- if (isOK) {
- that.setData({
- specificationText: that.data.specifications[0].title + '/' + that.data.specifications[0].price + '加币',
- price: Number(that.data.specifications[0].price),
- showSpecification: false
- })
- } else {
- wx.showToast({
- title: '您未完整录入数据',
- icon: 'none',
- mask: true,
- success() {
- setTimeout(() => {
- that.setData({
- specificationText: '统一规格',
- specifications: [],
- showSpecification: false
- })
- }, 1500)
- }
- })
- }
- },
-
- specificationsInput(e) {
- let index = e.currentTarget.dataset.index;
- let value = e.detail.value;
- let list = this.data.specifications;
- list[index].title = value;
- this.setData({
- specifications: list
- })
- },
-
- specificationsPriceInput(e) {
- let index = e.currentTarget.dataset.index;
- let value = e.detail.value;
- let list = this.data.specifications;
- list[index].price = value;
- this.setData({
- specifications: list
- })
- },
-
- addSpecification() {
- let bulk = {
- title: '',
- price: ''
- }
- let specifications = this.data.specifications;
- specifications.push(bulk)
- this.setData({
- specifications: specifications
- })
- },
-
- delSpecification(e) {
- let index = e.currentTarget.dataset.index
- let list = this.data.specifications;
- list.splice(index, 1);
- this.setData({
- specifications: list
- })
- },
-
- inputPrice(e) {
- this.setData({
- price: e.detail.value
- })
- },
-
- inputStock(e) {
- this.setData({
- stock: e.detail.value
- })
- },
-
- showCategoryPopup() {
- this.setData({
- showCategory: true
- })
- },
-
- closeCategoryPopup() {
- this.setData({
- showCategory: false
- })
- },
-
- selectCategory(e) {
- this.setData({
- categoryIndex: e.currentTarget.dataset.index
- })
- },
-
- changeIsAddToGoodsBank(e) {
- this.setData({
- isAddToGoodsBank: e.detail
- })
- },
-
- inputStartingPurchase(e) {
- this.setData({
- startingPurchase: e.detail.value
- })
- },
-
- inputPurchaseRestrictionsNumber(e) {
- this.setData({
- purchaseRestrictionsNumber: e.detail.value
- })
- },
-
- inputPurchaseRestrictionsSecond(e) {
- this.setData({
- purchaseRestrictionsSecond: e.detail.value
- })
- },
-
- inputStartingPurchaseNumber(e) {
- this.setData({
- startingPurchaseNumber: e.detail.value
- })
- },
-
- showLadderPricePopup() {
- let that = this;
- if (that.data.specifications.length == 0) {
- if (that.data.prices.length == 0) {
- that.setData({
- showLadderPrice: true,
- prices: [{
- number: '',
- price: ''
- },
- {
- number: '',
- price: ''
- }
- ]
- })
- } else {
- that.setData({
- showLadderPrice: true
- })
- }
- } else {
- wx.showModal({
- title: '提示',
- content: '规格和阶梯价不可同时设置,如需设置阶梯价,则会清除规格内所有设置',
- success(res) {
- if (res.confirm) {
- if (that.data.prices.length == 0) {
- that.setData({
- specifications: [],
- showLadderPrice: true,
- prices: [{
- number: '',
- price: ''
- },
- {
- number: '',
- price: ''
- }
- ]
- })
- } else {
- that.setData({
- specifications: [],
- showLadderPrice: true
- })
- }
- } else if (res.cancel) {
- console.log('用户点击取消')
- }
- }
- })
- }
- },
-
- closeLadderPricePopup() {
- let that = this;
- let isOK = that.judgeNoyNullStepPrice(that.data.prices);
- if (isOK) {
- that.setData({
- specificationText: '统一规格',
- price: Number(that.data.prices[0].price),
- showLadderPrice: false
- })
- } else {
- wx.showToast({
- title: '您未完整录入数据',
- icon: 'none',
- mask: true,
- success() {
- setTimeout(() => {
- that.setData({
- specificationText: '统一规格',
- prices: [],
- showLadderPrice: false
- })
- }, 1500)
- }
- })
- }
- },
-
- ladderPriceNumInput(e) {
- let index = e.currentTarget.dataset.index;
- let value = e.detail.value;
- let list = this.data.prices;
- list[index].number = value;
- this.setData({
- prices: list
- })
- },
-
- ladderPricePriceInput(e) {
- let index = e.currentTarget.dataset.index;
- let value = e.detail.value;
- let list = this.data.prices;
- list[index].price = value;
- this.setData({
- prices: list
- })
- },
-
- addbulk() {
- let bulk = {
- number: '',
- price: ''
- }
- let prices = this.data.prices;
- prices.push(bulk)
- this.setData({
- prices: prices
- })
- },
-
- delbulk(e) {
- let index = e.currentTarget.dataset.index
- let list = this.data.prices;
- list.splice(index, 1);
- this.setData({
- prices: list
- })
- },
-
- submit() {
- let that = this;
-
- if (that.data.goodName == '') {
- wx.showToast({
- title: '商品名称未录入',
- icon: 'none',
- mask: true
- })
- return
- }
-
- if (that.data.goodIntro == '') {
- wx.showToast({
- title: '商品简介未录入',
- icon: 'none',
- mask: true
- })
- return
- }
-
- if (that.data.imageList.length == 0) {
- wx.showToast({
- title: '商品图片未添加',
- icon: 'none',
- mask: true
- })
- return
- }
-
- if (that.data.scribePrice == '') {
- wx.showToast({
- title: '划线价格未录入',
- icon: 'none',
- mask: true
- })
- return
- }
-
- if (that.data.price == '') {
- wx.showToast({
- title: '售价未录入',
- icon: 'none',
- mask: true
- })
- return
- }
-
- let stock = that.data.stock;
- if (stock == '') {
- stock = '99999999';
- }
-
-
-
-
-
-
-
-
-
- if (that.data.specifications.length == 0 && that.data.prices.length == 0) {
- let list = that.data.specifications;
- let obj = {};
- obj.title = '统一规格';
- obj.price = that.data.price;
- list.push(obj);
- that.setData({
- specifications: list
- })
- }
- wx.showLoading({
- title: '提交中',
- mask: true
- })
- wx.request({
- url: api.Commodity_add,
- header: {
- 'Authorization': wx.getStorageSync('token')
- },
- method: 'POST',
- data: {
- commodity_name: that.data.goodName,
- commodity_img: that.data.imageList,
- commodity_introduction: that.data.goodIntro,
- scribe_price: that.data.scribePrice,
- specifications: that.data.specifications,
- price: that.data.price,
- stock: stock,
- classification_id: that.data.categorys[that.data.categoryIndex].id,
- commodity_warehouse: that.data.isAddToGoodsBank,
- starting_purchase: that.data.startingPurchase,
- purchase_restrictions_number: that.data.purchaseRestrictionsNumber,
- purchase_restrictions_second: that.data.purchaseRestrictionsSecond,
- starting_purchase_number: that.data.startingPurchaseNumber,
- step_price: that.data.prices
- },
- success(res) {
- console.log(res);
- wx.hideLoading()
- if (res.data.code === 1) {
- wx.showToast({
- title: '提交成功',
- icon: 'success',
- mask: true,
- success() {
- if (that.data.enterType == 'announce') {
-
- let id = Number(res.data.data);
- wx.request({
- url: api.Commodity_info,
- header: {
- 'Authorization': wx.getStorageSync('token')
- },
- data: {
- id: id
- },
- success(ress) {
- console.log(ress);
- if (ress.data.code === 1) {
- let arr = [];
- let obj = ress.data.data;
- arr.push(obj);
- storageManager.set('subPageResult', arr);
- setTimeout(() => {
- wx.navigateBack({
- delta: 1,
- })
- }, 1500)
- }
- }
- })
- } else {
- setTimeout(() => {
- wx.navigateBack({
- delta: 1,
- })
- }, 1500)
- }
- }
- })
- } else {
- wx.showToast({
- title: res.data.msg,
- mask: true,
- icon: 'none'
- })
- }
- },
- fail(err) {
- wx.hideLoading()
- wx.showToast({
- title: '发起网络请求失败',
- icon: 'none',
- mask: true
- })
- },
- complete() {
-
- }
- })
- },
-
- judgeNoyNullStepPrice(arr) {
- let isOK = true;
- for (let i = 0; i < arr.length; i++) {
- if (arr[i].title == '' || arr[i].price == '') {
- isOK = false;
- }
- }
- return isOK;
- },
-
- judgeNoyNullSpecifications(arr) {
- let isOK = true;
- for (let i = 0; i < arr.length; i++) {
- if (arr[i].number == '' || arr[i].price == '') {
- isOK = false;
- }
- }
- return isOK;
- },
- })
|