Sayyoo-CCBHome-Contract-Export-Script
// ==UserScript==
// @name Sayyoo-CCBHome-Contract-Export-Script
// @namespace http://tampermonkey.net/
// @version SCCES-0.0.0.5
// @description try to take over the world!
// @author You
// @match https://hess.qiye.ccbhome.cn/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=hess.qiye.ccbhome.cn
// @grant GM_xmlhttpRequest
// @require https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js
// ==/UserScript==
// http://sygit.sayyoo.cn/public-group/tampermonkey-scripts/blob/master/hess_qiye_ccbhome_cn.monkey.js
'use strict';
function init() {
var actions_btn_box = document.querySelector(".actions")
var pagePathname = window.location.pathname;
if (actions_btn_box && pagePathname == '/changzu/contract/list') {
console.log("#》#》#》actions_btn_box:", actions_btn_box);
// 创建 Excel 导出页面元素
var export_excel_node = document.createElement("div");
export_excel_node.className = "btn-group pull-right";
export_excel_node.innerHTML = ''+
'<button id="sayyooContractInfoExport" href="javascript:void(0);" class="btn blue btn-circle contract_export_contract_pages" style="margin-right: 10px; margin-bottom: 10px;">'+
'<i class="fa fa-download"></i>'+
'随寓-合同导出'+
'</button>';
actions_btn_box.append(export_excel_node);
$("#sayyooContractInfoExport").on('click',function(){
var recordsTotal = $('#recordsTotal').val();
$('#exportModal').modal("show");
if(recordsTotal>0){
var _html = buildExportContainer(recordsTotal)
$("#exportContainer").html(_html);
$('.export_pages').empty();
var pageDiv = '';
export_per_count = 100;
if(recordsTotal<100){
$(".export_per_count").val(recordsTotal);
var export_per_count = recordsTotal;
}else{
$(".export_per_count").val(export_per_count);
}
var pages = Math.ceil(recordsTotal/export_per_count);
for(i = 1; i <= pages; i++){
if(i == 1){
pageDiv += '<button type="button" class="pageBtn btn white blue" data-id="' + i + '">' + i + '</button>';
}else{
pageDiv += '<button type="button" class="pageBtn btn white" data-id="' + i + '">' + i + '</button>';
}
}
$('.export_pages').html(pageDiv);
$(".export_pages").on("click", "button", function () {
$(this).parent().children("button.blue").removeClass("blue");
$(this).addClass("blue");
});
//监听提交事件
var export_process_status = 0;
$("#sayyooPageExport").on("click", function() {
if(export_process_status == 1){
alert('数据导出中,请勿重复提交');
return;
}
var page = $(".export_pages button.blue").attr("data-id");
if(page){
export_process_status = 1;
$("#point").text("");
var export_per_count = $('.export_per_count').val();
if(parseInt(export_per_count)){
var per_count = parseInt(export_per_count);
if(per_count > 100){
utils.alert("每页数量不能大于100");
return;
}
export_per_count = parseInt(per_count);
}else{
utils.alert("请输入每页数量");
return;
}
page = parseInt(page);
setTimeout(()=>{
DoSayyooExport((page-1)*export_per_count, export_per_count)
export_process_status = 0;
}, 100);
}else{
$("#point").text("请先选择需要导出的页数");
}
});
}else{
var __html = '<div class="user_info_tmpl">'
+'<div class="row form-horizontal">'
+'<div class="form-group">'
+'<label class="control-label col-md-8 col-sm-8 col-xs-8 text-right">没有查到数据,请修改查询条件或添加企业参数</label>'
+'</div>'
+'<div class="form-group">'
+'<label class="control-label col-md-2 col-sm-2 col-xs-2 text-right"></label>'
+'<div class="col-md-6 col-sm-6 col-xs-6">'
+'<button id="sayyooCancelExport" href="javascript:void(0);" class="btn blue btn-circle" style="margin-right:10px;margin-bottom: 10px;">'
+'<i class="fa fa-close"></i>'
+'取消</button>'
+'</div>'
+'</div>';
$("#exportContainer").html(__html);
}
$("#sayyooCancelExport").on("click", function() {
$("#exportModal").modal("hide");
});
})
//document.querySelector("#sayyooBtnExport").onclick = function(){
// DoSayyooExport();
//}
}
}
setTimeout(init, 1000);
function DoSayyooExport(start, length) {
var longRentCodeJson = sessionStorage.getItem('zeroCloud-longRentCode');
var sessionCode = JSON.parse(longRentCodeJson);
var project_id_select = document.getElementById("single")
var project_id_select_index = project_id_select.selectedIndex;
var project_id_option = project_id_select.options[project_id_select_index];
var project_id_value = project_id_option.value;
var project_name_value = project_id_value=='?' ? '全部项目' : project_id_option.text;
var village_id_select = document.getElementById("village_select")
var village_id_select_index = village_id_select.selectedIndex;
var village_id_option = village_id_select.options[village_id_select_index];
var village_id_value = village_id_option.value;
var begin_date_value = document.getElementById("begin_date").value;
var end_date_value = document.getElementById("end_date").value;
var signed_time_value = document.getElementById("signed_time").value;
var txtSearch_value = document.getElementById("txtSearch").value;
var status_value = document.querySelector('#status-container>.blue').getAttribute('data-status');
status_value = status_value? status_value : '';
var type_value = document.querySelector('#type-container>.blue').getAttribute('data-type');
type_value = type_value? type_value : '';
var dataRequestURL = 'https://hess.qiye.ccbhome.cn/changzu/contract/list-data?draw=2'+
'&start='+start+'&length='+length+'&search[value]=&search[regex]=false'+
'&project_id='+project_id_value +
'&village_id='+village_id_value +
'&keyword='+txtSearch_value +
'&begin_date='+begin_date_value +
'&end_date='+end_date_value +
'&signed_time='+signed_time_value +
'&status='+status_value +
'&contractType='+type_value +
'&rentDicountId='+
'&is_expire_contract='+
'&code='+sessionCode.content
'&form=1'+
'&_='+sessionCode.datetime;
console.log("#>#>#>#>#>#>#>#>"+dataRequestURL)
GM_xmlhttpRequest({
headers: {'content-type': 'application/json'},
responseType: 'json',
url: dataRequestURL,
method: 'GET',
onreadystatechange: (res) => {
if (res.readyState === 4) {
var responseJsonData = res.response.data;
for(let index = 0; index < responseJsonData.length; index++) {
fetchSetCustomerId(responseJsonData, index);
fetchSetRenterInfo(responseJsonData, index);
}
var jsonData = rebuildJsonData(responseJsonData);
var dataSheet = XLSX.utils.json_to_sheet(jsonData);
var jsonBlob = sheet2blob(dataSheet);
const downloadHref = URL.createObjectURL(jsonBlob);
const downloadLink = document.createElement("a");
downloadLink.href = downloadHref;
downloadLink.download = project_name_value+"租客合同信息统计表" + ".xlsx";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(downloadHref);
}
},
})
}
function buildExportContainer(recordsTotal){
return '<div class="user_info_tmpl">'
+'<div class="row form-horizontal">'
+'<div class="form-group">'
+'<label class="control-label col-md-4 col-sm-4 col-xs-4 text-right">总记录数</label>'
+'<div class="col-md-4 col-sm-4 col-xs-4">'
+'<input type="text" class="form-control js-edit_name" value="'+recordsTotal+'" readonly="true">'
+'</div>'
+'</div>'
+'</div>'
+'<div class="row form-horizontal">'
+'<div class="form-group">'
+'<label class="control-label col-md-4 col-sm-4 col-xs-4 text-right">每页数量</label>'
+'<div class="col-md-4 col-sm-4 col-xs-4">'
+'<input type="text" class="form-control js-edit_name export_per_count" readonly="true">'
+'</div>'
+'</div>'
+'</div>'
+'<div class="row form-horizontal">'
+'<div class="form-group">'
+'<label class="control-label col-md-4 col-sm-4 col-xs-4 text-right">分页页数</label>'
+'<div class="col-md-4 col-sm-4 col-xs-4 export_pages">'
+'</div>'
+'</div>'
+'</div>'
+'<div class="form-group">'
+'<label class="control-label col-md-4 col-sm-4 col-xs-4 text-right"></label>'
+'<div class="col-md-6 col-sm-6 col-xs-6" style="padding-left:0px;">'
+'<button id="sayyooPageExport" href="javascript:void(0);" class="btn blue btn-circle" style="margin-right:10%;margin-bottom: 10px;">'
+'<i class="fa fa-download"></i>'
+'导出</button>'
+'<button id="sayyooCancelExport" href="javascript:void(0);" class="btn blue btn-circle" style="margin-right:10px;margin-bottom: 10px;">'
+'<i class="fa fa-close"></i>'
+'取消</button>'
+'</div>'
+'</div>'
+'</div>'
+'</div>';
}
function fetchSetCustomerId(row_data, index){
var longRentCodeJson = sessionStorage.getItem('zeroCloud-longRentCode');
var sessionCode = JSON.parse(longRentCodeJson);
var dataRequestURL = 'https://hess.qiye.ccbhome.cn/changzu/contract/edit';
dataRequestURL = dataRequestURL+'?id='+row_data[index].id+'&code='+sessionCode.content;
console.log("#>#>#>#>#>#>#>#>fetchSetCustomerId "+dataRequestURL)
const xhr = new XMLHttpRequest();
xhr.open('GET', dataRequestURL, false);
xhr.send(null);
if(xhr.status = 200){
var domParser = new DOMParser();
var htmlDoc = domParser.parseFromString(xhr.responseText, "text/html");
var customer_id_node = htmlDoc.getElementById("hid_customer_id");
row_data[index]._customer_id = customer_id_node.value;
var salesManNameNode = htmlDoc.getElementById("salesManName");
row_data[index].sales_man_name = salesManNameNode.value;
var salesManPhoneNode = htmlDoc.getElementById("salesManPhone");
row_data[index].sales_man_phone = salesManPhoneNode.value;
var emergency_tel_node = htmlDoc.getElementById("emergency_tel");
row_data[index].emergency_tel = emergency_tel_node.value;
}
}
function fetchSetRenterInfo(row_data, index){
var longRentCodeJson = sessionStorage.getItem('zeroCloud-longRentCode');
var sessionCode = JSON.parse(longRentCodeJson);
var dataRequestURL = 'https://hess.qiye.ccbhome.cn/changzu/renter/query-renter';
dataRequestURL = dataRequestURL+'?id='+row_data[index]._customer_id+'&projectId='+row_data[index].project_id+'&code='+sessionCode.content;
console.log("#>#>#>#>#>#>#>#>fetchSetRenterInfo "+dataRequestURL)
const xhr = new XMLHttpRequest();
xhr.open('GET', dataRequestURL, false);
xhr.send(null);
if(xhr.status = 200){
var renterInfo = JSON.parse(xhr.responseText);
row_data[index].customer_phone = renterInfo.phone;
}
}
function rebuildJsonData(jsonData){
var buildJson = [];
for(let i = 0; i < jsonData.length; i++) {
var buildJsonRow = {
系统合同编号 : jsonData[i].id,
自定义合同编号 : jsonData[i].contract_no,
房源编号 : jsonData[i].room_id,
房号 : jsonData[i].room_num,
月租金 : jsonData[i].monthly_rent,
押金 : jsonData[i].hasDeposite,
押金类型 : '',
转租服务费_换房服务费 : jsonData[i].id,
签约日期 : jsonData[i].signed_time,
承租日 : jsonData[i].origin_begin_date,
到期日 : jsonData[i].origin_end_date,
实际退房日期 : jsonData[i].end_date,
付款周期 : jsonData[i].pay_period,
客户姓名 : jsonData[i].customer_name,
证件类型 : jsonData[i].card_type,
证件号码 : jsonData[i].card_num,
客户电话 : jsonData[i].customer_phone,
紧急联系人姓名 : jsonData[i].emergency_name,
紧急联系人电话 : jsonData[i].emergency_tel,
通讯地址 : '',
电子邮箱 : '',
账单交费期限 : '每月'+jsonData[i].pay_day + '号' ,
租赁面积 : jsonData[i].area,
折扣方案 : '',
对外房源编号 : '',
业务人员姓名 : jsonData[i].sales_man_name,
业务人员手机号码 : jsonData[i].sales_man_phone,
电费押金 : '',
出租类型 : '',
合同备注 : '',
上上签合同编号 : jsonData[i].fsdid,
房源获取渠道 : jsonData[i].sourceChannel
};
buildJson.push(buildJsonRow);
}
return buildJson;
}
function sheet2blob(sheet, sheetName) {
sheetName = sheetName || 'sheet1';
var workbook = {
SheetNames: [sheetName],
Sheets: {}
};
workbook.Sheets[sheetName] = sheet;
// 生成excel的配置项
var wopts = {
bookType: 'xlsx', // 要生成的文件类型
bookSST: false, // 是否生成Shared String Table,官方解释是,如果开启生成速度会下降,但在低版本IOS设备上有更好的兼容性
type: 'binary'
};
var wbout = XLSX.write(workbook, wopts);
var blob = new Blob([s2ab(wbout)], {type:"application/octet-stream"});
// 字符串转ArrayBuffer
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
return buf;
}
return blob;
}