微信JS-SDK加载验证流程和手机图片上传

参考: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 . '&timestamp=' . $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 ,如需转载请自行联系原作者






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值