微博把mid加密了,要转化才能变成id,所以两个之间需要想互转换,js代码如下:
<!DOCTYPE html>
<html>
<head>
<title>this is weibo id2mid</title>
</head>
<body>
</body>
<script type="text/javascript">
this.str62keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* 10进制值转换为62进制
* @param {String} int10 10进制值
* @return {String} 62进制值
*/
window.fun1 = function int10to62(int10) {
var s62 = '';
var r = 0;
while (int10 != 0) {
r = int10 % 62;
s62 = this.str62keys.charAt(r) + s62;
int10 = Math.floor(int10 / 62);
}
return s62;
}
/**
* 62进制值转换为10进制
* @param {String} str62 62进制值
* @return {String} 10进制值
*/
window.fun2 = function str62to10(str62) {
var i10 = 0;
for (var i = 0; i < str62.length; i++) {
var n = str62.length - i - 1;
var s = str62.substr(i, 1); // str62[i]; 字符串用数组方式获取,IE下不支持为“undefined”
i10 += parseInt(this.str62keys.indexOf(s)) * Math.pow(62, n);
}
return i10;
}
/**
* id转换为mid
* @param {String} id 微博id,如 "201110410216293360"
* @return {String} 微博mid,如 "wr4mOFqpbO"
*/
window.fun3 = function id2mid(id) {
if (typeof (id) != 'string') {
return false; // id数值较大,必须为字符串!
}
var mid = '';
for (var i = id.length - 7; i > -7; i = i - 7) //从最后往前以7字节为一组读取mid
{
var offset1 = i < 0 ? 0 : i;
var offset2 = i + 7;
var num = id.substring(offset1, offset2);
num = fun1(num);
mid = num + mid;
}
return mid;
}
/**
* mid转换为id
* @param {String} mid 微博mid,如 "wr4mOFqpbO"
* @return {String} 微博id,如 "201110410216293360"
*/
window.fun4 = function mid2id(mid) {
var id = '';
for (var i = mid.length - 4; i > -4; i = i - 4) //从最后往前以4字节为一组读取mid字符
{
var offset1 = i < 0 ? 0 : i;
var len = i < 0 ? parseInt(mid.length % 4) : 4;
var str = mid.substr(offset1, len);
str = fun2(str).toString();
if (offset1 > 0) //若不是第一组,则不足7位补0
{
while (str.length < 7) {
str = '0' + str;
}
}
id = str + id;
}
return id;
}
console.log(fun4('H2NHjrBuh'));
</script>
</html>
分类: web
标签: javascript
搜索
标签
study
ab
amap
apache
apahe
awk
aws
bat
centos
CFS
chrome
cmd
cnpm
composer
consul
crontab
css
curl
cygwin
devops
di
docker
docker,docker-compose
ethereum
excel
fiddler
fluentd
framework
front-end
git
gitgui
github
glide
go
golang
gorm
grafana
gzip
ioc
item2
iterm2
javascript
jenkins
jsonp
kafka
laradock
laravel
larval
linux
liunux
log
mac
mac, wi-fi
macos
magento
mariaDB
minikube
mongoDB
msp
mysql
netbeans
nginx
nodejs
nohup
npm
nsq
oracle
php
php-fpm
php7
phpstorm
php扩展
Protobuf
python
redis
scp
server
shell
soap
socket
socket5
sql
sre
ssdb
ssh
ssl
study
sublime
swift
system
td-agent
uml
v2ray
vagrant
vagrnat
vim
vpn
vue
vue.js
webpack
webrtc
websocket
webtatic
windows
windows7
word
wps
xdebug
yarn
yii2
yum
zookeeper
世界国家
互联网
以太坊
分类
前端
小程序
打印机
排序算法
搞笑
权限
粤语
缓存
网络
虚拟机
视频
设计模式
项目管理
热门文章
友情链接