ts_aimz/pages/shop/publishCopyright/publishCopyright.js

449 lines
13 KiB
JavaScript
Raw Normal View History

2025-06-09 17:02:32 +08:00
// pages/shop/publishCopyright/publishCopyright.js
import Shop from '../../../net/api/shop'
import {
upShopImgUrl
} 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
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
wx.setNavigationBarTitle({
title: '软著发布',
})
wx.setNavigationBarColor({
frontColor: '#000000', // 必写项,字体颜色仅支持#ffffff和#000000
backgroundColor: '#F0F0F0', // 传递的颜色值,仅支持十六进制颜色
animation: { // 可选项
duration: 500,
timingFunc: 'easeIn'
}
})
this.setData({
selPersonType: this.data.personType[0],
selectFile: this.selectFile.bind(this),
uploadFile: this.uploadFile.bind(this)
})
this.getDic()
},
inputRightName(e) {
this.setData({
rightName: e.detail.value
})
},
inputRightPrice(e) {
this.setData({
rightPrice: e.detail.value
})
},
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 = new Set(e.detail.value);
const rightTypeMap = new Map();
this.data.rightType.forEach(item => {
rightTypeMap.set(item.dataId, item);
});
const selRightType = [...selectedIds].map(id => {
const item = rightTypeMap.get(id);
if (item) {
item.checked = true;
}
return item;
}).filter(Boolean);
this.setData({
selRightType,
rightType: [...this.data.rightType]
});
},
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
})
},
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
})
//默认加载第一个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);
})
},
//获取区域
doGetArea(pId) {
wx.showLoading({
title: '加载中...',
})
const _self = this
Shop.doGetAreaList(pId)
.then(res => {
wx.hideLoading()
console.log(res)
if (res) {
_self.setData({
cityList: res,
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
})
}
}
}
},
//保存商品
doSave() {
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(() => {
wx.navigateBack()
}, 1500);
})
.catch(err => {
wx.hideLoading()
_self.setData({
msgType: 'error',
msgHint: err.msg ? err.msg : '网络错误,请稍后重试',
msgShow: true
})
})
}
},
//校验参数
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 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: this.data.selRightType.dataId
}
return data
}
})