微信扫码登录

package video.controller;

import video.config.WeChatConfig;
import video.domain.User;
import video.dto.JsonData;
import video.utils.HttpUtils;
import video.utils.JwtUtil;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;


/**微信扫描登录功能*
@Controller
@RequestMapping("/api/v1/wechat")
public class WechatController {

@Autowired
private WeChatConfig weChatConfig;

/**获取二维码链接*/
@GetMapping(value = "login_url")
@ResponseBody
public JsonData loginUrl(@RequestParam(value = "access_page",required = true) String accessPage) throws UnsupportedEncodingException {
String openRedirectUrl = weChatConfig.getOpenRedirectUrl();//回调地址
String callBackUrl = URLEncoder.encode(openRedirectUrl, "GBK");//进行编码
String qrCodeUrl = String.format(weChatConfig.getOpenQrcodeUrl(), weChatConfig.getOpenAppId(), callBackUrl, accessPage);
return JsonData.success(qrCodeUrl);
}


/**
* 信扫码登录,回调地址
* @param code 微信返回的code
* @param state 登录时,应用系统当前的页面地址
* @param response 用于跳转页面
*/
@GetMapping(value = "/user/callback")
public void wechatUserCallBack(@RequestParam(value = "code", required = true) String code, String state, HttpServletResponse response) throws IOException {
//TODO:userService.saveWechatUser(code);
User user = saveWeChatUser(code);
if (user != null) {
String token = JwtUtil.geneJsonWebJwt(user);
// state 当前用户的页面地址,需要拼接 http:// 这样才不会站内跳转
response.sendRedirect(state + "?token=" + token + "&head_img=" + user.getHeadImg()
+ "&name=" + URLEncoder.encode(user.getName(),"UTF-8"));
}
}

//************************以下为UserService中方法******************************//

public User saveWeChatUser(String code) {
//获取access_token
String accessTokenUrl = String.format(weChatConfig.getOpenAccessTokenUrl(),
weChatConfig.getOpenAppId(),weChatConfig.getOpenAppSecret(),code);
Map<String, Object> baseMap = HttpUtils.doGet(accessTokenUrl);

if(baseMap == null || baseMap.isEmpty()){
return null;
}

String accessToken = (String)baseMap.get("access_token");
String openId = (String) baseMap.get("openid");

User dbUser = getUserByOpenId(openId);
if (dbUser != null) {
return dbUser;
}

//获取用户基本信息
String userInfoUrl = String.format(weChatConfig.getOpenUserInfoUrl(),
accessToken, weChatConfig.getOpenAppId(),weChatConfig.getLanguage());
Map<String, Object> userInfoMap = HttpUtils.doGet(userInfoUrl);

if(userInfoMap == null || userInfoMap.isEmpty()){
return null;
}

String openid = (String) userInfoMap.get("openid");
String nickname = (String)userInfoMap.get("nickname");
Double sex = (Double) userInfoMap.get("sex");
String country = (String) userInfoMap.get("country");
String province = (String) userInfoMap.get("province");
String city = (String)userInfoMap.get("city");
String headimgurl = (String) userInfoMap.get("headimgurl");
String address = new StringBuilder(country).append("||").append(province).append("||").append(city).toString();
try {
nickname = new String(nickname.getBytes("ISO-8859-1"),"UTF-8");
address = new String(address.getBytes("ISO-8859-1"), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}

User user = new User();
user.setName(nickname);
user.setHeadImg(headimgurl);
user.setCity(address);
user.setCreateTime(new Date());
user.setOpenid(openid);
user.setSex(sex.intValue());
saveUser(user);
return user;
}

/**
* 保存用户
* @param user
* @return
*/
public Integer saveUser(User user) {
//TODO: int id = userMapper.save(user);
return user.getId();
}

/**
* 根据openid找用户
* @param openId
* @return
*/
public User getUserByOpenId(String openId) {
//todo:User user = userMapper.getUserByOpenId(openId);
return new User();
}
}
posted @ 2019-07-03 09:24  scwyfy  阅读(329)  评论(0编辑  收藏  举报