thinkphp 위챗 개발 jssdk 이미지 업로드 및 로컬 서버 다운로드

5467 단어 thinkphp
public function test2(){

        $Weixin = new \Weixin\Controller\BaseController();

        $this->assign('signPackage', $Weixin->jssdk->GetSignPackage());

        $this->display();

}


html 핵심 코드
<script type="text/javascript" src="__STATIC__/jquery-2.0.3.min.js"></script>

    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<form action="{:U('doJoin')}"  method="post">

<h3 class="title"> :</h3>

                <div>   

                   <img class="preview" src="{$vote.vote_pic|get_cover='thumb'}" alt="">

                   <if condition="!$vote['status']" ><button class="uploadImage" type="button" id="vote_pic"> ( 800*600 png,jpg, gif )</button></if>

                   <input type="hidden" name="vote_pic">                   

                </div>

<button class="bbon" type="submit" id="submit"> </button>

</form>

<script>  

 wx.config({

    debug: false,

    appId: '<?php echo $signPackage["appId"];?>',

    timestamp: <?php echo $signPackage["timestamp"];?>,

    nonceStr: '<?php echo $signPackage["nonceStr"];?>',

    signature: '<?php echo $signPackage["signature"];?>',

    jsApiList: [

    //   API  

    'chooseImage',

    'previewImage',

    'uploadImage',

    'downloadImage'

    ]

  });

  wx.ready(function () {

// 5.1  、 

      var images = {

        localId: [],

        serverId: []

      };



// 5.3  

$(".uploadImage").click(function(){

        var that =$(this);

        images.localId = [];

        wx.chooseImage({

          success: function (res) {

            images.localId = res.localIds;

            if (images.localId.length == 0) {

                alert('  chooseImage  ');

                return;

            }

            if(images.localId.length > 1) {

                alert(' , ');

                images.localId = [];

                return;

            }

            var i = 0, length = images.localId.length;

            images.serverId = [];

            function upload() {

                         wx.uploadImage({

                                    localId: images.localId[i],

                                    success: function (res) {

                                        i++;                             

                                       that.siblings('img.preview').attr('src',images.localId[0]);

                                        // alert(' :' + i + '/' + length);                                       

                                        images.serverId.push(res.serverId);

                                        that.siblings('input[type=hidden]').val(images.serverId[0]);

                                        // alert( that.siblings('input[type=hidden]').val());

                                        if (i < length) {

                                                upload();

                                        }

                                    },

                                    fail: function (res) {

                                                alert(JSON.stringify(res));

                                     }

                          });

            }

            upload();

          }

        });

});

        



$("#submit").click(function(){

    $.ajax({

            type: 'post',

            url: $("form").attr('action')  ,

            data : $("form").serialize(),

            dataType: 'json',

            success : function(data){

                    if(data.status){

                                       alert(data.info);

                                       window.location.href = data.url;      

                    }else{                      

                    }

                    

                                            

            }

        });

    return false;

})     

  });

</script>


서버 측에 제출 처리 과정은 다음과 같습니다.
public doJoin(){

        if(IS_POST && $_POST['vote_pic']){   // vote_pic id

            $pic_id = $_POST['vote_pic'];

            $return = array();

            C('WEIXIN_UPLOAD') = './Uploads/Weixin/';//             

            $dir = realpath(C('WEIXIN_UPLOAD')).'/'.date('Y_m_d').'/';//     

            if(!file_exists($dir)){

                mkdir($dir,'0777');

            }

            $Weixin = new \Weixin\Controller\BaseController();

            $pic_url = $Weixin->WechatAuth->mediaGet($pic_id); // 

            $time = time().substr($pic_id, 9,3);

            $filename = 'wx_'.$time.'.jpg';   // 

            $Http = new \Org\Net\Http;

            if($Http::curlDownload($pic_url,$dir.$filename)){//http 

                $this->success(' ',U('index'));

            }   





        }



        



    }


좋은 웹페이지 즐겨찾기