Add the function of updating badge times. #1

Merged
rainerosion merged 3 commits from feature-update into master 2022-03-07 20:26:21 +08:00

View File

@ -1,6 +1,6 @@
"auto"
// 徽章数据
var storage = storages.create("com.netease.sky:badge");
var storage = storages.create("com.netease.sky:badge1");
// storage.clear();
var global_option = getMenu();
if (!storage.contains("menu")) {
@ -49,6 +49,8 @@ function startSky() {
console.log(options[i]);
let value = menu_map[options[i]];
if (value.length > 0) {
// 计算菜单长度
let len = Object.keys(menu_map).length;
switch (value) {
case "add":
toast("添加徽章");
@ -85,8 +87,8 @@ function startSky() {
startSky();
break;
case "delete":
let len = Object.keys(menu_map).length
if (len > 3) {
// let len = Object.keys(menu_map).length;
if (len - 1 > Object.keys(getMenu()).length) {
delete_options = [];
storage_map = {};
Object.keys(menu_map).map(function (key, index) {
@ -94,7 +96,7 @@ function startSky() {
if (!isOptions(key, menu_map)) {
delete_options.push(key);
}
if(!isSwitchOptions(key, menu_map)){
if (!isSwitchOptions(key, menu_map)) {
storage_map[key] = menu_map[key]
}
});
@ -115,6 +117,55 @@ function startSky() {
selectAppChannelPackageName();
startSky();
break;
case "update":
// 更新徽章次数
// let len = Object.keys(menu_map).length;
console.log("len => " + len + " menuLen => " + Object.keys(getMenu()).length);
if (len - 1 > Object.keys(getMenu()).length) {
update_options = [];
storage_map = {};
Object.keys(menu_map).map(function (key, index) {
console.log("key => " + key, "index => " + index);
if (!isOptions(key, menu_map)) {
update_options.push(key);
}
if (!isSwitchOptions(key, menu_map)) {
storage_map[key] = menu_map[key]
}
});
let update_index = dialogs.select("请选择需要更新的徽章", update_options);
// console.log("update => " + update_options[update_index]);
// 获取数据
let uri = storage_map[update_options[update_index]];
// 截取前缀
let prefix = uri.substring(0, uri.length - 6);
// 计数器
let counter = uri.substring(uri.length - 6);
// console.log("prefix => " + prefix + ",counter => " + counter + ",counterDec => " + hex2dec(counter));
// 保存数据
let num = rawInput("请在输入框输入新的次数,当前:" + hex2dec(counter) + "次").trim();
rules = /^[0-9]+$/;
if (rules.test(num)) {
if (num < hex2dec(counter)) {
toast("将要更新的次数[" + num + "]小于当前次数[" + hex2dec(counter) + "]可能会无法使用,请悉知!!!");
}
// 转换为16进制
let counter_hex = dec2hex(num, 6)
// console.log("num => " + num + ",hex => " + counter_hex + "newData => " + (prefix + counter_hex));
// 更新数据
storage_map[update_options[update_index]] = prefix + counter_hex;
storage.put("menu", storage_map);
} else {
toast("徽章次数只能输入整数。");
return;
}
startSky();
} else {
toast("你没有录入任何徽章信息");
// 再次打开菜单
startSky();
}
break;
default:
// 启动app
console.log(value);
@ -134,7 +185,12 @@ function startSky() {
* @returns {boolean}
*/
function isOptions(key, menu) {
if (menu[key] == "add" || menu[key] == "delete" || menu[key] == "clear" || menu[key] == "channel") {
if (menu[key] == "add"
|| menu[key] == "delete"
|| menu[key] == "clear"
|| menu[key] == "channel"
|| menu[key] == "update"
) {
return true;
}
return false;
@ -147,7 +203,7 @@ function isOptions(key, menu) {
* @param menu
* @returns {boolean}
*/
function isSwitchOptions(key, menu) {
function isSwitchOptions(key, menu) {
if (menu[key] == "channel") {
return true;
}
@ -179,7 +235,7 @@ function selectAppChannelPackageName() {
"[C] 小米": "com.netease.sky.mi",
"[C] VIVO": "com.netease.sky.vivo",
"[C] 应用宝": "com.tencent.tmgp.eyou.eygy",
"[C] 华为" : "com.netease.sky.huawei",
"[C] 华为": "com.netease.sky.huawei",
"[I] 国际服": "com.tgc.sky.android",
};
let channel_options = Object.keys(package_map).map(function (data) {
@ -205,7 +261,8 @@ function getMenu() {
return {
"[O] 录入徽章": "add",
"[O] 清空徽章": "clear",
"[O] 删除徽章": "delete"
"[O] 删除徽章": "delete",
"[O] 更新次数": "update"
};
}
@ -250,4 +307,33 @@ function currentChannel() {
storage.put("channel", channel_map);
}
return storage.get("channel");
}
/**
* 16进制转10进制
* @param {*} hex 16进制
* @returns
*/
function hex2dec(hex) {
return parseInt(hex, 16);
}
/**
* 10进制转16进制
*
* @param {*} dec 10进制
* @param {*} len 长度
* @returns
*/
function dec2hex(dec, len) {
var hex = "";
while (dec) {
var last = dec & 15;
hex = String.fromCharCode(((last > 9) ? 55 : 48) + last) + hex;
dec >>= 4;
}
if (len) {
while (hex.length < len) hex = '0' + hex;
}
return hex;
}