// pages/shop/publishCopyright/publishCopyright.js import Shop from '../../../net/api/shop' import { upShopImgUrl, sImgPrefix } from '../../../net/mainUrl' const { isValidPhone } = require('../../../utils/validator') const Utils = require('../../../utils/util') const Cache = require('../../../utils/storage') Page({ /** * 页面的初始数据 */ data: { // 0b00884a-f7a2-425f-93e5-599fbaad4bde 软著分类 // ce3ded65-68ed-4f42-89da-de1b813b8f7e 证件类型 curStartDate: Utils.currentDate(), personType: [{ dataName: '自然人', dataId: '1' }, { dataName: '法人', dataId: '2' }, { dataName: '非法人组织或其他', dataId: '3' }], selPersonType: null, creType: [], rightType: [], areaList: [], //省 cityList: [], //市 rightName: '', //软著名称 orgName: '', //著作权人姓名 orgPhone: '', //联系电话 cardNumber: '', //证件号码 selRightType: [], //选中分类 rightPrice: '', //售价 rightStopDate: '', //停止日期 selCreType: null, //选中证件类型 showArea: false, prevValue: [0, 0], // 用于存储上一次的值 selArea: null, selCity: null, msgHint: '', msgType: 'info', msgShow: false, files: [], showType: false, goodsId: '', goods: null, }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { wx.setNavigationBarTitle({ title: '我要卖软著', }) wx.setNavigationBarColor({ frontColor: '#000000', // 必写项,字体颜色仅支持#ffffff和#000000 backgroundColor: '#FFFFFF', // 传递的颜色值,仅支持十六进制颜色 animation: { // 可选项 duration: 500, timingFunc: 'easeIn' } }) this.setData({ selPersonType: this.data.personType[0], selectFile: this.selectFile.bind(this), uploadFile: this.uploadFile.bind(this) }) const id = options.id if (id && id != '') { this.setData({ goodsId: id }) this.doGetGoodsDetail() } else { this.getDic() } }, //获取商品详情 doGetGoodsDetail() { wx.showLoading({ title: '加载中...', }) const _self = this Shop.doGetGoodsDetail(_self.data.goodsId) .then(res => { wx.hideLoading() var tempFile = [{ fileId: res.goodsLocalPhoto, fileName: 'ceshi', fileSize: 223, fileUrl: sImgPrefix + res.goodsLocalPhoto, url: sImgPrefix + res.goodsLocalPhoto }] _self.setData({ rightName: res.goodsName, rightPrice: res.goodsPrice, rightStopDate: res.goodsLastTime, orgName: res.goodsLeader, orgPhone: res.goodsLeaderPhone, cardNumber: res.goodsLeaderIdcard, files: tempFile, goods: res }) _self.getDic() }) .catch(err => { wx.hideLoading() _self.setData({ msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgType: 'error', msgShow: true }) setTimeout(() => { wx.navigateBack() }, 1500); }) }, inputRightName(e) { this.setData({ rightName: e.detail.value }) }, doClearRightName() { this.setData({ rightName: '' }) }, inputRightPrice(e) { this.setData({ rightPrice: e.detail.value }) }, doClearPrice() { this.setData({ rightPrice: 0 }) }, inputOrgName(e) { this.setData({ orgName: e.detail.value }) }, inputCardNumber(e) { this.setData({ cardNumber: e.detail.value }) }, //著作权人类别 bindChangePersonType(e) { this.setData({ selPersonType: this.data.personType[e.detail.value] }) }, clearPersonType() { this.setData({ selPersonType: null }) }, //证件类型 bindChangeCreType(e) { this.setData({ selCreType: this.data.creType[e.detail.value] }) }, clearCreType() { this.setData({ selCreType: null }) }, //选择软著分类 onShowRightType() { this.setData({ showType: true }) }, //选择软著分类 bindChangeRightType(e) { const selectedIds = e.detail.value; const selectedItems = []; for (let i = 0; i < this.data.rightType.length; i++) { const item = this.data.rightType[i]; item.checked = selectedIds.indexOf(item.dataId) !== -1; if (item.checked) { selectedItems.push(item); } } this.setData({ rightType: this.data.rightType, selRightType: selectedItems }) }, clearRightType() { this.data.rightType.forEach(item => { item.checked = false }) this.setData({ selRightType: [], rightType: this.data.rightType }) }, confirmSelRightType() { this.setData({ showType: false }) }, //显示日期选择 bindDateChange(e) { this.setData({ rightStopDate: e.detail.value }) }, clearTime() { this.setData({ rightStopDate: '' }) }, deleteImage(e) { var index = e.detail.index; this.data.files.splice(index, 1); }, selectFile(files) { console.log('files', files) }, uploadFile(files) { console.log(files) const tempFilePaths = files.tempFilePaths; const token = Cache.get('token') const header = {} if (token) { header.Auth = `Bearer ${token}`; } var that = this for (let i = 0; i < tempFilePaths.length; i++) { wx.uploadFile({ url: upShopImgUrl, header: header, filePath: tempFilePaths[i], name: 'image', success(res) { console.log(res) let result = JSON.parse(res.data) that.data.files.push(result.data) }, fail(err) { console.log(err); } }) } // 文件上传的函数,返回一个promise return new Promise((resolve, reject) => { var result = {}; result['urls'] = tempFilePaths; resolve(result); }) }, //显示地区选择 onShowArea() { this.setData({ showArea: true }) }, clearArea() { this.setData({ selArea: null, selCity: null }) }, confirmSelArea() { this.setData({ showArea: false }) }, inputOrgPhone(e) { this.setData({ orgPhone: e.detail.value }) }, doClearPhone() { this.setData({ orgPhone: '' }) }, //获取字典 getDic() { const _self = this const cre = Shop.doGetGoodsDic('0b00884a-f7a2-425f-93e5-599fbaad4bde') //软著分类 const right = Shop.doGetGoodsDic('ce3ded65-68ed-4f42-89da-de1b813b8f7e') //证件类型 const area = Shop.doGetAreaList('0') const list = [cre, right, area] Promise.all(list) .then(res => { if (res) { _self.setData({ rightType: res[0], creType: res[1], areaList: res[2] }) _self.data.rightType.forEach(item => { item.checked = false }) //判断是否是编辑 if (_self.data.goodsId != '') { _self.data.rightType.forEach(item => { // selRightType if (_self.data.goods.goodsType.indexOf(item.dataId) != -1) { item.checked = true _self.data.selRightType.push(item) } }) var tempPersonType _self.data.personType.forEach(item => { if (_self.data.goods.goodsLeaderType == item.dataId) { tempPersonType = item } }) var tempCreType _self.data.creType.forEach(item => { if (_self.data.goods.goodsLeaderIdcardType == item.dataId) { tempCreType = item } }) _self.setData({ rightType: _self.data.rightType, selRightType: _self.data.selRightType, selPersonType: tempPersonType, selCreType: tempCreType }) _self.doGetProvince(_self.data.goods.goodsLeaderCity1) } else { //默认加载第一个area _self.setData({ selCreType: _self.data.creType[0], selArea: _self.data.areaList[0], rightType: _self.data.rightType }) _self.doGetArea(_self.data.selArea.areaId) } } }) .catch(err => { _self.setData({ msgShow: true, msgHint: '数据加载错误,请稍后重试', msgType: 'error' }) setTimeout(() => { wx.navigateBack() }, 1500); }) }, //获取省 doGetProvince(id) { wx.showLoading({ title: '加载中...', }) const _self = this Shop.doGetAreaDetail(id) .then(res => { wx.hideLoading() _self.setData({ selArea: res }) _self.doGetArea(_self.data.selArea.areaId) }) .catch(err => { wx.hideLoading() //重置selArea selCity _self.setData({ selArea: null, selCity: null }) }) }, //获取区域 doGetArea(pId) { wx.showLoading({ title: '加载中...', }) const _self = this Shop.doGetAreaList(pId) .then(res => { wx.hideLoading() if (res) { _self.setData({ cityList: res }) if (_self.data.goodsId != '') { var tempCity _self.data.cityList.forEach(item => { if (_self.data.goods.goodsLeaderCity2 == item.areaId) { tempCity = item } }) _self.setData({ selCity: tempCity }) } else { _self.setData({ selCity: res[0] }) } } }) .catch(err => { wx.hideLoading() _self.setData({ msgShow: true, msgHint: '数据加载错误,请稍后重试', msgType: 'error' }) }) }, //选中省市 areaChange(e) { const _self = this const currentValue = e.detail.value; const prevValue = _self.data.prevValue; _self.setData({ prevValue: currentValue }); for (let i = 0; i < currentValue.length; i++) { if (currentValue[i] !== prevValue[i]) { if (i == 0) { //获取city const area = _self.data.areaList[currentValue[0]] _self.setData({ selArea: area }) _self.doGetArea(area.areaId) } else { const city = _self.data.cityList[currentValue[1]] _self.setData({ selCity: city }) } } } }, //发布新商品 doConfirmPublish() { const isLegal = this.checkParams() if (isLegal) { const _self = this const data = _self.buildParams() wx.showLoading({ title: '保存中...', }) Shop.doSaveGoods(data) .then(res => { wx.hideLoading() _self.setData({ msgType: 'success', msgHint: '保存成功', msgShow: true }) setTimeout(() => { _self.backPageRefresh() }, 1500); }) .catch(err => { wx.hideLoading() _self.setData({ msgType: 'error', msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgShow: true }) }) } }, //修改新商品 doConfirmUpdate() { const isLegal = this.checkParams() if (isLegal) { const _self = this const data = _self.buildParams() wx.showLoading({ title: '保存中...', }) Shop.doUpdateGoods(_self.data.goodsId, data) .then(res => { wx.hideLoading() //修改成功 _self.setData({ msgType: 'success', msgHint: '修改成功', msgShow: true }) setTimeout(() => { _self.backPageRefresh() }, 1500); }) .catch(err => { wx.hideLoading() _self.setData({ msgType: 'error', msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgShow: true }) }) } }, //保存商品 doSave() { if (this.data.goodsId != '') { //编辑 this.doConfirmUpdate() } else { //新增 this.doConfirmPublish() } }, //校验参数 checkParams() { const rules = [{ field: 'rightName', message: '请输入软著名称', validator: v => v.trim() !== '' }, { field: 'selRightType', message: '请选择软著分类', validator: v => v !== null }, { field: 'rightPrice', message: '请输入软著售价', validator: v => v.trim() !== '' }, { field: 'rightStopDate', message: '请选择截止售卖日期', validator: v => v.trim() !== '' }, { field: 'files', message: '请上传软著图片', validator: v => v && v.length > 0 }, { field: 'selPersonType', message: '请选择著作权人类别', validator: v => v !== null }, { field: 'orgName', message: '请输入姓名或机构名称', validator: v => v.trim() !== '' }, { field: ['selArea', 'selCity'], message: '请选择所在省市', validator: (_, data) => data.selArea !== null && data.selCity !== null }, { field: 'orgPhone', message: '请输入合法的联系电话', validator: v => v.trim() !== '' && isValidPhone(v.trim()) }, { field: 'selCreType', message: '请选择证件类型', validator: v => v !== null }, { field: 'cardNumber', message: '请输入证件号码', validator: v => v.trim() !== '' } ]; for (const rule of rules) { let isValid; if (Array.isArray(rule.field)) { // 多字段联合验证 isValid = rule.validator(null, this.data); } else { // 单字段验证 const value = this.data[rule.field]; isValid = rule.validator(value, this.data); } if (!isValid) { this.setData({ msgType: 'error', msgHint: rule.message, msgShow: true }); return false; } } return true; }, buildParams() { const typeIds = this.data.selRightType.map(item => item.dataId).join(','); const data = { goodsLastTime: this.data.rightStopDate, goodsLeader: this.data.orgName, goodsLeaderCity1: this.data.selArea.areaId, goodsLeaderCity2: this.data.selCity.areaId, goodsLeaderIdcard: this.data.cardNumber, goodsLeaderIdcardType: this.data.selCreType.dataId, goodsLeaderPhone: this.data.orgPhone, goodsLeaderType: this.data.selPersonType.dataId, goodsName: this.data.rightName, goodsLocalPhoto: this.data.files[0].fileId, goodsPrice: this.data.rightPrice, goodsType: typeIds } return data }, backPageRefresh() { let pages = getCurrentPages(); let beforePage = pages[pages.length - 2]; beforePage.setData({ needRefresh: true }) wx.navigateBack() } })