Browse Source

添加微信小程序授权

半月无霜 4 years ago
parent
commit
5e89b3080e

+ 2 - 0
ymall/src/main/java/com/liangjian11/ymall/config/WxMaConfiguration.java

@@ -24,6 +24,8 @@ import java.util.stream.Collectors;
 @Configuration
 @EnableConfigurationProperties(WxMaProperties.class)
 public class WxMaConfiguration {
+	public static final String MINIAPP_APPID = "wx16d19a8b8df3fb06";
+	
 	private WxMaProperties properties;
 	
 	private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap();

+ 51 - 5
ymall/src/main/java/com/liangjian11/ymall/controller/common/LoginController.java

@@ -1,8 +1,13 @@
 package com.liangjian11.ymall.controller.common;
 
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.api.WxMaUserService;
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.liangjian11.ymall.config.WxMaConfiguration;
 import com.liangjian11.ymall.handle.WxMpHandle;
 import com.liangjian11.ymall.mapper.DomainConfigMapper;
 import com.liangjian11.ymall.model.DomainConfig;
@@ -11,14 +16,12 @@ import com.liangjian11.ymall.service.UserAccountService;
 import com.liangjian11.ymall.utils.DataChangeUtil;
 import com.liangjian11.ymall.utils.ResultInfo;
 import com.liangjian11.ymall.utils.ResultUtil;
+import me.chanjar.weixin.common.error.WxErrorException;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-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.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.Cookie;
@@ -31,6 +34,7 @@ import java.net.URLEncoder;
 @RestController
 @RequestMapping("/ymall")
 public class LoginController {
+	
 	private Logger logger = LoggerFactory.getLogger(LoginController.class);
 	
 	@Autowired
@@ -85,6 +89,48 @@ public class LoginController {
 		return null;
 	}
 
-
+	@PostMapping("/code2seesion")
+	public ResultInfo miniappCode2seesion(@RequestBody JSONObject json){
+		String code = json.getString("code2seesion");
+		logger.debug("小程序登录code:" + code);
+		if(StringUtils.isBlank(code))
+			return ResultUtil.createFail("code不能为空");
+		
+		try {
+			WxMaService wxService = WxMaConfiguration.getMaService(WxMaConfiguration.MINIAPP_APPID);
+			WxMaUserService userService = wxService.getUserService();
+			WxMaJscode2SessionResult info = userService.getSessionInfo(code);
+			logger.debug(info.toString());
+			return ResultUtil.createSuccess("成功", info);
+			// unionId判断用户是否存在
+//			uaService.mobileIsNotEmpty(info.getUnionid());
+		} catch (WxErrorException e) {
+			logger.error("小程序登录失败", e);
+		}
+		return ResultUtil.createFail("服务器繁忙,请稍后再试!");
+	}
+	
+	@PostMapping("/phone")
+	public ResultInfo getPhone(@RequestBody JSONObject json){
+		logger.info(json.toJSONString());
+		String sessionKey = json.getString("sessionKey");
+		String signature = json.getString("signature");
+		String rawData = json.getString("rawData");
+		String encryptedData = json.getString("encryptedData");
+		String iv = json.getString("iv");
+		
+		WxMaService wxService = WxMaConfiguration.getMaService(WxMaConfiguration.MINIAPP_APPID);
+		WxMaUserService userService = wxService.getUserService();
+		if(!userService.checkUserInfo(sessionKey, rawData, signature))
+			return ResultUtil.createFail("检查失败,sessionKey异常");
+		WxMaPhoneNumberInfo phoneNoInfo = userService.getPhoneNoInfo(sessionKey, encryptedData, iv);
+		logger.info(phoneNoInfo.toString());
+		// TODO 将手机号存入数据库
+		
+		return ResultUtil.createSuccess("成功", phoneNoInfo);
+	}
+	
+	
+	
 
 }