舉例如下:
前端:
function void_invoice(){
var sIndex = $("#dgMaster").datagrid("getSelectedIndex");
if (sIndex >= 0) {
var status = $("#dgMaster").datagrid("getRows")[sIndex].狀態;
var ivono = $("#dgMaster").datagrid("getRows")[sIndex].發票號碼;
var cdate = $("#dgMaster").datagrid("getRows")[sIndex].發票日期;
cdate = cdate.replace(new RegExp("-", "g"), "").substring(0,8);
if(status!="開立"){
$.alert('無法呈送','danger');
}else{
$.callMethod('發票資料','void_invoice',{type:1,ivono:ivono,InvoiceDate:cdate},function(result){
$.alert(result,'info');
var page = $('#dgMaster').datagrid('options').page;
$('#dgMaster').datagrid('load', { page: page});
});
}
  ; }else {
$.alert('請選擇一筆發票','info');
}
}
後端:
//作廢發票
exports.void_invoice = function (param, callback) {
var datamodule = this;
var https = datamodule.getModule('https');
var querystring = datamodule.getModule('querystring');
var Buffer = datamodule.getModule('buffer').Buffer;
var clientInfo = this.clientInfo;
var ivono = param.ivono;
//發票日期
var InvoiceDate = param.InvoiceDate;
var csv = '"InvoiceNumber","InvoiceDate"\n"' + ivono + '","' + InvoiceDate + '"\n"Finish"';
datamodule.echo("csv:" + csv);
var post_data = querystring.stringify({
'id': 'XXXXXXXXX',
'user': ' XXXXXXXXX',
'passwd': ' XXXXXXXXX',
'csv': Buffer.from(csv).toString('base64')
});
datamodule.echo("post_data:" + post_data);
const options = {
hostname: 'api-test.cxn.com.tw',
port: 443,
path: '/c0501.php',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length':Buffer.byteLength(post_data)
}
}
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', (d) => {
var errmsg = "";
var result = Buffer.from(d, 'base64').toString().split("&");
if (result[0] == "rtcode=0000") {
//成功取號後,寫入發票號碼
var sql = "update 發票資料 set 狀態 ='作廢' where 流水編號 ='" + ivono + "'"; //SQL條件句
this.query(clientInfo,clientInfo.database, [sql], {
check: true
}, function (err, result) {
if (err.message == 'no row effect.') {
callback(new Error('請再執行一次')); //修改顯示的 錯誤訊息
} else if (err) {
callback(err);
} else {
callback(null, "**完成取號**");//這句不會執行
}
});
callback(null, "完成");
} else {
errmsg = Buffer.from(result[1].split("=")[1], 'base64').toString();
callback(null,"錯誤訊息A: " + errmsg + "(" + result[0] + ")");
}
})
})
req.on('error', (error) => {
callback(null, "錯誤訊息B: " + errmsg);
})
req.write(post_data)
req.end()
};