js: UrlDecode解码、UUID和GUID、阿拉伯数字转为中文数字

本文介绍了JavaScript中的UrlDecode函数,展示了如何使用内置函数和自定义函数进行URL解码,并给出了生成UUID和GUID的示例。同时,还展示了如何将阿拉伯数字转换为中文数字的函数,以及作者作为前端开发者的分享和教学内容。

UrlDecode解码:

UrlDecode 是一个 JavaScript 函数,用于将经过 URL 编码的字符串转换为普通字符串。

URL 编码是将特殊字符转换为它们的百分比编码表示形式的过程。这些特殊字符包括空格、斜线、井号(#)等。UrlDecode 函数将这些百分比编码转换回相应的特殊字符。

UrlDecode 函数接受一个参数,即需要解码的字符串。它返回解码后的字符串。

JavaScript 自带的解码函数: decodeURIComponent ('url')

// JavaScript 自带的解码函数: decodeURIComponent
console.log(decodeURIComponent(`https://open.weixin.qq.com/connect/oauth2/authorize?id=123&redirect_uri=https%3a%2f%2fmy.test.cn%2fxui%3fAppKey%3daka`));

// 自己实现一个解码函数
function UrlDecode(zipStr) { 
  var uzipStr=""; 
  for(var i=0;i<zipStr.length;i++){ 
    var chr = zipStr.charAt(i); 
    if(chr == "+"){ 
      uzipStr+=" "; 
    }else if(chr=="%"){ 
      var asc = zipStr.substring(i+1,i+3); 
      if(parseInt("0x"+asc)>0x7f){ 
        uzipStr+=decodeURI("%"+asc.toString()+zipStr.substring(i+3,i+9).toString()); 
        i+=8; 
      }else{ 
        uzipStr+=AsciiToString(parseInt("0x"+asc)); 
        i+=2; 
      } 
    }else{ 
      uzipStr+= chr; 
    } 
  } 

  return uzipStr; 
}
function AsciiToString(asccode) { 
  return String.fromCharCode(asccode); 
}
// 测试 解码
console.log(UrlDecode(`https://open.weixin.qq.com/connect/oauth2/authorize?id=123&redirect_uri=https%3a%2f%2fmy.test.cn%2fxui%3fAppKey%3daka`));

打印出解码后的url:

用js写函数生成UUID 和 GUID:

function guid() {
  function S8() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (S8() + S8() + "-" + S8() + "-" + S8() + "-" + S8() + "-" + S8() + S8() + S8());
}

console.log(guid())

function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";
 
  var uuid = s.join("");
  return uuid;
}

console.log(uuid())

阿拉伯数字转换为中文数字:

function numberToChinese(num) { const chineseNumber = [ "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" ];
 const chineseUnit = ["", "十", "百", "千", "万", "亿"]; let result = ""; let unitIndex = 0; // 处理特殊情况:零 if (num === 0) { return chineseNumber[num]; } 
// 处理负数 if (num < 0) { result += "负"; num = Math.abs(num); } 
// 转换为字符串,便于逐位处理 const numString = num.toString(); 
for (let i = numString.length - 1; i >= 0; i--) { const digit = parseInt(numString[i]);
let digitResult = "";
 if (digit > 0) { 
	digitResult += chineseNumber[digit]; 
	digitResult += chineseUnit[unitIndex]; 
}
 result = digitResult + result; unitIndex++; }
 return result; 
} 
console.log(numberToChinese(12345)); // 输出 "一万二千三百四十五"
console.log(numberToChinese(7890)); // 输出 "七千八百九十"
console.log(numberToChinese(11)); // 输出 "十一"
console.log(numberToChinese(0)); // 输出 "零" 
console.log(numberToChinese(-123)); // 输出 "负一百二十三"

欢迎关注我的原创文章:小伙伴们!我是一名热衷于前端开发的作者,致力于分享我的知识和经验,帮助其他学习前端的小伙伴们。在我的文章中,你将会找到大量关于前端开发的精彩内容。

学习前端技术是现代互联网时代中非常重要的一项技能。无论你是想成为一名专业的前端工程师,还是仅仅对前端开发感兴趣,我的文章将能为你提供宝贵的指导和知识。

在我的文章中,你将会学到如何使用HTML、CSS和JavaScript创建精美的网页。我将深入讲解每个语言的基础知识,并提供一些实用技巧和最佳实践。无论你是初学者还是有一定经验的开发者,我的文章都能够满足你的学习需求。

此外,我还会分享一些关于前端开发的最新动态和行业趋势。互联网技术在不断发展,新的框架和工具层出不穷。通过我的文章,你将会了解到最新的前端技术趋势,并了解如何应对这些变化。

我深知学习前端不易,因此我将尽力以简洁明了的方式解释复杂的概念,并提供一些易于理解的实例和案例。我希望我的文章能够帮助你更快地理解前端开发,并提升你的技能。

如果你想了解更多关于前端开发的内容,不妨关注我的原创文章。我会不定期更新,为你带来最新的前端技术和知识。感谢你的关注和支持,我们一起探讨交流技术共同进步,期待与你一同探索前端开发的奇妙世界!

import subprocess import json import os import time import requests import base64 input_data = { "comm": { "cv": 4747474, "ct": 24, "format": "json", "inCharset": "utf-8", "outCharset": "utf-8", "notice": 0, "platform": "yqq.json", "needNewCode": 1, "uin": 3437490239, "g_tk_new_20200303": 266031493, "g_tk": 266031493 }, "req_1": { "method": "DoSearchForQQMusicDesktop", "module": "music.search.SearchCgiService", "param": { "remoteplace": "txt.yqq.top", "searchid": "61396278381243620", "search_type": 0, "query": "张杰", "page_num": 1, "num_per_page": 10 } } } node = r"D:\pycharm\test\.venv\js文件\node.exe" current_script_folder = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(current_script_folder, "Q3.js") result = subprocess.run( [node,path , json.dumps(input_data).replace('','')], capture_output=True, text=True, check=True, ) output = result.stdout.strip() # print(output) sign = output.split('\n')[0] data = output.split('\n')[-1] print('sign:', sign) print('post_data:', data) #模拟浏览器 headers = { 'Cookie': 'eas_sid=c1779514I3b1o2U223t4A8x0f5; pgv_pvid=4509586368; qq_domain_video_guid_verify=158cddf59bf7e531; _qimei_uuid42=19818120e11100b88e029b084d178d050f9dcdf2da; _qimei_fingerprint=613e16c97d89cd35354e261972e2309c; _qimei_h38=527ff4478e029b084d178d0502000007d19818; _qimei_q32=a542e33bbee99464d716925d62396560; _qimei_q36=06a5cc8a07d3e04de8c26903300018e19804; _qimei_i_3=54df24869c5b04d8c790f93852d521b5a4e6adf2160a0ad1e5877b5170c22964353034943c89e2a7bca0; fqm_pvqid=04e2e596-f91f-4e52-bc0d-460e388dc6dd; ts_refer=cn.bing.com/; ts_uid=3114094305; RK=Lx/xUywbuF; ptcz=f71a2eea57237082c921cbf2a4796c6829ca24503c07855acc91cfdae9a79dbd; o_cookie=3437490239; yyb_muid=1E53A5109E0C60303CC7B3639A0C6194; tvfe_boss_uuid=d5b60557d4af327d; _qimei_i_2=77f96882930b5588929faf305cd070b5f7eaf0a41a080184bd8f2b5b2693206d6936369c3089e6daa892; _qimei_i_1=46c06b87975d068dc593f9380e8471e9a5eeacf5465a048ae08f2d582493206c616330c13980eadddeb2a1ce; psrf_qqrefresh_token=25306F9170E324A9F1667C9FDF97C67D; wxrefresh_token=; psrf_access_token_expiresAt=1767849764; euin=oivi7ivqoe-iNv**; qm_keyst=Q_H_L_63k3NIt-qxN6LBudenJ0x5o6up-IuZQ7i8ttSWHuB5ne5sX4uqNETm1KTldM7KMnaGdRSRi_QX5HIv_2Rwcb3X26oGu-fGA; psrf_qqopenid=C3B45576011C2B3CF3274799A5073476; tmeLoginType=2; wxopenid=; qqmusic_key=Q_H_L_63k3NIt-qxN6LBudenJ0x5o6up-IuZQ7i8ttSWHuB5ne5sX4uqNETm1KTldM7KMnaGdRSRi_QX5HIv_2Rwcb3X26oGu-fGA; psrf_musickey_createtime=1762665764; wxunionid=; psrf_qqunionid=5C0557422511695E4BDB989B0055D221; music_ignore_pskey=202306271436Hn@vBj; uin=3437490239; psrf_qqaccess_token=E6A4F6CB8EB3F89857623B9D4F236137; fqm_sessionid=b1ec80c3-6c58-4d8e-922e-d2a6c767b350; pgv_info=ssid=s379673328; ts_last=y.qq.com/n/ryqq/player', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0', 'Referer': 'https://y.qq.com/' } #请求网址 url = 'https://u6.y.qq.com/cgi-bin/musics.fcg' # 查询参数 params = { '_': int(time.time() * 1000), 'encoding': 'ag-1', 'sign': sign } response = requests.post(url=url, params=params,data=data ,headers=headers ) print(response.text) content = response.content bs64 = base64.b64decode(content).decode('utf-8') print(bs64) 我这个代码有什么问题
11-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值