ZiGma

【JSBox】利用JSBox在朋友圈斗图(二)

字数统计: 384阅读时长: 2 min
2018/04/29 Share

前言

最近继续在研究JSBox,并且发现微信似乎不屏蔽七牛云的链接了,所以将之前图片评论的js移植到了七牛云。七牛云上传是要上传凭证的,怎么计算Token花费不少时间,之前做的方案代码过多,运行效率不高,参考了RYAN大的QiniuManager后成功移植。

安装链接

详细代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const CryptoJS = require("crypto-js")
String.prototype.urlsafe = function() {
return this.replace(/\+/g, "-").replace(/\//g, "_")
}
//计算Token
//你的AK
var AK = ""
//你的SK
var SK = ""
//你的储存空间名
var bucket = ""
//你的储存域名
var domain = ""
var deadline = Math.round(new Date().getTime() / 1000) + 1 * 3600
var params = {scope: bucket,deadline: deadline}
var policy = JSON.stringify(params)
var policyEncoded = $text.base64Encode(policy).urlsafe()
var sign = CryptoJS.HmacSHA1(policyEncoded, SK)
var signEncoded = sign.toString(CryptoJS.enc.Base64).urlsafe()
var token = AK + ":" + signEncoded + ":" + policyEncoded

function PickImage() {
$photo.pick({
format: "image",
handler: function(resp) {
var image = resp.image
if (image) {
Upload(image)
} else {
$ui.loading(false)
}
}
})
}

function Upload(image) {
$http.upload({
url: "http://upload.qiniu.com/",
form: {
token: token
},
files: [{
"image": image,
"name": "file",
"filename": "file"
}],
progress: function(percentage) {

},
handler: function(resp) {
var url = domain + resp.data.key
if (url) {
Shorturl(url)
} else {
$ui.alert("图片上传失败")
}
}
})
}

function Shorturl(url) {
$http.shorten({
url: url,
handler: function(url) {
if (url) {
$clipboard.text = "图片评论 " + url
$ui.toast("复制成功", 2)
$app.close(2)
}
}
})
}

var inputImage = $context.image
if (inputImage) {
Upload(inputImage)
}else{
PickImage()
}
CATALOG
  1. 1. 前言
  2. 2. 安装链接
  3. 3. 详细代码