榆山
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

709 lines
22 KiB

const config = require('../../../../utils/config')
import {
$wuxActionSheet
} from '../../../../dist/index'
import regeneratorRuntime from '../../../../utils/runtime.js'
import {
getweekRecordOptionList,
submitWeekRocord,
getWeekRecordList,
getWeekRecordDetail,
updateWeekRocord
} from '../../../../api/weeklyIndex'
Page({
data: {
peopleNum: 0,
weeklyList: [],
showflag: [], //true是 false:否
/***图片 **/
uploadImageList: [],
imageId: 1,
noteContent: '',
imageListlenght: 9,
placeholder: '请输入内容(2000字内)',
isUploading: false, //true正在上传
isPublish: false, //true正在发送请求
images: [],
/***图片 **/
weeklyType: 'publish',
infoId: 'a8c3a8d5117fd8bc28596d5f348bcfaf',
isShowWords: true,
id: '', //修改周报时提交的id
loadVisible: false,
loadType: 'more',
},
async onLoad(options) {
this.setData({
loadVisible: true
})
this.setData({
weeklyType: options.weeklyType,
infoId: options.infoId
})
if (this.data.weeklyType == 'change') {
await this.getWeekRecordDetail()
this.setData({
loadVisible: false
})
} else if (this.data.weeklyType == 'publish') {
await this.getweekRecordOptionList()
this.setData({
loadVisible: false
})
}
},
onShow() {
this.setData({
isPublish: false
})
},
onChange(e) {
let {
childindex,
parentindex
} = e.currentTarget.dataset
// console.log(parentindex,childindex)
this.setData({
[`weeklyList[${parentindex}].totalList[${childindex}].total`]: e.detail.value
})
},
triggerShowFlag(e) {
let {
index
} = e.currentTarget.dataset
this.setData({
[`showflag[${index}]`]: !this.data.showflag[index]
})
// 变为否的时候,把内容清除----->数字都变为0
// if (!this.data.showflag[index]) {
// this.data.weeklyList[index].totalList.forEach((element, elementIndex) => {
// this.setData({
// [`weeklyList[${index}].totalList[${elementIndex}].total`]: 0
// })
// })
// }
},
/********************内容及图片************ */
// 双向绑定 内容输入框
bindTextareaInput(e) {
this.setData({
noteContent: e.detail.value
})
},
// 删除选中的图片
deleteImage(e) {
console.log('e删除', e)
const index = this.data.uploadImageList.findIndex(item => item.imageId === e.currentTarget.dataset.imageid)
console.log('删除的index', index)
if (index > -1) {
this.data.uploadImageList.splice(index, 1)
this.setData({
uploadImageList: this.data.uploadImageList
})
}
},
// 选择图片 上传弹窗 - 上传图片方式 - 选择图片 - 上传图片 - 回调赋值
chooseImage() {
const _this = this
console.log('::::::::' + this.data.isUploading)
let number_a = 0;
if (this.data.isUploading < 9) {
number_a = this.data.imageListlenght - _this.data.uploadImageList.length
}
if (this.data.isUploading) {
wx.showToast({
title: '图片正在上传',
icon: 'none'
})
console.log('禁止点击~~~')
return false;
}
$wuxActionSheet().showSheet({
buttons: [{
text: '拍照'
},
{
text: '从相册中获取'
},
],
className: 'dialog-class',
buttonClicked(index) {
console.log('index', index)
_this.setData({
isUploading: true
})
if (index === 0) {
console.log('选择图片')
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['camera'],
success(res) {
const uploadImageList = [..._this.data.uploadImageList]
if (res.tempFiles[0].size <= 5 * 1024 * 1024) {
uploadImageList.push({
uploaded: false,
ossUrl: '',
imgUrl: res.tempFiles[0].path,
imageId: ++_this.data.imageId,
thumbnail: ''
})
} else {
wx.showToast({
title: '图片上限5M,请压缩后重试~',
icon: 'none',
duration: 1500
})
return false
}
_this.setData({
uploadImageList,
})
wx.uploadFile({
url: `${config.CLERKONLINE()}oss/file/uploadImg`,
filePath: res.tempFilePaths[0],
name: 'file',
formData: {
uploadType: _this.data.uploadType
},
header: {
'Content-type': 'application/json;',
'Authorization': wx.getStorageSync('token')
},
success(fileRes) {
if (!JSON.parse(fileRes.data).data) {
wx.showToast({
title: '图片上传失败,请重试~',
icon: 'none',
duration: 1500
})
console.log('删除的id', _this.data.imageId)
// 删除
const index = _this.data.uploadImageList.findIndex(item => item.imageId === _this.data.imageId)
console.log('删除的index', index)
if (index > -1) {
_this.data.uploadImageList.splice(index, 1)
_this.setData({
uploadImageList: _this.data.uploadImageList
})
}
// wx.hideToast(); //关闭提示框
_this.setData({
isUploading: false
})
} else {
uploadImageList[uploadImageList.length - 1].uploaded = true
uploadImageList[uploadImageList.length - 1].ossUrl = JSON.parse(fileRes.data).data.imgUrl,
uploadImageList[uploadImageList.length - 1].thumbnail = JSON.parse(fileRes.data).data.thumbnail
wx.hideToast(); //关闭提示框
_this.setData({
uploadImageList,
isUploading: false
})
}
},
fail() {
wx.hideToast(); //关闭提示框
_this.setData({
uploadImageList: [],
isUploading: false
})
wx.showToast({
title: '图片上传失败,请重试~',
icon: 'none',
duration: 1500
})
}
})
},
fail(res) {
wx.hideToast(); //关闭提示框
console.log('取消的函数', res)
_this.setData({
isUploading: false,
})
}
})
} else if (index === 1) {
console.log("本地选择")
console.log('number_a::::' + number_a)
wx.chooseImage({
// count: 3 - _this.data.uploadImageList.length,
count: number_a,
sizeType: ['original', 'compressed'],
sourceType: ['album'],
success(res) {
let deleteLength = _this.data.uploadImageList.length
let imgIndex = 0; //上传完成的第几张照片
console.log('lll', res.tempFiles)
const uploadImageList = []
const endIndex = _this.data.uploadImageList.length
console.log('2222' + endIndex)
res.tempFiles.forEach(item => {
if (item.size <= 5 * 1024 * 1024) {
uploadImageList.push({
uploaded: false,
ossUrl: '',
imgUrl: item.path,
imageId: ++_this.data.imageId,
thumbnail: ''
})
} else {
wx.showToast({
title: '图片上限5M,请压缩后重试~',
icon: 'none',
duration: 1500
})
}
})
_this.setData({
uploadImageList: [..._this.data.uploadImageList, ...uploadImageList]
})
const endIndex2 = res.tempFiles.length
console.log('endIndex2', _this.data.uploadImageList.length)
uploadImageList.forEach((item, index) => {
return (async function (index) {
await wx.uploadFile({
url: `${config.CLERKONLINE()}oss/file/uploadImg`,
filePath: res.tempFilePaths[index],
name: 'file',
formData: {
uploadType: _this.data.uploadType
},
header: {
'Content-type': 'application/json',
'Authorization': wx.getStorageSync('token')
},
success(fileRes) {
// if (!JSON.parse(fileRes.data).data) {
// wx.showToast({
// title: '图片上传失败,请重试~',
// icon: 'none',
// duration: 1500
// })
// console.log('删除的id', _this.data.imageId)
// // 删除
// const index = _this.data.uploadImageList.findIndex(item => item.imageId === _this.data.imageId)
// console.log('删除的index', index)
// if (index > -1) {
// _this.data.uploadImageList.splice(index, 1)
// _this.setData({
// uploadImageList: _this.data.uploadImageList
// })
// }
// // wx.hideToast(); //关闭提示框
// _this.setData({
// isUploading: false
// })
// } else {
// imgIndex = imgIndex + 1;
// uploadImageList[index].uploaded = true
// uploadImageList[index].ossUrl = JSON.parse(fileRes.data).data.imgUrl
// uploadImageList[index].thumbnail = JSON.parse(fileRes.data).data.thumbnail
// _this.data.uploadImageList = _this.data.uploadImageList.slice(0, endIndex)
// _this.setData({
// uploadImageList: [..._this.data.uploadImageList, ...uploadImageList]
// })
// console.log('index', index)
// wx.hideToast(); //关闭提示框
// if (imgIndex == uploadImageList.length) {
// _this.setData({
// isUploading: false,
// })
// }
// }
if (!JSON.parse(fileRes.data).data) {
console.log('成功222', _this.data.uploadImageList, deleteLength)
wx.showToast({
title: '图片上传失败,请重试~',
icon: 'none',
duration: 1500
})
_this.data.uploadImageList.splice(deleteLength, _this.data.uploadImageList.length - deleteLength)
_this.setData({
uploadImageList: _this.data.uploadImageList
})
_this.setData({
isUploading: false
})
} else {
imgIndex = imgIndex + 1;
uploadImageList[index].uploaded = true
uploadImageList[index].ossUrl = JSON.parse(fileRes.data).data.imgUrl
uploadImageList[index].thumbnail = JSON.parse(fileRes.data).data.thumbnail
_this.data.uploadImageList = _this.data.uploadImageList.slice(0, endIndex)
_this.setData({
uploadImageList: [..._this.data.uploadImageList, ...uploadImageList]
})
console.log('index', index)
wx.hideToast(); //关闭提示框
if (imgIndex == uploadImageList.length) {
_this.setData({
isUploading: false,
})
}
}
},
fail() {
wx.hideToast(); //关闭提示框
_this.setData({
uploadImageList: [],
isUploading: false
})
wx.showToast({
title: '图片上传失败,请重试~',
icon: 'none',
duration: 1500
})
}
})
})(index)
})
},
fail(res) {
console.log('取消的函数', res)
wx.hideToast(); //关闭提示框
_this.setData({
isUploading: false,
})
}
})
}
return true
},
cancelText: '取消',
cancel() {
wx.hideToast(); //关闭提示框
_this.setData({
isUploading: false
})
},
destructiveButtonClicked() {},
})
},
/********************内容及图片************ */
// 提示
showWords() {
if (this.data.showflag.indexOf(true) == -1) {
console.log('不包含是')
let words = [`周报:第${this.data.weeklyList.length+1}项未填写,请完善周报告`]
if (!this.data.noteContent) {
wx.showModal({
title: '填写未完成',
content: `${words}`,
confirmText: '继续填写',
confirmColor: '#01C15C',
showCancel: false,
success(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
return true
}
return false
} else {
console.log('包含是')
let words = []
// 判断是否
let test = false;
this.data.weeklyList.forEach((element, index) => {
// 如果是true,就去判断内容是否填写完整
if (this.data.showflag[index]) {
this.data.weeklyList[index].totalList.forEach((childItem, childIndex) => {
if (childItem.mustFlag == 1) {
// 1 是必填
if (childItem.total == 0) {
test = true;
let showToast = `${index+1}`
words.push(showToast)
}
} else if (childItem.mustFlag == 0) {
if (childItem.total !== 0) {
return false
}
}
})
}
})
if (!this.data.noteContent) {
words.push(`${this.data.weeklyList.length + 1}`)
test = true;
}
if (test) {
words.push('未填写,请完善周报告')
let newWord = Array.from(new Set(words)).join('、')
let str = "周报:"
let newWords = str.concat(newWord)
wx.showModal({
title: '填写未完成',
content: `${newWords.replace(/(.*)、/,'$1')}`,
confirmText: '继续填写',
confirmColor: '#01C15C',
showCancel: false,
success(res) {
if (res.confirm) {
console.log('用户点击确定')
}
}
})
return true
}
}
},
// 获得周报选项
async getweekRecordOptionList() {
try {
let {
data
} = await getweekRecordOptionList()
// data.forEach((element) => {
// element.totalDescribe = element.totalDescribe + ' ?'
// })
this.setData({
weeklyList: data
})
// 填充是否
let showflag = new Array(this.data.weeklyList.length).fill(true)
this.setData({
showflag,
})
} catch (err) {
}
},
// 获得图片路径
getImagesUrl() {
let images = [];
for (var i = 0; i < this.data.uploadImageList.length; i++) {
const imgUrl = this.data.uploadImageList[i].ossUrl
const thumbnail = this.data.uploadImageList[i].thumbnail
images.push({
thumbnail: thumbnail,
imgUrl: imgUrl
})
}
this.setData({
images,
})
},
// 提交接口
async submitWeekRocord() {
// 拿到图片地址
this.getImagesUrl()
// 判断是否
let totalData = []
this.data.weeklyList.forEach((element, index) => {
let totalList = []
// 如果是true,就去判断内容是否填写完整
if (this.data.showflag[index]) {
this.data.weeklyList[index].totalList.forEach((childItem, childIndex) => {
totalList.push({
totalConfigId: childItem.totalConfigId,
relationId: childItem.relationId,
total: childItem.total
})
})
totalData.push({
resourceId: element.resourceId,
totalList
})
}
})
console.log('totalData', totalData)
let obj = {
content: this.data.noteContent,
totalData,
images: this.data.images
}
obj.totalData = totalData
console.log('obj', obj)
try {
await submitWeekRocord(obj)
wx.hideLoading()
wx.showModal({
title: '', //提示的标题,
content: '工作内容已提交,请前往"我的足迹"查看内容', //提示的内容,
showCancel: false, //是否显示取消按钮,
cancelText: '取消', //取消按钮的文字,默认为取消,最多 4 个字符,
cancelColor: '#000000', //取消按钮的文字颜色,
confirmText: '确定', //确定按钮的文字,默认为取消,最多 4 个字符,
confirmColor: '#3CC51F', //确定按钮的文字颜色,
success: res => {
if (res.confirm) {
wx.reLaunch({
url: '/pages/index/index'
});
} else if (res.cancel) {
this.setData({
isPublish: false
})
}
}
});
} catch (err) {
this.setData({
isPublish: false
})
}
},
// 更新周报
async updateWeekRocord() {
// 拿到图片地址
this.getImagesUrl()
// 判断是否
let totalData = []
this.data.weeklyList.forEach((element, index) => {
let totalList = []
// 如果是true,就去判断内容是否填写完整
if (this.data.showflag[index]) {
this.data.weeklyList[index].totalList.forEach((childItem, childIndex) => {
totalList.push({
totalConfigId: childItem.totalConfigId,
relationId: childItem.relationId,
total: childItem.total
})
})
totalData.push({
resourceId: element.resourceId,
totalList
})
}
})
console.log('totalData', totalData)
let obj = {
id: this.data.id,
content: this.data.noteContent,
totalData,
images: this.data.images
}
obj.totalData = totalData
console.log('obj', obj)
try {
await updateWeekRocord(obj)
wx.hideLoading()
wx.showModal({
title: '', //提示的标题,
content: '工作内容已提交,请前往"我的足迹"查看内容', //提示的内容,
showCancel: false, //是否显示取消按钮,
cancelText: '取消', //取消按钮的文字,默认为取消,最多 4 个字符,
cancelColor: '#000000', //取消按钮的文字颜色,
confirmText: '确定', //确定按钮的文字,默认为取消,最多 4 个字符,
confirmColor: '#3CC51F', //确定按钮的文字颜色,
success: res => {
if (res.confirm) {
wx.reLaunch({
url: '/pages/index/index'
});
} else if (res.cancel) {
this.setData({
isPublish: false
})
}
}
});
} catch (err) {
this.setData({
isPublish: false
})
}
},
// 获得编辑周报详情
async getWeekRecordDetail() {
let {
data
} = await getWeekRecordDetail(this.data.infoId)
let uploadImageList = []
data.images.forEach((element) => {
uploadImageList.push({
uploaded: true,
ossUrl: element.imgUrl,
imgUrl: element.thumbnail,
imageId: ++this.data.imageId,
thumbnail: element.thumbnail,
})
})
this.setData({
// weeklyList: data.totalData,
noteContent: data.content,
uploadImageList,
showflag: [],
id: data.id
})
// 填充是否
let showflag = []
let weeklyList = data.totalData
weeklyList.forEach((element, index) => {
element.totalDescribe = element.totalDescribe.replace('{0}', '是否') + ' ?'
element.totalList.forEach((item, itemIndex) => {
if (item.total !== '') {
showflag[index] = true
item.total = item.total.slice(0, 1)
} else if (item.total == '') {
showflag[index] = false
}
})
})
this.setData({
showflag,
weeklyList
})
},
// 发布
async goPublish() {
if (this.data.showflag.includes(true)) {
console.log('包含是')
}
// 判断内容是否输入
if (this.showWords()) {
return
}
if (this.data.isUploading) {
wx.showModal({
title: '', //提示的标题,
content: '图片正在发布中,请稍等', //提示的内容,
showCancel: false, //是否显示取消按钮,
confirmText: '确定', //确定按钮的文字,默认为取消,最多 4 个字符,
confirmColor: '#3CC51F', //确定按钮的文字颜色,
success: res => {
if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
return
}
wx.showLoading({
title: '发布中',
mask: true
})
if (this.data.isPublish) {
return
}
if (this.data.weeklyType == 'publish') {
this.setData({
isPublish: true,
})
await this.submitWeekRocord()
} else if (this.data.weeklyType == 'change') {
this.setData({
isPublish: true,
})
await this.updateWeekRocord()
}
},
})