有时候我们会在微信有这样的需求、在一个上传控件里面上传多个图片、而在微信里面使用file上传有些手机会崩溃而且、服务器端压缩图片效果也没有微信压缩效果好。这个就要使用微信的jssdk了。
如下就是微信中上传多个文件的方法:
function paizhao(t) { var serverId = []; if (t != undefined) { var mid = ""; for (var i = 0; i < t.length; i++) { if (i < t.length - 1) { mid = mid + t[i] + ","; } else { mid = mid + t[i]; } } $.ajax({ type: "POST", url: "api.php?type=weixin_upload", data: {mid: mid}, dataType: "json", async: false, success: function (data) { if (data.status == '1') { $("#mid").val(data.data); } else { alert("上传失败"); } }}) return t; } wx.chooseImage({//选着文件或者进行拍照 success: function (res) { var localIds = res.localIds; syncUpload(localIds); } }); var syncUpload = function (localIds) {//上传文件 var localId = localIds.pop(); wx.uploadImage({ localId: localId, isShowProgressTips: 1, success: function (res) { // var serverId = res.serverId; // 返回图片的服务器端ID serverId.push(res.serverId); //其他对serverId做处理的代码 if (localIds.length > 0) { syncUpload(localIds); } else { paizhao(serverId);// } } }); }; }
如上是上传代码。 还需要在自己的服务器处理微信返回的媒体id 然后去下载到服务器:
//微信上传下载接口 if ($type == "weixin_upload") { $mid = isset($_REQUEST["mid"]) ? $_REQUEST["mid"] : ""; if (empty($mid)) { $json = null; $json["status"] = '-1'; $json['msg'] = '为空'; exit; } $t = ""; $arr = explode(",", $mid); $jssdk = new JSSDK("XXX", "XXX", "");微信的demo api $token = $jssdk->getAccessToken(); for ($i = 0; $i < count($arr); $i++) { if ($arr[$i] != null or ! empty($arr[$i])) { $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$token&media_id={$arr[$i]}"; $path =rand(1000,99999).time() . '.jpg';//保存路径 downloadFile($url, $path);//下载图片 if ($i < count($arr) - 1) { $t = $t .$path . ","; } else { $t = $t .$path; } } } $json = null; $json["status"] = "1"; $json["data"] = $t; echo json_encode($json); } /** * 保存文件到本地 * @param 文件路径 $url * @param 保存本地路径 $savePath * @return string */ function downloadFile($url, $savePath = '') { $file = file_get_contents($url); file_put_contents($savePath, $file); return $savePath; }
如上是完整的微信中多文件上传简单实例、
如下是微信的demo api jssdk.php
<?php class JSSDK { public $appId; public $appSecret; public $url; public function __construct($appId, $appSecret,$url="") { $this->appId = $appId; $this->appSecret = $appSecret; $this->url = $url; } public function get_token() { $json = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret"); $arr = json_decode($json, true); return $arr["access_token"]; } public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); // 注意 URL 一定要动态获取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = $this->url; $timestamp = time(); $nonceStr = $this->createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } public function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } public function getJsApiTicket() { // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); // 如果是企业号用以下 URL 获取 ticket // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } public function getAccessToken() { // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 $data = json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { // 如果是企业号用以下URL获取access_token // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } public function httpGet($url) { return file_get_contents($url); } }
关键字词: