|
|
|
@ -66,46 +66,29 @@ class JiandaoyunService { |
|
|
|
|
|
|
|
// 使用简道云API v1的正确值格式,使用value字段来包装值
|
|
|
|
jiandaoyunData[mapping.userId] = { value: user.userId || '' }; |
|
|
|
jiandaoyunData[mapping.company] = { value: user.company || '' }; |
|
|
|
jiandaoyunData[mapping.nickName] = { value: user.name || '' }; |
|
|
|
jiandaoyunData[mapping.nickName] = { value: user.nickName || '' }; |
|
|
|
jiandaoyunData[mapping.phoneNumber] = { value: user.phoneNumber || '' }; |
|
|
|
jiandaoyunData[mapping.type] = { value: user.type || '' }; |
|
|
|
jiandaoyunData[mapping.city] = { value: user.city || '' }; |
|
|
|
|
|
|
|
// 转换cart_items数据(buyer)
|
|
|
|
const cartItems = databaseData.cartItems; |
|
|
|
console.log('购物车数据:', JSON.stringify(cartItems, null, 2)); |
|
|
|
|
|
|
|
if (cartItems.length > 0) { |
|
|
|
const firstCartItem = cartItems[0]; |
|
|
|
jiandaoyunData[mapping['productName-buyer']] = { value: firstCartItem.productName || '' }; |
|
|
|
jiandaoyunData[mapping['specification-buyer']] = { value: firstCartItem.specification || '' }; |
|
|
|
jiandaoyunData[mapping['quantity-buyer']] = { value: firstCartItem.quantity || '' }; |
|
|
|
// 计算毛重:数量 * 规格中的克数
|
|
|
|
const specification = firstCartItem.specification || ''; |
|
|
|
const weightPerUnit = parseInt(specification.match(/(\d+)克/)?.[1] || '0'); |
|
|
|
const quantity = parseInt(firstCartItem.quantity || '0'); |
|
|
|
const grossWeight = weightPerUnit * quantity; |
|
|
|
jiandaoyunData[mapping['grossWeight-buyer']] = { value: grossWeight.toString() }; |
|
|
|
jiandaoyunData[mapping['yolk-buyer']] = { value: firstCartItem.yolk || '' }; |
|
|
|
jiandaoyunData[mapping.authorizedRegion] = { value: user.authorizedRegion || '' }; |
|
|
|
|
|
|
|
// 转换负责人信息
|
|
|
|
const userManagement = databaseData.userManagement; |
|
|
|
console.log('负责人数据:', JSON.stringify(userManagement, null, 2)); |
|
|
|
if (userManagement) { |
|
|
|
jiandaoyunData[mapping.userName] = { value: userManagement.userName || '' }; |
|
|
|
} |
|
|
|
|
|
|
|
// 转换products数据(sell)
|
|
|
|
// 转换产品数据
|
|
|
|
const products = databaseData.products; |
|
|
|
console.log('产品数据:', JSON.stringify(products, null, 2)); |
|
|
|
|
|
|
|
if (products.length > 0) { |
|
|
|
const firstProduct = products[0]; |
|
|
|
jiandaoyunData[mapping['productName-sell']] = { value: firstProduct.productName || '' }; |
|
|
|
jiandaoyunData[mapping['specification-sell']] = { value: firstProduct.specification || '' }; |
|
|
|
jiandaoyunData[mapping['quantity-sell']] = { value: firstProduct.quantity || '' }; |
|
|
|
// 计算毛重:数量 * 规格中的克数
|
|
|
|
const specification = firstProduct.specification || ''; |
|
|
|
const weightPerUnit = parseInt(specification.match(/(\d+)克/)?.[1] || '0'); |
|
|
|
const quantity = parseInt(firstProduct.quantity || '0'); |
|
|
|
const grossWeight = weightPerUnit * quantity; |
|
|
|
jiandaoyunData[mapping['grossWeight-sell']] = { value: grossWeight.toString() }; |
|
|
|
jiandaoyunData[mapping['yolk-sell']] = { value: firstProduct.yolk || '' }; |
|
|
|
jiandaoyunData[mapping.productName] = { value: firstProduct.productName || '' }; |
|
|
|
jiandaoyunData[mapping.specification] = { value: firstProduct.specification || '' }; |
|
|
|
jiandaoyunData[mapping.quantity] = { value: firstProduct.quantity || '' }; |
|
|
|
jiandaoyunData[mapping.grossWeight] = { value: firstProduct.grossWeight || '' }; |
|
|
|
jiandaoyunData[mapping.yolk] = { value: firstProduct.yolk || '' }; |
|
|
|
} |
|
|
|
|
|
|
|
console.log('转换后的数据:', JSON.stringify(jiandaoyunData, null, 2)); |
|
|
|
@ -117,6 +100,9 @@ class JiandaoyunService { |
|
|
|
async isPhoneNumberExists(phoneNumber) { |
|
|
|
try { |
|
|
|
const mapping = config.fieldMapping; |
|
|
|
console.log(`检查电话号码 ${phoneNumber} 是否存在于简道云表单中...`); |
|
|
|
console.log(`使用的字段映射: ${mapping.phoneNumber}`); |
|
|
|
|
|
|
|
const url = `${this.baseUrl}/api/v1/app/${this.appId}/entry/${this.entryId}/data_list`; |
|
|
|
const headers = { |
|
|
|
'Content-Type': 'application/json', |
|
|
|
@ -136,13 +122,21 @@ class JiandaoyunService { |
|
|
|
} |
|
|
|
] |
|
|
|
}, |
|
|
|
page_size: 1 // 只需要知道是否存在,不需要返回所有结果
|
|
|
|
page_size: 10 // 返回更多结果以便调试
|
|
|
|
}; |
|
|
|
|
|
|
|
console.log('发送的查询请求:', JSON.stringify(payload, null, 2)); |
|
|
|
|
|
|
|
const response = await axios.post(url, payload, { headers }); |
|
|
|
|
|
|
|
console.log('查询响应状态:', response.status); |
|
|
|
console.log('查询响应数据:', JSON.stringify(response.data, null, 2)); |
|
|
|
|
|
|
|
// 如果返回的数据数量大于0,则表示该电话号码已存在
|
|
|
|
return response.data.data.length > 0; |
|
|
|
const exists = response.data.data.length > 0; |
|
|
|
console.log(`电话号码 ${phoneNumber} 存在: ${exists}`); |
|
|
|
|
|
|
|
return exists; |
|
|
|
} catch (error) { |
|
|
|
console.error('查询电话号码是否存在失败:', error.message); |
|
|
|
if (error.response) { |
|
|
|
@ -155,42 +149,63 @@ class JiandaoyunService { |
|
|
|
} |
|
|
|
|
|
|
|
// 批量提交数据到简道云
|
|
|
|
async batchSubmitData(dataList) { |
|
|
|
async batchSubmitData(dataList, connection) { |
|
|
|
const results = []; |
|
|
|
|
|
|
|
for (const data of dataList) { |
|
|
|
for (const item of dataList) { |
|
|
|
try { |
|
|
|
// 检查电话号码是否已存在
|
|
|
|
const phoneNumber = data.user.phoneNumber; |
|
|
|
const exists = await this.isPhoneNumberExists(phoneNumber); |
|
|
|
// 检查数据库中是否已有简道云记录ID
|
|
|
|
let recordId = null; |
|
|
|
if (item.user && item.user.jiandaoyun_record_id) { |
|
|
|
recordId = item.user.jiandaoyun_record_id; |
|
|
|
console.log(`从数据库获取到简道云记录ID: ${recordId}`); |
|
|
|
} |
|
|
|
|
|
|
|
if (recordId) { |
|
|
|
console.log(`使用记录ID ${recordId} 更新数据`); |
|
|
|
|
|
|
|
// 转换数据格式
|
|
|
|
const jiandaoyunData = this.transformDataToJiandaoyunFormat(item); |
|
|
|
|
|
|
|
if (exists) { |
|
|
|
console.log(`电话号码 ${phoneNumber} 已存在于简道云表单中,跳过同步`); |
|
|
|
// 更新数据
|
|
|
|
const result = await this.updateDataInForm(recordId, jiandaoyunData); |
|
|
|
results.push({ |
|
|
|
success: true, |
|
|
|
skipped: true, |
|
|
|
message: `电话号码 ${phoneNumber} 已存在,跳过同步`, |
|
|
|
originalData: data |
|
|
|
updated: true, |
|
|
|
data: result, |
|
|
|
originalData: item |
|
|
|
}); |
|
|
|
console.log('数据更新成功:', result); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
// 转换数据格式
|
|
|
|
const jiandaoyunData = this.transformDataToJiandaoyunFormat(data); |
|
|
|
const jiandaoyunData = this.transformDataToJiandaoyunFormat(item); |
|
|
|
|
|
|
|
// 提交数据
|
|
|
|
const result = await this.submitDataToForm(jiandaoyunData); |
|
|
|
|
|
|
|
// 保存返回的记录ID到数据库
|
|
|
|
if (result.data && result.data._id && connection && item.user && item.user.userId) { |
|
|
|
const newRecordId = result.data._id; |
|
|
|
await connection.execute( |
|
|
|
`UPDATE ${config.tables.users.name} SET jiandaoyun_record_id = ? WHERE userId = ?`, |
|
|
|
[newRecordId, item.user.userId] |
|
|
|
); |
|
|
|
console.log(`已将简道云记录ID ${newRecordId} 保存到数据库`); |
|
|
|
} |
|
|
|
|
|
|
|
results.push({ |
|
|
|
success: true, |
|
|
|
data: result, |
|
|
|
originalData: data |
|
|
|
originalData: item |
|
|
|
}); |
|
|
|
console.log('数据提交成功:', result); |
|
|
|
} catch (error) { |
|
|
|
results.push({ |
|
|
|
success: false, |
|
|
|
error: error.message, |
|
|
|
originalData: data |
|
|
|
originalData: item |
|
|
|
}); |
|
|
|
console.error('数据提交失败:', error.message); |
|
|
|
} |
|
|
|
@ -199,6 +214,69 @@ class JiandaoyunService { |
|
|
|
return results; |
|
|
|
} |
|
|
|
|
|
|
|
// 根据电话号码获取简道云记录ID
|
|
|
|
async getRecordIdByPhoneNumber(phoneNumber) { |
|
|
|
try { |
|
|
|
const mapping = config.fieldMapping; |
|
|
|
const url = `${this.baseUrl}/api/v1/app/${this.appId}/entry/${this.entryId}/data_list`; |
|
|
|
const headers = { |
|
|
|
'Content-Type': 'application/json', |
|
|
|
'Authorization': `Bearer ${this.apiKey}` |
|
|
|
}; |
|
|
|
|
|
|
|
// 构建查询条件:电话号码字段等于指定值
|
|
|
|
const payload = { |
|
|
|
filter: { |
|
|
|
rel: 'and', |
|
|
|
cond: [ |
|
|
|
{ |
|
|
|
field: mapping.phoneNumber, |
|
|
|
type: 'text', |
|
|
|
method: 'eq', |
|
|
|
value: phoneNumber |
|
|
|
} |
|
|
|
] |
|
|
|
}, |
|
|
|
page_size: 1 // 只需要一条记录即可
|
|
|
|
}; |
|
|
|
|
|
|
|
const response = await axios.post(url, payload, { headers }); |
|
|
|
|
|
|
|
// 如果找到记录,返回第一条记录的_id
|
|
|
|
if (response.data.data.length > 0) { |
|
|
|
return response.data.data[0]._id; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} catch (error) { |
|
|
|
console.error('获取记录ID失败:', error.message); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 更新简道云表单中的数据
|
|
|
|
async updateDataInForm(recordId, data) { |
|
|
|
try { |
|
|
|
const url = `${this.baseUrl}/api/v1/app/${this.appId}/entry/${this.entryId}/data_update`; |
|
|
|
const headers = { |
|
|
|
'Content-Type': 'application/json', |
|
|
|
'Authorization': `Bearer ${this.apiKey}` |
|
|
|
}; |
|
|
|
|
|
|
|
// 构建更新请求体:记录ID和要更新的数据
|
|
|
|
const payload = { |
|
|
|
entry_id: this.entryId, |
|
|
|
data_id: recordId, |
|
|
|
data: data |
|
|
|
}; |
|
|
|
|
|
|
|
const response = await axios.post(url, payload, { headers }); |
|
|
|
return response.data; |
|
|
|
} catch (error) { |
|
|
|
console.error('更新数据失败:', error.message); |
|
|
|
throw error; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 测试简道云API连接
|
|
|
|
async testApiConnection() { |
|
|
|
try { |
|
|
|
|