Skip to content

Commit

Permalink
Merge pull request #76 from taigongzhaihua/master
Browse files Browse the repository at this point in the history
✨新增适配支付宝网商银行转出 #59
🎨优化代码
  • Loading branch information
taigongzhaihua authored Apr 14, 2024
2 parents a52e6c1 + 6c7fff6 commit 8dace7f
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 76 deletions.
230 changes: 154 additions & 76 deletions src/rule/app/com.eg.android.AlipayGphone/支付宝消息盒子/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,41 @@ import { RuleObject } from "../../../../utils/RuleObject";
import { BillType } from "../../../../utils/BillType";
import { Currency } from "../../../../utils/Currency";

/**
* 初始化交易结果对象
* @param {Array} data - 交易数据
* @param {Object} pl - 交易数据的pl字段
* @returns {Object} - 初始化后的交易结果对象
*/
function initResult(data, pl) {
return {
type: null,
money: 0,
fee: 0,
shopName: "",
shopItem: "",
accountNameFrom: "",
accountNameTo: "",
currency: Currency['人民币'],
time: data[0].mct,
channel: "支付宝" + pl.title
};
}
//----------------------------------------------------------------------

/**
* 解析交易信息的核心函数
* @param {Array} data - 交易数据
* @returns {RuleObject|null} - 解析后的交易结果对象,如果无法解析则返回null
*/
function parseTransaction(data) {

// 解析data
try { data = JSON.parse(data); }
catch (e) { throw new Error("[支付宝消息盒子] Invalid data: " + data); }
try {
// 解析data
data = JSON.parse(data);
} catch (e) {
throw new Error("[支付宝消息盒子] Invalid data: " + data);
}

// 解析pl
const pl = JSON.parse(data[0].pl);
if (pl == null ||
pl.templateType == null
) {
if (pl == null || pl.templateType == null) {
return null;
}

// 初始化result
let result = initResult(data, pl);
const result = initResult(data, pl);

// 根据pl.templateType选择不同的处理函数
const actions = {
"BN": parseBN,
"S": parseS
};
(actions[pl.templateType] || (() => {
throw new error(`[支付宝消息盒子] Unknown templateType: ${pl.templateType}`);
}))(pl, result);
const action = actions[pl.templateType];
if (action) {
action(pl, result);
} else {
throw new Error(`[支付宝消息盒子] Unknown templateType: ${pl.templateType}`);
}

// 若result.type已设置,则返回RuleObject,否则返回null
return !isNaN(result.type) ?
Expand All @@ -69,6 +54,8 @@ function parseTransaction(data) {
: null;
}

//----------------------------------------------------------------------

/**
* 处理BN类型的模板
* @param {Object} pl - 解析后的pl对象
Expand All @@ -77,21 +64,24 @@ function parseTransaction(data) {
function parseBN(pl, result) {
// 解析content
let contentItems;
try { contentItems = JSON.parse(pl.content); }
catch (e) { throw new Error("[支付宝消息盒子] Invalid content: " + pl.content); }
try {
contentItems = JSON.parse(pl.content);
} catch (e) {
throw new Error("[支付宝消息盒子] Invalid content: " + pl.content);
}

// 处理contentItems.money的逻辑
let money = parseFloat(contentItems.money);
if (isNaN(money)) { throw new Error("[支付宝消息盒子] Invalid money: " + contentItems.money); }
if (isNaN(money)) {
throw new Error("[支付宝消息盒子] Invalid money: " + contentItems.money);
}
result.money = money;

// 处理pl.link的逻辑
try { handleLink(pl, result); }
catch (e) { throw new Error("[支付宝消息盒子] Error handling link: " + e.message); }
handleLink(pl, result);

// 处理contentItems.content的逻辑
try { handleContentItems(contentItems.content, result); }
catch (e) { throw new Error("[支付宝消息盒子] Error handling content items: " + e.message); }
handleContentItems(contentItems.content, result);
}

/**
Expand All @@ -100,50 +90,64 @@ function parseBN(pl, result) {
* @param {Object} result - 解析后的交易结果对象
*/
function parseS(pl, result) {
let dataItems;
try {
dataItems = JSON.parse(pl.extraInfo);
} catch (e) {
throw new Error("[支付宝消息盒子] Invalid extraInfo: " + pl.extraInfo);
const dataItems = JSON.parse(pl.extraInfo);
const handlers = {
"2021002117633826": handleReceipt,
"2015111300788246": handleInvestmentIncome,
"66666708": handleYueLibaoIncome,
"77700207": handleWebBankTransfer
};
const handler = handlers[pl.appId];
if (handler) {
handler(dataItems, pl, result);
} else {
throw new Error("[支付宝消息盒子] Unknown appId: " + pl.appId);
}
} catch (error) {
throw new Error("[支付宝消息盒子] Error parsing S type template: " + error.message);
}
}

if (pl.link.indexOf("appId=60000081") > 0) {
result.type = BillType.Income;
let money = parseFloat(dataItems.content.replace("收款金额¥", ""));
if (isNaN(money)) { throw new Error("[支付宝消息盒子] Invalid money: " + dataItems.content); }

result.money = money;
result.shopName = dataItems.assistMsg2;
result.shopItem = dataItems.assistMsg1;
result.accountNameFrom = '支付宝余额';
result.channel = '支付宝[收款码收款]';
} else if (pl.link.indexOf("appId=68688004") > 0) {
// 理财收益
result.type = BillType.Income;
let money = parseFloat(dataItems.mainText.replace("∝", "").replace("+", ""));
if (isNaN(money)) {
throw new Error("[支付宝消息盒子] Invalid money: " + dataItems.mainText);
}
result.money = money;
result.shopName = pl.title;
result.shopItem = `${dataItems.assistMsg1}${dataItems.subCgyLeftKey}${dataItems.subCgyLeftValue.replace("∝", "").replace("∝", "")}${dataItems.subCgyMiddleKey}${dataItems.subCgyMiddleValue.replace("∝", "").replace("∝", "")}${dataItems.subCgyRightKey}${dataItems.subCgyRightValue.replace("∝", "").replace("∝", "")})`;
result.accountNameFrom = '余利宝';
result.channel = '支付宝[理财收益]';
} else if (pl.link.indexOf("appId=66666708") > 0) {
// 余利宝收益
result.type = BillType.Income;
let money = parseFloat(dataItems.content);
if (isNaN(money)) {
throw new Error("[支付宝消息盒子] Invalid money: " + dataItems.content);
}
result.money = money;
result.shopName = pl.title;
result.shopItem = `${dataItems.assistMsg1}${dataItems.homePageTitle}`;
result.accountNameFrom = '余利宝';
result.channel = '支付宝[余利宝收益]';
//------ 辅助函数 -------------------------------------------------------

/**
* 设置结果对象的属性
* @param {Object} result - 交易结果对象
* @param {Object} properties - 属性对象
*/
function setResultProperties(result, properties) {
try {
Object.assign(result, properties);
} catch (error) {
throw new Error("[支付宝消息盒子] Error setting result properties: " + error.message);
}
}


/**
* 初始化交易结果对象
* @param {Array} data - 交易数据
* @param {Object} pl - 交易数据的pl字段
* @returns {Object} - 初始化后的交易结果对象
*/
function initResult(data, pl) {
return {
type: null,
money: 0,
fee: 0,
shopName: "",
shopItem: "",
accountNameFrom: "",
accountNameTo: "",
currency: Currency['人民币'],
time: data[0].mct,
channel: "支付宝" + pl.title
};
}

//------ parseBN子函数 --------------------------------------------------

/**
* 处理contentItems.content的逻辑
* @param {Array} contentItems - contentItems数组
Expand Down Expand Up @@ -199,6 +203,80 @@ function handleLink(pl, result) {
}
}

//------- parseS子函数 --------------------------------------------------

/**
* 处理收款码收款
* @param {Object} dataItems - 数据项对象
* @param {Object} pl - 解析后的pl对象
* @param {Object} result - 解析后的交易结果对象
*/
function handleReceipt(dataItems, pl, result) {
setResultProperties(result, {
type: BillType.Income,
money: parseFloat(dataItems.content.replace("收款金额¥", "")),
shopName: dataItems.assistMsg2,
shopItem: dataItems.assistMsg1,
accountNameFrom: '支付宝余额',
channel: '支付宝[收款码收款]'
});
}

/**
* 处理理财收益
* @param {Object} dataItems - 数据项对象
* @param {Object} pl - 解析后的pl对象
* @param {Object} result - 解析后的交易结果对象
*/
function handleInvestmentIncome(dataItems, pl, result) {
setResultProperties(result, {
type: BillType.Income,
money: parseFloat(dataItems.mainText.replace(/∝|\+/g, "")),
shopName: pl.title,
shopItem: `${dataItems.assistMsg1}${dataItems.subCgyLeftKey}${dataItems.subCgyLeftValue.replace(//g, "")}${dataItems.subCgyMiddleKey}${dataItems.subCgyMiddleValue.replace(//g, "")}${dataItems.subCgyRightKey}${dataItems.subCgyRightValue.replace(//g, "")})`,
accountNameFrom: '余利宝',
channel: '支付宝[理财收益]'
});
}


/**
* 处理余利宝收益
* @param {Object} dataItems - 数据项对象
* @param {Object} pl - 解析后的pl对象
* @param {Object} result - 解析后的交易结果对象
*/
function handleYueLibaoIncome(dataItems, pl, result) {
setResultProperties(result, {
type: BillType.Income,
money: parseFloat(dataItems.content),
shopName: pl.title,
shopItem: `${dataItems.assistMsg1}${dataItems.homePageTitle}`,
accountNameFrom: '余利宝',
channel: '支付宝[余利宝收益]'
});
}

/**
* 处理网商银行转出
* @param {Object} dataItems - 数据项对象
* @param {Object} pl - 解析后的pl对象
* @param {Object} result - 解析后的交易结果对象
*/
function handleWebBankTransfer(dataItems, pl, result) {
setResultProperties(result, {
type: BillType.Transfer,
money: parseFloat(dataItems.content),
shopName: pl.title,
shopItem: `${dataItems.homePageTitle}`,
accountNameFrom: `${dataItems.title}(${dataItems.assistMsg3})`,
accountNameTo: `${dataItems.assistMsg2}`,
channel: '支付宝[网商银行转出]'
});
}

//------- 主函数 --------------------------------------------------------

/**
* 获取解析后的交易结果对象
* @param {Array} data - 交易数据
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,18 @@ test("支付宝余利宝收益", () => testAnkio( '支付宝余利宝收益',[
}
]))

test("支付宝网商银行转出", () => testAnkio( '支付宝网商银行转出',[
{
type: 2,
money: 20000,
fee: 0,
shopName: "网商银行",
shopItem: "转账-转给江大 20000.00元",
accountNameFrom: "网商银行(9370)",
accountNameTo: "工商银行(8464)",
currency: "CNY",
time: 1712916392000,
channel: "支付宝[网商银行转出]"
}
]))

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"mk": 240452180632200011,
"st": 1,
"isSc": 0,
"appId": "",
"mct": 1712916392000,
"pl": "{\"templateType\":\"S\",\"commandType\":\"UPDATE\",\"expireLink\":\"https://render.alipay.com/p/f/fd-jblxfp45/pages/home/index.html\",\"withPushNfc\":\"Y\",\"msgType\":\"NOTICE\",\"appInfo\":\"{\\\"appName\\\":\\\"网商银行\\\",\\\"appId\\\":\\\"77700207\\\",\\\"linkUrl\\\":\\\"alipays://platformapi/startApp?appId\u003d77700207\\\",\\\"msgListConfig\\\":\\\"1\\\",\\\"logoUrl\\\":\\\"https://gw.alipayobjects.com/zos/nebulamng/8e2506da-3f89-464c-a6f4-41f07c3c263f/icon/7b5120beyyw.png@120w.png\\\"}\",\"businessId\":\"\",\"icon\":\"https://gw.alipayobjects.com/zos/nebulamng/8e2506da-3f89-464c-a6f4-41f07c3c263f/icon/7b5120beyyw.png@120w.png\",\"link\":\"alipays://platformapi/startapp?appId\u003d77700207\u0026page\u003dpages/myInfo/index?tradeNo\u003d2024041210130010111806180011820081791337\",\"msgId\":\"e8761f3b159efbb42dce2368523ef2d100567\",\"templateCode\":\"b1c93e6344f148f882f56cf96152a7fc\",\"templateId\":\"WALLET-FWC@remindNumber\",\"title\":\"网商银行\",\"content\":\"20000.00\",\"linkName\":\"查看详情\",\"bizName\":\"网商银行\",\"appId\":\"77700207\",\"msgCategory\":\"service\",\"subscribeMsg\":false,\"ih\":\"{\\\"extInfo\\\":{},\\\"iid\\\":\\\"105\\\",\\\"ioty\\\":\\\"aor\\\",\\\"itemBasicInfo\\\":{\\\"dn\\\":\\\"服务提醒\\\",\\\"ic\\\":\\\"https://gw.alipayobjects.com/zos/bmw-prod/b96c31e0-64ba-4cbb-9a64-73ff17d701fd.webp\\\",\\\"lk\\\":\\\"alipays://platformapi/startapp?appId\u003d20000235\u0026source\u003dfriendTab\\\"},\\\"itemUserRelation\\\":{\\\"ilf\\\":\\\"N\\\",\\\"uc\\\":{\\\"top\\\":\\\"0\\\",\\\"ic\\\":\\\"0\\\"},\\\"version\\\":0},\\\"ity\\\":\\\"105\\\",\\\"moty\\\":\\\"u\\\",\\\"msgInfo\\\":{\\\"bm\\\":\\\"转账-转给江大 20000.00元\\\",\\\"cmid\\\":\\\"e8761f3b159efbb42dce2368523ef2d100567\\\",\\\"lmt\\\":1712916392244,\\\"msrid\\\":\\\"b1c93e6344f148f882f56cf96152a7fc\\\",\\\"ncs\\\":\\\"N\\\",\\\"rps\\\":\\\"num\\\",\\\"sam\\\":\\\"Y\\\",\\\"sn\\\":\\\"网商银行\\\",\\\"ssrid\\\":\\\"\\\",\\\"urn\\\":1}}\",\"bizMonitor\":\"{\\\"isFollow\\\":false,\\\"serviceCode\\\":\\\"b1c93e6344f148f882f56cf96152a7fc\\\",\\\"bizName\\\":\\\"网商银行\\\",\\\"createTime\\\":1712916392253,\\\"targetDataId\\\":\\\"MG6eda4c8a3d264038a2b008704d9ed609_1\\\",\\\"appId\\\":\\\"77700207\\\",\\\"businessId\\\":\\\"\\\",\\\"messageId\\\":\\\"e8761f3b159efbb42dce2368523ef2d100567\\\",\\\"messageTitle\\\":\\\"网商银行\\\",\\\"templateId\\\":\\\"WALLET-FWC@remindNumber\\\"}\",\"subscribeConfig\":\"1\",\"gmtCreate\":1712916392244,\"gmtValid\":2028449192244,\"operate\":\"SEND\",\"templateName\":\"网商银行综合金融动账成功消息\",\"homePageTitle\":\"转账-转给江大 20000.00元\",\"attributes\":\"0000000000000001\",\"extraInfo\":\"{\\\"hiddenSumFlag\\\":\\\"N\\\",\\\"expireLink\\\":\\\"https://render.alipay.com/p/f/fd-jblxfp45/pages/home/index.html\\\",\\\"topSubContent\\\":\\\"交易成功\\\",\\\"preValue\\\":\\\"¥\\\",\\\"isNumberType\\\":true,\\\"icon\\\":\\\"https://gw.alipayobjects.com/zos/nebulamng/8e2506da-3f89-464c-a6f4-41f07c3c263f/icon/7b5120beyyw.png@120w.png\\\",\\\"link\\\":\\\"alipays://platformapi/startapp?appId\u003d77700207\u0026page\u003dpages/myInfo/index?tradeNo\u003d2024041210130010111806180011820081791337\\\",\\\"assistName2\\\":\\\"交易详情\\\",\\\"assistName3\\\":\\\"账号尾号\\\",\\\"assistName1\\\":\\\"交易类型\\\",\\\"subscribeConfig\\\":\\\"1\\\",\\\"title\\\":\\\"网商银行\\\",\\\"templateId\\\":\\\"WALLET-FWC@remindNumber\\\",\\\"content\\\":\\\"20000.00\\\",\\\"linkName\\\":\\\"查看详情\\\",\\\"assistMsg3\\\":\\\"9370\\\",\\\"assistMsg2\\\":\\\"工商银行(8464)\\\",\\\"assistMsg1\\\":\\\"转账-转给江大\\\",\\\"gmtValid\\\":2028449192244,\\\"sceneExt\\\":{\\\"sceneTemplateId\\\":\\\"WALLET-FWC@remindNumber\\\",\\\"sceneUrl\\\":\\\"alipays://platformapi/startApp?appId\u003d77700207\\\",\\\"sceneType\\\":\\\"miniApp\\\",\\\"sceneName\\\":\\\"网商银行\\\",\\\"sceneId\\\":\\\"77700207\\\",\\\"sceneIcon\\\":\\\"https://gw.alipayobjects.com/zos/nebulamng/8e2506da-3f89-464c-a6f4-41f07c3c263f/icon/7b5120beyyw.png@120w.png\\\",\\\"sceneExtInfo\\\":\\\"\\\",\\\"sceneTitle\\\":\\\"进入小程序\\\"},\\\"homePageTitle\\\":\\\"转账-转给江大 20000.00元\\\",\\\"buttonLink\\\":\\\"alipays://platformapi/startapp?appId\u003d77700207\u0026page\u003dpages/myInfo/index?tradeNo\u003d2024041210130010111806180011820081791337\\\"}\"}"
}
]

0 comments on commit 8dace7f

Please sign in to comment.