|
|
@@ -0,0 +1,112 @@
|
|
|
+package com.uas.sso.weixin.controller;
|
|
|
+
|
|
|
+import com.uas.sso.controller.BaseController;
|
|
|
+import com.uas.sso.entity.App;
|
|
|
+import com.uas.sso.entity.User;
|
|
|
+import com.uas.sso.service.AppService;
|
|
|
+import com.uas.sso.weixin.entity.OAuthInfo;
|
|
|
+import com.uas.sso.entity.Token;
|
|
|
+import com.uas.sso.service.UserService;
|
|
|
+import com.uas.sso.weixin.entity.UserInfo;
|
|
|
+import com.uas.sso.weixin.service.WeChatService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.ui.ModelMap;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.URLEncoder;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wangmh
|
|
|
+ * @create 2018-06-07 11:27
|
|
|
+ * @desc 微信登录
|
|
|
+ **/
|
|
|
+@RequestMapping("/weChat")
|
|
|
+@RestController
|
|
|
+public class WeChatController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WeChatService weChatService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AppService appService;
|
|
|
+
|
|
|
+ private static final String DEFAULT_WX_APPID = "wxa14aec4edce8a2d2";
|
|
|
+
|
|
|
+ private static final String WX_APPSECRET = "9749d2df03eb161f4d4075df69c2e2c9";
|
|
|
+
|
|
|
+ private static final String WX_REDIRECT_URI = "http://sso.ubtob.com/";
|
|
|
+
|
|
|
+ private static final String WX_LOGIN_SCOPE = "snsapi_login";
|
|
|
+
|
|
|
+ @RequestMapping("/getQrUrl")
|
|
|
+ public ModelMap getQrUrl(@RequestParam(defaultValue = "sso") String appId, @RequestParam String url) {
|
|
|
+ try {
|
|
|
+ url = URLEncoder.encode(url, "utf8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException("程序编码异常");
|
|
|
+ }
|
|
|
+ String qrUrl = "https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
|
|
|
+ App app = appService.findOne(appId);
|
|
|
+ String wx_appid = DEFAULT_WX_APPID;
|
|
|
+ if (app != null && !StringUtils.isEmpty(app.getWxAppid())) {
|
|
|
+ wx_appid = app.getWxAppid();
|
|
|
+ }
|
|
|
+ qrUrl = qrUrl.replace("APPID", wx_appid).replace("REDIRECT_URI", url).replace("SCOPE", WX_LOGIN_SCOPE).replace("STATE", "");
|
|
|
+ return success(qrUrl);
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/getQrParams")
|
|
|
+ public ModelMap getQrParams(String appId) {
|
|
|
+ ModelMap params = new ModelMap();
|
|
|
+ App app = appService.findOne(appId);
|
|
|
+ String wx_appid = DEFAULT_WX_APPID;
|
|
|
+ if (app != null && !StringUtils.isEmpty(app.getWxAppid())) {
|
|
|
+ wx_appid = app.getWxAppid();
|
|
|
+ }
|
|
|
+ params.addAttribute("appid", wx_appid).addAttribute("state", "");
|
|
|
+ return params;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绑定微信账号
|
|
|
+ * @param t 微信登录失败返回的token
|
|
|
+ * @param username 绑定用户名
|
|
|
+ * @param password 绑定密码
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/addAccount", method = RequestMethod.POST)
|
|
|
+ public ModelMap addAccount(@RequestParam String t, String username, String password) {
|
|
|
+ Token token = tokenService.findOne(t);
|
|
|
+ if (token == null || token.isExpired()) {
|
|
|
+ return error("绑定失败,请重新扫描二维码");
|
|
|
+ }
|
|
|
+
|
|
|
+ Token unionidToken = tokenService.findOne(t);
|
|
|
+ if (unionidToken != null) {
|
|
|
+ OAuthInfo oAuthInfo = (OAuthInfo) unionidToken.getBind();
|
|
|
+ User user = userService.bindUnionId(username, password, oAuthInfo.getUnionid());
|
|
|
+ request.getSession().setAttribute("userUU", user.getUserUU());
|
|
|
+ }
|
|
|
+
|
|
|
+ tokenService.delete(t);
|
|
|
+
|
|
|
+ return success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping(value = "/userInfo/{token}", method = RequestMethod.GET)
|
|
|
+ public ModelMap findUserByToken(@PathVariable("token") String t) {
|
|
|
+ Token token = tokenService.findOne(t);
|
|
|
+ Assert.notNull(token, "验证信息过期,请重新扫码");
|
|
|
+ Assert.isTrue(token.getBind() instanceof OAuthInfo, "参数错误");
|
|
|
+
|
|
|
+ OAuthInfo oAuthInfo = (OAuthInfo) token.getBind();
|
|
|
+ UserInfo userInfo = weChatService.findUserByUnionid(oAuthInfo.getUnionid(), oAuthInfo.getAccess_token());
|
|
|
+ return success(userInfo);
|
|
|
+ }
|
|
|
+}
|