// pages/shop/publishCopyright/publishCopyright.js import Shop from '../../../net/api/shop' const { isValidPhone } = require('../../../utils/validator') Page({ /** * 页面的初始数据 */ data: { personType: [{ dataName: '自然人', dataId: '1' }, { dataName: '法人', dataId: '2' }, { dataName: '非法人组织或其他', dataId: '3' }], selPersonType: null, creType: [], areaList: [], //省 cityList: [], //市 orgName: '', //著作权人姓名 orgPhone: '', //联系电话 cardNumber: '', //证件号码 selRightType: [], //选中分类 rightPrice: '', //售价 rightStopDate: '', //停止日期 selCreType: null, //选中证件类型 showArea: false, prevValue: [0, 0], // 用于存储上一次的值 selArea: null, selCity: null, msgHint: '', msgType: 'info', msgShow: false, showType: false, orderId: '', }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { wx.setNavigationBarTitle({ title: '受让人信息', }) wx.setNavigationBarColor({ frontColor: '#000000', // 必写项,字体颜色仅支持#ffffff和#000000 backgroundColor: '#FFFFFF', // 传递的颜色值,仅支持十六进制颜色 animation: { // 可选项 duration: 500, timingFunc: 'easeIn' } }) var orderId = options.orderId if (orderId && orderId != '') { this.setData({ orderId: orderId, selPersonType: this.data.personType[0], //默认选中第一项 }) this.getDic() } else { this.setData({ msgHint: '数据有误,请稍后重试', msgType: 'error', msgShow: true }) setTimeout(() => { wx.navigateBack() }, 1200); } }, inputRightName: function (e) { this.setData({ rightName: e.detail.value }) }, doClearRightName: function () { this.setData({ rightName: '' }) }, inputRightPrice: function (e) { this.setData({ rightPrice: e.detail.value }) }, doClearPrice: function () { this.setData({ rightPrice: 0 }) }, inputOrgName: function (e) { this.setData({ orgName: e.detail.value }) }, inputCardNumber: function (e) { this.setData({ cardNumber: e.detail.value }) }, //著作权人类别 bindChangePersonType: function (e) { this.setData({ selPersonType: this.data.personType[e.detail.value] }) }, clearPersonType: function () { this.setData({ selPersonType: null }) }, //证件类型 bindChangeCreType: function (e) { this.setData({ selCreType: this.data.creType[e.detail.value] }) }, clearCreType: function () { this.setData({ selCreType: null }) }, //选择软著分类 onShowRightType: function () { this.setData({ showType: true }) }, //选择软著分类 bindChangeRightType: function (e) { var selectedIds = e.detail.value; var selectedItems = []; for (var i = 0; i < this.data.rightType.length; i++) { var 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: function () { this.data.rightType.forEach(function (item) { item.checked = false }) this.setData({ selRightType: [], rightType: this.data.rightType }) }, confirmSelRightType: function () { this.setData({ showType: false }) }, //显示地区选择 onShowArea: function () { this.setData({ showArea: true }) }, clearArea: function () { this.setData({ selArea: null, selCity: null }) }, confirmSelArea: function () { this.setData({ showArea: false }) }, inputOrgPhone: function (e) { this.setData({ orgPhone: e.detail.value }) }, doClearPhone: function () { this.setData({ orgPhone: '' }) }, //获取字典 getDic: function () { var _self = this var right = Shop.doGetGoodsDic('ce3ded65-68ed-4f42-89da-de1b813b8f7e') //证件类型 var area = Shop.doGetAreaList('0') var list = [right, area] Promise.all(list) .then(function (res) { if (res) { _self.setData({ creType: res[0], areaList: res[1] }) //默认加载第一个area _self.setData({ selCreType: _self.data.creType[0], selArea: _self.data.areaList[0] }) _self.doGetArea(_self.data.selArea.areaId) } }) .catch(function (err) { _self.setData({ msgShow: true, msgHint: '数据加载错误,请稍后重试', msgType: 'error' }) setTimeout(function () { wx.navigateBack() }, 1500); }) }, //获取区域 doGetArea: function (pId) { wx.showLoading({ title: '加载中...', }) var _self = this Shop.doGetAreaList(pId) .then(function (res) { wx.hideLoading() if (res) { _self.setData({ cityList: res }) _self.setData({ selCity: res[0] }) } }) .catch(function (err) { wx.hideLoading() _self.setData({ msgShow: true, msgHint: '数据加载错误,请稍后重试', msgType: 'error' }) }) }, //选中省市 areaChange: function (e) { var _self = this var currentValue = e.detail.value; var prevValue = _self.data.prevValue; _self.setData({ prevValue: currentValue }); for (var i = 0; i < currentValue.length; i++) { if (currentValue[i] !== prevValue[i]) { if (i == 0) { //获取city var area = _self.data.areaList[currentValue[0]] _self.setData({ selArea: area }) _self.doGetArea(area.areaId) } else { var city = _self.data.cityList[currentValue[1]] _self.setData({ selCity: city }) } } } }, //校验参数 checkParams: function () { var rules = [{ field: 'selPersonType', message: '请选择受让人类别', validator: function (v) { return v !== null; } }, { field: 'orgName', message: '请输入姓名或机构名称', validator: function (v) { return typeof v === 'string' && v.trim().length > 0; } }, { field: ['selArea', 'selCity'], message: '请选择所在省市', validator: function (_, data) { return data.selArea !== null && data.selCity !== null; } }, { field: 'orgPhone', message: '请输入合法的联系电话', validator: function (v) { // 先确保是字符串,再进行电话格式验证 return typeof v === 'string' && v.trim().length > 0 && isValidPhone(v.trim()); } }, { field: 'selCreType', message: '请选择证件类型', validator: function (v) { return v !== null; } }, { field: 'cardNumber', message: '请输入证件号码', validator: function (v) { return typeof v === 'string' && v.trim().length > 0; } } ]; for (var i = 0; i < rules.length; i++) { var rule = rules[i]; var isValid; if (Array.isArray(rule.field)) { // 多字段联合验证 isValid = rule.validator(null, this.data); } else { // 单字段验证 var 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 { selArea, selCity } = this.data var data = { buyCity: selArea.areaId + ',' + selCity.areaId, buyIdcard: this.data.cardNumber, buyIdcardType: this.data.selCreType.dataId, buyName: this.data.orgName, buyPhone: this.data.orgPhone, buyType: this.data.selPersonType.dataId } return data }, backPageRefresh() { var pages = getCurrentPages(); var beforePage = pages[pages.length - 2]; beforePage.setData({ needRefresh: true }) wx.navigateBack() }, //提交 async doSave() { try { const isLegal = this.checkParams() if (isLegal) { const _self = this const data = _self.buildParams() wx.showLoading({ title: '上报中...', }) await Shop.doSaveBuyPersonInfo(_self.data.orderId, data) wx.hideLoading() _self.setData({ msgHint: '上报成功', msgType: 'success', msgShow: true }) setTimeout(() => { _self.backPageRefresh() }, 1200); } } catch (err) { wx.hideLoading() this.setData({ msgHint: err.msg ? err.msg : '网络错误,请稍后重试', msgType: 'error', msgShow: true }) } } })