623 lines
22 KiB
JavaScript
623 lines
22 KiB
JavaScript
'use strict';
|
|
const {
|
|
verify
|
|
} = require("crypto");
|
|
var CryptoJS = require("crypto-js");
|
|
var vk; // 全局vk实例
|
|
// 涉及的表名
|
|
const dbName = require("../../dao/config.js")
|
|
|
|
var db = uniCloud.database(); // 全局数据库引用
|
|
var _ = db.command; // 数据库操作符
|
|
var $ = _.aggregate; // 聚合查询操作符
|
|
/**
|
|
* 权限注意:访问以下链接查看
|
|
* 文档地址:https://vkdoc.fsq.pub/client/uniCloud/cloudfunctions/cloudObject.html#内置权限
|
|
*/
|
|
var cloudObject = {
|
|
isCloudObject: true, // 标记为云对象模式
|
|
/**
|
|
* 请求前处理,主要用于调用方法之前进行预处理,一般用于拦截器、统一的身份验证、参数校验、定义全局对象等。
|
|
* 文档地址:https://vkdoc.fsq.pub/client/uniCloud/cloudfunctions/cloudObject.html#before-预处理
|
|
*/
|
|
_before: async function () {
|
|
vk = this.vk; // 将vk定义为全局对象
|
|
// let { customUtil, uniID, config, pubFun } = this.getUtil(); // 获取工具包
|
|
},
|
|
/**
|
|
* 请求后处理,主要用于处理本次调用方法的返回结果或者抛出的错误
|
|
* 文档地址:https://vkdoc.fsq.pub/client/uniCloud/cloudfunctions/cloudObject.html#after-后处理
|
|
*/
|
|
_after: async function (options) {
|
|
let {
|
|
err,
|
|
res
|
|
} = options;
|
|
if (err) {
|
|
return; // 如果方法抛出错误,直接return;不处理
|
|
}
|
|
return res;
|
|
},
|
|
/**
|
|
* 获取列表
|
|
* @url client/fadada.getList 前端调用的url参数地址
|
|
*/
|
|
getList: async function (data) {
|
|
let res = {
|
|
code: 0,
|
|
msg: ''
|
|
};
|
|
let {
|
|
uid
|
|
} = this.getClientInfo(); // 获取客户端信息
|
|
// 业务逻辑开始-----------------------------------------------------------
|
|
|
|
|
|
// 业务逻辑结束-----------------------------------------------------------
|
|
return res;
|
|
},
|
|
/**
|
|
* 模板函数
|
|
* @url client/fadada.test 前端调用的url参数地址
|
|
*/
|
|
test: async function (data) {
|
|
|
|
},
|
|
/**
|
|
* 实名认证异步通知回调
|
|
* @param {Object} data
|
|
*/
|
|
async pub_verifyNotify(data) {
|
|
let {
|
|
customUtil,
|
|
uniID,
|
|
config,
|
|
pubFun
|
|
} = this.getUtil(); // 获取工具包
|
|
console.log('fadada res:', data)
|
|
const {
|
|
customerId
|
|
} = data;
|
|
// 查询认证结果
|
|
let user = await vk.baseDao.findByWhereJson({
|
|
dbName: "uni-id-users",
|
|
whereJson: {
|
|
fadadaCustomerId: customerId,
|
|
}
|
|
});
|
|
let res, fadadaConfirm = false
|
|
let authStatus = 1
|
|
if (user.accountType === 1) {
|
|
res = await pubFun.fadada('find_personCertInfo.api', {
|
|
verified_serialno: user.fadadaVerify.transactionNo
|
|
})
|
|
console.log('fadada check res:', res)
|
|
// 认证结果
|
|
const {person} = res.data
|
|
if (res.code !== 1 || ![2, 3].includes(parseInt(person.status))) {
|
|
return
|
|
}
|
|
fadadaConfirm = parseInt(person.status) === 2
|
|
} else {
|
|
res = await pubFun.fadada('find_companyCertInfo.api', {
|
|
verified_serialno: user.fadadaVerify.transactionNo
|
|
})
|
|
console.log('fadada check res:', res)
|
|
// 认证结果
|
|
const {company, manager} = res.data
|
|
if (res.code !== 1 || ![3, 4].includes(parseInt(company.status)) || ![2, 3].includes(parseInt(manager.status))) {
|
|
return
|
|
}
|
|
fadadaConfirm = parseInt(company.status) === 4 && parseInt(manager.status) === 2
|
|
if (parseInt(company.status) === 3) authStatus = 0
|
|
}
|
|
|
|
|
|
// 认证成功
|
|
await vk.baseDao.setById({
|
|
dbName: "fadada-verify-record",
|
|
dataJson: {
|
|
_id: user._id,
|
|
...res.data
|
|
}
|
|
})
|
|
|
|
|
|
await vk.baseDao.updateById({
|
|
dbName: "uni-id-users",
|
|
id: user._id,
|
|
dataJson: {
|
|
person: {
|
|
...res.data.person,
|
|
addressRegion: {
|
|
province: {},
|
|
city: {},
|
|
area: {},
|
|
}
|
|
},
|
|
company: {
|
|
...res.data.company, ...{
|
|
mainSpotCategoryIds: [],
|
|
addressRegion: {
|
|
province: {},
|
|
city: {},
|
|
area: {},
|
|
}
|
|
}
|
|
},
|
|
manager: res.data.manager,
|
|
bankCard: res.data.bankCard,
|
|
fadadaConfirm,
|
|
// status: 1,
|
|
authType: 1,
|
|
// authStatus: 0,
|
|
userLevel: 0,
|
|
userGrade: 0,
|
|
authSubmitTime: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
|
|
}
|
|
});
|
|
|
|
if (user.inviter_uid && user.inviter_uid.length > 0) {
|
|
await vk.baseDao.updateById({
|
|
dbName: "uni-id-users",
|
|
id: user.inviter_uid[0],
|
|
dataJson: {
|
|
inviteNum: _.inc(1)
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
return res
|
|
},
|
|
/**
|
|
* 合同签署回调处理
|
|
* @param {Object} data
|
|
*/
|
|
async pub_contractNotify(data) {
|
|
let {
|
|
customUtil,
|
|
uniID,
|
|
config,
|
|
pubFun
|
|
} = this.getUtil(); // 获取工具包
|
|
// 开启事务
|
|
const transaction = await vk.baseDao.startTransaction();
|
|
|
|
const {
|
|
transaction_id,
|
|
contract_id,
|
|
timestamp,
|
|
result_code,
|
|
download_url,
|
|
viewpdf_url
|
|
} = data
|
|
try {
|
|
if (parseInt(result_code) == 3000) {
|
|
// 获取用户信息
|
|
const userContract = await vk.daoCenter.userContractDao.updateAndReturn({
|
|
db: transaction,
|
|
whereJson: {
|
|
transactionId: transaction_id,
|
|
sign: false
|
|
},
|
|
dataJson: {
|
|
sign: true,
|
|
signedAt: timestamp,
|
|
}
|
|
})
|
|
|
|
let contract = await vk.daoCenter.contractDao.updateAndReturn({
|
|
db: transaction,
|
|
whereJson: {
|
|
_id: userContract.contractId,
|
|
status: 1,
|
|
},
|
|
dataJson: {
|
|
signCount: _.inc(1),
|
|
contractHistory: _.push(download_url)
|
|
}
|
|
})
|
|
|
|
if (userContract.userId === contract.userId) {
|
|
const userInfo = await vk.baseDao.findByWhereJson({
|
|
dbName: 'uni-id-users',
|
|
whereJson: {
|
|
_id: userContract.userId
|
|
}
|
|
})
|
|
let otherList = await vk.daoCenter.userContractDao.selects({
|
|
whereJson: {
|
|
contractId: userContract.contractId,
|
|
}
|
|
}
|
|
)
|
|
otherList.rows = otherList.rows.filter(item => item.userId !== userContract.userId)
|
|
for (let i = 0; i < otherList.rows.length; i++) {
|
|
const thirdUserItem = otherList.rows[i]
|
|
const thirdUser = await vk.baseDao.findByWhereJson({
|
|
dbName: 'uni-id-users',
|
|
whereJson: {
|
|
_id: thirdUserItem.userId
|
|
}
|
|
})
|
|
if (thirdUser !== null) {
|
|
await pubFun.sendNotifySms(thirdUser._id, 'SMS_474570002', {
|
|
name: thirdUser.accountType === 2 ? thirdUser.company.companyName : thirdUser.person.personName,
|
|
name2: userInfo.accountType === 2 ? userInfo.company.companyName : userInfo.person.personName,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
if (contract.type !== 'sign') {
|
|
// 用户交易额
|
|
const user = await vk.baseDao.updateAndReturn({
|
|
dbName: "uni-id-users",
|
|
whereJson: {
|
|
_id: userContract.userId
|
|
},
|
|
dataJson: {
|
|
transactionAmount: _.inc(parseFloat(contract.amount))
|
|
}
|
|
})
|
|
|
|
if (user.userLevel > -1) {
|
|
const userLevel = await vk.baseDao.findByWhereJson({
|
|
dbName: 'jjx-user-level',
|
|
whereJson: {
|
|
level: user.userLevel
|
|
},
|
|
})
|
|
await vk.baseDao.update({
|
|
dbName: "uni-id-users",
|
|
whereJson: {
|
|
_id: userContract.userId
|
|
},
|
|
dataJson: {
|
|
userGrade: Math.floor(user.transactionAmount / parseFloat(userLevel.upgradeAmount))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
if (contract.signCount == contract.partyCount) {
|
|
let sheetId = null
|
|
contract = await vk.daoCenter.contractDao.updateAndReturn({
|
|
db: transaction,
|
|
whereJson: {
|
|
_id: userContract.contractId,
|
|
status: 1
|
|
},
|
|
dataJson: {
|
|
status: 2
|
|
}
|
|
})
|
|
|
|
console.log('contract.type', contract.type)
|
|
|
|
// 合同归档
|
|
const contractFilingRes = await pubFun.fadada('contractFiling.api', {
|
|
contract_id: contract.transactionId
|
|
})
|
|
// console.log('contractFilingRes', contractFilingRes)
|
|
|
|
// 添加订单
|
|
// type 4: 询价 -- enquiry
|
|
// type 5: 加工 -- process
|
|
// type 6: 旧货 -- old
|
|
const otherContract = await vk.baseDao.findByWhereJson({
|
|
dbName: 'jjx-user-contract',
|
|
whereJson: {
|
|
contractId: userContract.contractId,
|
|
role: userContract.role === 'seller' ? 'buyer' : 'seller'
|
|
}
|
|
})
|
|
let sellerUid = null, buyerUid = null
|
|
if (userContract.role === 'seller') {
|
|
sellerUid = userContract.userId
|
|
buyerUid = otherContract.userId
|
|
} else {
|
|
sellerUid = otherContract.userId
|
|
buyerUid = userContract.userId
|
|
}
|
|
let type = null, goodsName = null, makeOrder = false, sheet = null
|
|
if (contract.type === 'enquiry') {
|
|
type = 4
|
|
goodsName = '询价单'
|
|
sheet = await vk.baseDao.updateAndReturn({
|
|
dbName: "jjx-enquiry-sheet",
|
|
whereJson: {
|
|
_id: contract.sheetId
|
|
},
|
|
dataJson: {
|
|
status: 2
|
|
}
|
|
})
|
|
sheetId = contract.sheetId
|
|
if (sheet.tradeMethod === 1) makeOrder = true
|
|
} else if (contract.type === 'process') {
|
|
type = 5
|
|
goodsName = '加工单'
|
|
sheet = await vk.baseDao.updateAndReturn({
|
|
dbName: "jjx-process-sheet",
|
|
whereJson: {
|
|
_id: contract.sheetId
|
|
},
|
|
dataJson: {
|
|
status: 2
|
|
}
|
|
})
|
|
sheetId = contract.sheetId
|
|
if (sheet.tradeMethod === 1) makeOrder = true
|
|
} else if (contract.type === 'old') {
|
|
await vk.baseDao.updateAndReturn({
|
|
dbName: "jjx-old",
|
|
whereJson: {
|
|
_id: contract.sheetId
|
|
},
|
|
dataJson: {
|
|
status: 2
|
|
}
|
|
})
|
|
} else if (contract.type === 'sign') {
|
|
if (contract.tradeMethod === 1) {
|
|
makeOrder = true
|
|
type = 14
|
|
goodsName = '金久信签约宝'
|
|
}
|
|
} else if (contract.type === 'self-support') {
|
|
makeOrder = true
|
|
type = 15
|
|
goodsName = '金久信自营询价'
|
|
sheetId = contract.sheetId
|
|
}
|
|
if (makeOrder) {
|
|
const orderId = await vk.baseDao.add({
|
|
dbName: "jjx-order",
|
|
dataJson: {
|
|
orderNo: vk.pubfn.createOrderNo("39XU", 20),
|
|
uid: buyerUid,
|
|
sellerUid,
|
|
orderStatus: 0,
|
|
payStatus: 0,
|
|
type,
|
|
amount: contract.amount,
|
|
offAmount: contract.amount,
|
|
signMethod: 0,
|
|
contractId: contract._id,
|
|
sheetId
|
|
}
|
|
})
|
|
|
|
await vk.baseDao.add({
|
|
dbName: "jjx-order-goods",
|
|
dataJson: {
|
|
orderId,
|
|
goodsId: null,
|
|
num: 1,
|
|
goodsName,
|
|
goodsPrice: contract.amount,
|
|
goodsCover: null,
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
await transaction.commit();
|
|
} catch (e) {
|
|
await transaction.rollback();
|
|
console.log('fadada err:', e)
|
|
return false
|
|
}
|
|
return data;
|
|
},
|
|
/**
|
|
* 获取用户认证结果
|
|
*/
|
|
async getVerifyResult() {
|
|
let {
|
|
customUtil,
|
|
uniID,
|
|
config,
|
|
pubFun
|
|
} = this.getUtil(); // 获取工具包
|
|
let res = {
|
|
code: 0,
|
|
msg: ''
|
|
};
|
|
let {
|
|
uid
|
|
} = this.getClientInfo(); // 获取客户端信息
|
|
let {
|
|
accountType,
|
|
fadadaCustomerId
|
|
} = await this.getUserInfo();
|
|
|
|
res.data = await vk.baseDao.findById({
|
|
dbName: "fadada-verify-record",
|
|
id: uid
|
|
});
|
|
|
|
return res
|
|
},
|
|
/**
|
|
* 获取实名认证地址
|
|
*/
|
|
async getVerifyUrl(event) {
|
|
let {
|
|
customUtil,
|
|
uniID,
|
|
config,
|
|
pubFun
|
|
} = this.getUtil(); // 获取工具包
|
|
let res = {
|
|
code: 0,
|
|
msg: ''
|
|
};
|
|
let {
|
|
uid
|
|
} = this.getClientInfo(); // 获取客户端信息
|
|
let {
|
|
accountType,
|
|
fadadaCustomerId,
|
|
fadadaVerify
|
|
} = await this.getUserInfo();
|
|
// 业务逻辑开始-----------------------------------------------------------
|
|
|
|
if (fadadaVerify && !event.reset) {
|
|
res.data = fadadaVerify
|
|
return res;
|
|
}
|
|
|
|
|
|
let verify_url_res;
|
|
|
|
let return_url = event.isMobile ? 'https://m.jinjiuxinzg.cn/pages/user/user' : 'https://www.jinjiuxinzg.cn/#/pages/user/user?menu=fadada'
|
|
if (event.isSignOnly) {
|
|
return_url = event.isMobile ? 'https://m.jinjiuxinzg.cn/signOnly/#/' : 'https://www.jinjiuxinzg.cn/signOnly/#/pages/user/user?menu=fadada'
|
|
}
|
|
let notify_url = 'https://tcb-n44byxi8bmrfrki-d4bn1c4a206d.service.tcloudbase.com/fadada_verify_notify'
|
|
if (accountType === 1) {
|
|
verify_url_res = await pubFun.fadada('get_person_verify_url.api', {
|
|
customer_id: fadadaCustomerId,
|
|
verified_way: 2,
|
|
page_modify: 1,
|
|
cert_flag: 1,
|
|
// notify_url: 'https://fc-mp-0737e52b-fffa-4223-bcdf-d4ad3d9968a5.next.bspapp.com/fadada_verify_notify',
|
|
// return_url: 'https://static-mp-0737e52b-fffa-4223-bcdf-d4ad3d9968a5.next.bspapp.com/#/pages/user/user?menu=fadada'
|
|
notify_url,
|
|
return_url
|
|
|
|
})
|
|
} else {
|
|
verify_url_res = await pubFun.fadada('get_company_verify_url.api', {
|
|
customer_id: fadadaCustomerId,
|
|
page_modify: 1,
|
|
cert_flag: 1,
|
|
// notify_url: 'https://fc-mp-0737e52b-fffa-4223-bcdf-d4ad3d9968a5.next.bspapp.com/fadada_verify_notify',
|
|
// return_url: 'https://static-mp-0737e52b-fffa-4223-bcdf-d4ad3d9968a5.next.bspapp.com/#/pages/user/user?menu=fadada'
|
|
notify_url,
|
|
return_url
|
|
})
|
|
}
|
|
|
|
console.log('verify_url_data: ', {
|
|
customer_id: fadadaCustomerId,
|
|
verified_way: 2,
|
|
page_modify: 1,
|
|
cert_flag: 1,
|
|
// notify_url: 'https://fc-mp-0737e52b-fffa-4223-bcdf-d4ad3d9968a5.next.bspapp.com/fadada_verify_notify',
|
|
// return_url: 'https://static-mp-0737e52b-fffa-4223-bcdf-d4ad3d9968a5.next.bspapp.com/#/pages/user/user?menu=fadada'
|
|
notify_url,
|
|
return_url
|
|
|
|
});
|
|
console.log('verify_url_res: ', verify_url_res);
|
|
let {
|
|
transactionNo,
|
|
url
|
|
} = verify_url_res.data
|
|
var parsedWordArray = CryptoJS.enc.Base64.parse(url);
|
|
url = parsedWordArray.toString(CryptoJS.enc.Utf8);
|
|
|
|
await vk.baseDao.update({
|
|
dbName: "uni-id-users",
|
|
whereJson: {
|
|
_id: uid
|
|
},
|
|
dataJson: {
|
|
fadadaVerify: {
|
|
transactionNo,
|
|
url
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
res.data = {
|
|
transactionNo,
|
|
url
|
|
}
|
|
|
|
// 业务逻辑结束-----------------------------------------------------------
|
|
return res;
|
|
|
|
},
|
|
/**
|
|
* 调用此接口将customer_id与实名交易号进行绑定,
|
|
* 绑定成功后该customer_id无法再次绑定其它不同主体认证交易号。
|
|
* 必须绑定成功后续才可发起签署
|
|
*/
|
|
async applyCert({
|
|
customerId,
|
|
transactionNo
|
|
}) {
|
|
let {
|
|
customUtil,
|
|
uniID,
|
|
config,
|
|
pubFun
|
|
} = this.getUtil(); // 获取工具包
|
|
},
|
|
async generateContract() {
|
|
let {
|
|
customUtil,
|
|
uniID,
|
|
config,
|
|
pubFun
|
|
} = this.getUtil(); // 获取工具包
|
|
let res = {
|
|
code: 0,
|
|
msg: ''
|
|
};
|
|
let {
|
|
uid
|
|
} = this.getClientInfo(); // 获取客户端信息
|
|
const contract_id = vk.pubfn.createOrderNo("Contract", 25);
|
|
const parameter_map = {
|
|
saler: '网宿科技'
|
|
};
|
|
const dynamic_tables = [{
|
|
headers: ["采购商品", "采购数量", "商品单位"], //表头,
|
|
datas: [
|
|
["螺纹钢", "200", "米"]
|
|
],
|
|
pageBegin: -1
|
|
}]
|
|
const params = {
|
|
template_id: 'sale',
|
|
doc_title: '测试买卖合同',
|
|
contract_id: contract_id,
|
|
fill_type: 0, // 填充类型 0 pdf 模板、1 在线填充模板
|
|
parameter_map: JSON.stringify(parameter_map), //填充内容
|
|
dynamic_tables: JSON.stringify(dynamic_tables), //填充表格
|
|
}
|
|
console.log(params);
|
|
res.data = await pubFun.fadada('generate_contract.api', params)
|
|
return res;
|
|
},
|
|
async getFile({uuid}) {
|
|
let {
|
|
customUtil,
|
|
uniID,
|
|
config,
|
|
pubFun
|
|
} = this.getUtil();
|
|
const res = {
|
|
code: 0,
|
|
msg: ''
|
|
}
|
|
const file = await pubFun.fadada('get_file.api', {
|
|
uuid
|
|
})
|
|
res.msg = await vk.uploadFile({
|
|
title: "上传中...",
|
|
file,
|
|
needSave: true,
|
|
});
|
|
return res
|
|
}
|
|
}
|
|
|
|
module.exports = cloudObject; |