// ==UserScript== // @name Sayyoo-CCBHome-Contract-Export-Script // @namespace http://tampermonkey.net/ // @version SCCES-0.0.0.3 // @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 = ''+ ''; actions_btn_box.append(export_excel_node); document.querySelector("#sayyooBtnExport").onclick = function(){ DoSayyooExport(); } } } setTimeout(init, 1000); function DoSayyooExport() { var longRentCodeJson = sessionStorage.getItem('zeroCloud-longRentCode'); console.log("#>#>#>#>#>#>#>#>"+longRentCodeJson) 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 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=0&length=20&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 = "sayyooExport" + ".xlsx"; document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink); URL.revokeObjectURL(downloadHref); } }, }) } 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; }