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

参考:http://blog.bwphp.cn/?p=617

<?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>


你可能感兴趣的:(手机图片)