Java版微信开发接口请求技术问题

時間會抹平那些皺褶遺 发布于 2015/05/13 13:35
阅读 2K+
收藏 0

Java开发微信具体怎么做?谁有详细的案例借我参考参考。接口部分的获取(OpenId、access_token及微信用户信息)及请求即可

加载中
0
渔樵耕读
渔樵耕读

不嫌代码写的烂 可以参考下

https://github.com/ren2881971/WeChat

着急实现功能 没重构

0
cnDavidChen
cnDavidChen
/**
 * 微信授权
 * 
 *
 */
public class CUserOauthWeixinRedirect extends ABaseCommand {
private static final String URL_OAUTH_ACCESS_TOKEN = 
"https://api.weixin.qq.com/sns/oauth2/access_token";

private static final String URL_OAUTH_USER_INFO = "https://api.weixin.qq.com/sns/userinfo";

/** 状态:module0xxx010010 模块0xxx0识别ID*/
private String state;
/** 换取access_token票据 */
private String code;

String module;
String id;
String ip;

@Override
public void init() throws Exception {
state = getPara("state");
if (StrKit.isBlank(state)) {
throw new ArgException("状态不正确");
}

String[] moduleInfoArray = state.split(Define.WEIXIN_MODULE_OAUTH_SPLITER);
if (moduleInfoArray.length != 2) {
throw new ArgException("状态不正确");
}

module = moduleInfoArray[0];
if (!Define.validateWeixinModule(module)) {
throw new ArgException("模块不存在");
}

id = moduleInfoArray[1];

code = getPara("code");
Util.assertNotBlank(code, "code不能为空");

ip = "%";
}

@Override
public void doCommand(AResult result) throws Exception {
RedirectResult redirectResult = (RedirectResult) result;

Map<String, String> queryParas = new HashMap<String, String>();

queryParas.put("grant_type", "authorization_code");
queryParas.put("appid", Define.WEIXIN_APP_ID);
queryParas.put("secret", Define.WEIXIN_APP_SECRET);
queryParas.put("code", code);

String responseStr = null;
try {
responseStr = HttpKit.get(URL_OAUTH_ACCESS_TOKEN, queryParas);

} catch (Exception e) {
e.printStackTrace();
throw new BizException("微信授权失败");
}
System.out.println(responseStr);

JSONObject accessObj = JSONObject.parseObject(responseStr);
JSONObject userObj = fetchUserInfo(accessObj.getString("access_token"), accessObj.getString("openid"));
userObj.put("ip", ip);
userObj.put("token", accessObj.getString("access_token"));
userObj.put("refreshToken", accessObj.getString("refresh_token"));
userObj.put("expireSeconds", accessObj.getInteger("expires_in"));

UserBind userBind = UserService.service.findUserBindBySource(Define.USER_BIND_TYPE_WEIXIN, userObj.getString("fromId"));

CurrentUser currentUser;
User user = null;
if (userBind == null) {
user = new User();

AuthService.service.registerFromThird(Define.USER_BIND_TYPE_WEIXIN, userObj, user);

System.out.println("用户[openid=" + userObj.getString("fromId") + ", nickname=" + userObj.getString("nickname") + "]注册成功,用户ID为[" + user.getId() + "]");

}  else {
user = User.dao.findById(userBind.getToId());
}
currentUser = AuthService.service.login(user, ip);

System.out.println("正在跳转....");

redirectResult.setUrl("http://" + Define.APP_DOMAIN + getRedirectUrl() + "?u=" + currentUser.getId() + "&t=" + currentUser.getToken());
}

private JSONObject fetchUserInfo(String accessToken, String openId) {
Map<String, String> queryParas = new HashMap<String, String>();
queryParas.put("access_token", accessToken);
queryParas.put("openid", openId);

String response = HttpKit.get(URL_OAUTH_USER_INFO, queryParas);
System.out.println("user info: " + response);

JSONObject rawUserObj = JSONObject.parseObject(response);

JSONObject userObj = new JSONObject();
userObj.put("fromId", rawUserObj.getString("openid"));
userObj.put("nickname", rawUserObj.getString("nickname"));

int sex = rawUserObj.getInteger("sex");
userObj.put("gender", (sex == 0 || sex == 1) ? Define.GENDER_MALE : Define.GENDER_FEMALE);
userObj.put("province", rawUserObj.getString("province"));
userObj.put("city", rawUserObj.getString("city"));
userObj.put("country", rawUserObj.getString("country"));
userObj.put("avatar", rawUserObj.getString("headimgurl"));

return userObj;
}

// TODO 策略模式重写
private String getRedirectUrl() {
String redirectUrl = "/statics/shop_locals.html";

// 菜单
if (Define.WEIXIN_MODULE_MENU.equals(module)) {
int menuId = Integer.parseInt(id);

if (!Define.validateWeixinModuleMenuId(menuId)) {
throw new ArgException("模块不存在");
}

switch (menuId) {
// --------我要签到
case Define.WEIXIN_MODULE_MENU_ID_CHECKIN_SIGN: {
redirectUrl = "/statics/checkin_sign.html";
break;
}

// --------签到记录
case Define.WEIXIN_MODULE_MENU_ID_CHECKIN_HISTORY: {
redirectUrl = "/statics/checkin_history.html";
break;
}

case Define.WEIXIN_MODULE_MENU_ID_SHOP_LOCALS: {
redirectUrl = "/statics/shop_locals.html";
break;
}

case Define.WEIXIN_MODULE_MENU_ID_SHOP_ACTIVITIES: {
redirectUrl = "/statics/shop_activities.html";
break;
}

// --------我的红包
case Define.WEIXIN_MODULE_MENU_ID_GIFTS: {
redirectUrl = "/statics/gifts.html";
break;
}

// --------我的消息
case Define.WEIXIN_MODULE_MENU_ID_MESSAGES: {
redirectUrl = "/statics/messages.html";
break;
}

// --------我的资料
case Define.WEIXIN_MODULE_MENU_ID_PROFILE: {
redirectUrl = "/statics/profile.html";
break;
}

default:
break;
}
}

// TODO 活动
if (Define.WEIXIN_MODULE_ACTIVITY.equals(module)) {

}

return redirectUrl;
}

@Override
public AResult initResult() {
return new RedirectResult();
}


用的是jfinal,可能不好理解,不过核心代码都在。

思路是:获取到用户信息后,让用户处于登录状态。并且根据state,跳转到相关的页面。

菜单中配置的url形如:



0
simlegate
simlegate

引用来自“渔樵耕读”的评论

不嫌代码写的烂 可以参考下

https://github.com/ren2881971/WeChat

着急实现功能 没重构

appID=wx29bedaf2e1bd9d2d

appSecret=**********************

开源代码不要把这些放出来啊。

渔樵耕读
渔樵耕读
哎呀呀 失误失误
0
simlegate
simlegate
可以试试这个https://github.com/chanjarster/weixin-java-tools
渔樵耕读
渔樵耕读
这个库 真的很不错。 我一直在学习他~
0
smarthjx636
smarthjx636
记得一定要交300给腾讯,不然 。
返回顶部
顶部