Files
2026-10-08 18:18:21 +08:00

221 lines
8.4 KiB
JavaScript

'use strict';
const dbName = require('../../../../dao/config.js')
module.exports = {
/**
* 发起/创建合同
* @url client/contract/kh/launch 前端调用的url参数地址
* data 请求参数
* @param {String} params1 参数1
*/
main: async (event) => {
let {data = {}, userInfo, util, filterResponse, originalParam} = event;
let {customUtil, uniID, config, pubFun, vk, db, _} = util;
let {uid, contractId, title, contractFile, otherFile, signEndDate, buyerSignKeyword, sellerSignKeyword} = data;
let res = {code: 0, msg: ""};
// 业务逻辑开始-----------------------------------------------------------
const otherUser = await vk.baseDao.findByWhereJson({
dbName: 'jjx-user-contract',
whereJson: {
contractId: contractId,
userId: _.neq(uid)
}
})
if (data.thirdList && data.thirdList.length > 0) {
for (let i = 0; i < data.thirdList.length; i++) {
if (data.thirdList[i].phone) {
const thirdUser = await vk.baseDao.findByWhereJson({
dbName: 'uni-id-users',
whereJson: {
mobile: data.thirdList[i].phone
}
})
if (!thirdUser) return {
code: -1,
msg: `该用户:${data.thirdList[i].phone}不存在`
}
if (!thirdUser.fadadaConfirm) return {
code: -1,
msg: `该用户:${data.thirdList[i].phone}未通过法大大认证,无法签约`
}
if (thirdUser.uid === uid) return {
code: -1,
msg: '第三方不能为自己'
}
if (thirdUser.uid === otherUser._id) return {
code: -1,
msg: '第三方不能与对方为相同账号'
}
}
}
}
const contract = await vk.baseDao.findByWhereJson({
dbName: dbName.contract,
whereJson: {
_id: contractId,
userId: uid
}
})
console.log('contract: ', contract);
let newTransactionId = vk.pubfn.createOrderNo("C", 20)
let doc_url = contractFile
if (Array.isArray(doc_url)) doc_url = doc_url[doc_url.length - 1]
const uploaddocs = await pubFun.fadada('uploaddocs.api', {
contract_id: newTransactionId,
doc_title: title,
doc_url,
// file: doc_url,
doc_type: '.pdf'
})
let otherFileResList = []
// 存在附件则合并
if (otherFile && otherFile.length > 0) {
for (let i = 0; i < otherFile.length; i++) {
otherFileResList.push(newTransactionId + i.toString())
await pubFun.fadada('uploaddocs.api', {
contract_id: newTransactionId + i.toString(),
doc_title: title + `:附件${i + 1}`,
doc_url: otherFile[i],
doc_type: '.pdf'
})
}
otherFileResList.unshift(newTransactionId)
// 合并接口
newTransactionId = vk.pubfn.createOrderNo("MC", 20)
const mergeRes = await pubFun.fadada('merge_contract.api', {
contract_id: newTransactionId,
doc_title: title,
contract_ids: otherFileResList.join()
})
console.log('mergeRes', mergeRes)
console.log('otherFileResList', otherFileResList)
}
await vk.baseDao.update({
dbName: dbName.contract,
whereJson: {
_id: contractId
},
dataJson: {
transactionId: newTransactionId,
}
})
if (parseInt(uploaddocs.code) === 1000) {
let partyCount = 2
if (data.thirdList && data.thirdList.length > 0) partyCount += data.thirdList.filter(item => !!item.phone).length
const contract = await vk.baseDao.updateAndReturn({
dbName: dbName.contract,
whereJson: {
_id: contractId,
userId: uid
},
dataJson: {
status: 1,
docTitle: title,
signEndDate,
otherFile,
partyCount,
contractBase: contractFile,
buyerSignKeyword,
sellerSignKeyword
}
})
if (contract.type === 'enquiry') {
await vk.baseDao.update({
dbName: 'jjx-enquiry-sheet',
whereJson: {
_id: contract.sheetId
},
dataJson: {
status: 2,
}
})
}
// const otherUser = await vk.baseDao.findByWhereJson({
// dbName: 'jjx-user-contract',
// whereJson: {
// contractId: contractId,
// userId: _.neq(uid)
// }
// })
await vk.baseDao.add({
dbName: "jjx-message",
dataJson: {
uid: otherUser.userId,
title: `您有新的合同待签署`,
content: `您有新的合同待签署,赶紧去签合同吧`,
type: 'contract',
hasRead: false,
}
});
// await pubFun.sendNotifySms(otherUser.userId, 'SMS_474525001')
await vk.baseDao.del({
dbName: 'jjx-user-contract',
whereJson: {
contractId,
role: 'third',
}
})
await vk.baseDao.del({
dbName: 'jjx-message',
whereJson: {
contractId,
type: 'contract',
isThird: true
}
})
if (data.thirdList && data.thirdList.length > 0) {
console.log('data.thirdList', data.thirdList)
for (let i = 0; i < data.thirdList.length; i++) {
if (data.thirdList[i].phone) {
const thirdUser = await vk.baseDao.findByWhereJson({
dbName: 'uni-id-users',
whereJson: {
mobile: data.thirdList[i].phone
}
})
if (thirdUser) {
await vk.daoCenter.userContractDao.adds([
{
transactionId: vk.pubfn.createOrderNo("ST", 20),
userId: thirdUser.uid,
role: 'third',
sign: false,
contractId,
keyword: data.thirdList[i].signKeyword,
},
])
await vk.baseDao.add({
dbName: "jjx-message",
dataJson: {
uid: thirdUser.uid,
title: `合同`,
content: `您有合同待签署,赶紧去签订合同吧`,
type: 'contract',
hasRead: false,
contractId,
isThird: true
}
});
}
// await pubFun.sendNotifySms(thirdUser._id, 'SMS_474525001')
}
}
}
res.data = uploaddocs.result
} else {
return {
code: -1,
msg: uploaddocs.msg
}
}
// 业务逻辑结束-----------------------------------------------------------
return res;
}
}