resources/ioc/conf.js
12 回复
resources/ioc/conf.js
var ioc = {
conf : {
type : "org.nutz.ioc.impl.PropertiesProxy",
fields : {
utf8 : false,
paths : [ "config/" ]
}
}
}
然后我的一个类:
@IocBean
public class WxService {
@Inject
private PropertiesProxy conf;
....
}
这个类里拿conf时是null
package com.yixin.gglm.service;
import com.sun.prism.impl.Disposer;
import org.nutz.dao.entity.Record;
import org.nutz.http.Http;
import org.nutz.http.Response;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.Mvcs;
import javax.servlet.http.HttpSession;
@IocBean
public class WxService {
@Inject
private PropertiesProxy conf;
static Log log = Logs.get();
public static void main(String[] args) throws Exception {
// getOpenId("");
}
public String getOpenId(String code) throws Exception{
// String realUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=wxed8ad8b76654736c&secret=9358666a8fde6a5b91faa354e1d05e44&js_code=011OJVEN1Y3rJ515yGEN1hFQEN1OJVEg&grant_type=authorization_code";
// HttpSession httpSession = Mvcs.getHttpSession();
// Object openid = httpSession.getAttribute("openid");
// if(openid != null){
// return openid.toString();
// }
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
String realUrl = String.format(url, conf.get("wx.appid"), conf.get("wx.appsecret"), code);
Response response = Http.get(realUrl);
if(response.getStatus() == 200){
log.debug(response.getContent());
Record record = Json.fromJson(Record.class, response.getContent());
// httpSession.setAttribute("openid",(String) record.get("openid"));
return (String) record.get("openid");
}else{
return null;
}
}
private static NutMap nm = new NutMap();
public WxService(){
init();
}
void init(){
NutMap map = new NutMap();
map.put("appid", conf.get("wx.appid"));
map.put("secret", conf.get("wx.appsecret"));
map.put("grant_type", "client_credential");
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=%s&appid=%s&secret=%s";
url = String.format(url,"client_credential",conf.get("wx.appid"),conf.get("wx.appsecret"));
Response response = Http.get(url, 60000);
if(response.isOK()){
log.debug(response.getContent());
NutMap nutMap = Json.fromJson(NutMap.class, response.getContent());
nm.setv("time",System.currentTimeMillis());
nm.setv("access_token",nutMap.get("access_token"));
}
}
public String getAccess_token(){
Object time = nm.get("time");
if(time == null){
return null;
}
synchronized (WxService.class){
long l = System.currentTimeMillis() - (long)time;
if(l > 7200000){//大于两个小时
init();
return String.valueOf(nm.get("access_token"));
}else{
return String.valueOf(nm.get("access_token"));
}
}
}
}
添加回复
请先登陆