ts_aimz/pages/shop/publishCopyright/publishCopyright.js
2025-06-20 18:06:24 +08:00

655 lines
21 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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: {
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: function (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)
})
var id = options.id
if (id && id != '') {
this.setData({
goodsId: id
})
this.doGetGoodsDetail()
} else {
this.getDic()
}
},
//获取商品详情
doGetGoodsDetail: function () {
wx.showLoading({
title: '加载中...',
})
var _self = this
Shop.doGetGoodsDetail(_self.data.goodsId)
.then(function (res) {
wx.hideLoading()
var tempFile = [{
fileId: res.goodsLocalPhoto,
fileName: 'image',
fileSize: 0,
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) {
wx.hideLoading()
_self.setData({
msgHint: err.msg ? err.msg : '网络错误,请稍后重试',
msgType: 'error',
msgShow: true
})
setTimeout(function () {
wx.navigateBack()
}, 1500);
})
},
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
})
},
//显示日期选择
bindDateChange: function (e) {
this.setData({
rightStopDate: e.detail.value
})
},
clearTime: function () {
this.setData({
rightStopDate: ''
})
},
deleteImage: function (e) {
var index = e.detail.index;
this.data.files.splice(index, 1);
},
selectFile: function (files) {
console.log('files', files)
},
uploadFile: function (files) {
console.log(files)
var tempFilePaths = files.tempFilePaths;
var token = Cache.get('token')
var header = {}
if (token) {
header.Auth = 'Bearer ' + token;
}
var that = this
for (var i = 0; i < tempFilePaths.length; i++) {
wx.uploadFile({
url: upShopImgUrl,
header: header,
filePath: tempFilePaths[i],
name: 'image',
success: function (res) {
console.log(res)
var result = JSON.parse(res.data)
that.data.files.push(result.data)
},
fail: function (err) {
console.log(err);
}
})
}
// 文件上传的函数返回一个promise
return new Promise(function (resolve, reject) {
var result = {};
result['urls'] = tempFilePaths;
resolve(result);
})
},
//显示地区选择
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 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]
Promise.all(list)
.then(function (res) {
if (res) {
_self.setData({
rightType: res[0],
creType: res[1],
areaList: res[2]
})
_self.data.rightType.forEach(function (item) {
item.checked = false
})
//判断是否是编辑
if (_self.data.goodsId != '') {
_self.data.rightType.forEach(function (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(function (item) {
if (_self.data.goods.goodsLeaderType == item.dataId) {
tempPersonType = item
}
})
var tempCreType
_self.data.creType.forEach(function (item) {
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
}
})
_self.setData({
rightType: _self.data.rightType,
selRightType: _self.data.selRightType,
selPersonType: tempPersonType,
selCreType: tempCreType,
selArea: tempSelArea,
'prevValue[0]': tempIndex
})
_self.doGetArea(_self.data.selArea.areaId, false)
} else {
//默认加载第一个area
_self.setData({
selCreType: _self.data.creType[0],
selArea: _self.data.areaList[0],
rightType: _self.data.rightType
})
_self.doGetArea(_self.data.selArea.areaId, false)
}
}
})
.catch(function (err) {
_self.setData({
msgShow: true,
msgHint: '数据加载错误,请稍后重试',
msgType: 'error'
})
setTimeout(function () {
wx.navigateBack()
}, 1500);
})
},
//获取区域
doGetArea: function (pId, isChange) {
wx.showLoading({
title: '加载中...',
})
var _self = this
Shop.doGetAreaList(pId)
.then(function (res) {
wx.hideLoading()
if (res) {
_self.setData({
cityList: res
})
if (_self.data.goodsId != '') {
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
})
}
} else {
_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, true)
} else {
var city = _self.data.cityList[currentValue[1]]
_self.setData({
selCity: city
})
}
}
}
},
//发布新商品
doConfirmPublish: function () {
var isLegal = this.checkParams()
if (isLegal) {
var _self = this
var data = _self.buildParams()
wx.showLoading({
title: '保存中...',
})
Shop.doSaveGoods(data)
.then(function (res) {
wx.hideLoading()
_self.setData({
msgType: 'success',
msgHint: '保存成功',
msgShow: true
})
setTimeout(function () {
_self.backPageRefresh()
}, 1500);
})
.catch(function (err) {
wx.hideLoading()
_self.setData({
msgType: 'error',
msgHint: err.msg ? err.msg : '网络错误,请稍后重试',
msgShow: true
})
})
}
},
//修改新商品
doConfirmUpdate: function () {
var isLegal = this.checkParams()
if (isLegal) {
var _self = this
var data = _self.buildParams()
wx.showLoading({
title: '保存中...',
})
Shop.doUpdateGoods(_self.data.goodsId, data)
.then(function (res) {
wx.hideLoading()
//修改成功
_self.setData({
msgType: 'success',
msgHint: '修改成功',
msgShow: true
})
setTimeout(function () {
_self.backPageRefresh()
}, 1500);
})
.catch(function (err) {
wx.hideLoading()
_self.setData({
msgType: 'error',
msgHint: err.msg ? err.msg : '网络错误,请稍后重试',
msgShow: true
})
})
}
},
//保存商品
doSave: function () {
if (this.data.goodsId != '') {
//编辑
this.doConfirmUpdate()
} else {
//新增
this.doConfirmPublish()
}
},
//校验参数
checkParams: function () {
var rules = [{
field: 'rightName',
message: '请输入软著名称',
validator: function (v) {
// 先判断是否为字符串再调用trim()
return typeof v === 'string' && v.trim().length > 0;
}
},
{
field: 'selRightType',
message: '请选择软著分类',
validator: function (v) {
return v !== null && v.length > 0;
}
},
{
field: 'rightPrice',
message: '请输入软著售价',
validator: function (v) {
// 价格字段可能为数字类型,先转为字符串再处理
return typeof v === 'string' || typeof v === 'number' ?
('' + v).trim().length > 0 : false;
}
},
{
field: 'rightStopDate',
message: '请选择截止售卖日期',
validator: function (v) {
return typeof v === 'string' && v.trim().length > 0;
}
},
{
field: 'files',
message: '请上传软著图片',
validator: function (v) {
return v && v.length > 0;
}
},
{
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: function () {
var typeIds = this.data.selRightType.map(function (item) {
return item.dataId
}).join(',');
var 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: function () {
var pages = getCurrentPages();
var beforePage = pages[pages.length - 2];
beforePage.setData({
needRefresh: true
})
wx.navigateBack()
}
})