对请求URL签名,设置接口权限:
WxApi2 wxApi2 = wxConfigService.getWxApi2(wxid);
if (Lang.isEmpty(Globals.memoryJsapiTicketStore.get(wxid))) {
Globals.memoryJsapiTicketStore.put(wxid, new MemoryJsapiTicketStore());
}
MemoryJsapiTicketStore memoryJsapiTicketStore = Globals.memoryJsapiTicketStore.get(wxid);
wxApi2.setJsapiTicketStore(memoryJsapiTicketStore);
String url = "http://" + Globals.AppDomain + Globals.AppBase + "/public/wx/bzsx/wdsq/sc/" + id;
NutMap jsConfig = wxApi2.genJsSDKConfig(url, "chooseImage", "previewImage", "uploadImage", "downloadImage");
req.setAttribute("jsConfig", Json.toJson(jsConfig));
网页:
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
$("#div01").bind("click",function () {
var d=$(this);
wx.chooseImage({
count: 9, // 最大9 默认值
sizeType: ['compressed'], //只上传压缩图
sourceType: ['album', 'camera'], //从相册或者拍照
success: function (res) {
var localIds = res.localIds;//本地资源URL
var l=d.find(".leftdiv");
var str="";
$("#pic01").val("");
$.each(localIds,function (i,img) {//显示图片
str+='<img src="'+img+'" style="vertical-align: middle;width:35px;height: 35px;">';
});
l.html(str);
var j = 0, length = localIds.length;
var upload = function() {
wx.uploadImage({
localId: localIds[j],
isShowProgressTips: 1,//显示上传进度
success: function (res) {
var serverId = res.serverId;//media_id 用于下载
$("#pic01").val($("#pic01").val()+","+serverId);
j++;
if (j < length) {
upload();//一个图片上传完了才能上传第二个
}
}
});
};
upload();
}
});
});
wx.config(${jsConfig!});
wx.error(function(res){
if(res.errMsg){
alert(res.errMsg);
}
});
wx.ready(function(){
});
</script>
图片处理代码:
NutResource nutResource = wxApi2.media_get(serverId);//拉取微信图片到本地
String p = Globals.AppRoot;
String f = Globals.AppUploadPath + "/wx/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32();
Files.write(new File(p + f + ".jpg"), nutResource.getInputStream());//写文件
Images.zoomScale(p + f + ".jpg", p + f + "_s.jpg", 50, 50, Color.black);//生成缩略图,设置背景色