java设计按月每天签到_签到功能java实现

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

https://github.com/ren2881971/WeChat

着急实现功能 没重构 ######

/**

* 微信授权

*

*

*/

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 queryParas = new HashMap();

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 queryParas = new HashMap();

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形如:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe0a2ed1be585xxxx&redirect_uri=http://mywebsite.com/user/oauthWeixin&response_type=code&scope=snsapi_userinfo&state=menu0xxx020010#wechat_redirect

######

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

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

https://github.com/ren2881971/WeChat

着急实现功能 没重构

appID=wx29bedaf2e1bd9d2d

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

开源代码不要把这些放出来啊。 ######哎呀呀 失误失误######可以试试这个https://github.com/chanjarster/weixin-java-tools######这个库 真的很不错。 我一直在学习他~######记得一定要交300给腾讯,不然 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值