ts_aimz/pages/shop/publishCopyright/publishCopyright.js

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