Heim  >  Artikel  >  Backend-Entwicklung  >  javascript - php 微信开发中图片长传的问题?

javascript - php 微信开发中图片长传的问题?

WBOY
WBOYOriginal
2016-06-06 20:12:451203Durchsuche

大神们,php微信服务号开发,有没有好的图片上传插件推荐。自己找个图片上传插件,微信端不怎么支。

回复内容:

大神们,php微信服务号开发,有没有好的图片上传插件推荐。自己找个图片上传插件,微信端不怎么支。

这个一般都是用微信自己的js-sdk插件,微信的策略是:

<code>配置`js-sdk`,成功后调用微信接口比如:拍照、图片选择、图片上传,当然图片是上传到微信服务器的,然后你需要做的是在程序后端利用微信上传返回的`meidia_id`来下载上传的图片,这样就不需要自己找插件了,而且你自己找的插件微信上也不会支持的;</code>
<code>wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: '{$appid}', // 必填,公众号的唯一标识
        timestamp: "{$timestamp}", // 必填,生成签名的时间戳
        nonceStr: '{$noncestr}', // 必填,生成签名的随机串
        signature: '{$signature}',// 必填,签名,见附录1
        jsApiList: ['chooseImage','previewImage','uploadImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
    });
    $(".box-fill").each(function(){
        var ele = $(this);
        $(this).click(function(){
            wx.chooseImage({
                count: 1, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
                success: function (res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
                    var $imgStr = "<img  src="%22+%20localIds%20+%22"   style="max-width:90%" alt="javascript - php 微信开发中图片长传的问题?" >";
                    ele.html($imgStr);
                    wx.uploadImage({
                        localId: localIds.toString(), // 需要上传的图片的本地ID,由chooseImage接口获得
                        isShowProgressTips: 1, // 默认为1,显示进度提示
                        success: function (res) {
                            var serverId = res.serverId; // 返回图片的服务器端ID
                            uploadServerId(serverId, ele.data('type'));
                        }
                    });
                }
            });
        })
    })
    /**
     * 上传服务id,也就是可以在微信服务器下载图片的media_id
     * @param serverId
     * @param type 上传图片类型
     */
    function uploadServerId(serverId, type){
        $.post("{:U('uploadServerId')}", {openId: "{$openid}", serverId: serverId, type: type},function(ret){
            if(ret.status == -1){
                alert(ret.msg);
            }else if(ret.status == 1){
                if(ret.finish){
                    $(".filters").hide();
                }
            }else{
                alert('请关闭页面后重新登陆上传!');
            }
        })
    }</code>

另外,建议你去微信公众平台看下js-sdk的开发文档,因为这涉及到了后端的一些东西,微信要求部分信息需要在服务端完成,而不是在前端

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn