<?php
/**
* 微信加载JS_SDK验证流程
* 通过JS_SDK完成手机图片的上传
*
*/
//测试账号的appid和secret
$appid = "wx7cc5a26364d36484";
$secret = "eaa494de470bc150cbe05bd4484215c0";
//通过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://weixin.bwphp.cn/weixin.php'));
echo $signature;
?>
<br><br>
<button οnclick="uploadimg()">upload</button><button οnclick="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>
微信JS-SDK加载验证流程和手机图片上传
最新推荐文章于 2019-12-02 14:57:46 发布