参考:http://blog.bwphp.cn/?p=617
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
80
81
82
83
84
85
86
87
|
<?php
/**
* 微信加载JS_SDK验证流程
* 通过JS_SDK完成手机图片的上传
*
*/
//测试账号的appid和secret
$appid
=
"wx69ee1b32fb7e9121"
;
$secret
=
"b5606c679038148d6e858687bc830a18"
;
//通过url获取token
$url
=
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
.
$appid
.
"&secret="
.
$secret
;
$res
=
file_get_contents
(
$url
);
$data
= json_decode(
$res
);
//通过获取的token值获取jsapi临时票据ticket
$jsapi_ticket_json
=
file_get_contents
(
'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='
.
$data
->access_token.
'&type=jsapi'
);
$jsapi_ticket_obj
= json_decode(
$jsapi_ticket_json
);
$jsapi_ticket
=
$jsapi_ticket_obj
->ticket;
//设置随机字符串和时间戳
$noncestr
=
'qwertyuiop'
;
$timestamp
= time();
//获取加密验证签名
$signature
= sha1(sprintf(
'jsapi_ticket='
.
$jsapi_ticket
.
'&noncestr='
.
$noncestr
.
'×tamp='
.
$timestamp
.
'&url=http://zhangsixia.bwphp.cn/wei1.php'
));
echo
$signature
;
?>
<br><br>
<button onclick=
"uploadimg()"
>upload</button><button onclick=
"selectimg()"
>selectimg!!!!</button>
<script src=
'http://res.wx.qq.com/open/js/jweixin-1.0.0.js'
></script>
<script type=
"text/javascript"
>
//配置JS_SDK获取加载JS_SDK文件(jweixin-1.0.0.js)的权限
wx.config({
debug: true,
appId:
'<?php echo $appid; ?>'
,
timestamp: <?php
echo
$timestamp
; ?>,
nonceStr:
'<?php echo $noncestr; ?>'
,
signature:
'<?php echo $signature; ?>'
,
jsApiList: [
'uploadImage'
,
'downloadImage'
,
'previewImage'
,
'chooseImage'
]
});
//定义全局变量
var
images = {localIds:[],serverId:[]};
//加载成功后,检测接口权限
wx.ready(
function
(){
wx.checkJsApi({
jsApiList: [
'chooseImage'
,
'uploadImage'
,
'previewImage'
,
'downloadImage'
],
success:
function
(res) {}
});
});
wx.error(
function
(res){
alert(
'no'
);
});
//选择图片
function
selectimg(){
wx.chooseImage({
success:
function
(res) {
images.localIds = res.localIds;
document.getElementById(
'localimg'
).src = localIds;
}
});
}
//上传图片,上传的图片在微信的文件服务器存储时间有限,要下载到应用服务器
function
uploadimg(){
for
(
var
i = 0 ;i < images.localIds.length ; i++){
wx.uploadImage({
localId: images.localIds[i],
isShowProgressTips: 1,
success:
function
(res) {
images.serverId.push(res.serverId);
alert(images.serverId);
},
fail:
function
(res) {
alert(
"upload fail!"
+res.errMsg);
}
});
}
}
</script>
|
本文转自噼里啪啦啦 51CTO博客,原文链接:http://blog.51cto.com/pilipala/1661910
,如需转载请自行联系原作者