Skip to content

Commit

Permalink
🎨 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Jones committed Apr 13, 2024
1 parent 963b4df commit 7139834
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ function parseTransaction(data) {
catch (e) { throw new Error("[支付宝消息盒子] Invalid data: " + data); }

// 解析pl
let pl = JSON.parse(data[0].pl);

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

// 初始化result
let result = initResult(data, pl);
Expand All @@ -48,7 +50,7 @@ function parseTransaction(data) {
"S": parseS
};
(actions[pl.templateType] || (() => {
console.error(`[支付宝消息盒子] Unknown templateType: ${pl.templateType}`);
throw new error(`[支付宝消息盒子] Unknown templateType: ${pl.templateType}`);
}))(pl, result);

// 若result.type已设置,则返回RuleObject,否则返回null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,54 @@ import { BillType } from "../../../../utils/BillType";
import { RuleObject } from "../../../../utils/RuleObject";
import { Currency } from "../../../../utils/Currency";

/**
* 解析支付宝账单数据
* @param {string} data - 包含支付宝账单数据的JSON字符串
* @returns {RuleObject|null} - 解析后的规则对象,如果解析失败则返回null
*/
export function get(data) {
try {
// 解析数据
const { extension, fields } = JSON.parse(data);
if (!extension || !fields) {
return null;
}

// 创建结果对象
const result = createResultObject(extension);

// 处理每个字段元素
fields.forEach(element => processElement(element, result));

// 处理业务类型
if (!processBizType(extension, result)) {
return null;
}

// 创建并返回规则对象
return new RuleObject(
result.type,
result.money,
result.shopName,
result.shopItem,
result.accountNameFrom,
result.accountNameTo,
result.fee,
result.currency,
result.time,
result.channel
);
} catch (error) {
throw new Error(`[支付宝账单] get function: ${error}`);
}
}

/**
* 解析元素并更新结果对象
* @param {Object} element - 要解析的元素对象
* @param {Object} element - 字段元素对象
* @param {Object} result - 结果对象
*/
const processElement = (element, result) => {
function processElement(element, result) {
try {
if (!element.value) {
return;
Expand All @@ -31,16 +73,16 @@ const processElement = (element, result) => {
break;
}
} catch (error) {
throw new error(`[支付宝账单] Error processing element: ${error}`);
throw new Error(`[支付宝账单] Error processing element: ${error}`);
}
}

/**
* 创建结果对象
* @param {Object} extension - 扩展对象
* @param {Object} extension - 扩展数据对象
* @returns {Object} - 结果对象
*/
const createResultObject = (extension) => {
function createResultObject(extension) {
return {
type: 0,
money: 0,
Expand All @@ -57,11 +99,11 @@ const createResultObject = (extension) => {

/**
* 处理业务类型并更新结果对象
* @param {Object} extension - 扩展对象
* @param {Object} extension - 扩展数据对象
* @param {Object} result - 结果对象
* @returns {boolean} - 是否成功处理业务类型
* @returns {boolean} - 处理结果
*/
const processBizType = (extension, result) => {
function processBizType(extension, result) {
switch (extension.bizType) {
case "CHARGE":
result.channel = "支付宝[收钱码服务费]";
Expand All @@ -86,40 +128,3 @@ const processBizType = (extension, result) => {
}
return true;
}

/**
* 根据给定的数据获取规则对象
* @param {string} data - 要解析的数据
* @returns {RuleObject|null} - 解析后的规则对象,如果解析失败则返回null
*/
export function get(data) {
try {
data = JSON.parse(data);
const { extension, fields } = data;
if (!extension || !fields) {
return null;
}

let result = createResultObject(extension);

fields.forEach(element => processElement(element, result));

if (!processBizType(extension, result)) {
return null;
}

return new RuleObject(
result.type,
result.money,
result.shopName,
result.shopItem,
result.accountNameFrom,
result.accountNameTo,
result.fee,
result.currency,
result.time,
result.channel);
} catch (error) {
throw new error(`[支付宝账单] get function: ${error}`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ import {BillType} from "../../../../utils/BillType";
import {RuleObject} from "../../../../utils/RuleObject";
import {Currency} from "../../../../utils/Currency";

/**
* 处理数据并返回结果
* @param {string} data - 要处理的数据
* @returns {RuleObject|null} - 处理结果对象,如果处理失败则返回null
*/
export function get(data) {
try {
data = JSON.parse(data);
const {description, source, title } = data.mMap;
// 解析数据
const {description, source, title } = JSON.parse(data).mMap;

// 检查源名称和标题是否匹配
if (source !== "收钱吧福利社" || title !== "交易完成通知") {
return null;
}

const [_, money,_text1, shopName,shopItem] = description.match(/(\d+(\.\d{2})?)\n(.*?) (.*?)?\n\d+/);
// 使用解构赋值从description.match数组中提取值
const [_, money, , shopName, shopItem] = description.match(/(\d+(\.\d{2})?)\n(.*?) (.*?)?\n\d+/);

// 创建并返回RuleObject对象
return new RuleObject(
BillType.Expend,
parseFloat(money),
Expand All @@ -26,6 +34,6 @@ export function get(data) {
`微信[收钱吧消费通知]`
);
} catch (error) {
throw new error(`[微信公众号收钱吧福利社] get function: ${error}`);
throw new Error(`[微信公众号收钱吧福利社] get function: ${error}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,46 @@ import { RuleObject } from "../../../../utils/RuleObject";
import { BillType } from "../../../../utils/BillType";
import { Currency } from "../../../../utils/Currency";

// 定义源名称和需要匹配的标题数组
const SOURCE_NAME = "长沙住房公积金";
const TITLES = ["公积金账户资金变动提醒"];

// 定义用于解析文本的正则表达式
const regex = /(.*?)\n(.*?)\n(.*?)\n(.*?)\n.*/;

/**
* 解析文本并返回解析结果。
* @param {string} text - 需要解析的文本。
* @returns {Object|null} - 解析后的结果对象,如果解析失败则返回null
* 解析文本数据,提取所需信息
* @param {string} text - 待解析的文本数据
* @returns {Object|null} - 解析后的数据对象,如果解析失败则返回null
*/
function parseText(text) {
let match = text.match(regex);
const match = text.match(regex);
if (!match) return null;

// 解析数据
let type = match[3].includes("汇缴") ? BillType.Income : null;
let time = match[2];
let shopName = SOURCE_NAME;
let shopItem = match[3];
let money = parseFloat(match[4]);
let accountNameFrom = match[1];
let channel = match[3].includes("汇缴") ? "微信[长沙公积金汇缴]":"";
const [, accountNameFrom, time, shopItem, money] = match;
const type = shopItem.includes("汇缴") ? BillType.Income : null;
const channel = shopItem.includes("汇缴") ? "微信[长沙公积金汇缴]" : "";

// 返回解析结果
return {
type,
time,
shopName,
shopName: SOURCE_NAME,
shopItem,
money,
money: parseFloat(money),
accountNameFrom,
channel
};
}

/**
* 从微信公众号长沙住房公积金中解析数据并返回RuleObject对象。
* @param {string} data - 包含数据的JSON字符串。
* @returns {RuleObject|null} - 解析后的RuleObject对象,如果解析失败则返回null。
* 根据数据获取规则对象
* @param {string} data - 数据字符串
* @returns {RuleObject|null} - 规则对象,如果获取失败则返回null
*/
export function get(data) {
// 解析数据
data = JSON.parse(data);
let mapItem = data.mMap;

// 检查源名称和标题是否匹配
const mapItem = JSON.parse(data).mMap;
if (mapItem.source !== SOURCE_NAME || !TITLES.includes(mapItem.title)) return null;

// 解析文本
let parsedData = parseText(mapItem.description);
// 检查解析结果是否有效
if (!parsedData || parsedData.type===null) return null;
const parsedData = parseText(mapItem.description);
if (!parsedData || parsedData.type === null) return null;

// 创建并返回RuleObject对象
return new RuleObject(
parsedData.type,
parsedData.money,
Expand All @@ -71,3 +55,4 @@ export function get(data) {
parsedData.channel
);
}

35 changes: 20 additions & 15 deletions src/rule/app/com.tencent.mm/微信公众号长沙银行/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@ const regex = /交易时间:(.*?)\n交易类型:(.*?)(个人账户:尾
* @returns {Object|null} - 解析结果对象,如果解析失败则返回null
*/
function parseText(text) {
let match = text.match(regex);
const match = text.match(regex);
if (!match) return null;
let currentYear = new Date().getFullYear(); // 获取当前年份
const currentYear = new Date().getFullYear(); // 获取当前年份

let type = match[2].includes("支付取出") ? BillType.Expend : null;
let time = `${currentYear}${match[1]}`;
let shopItem = match[5];
let money = parseFloat(match[4]);
let accountNameFrom = `长沙银行(${match[3]})`;
// 使用解构赋值从match数组中提取值
const [, time, type, account, money, shopItem] = match;
const accountNameFrom = `长沙银行(${account})`;

return {
type,
time,
type: type.includes("支付取出") ? BillType.Expend : null,
time: `${currentYear}${time}`,
shopItem,
money,
money: parseFloat(money.replace(",", "")),
accountNameFrom
};
}
Expand All @@ -42,16 +40,23 @@ function parseText(text) {
*/
export function get(data) {
// 解析数据
data = JSON.parse(data);
let mapItem = data.mMap;
const mapItem = JSON.parse(data).mMap;

// 检查源名称和标题是否匹配
if (mapItem.source !== SOURCE_NAME || !TITLES.includes(mapItem.title)) return null;
if (mapItem.source !== SOURCE_NAME ||
!TITLES.includes(mapItem.title)
) {
return null;
}

// 解析文本
let parsedData = parseText(mapItem.description);
const parsedData = parseText(mapItem.description);
// 检查解析结果是否有效
if (!parsedData || parsedData.type===null) return null;
if (!parsedData ||
parsedData.type === null
) {
return null;
}

// 创建并返回RuleObject对象
return new RuleObject(
Expand Down
9 changes: 7 additions & 2 deletions src/rule/app/com.tencent.mm/微信支付/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ function parseWeChatText(text) {
export function get(data) {
const mapItem = JSON.parse(data).mMap;
if (mapItem.source !== SOURCE_NAME_WECHAT ||
!TITLES_WECHAT.includes(mapItem.title.replace(/\d+\.\d{2}/, "")))
!TITLES_WECHAT.includes(
mapItem.title.replace(/\d+\.\d{2}/, "")
)
){
return null;

}
// 解析文本
const parsedText = parseWeChatText(mapItem.description);
if (!parsedText || parsedText.type === null) return null;

// 创建并返回RuleObject对象
return new RuleObject(
parsedText.type,
parsedText.money,
Expand Down

0 comments on commit 7139834

Please sign in to comment.