记得上下班打卡 | git大法好,push需谨慎

Commit 3d86285a authored by jiangxiulong's avatar jiangxiulong

登陆

parent bd04ba3c
package com.liquidnet.service.sweet.controller;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.sweet.service.impl.SweetLoginServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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;
@Api(tags = "小程序登陆")
@RestController
@RequestMapping("/sweet-login")
public class SweetLoginController {
@Autowired
private SweetLoginServiceImpl sweetLoginService;
@GetMapping("userInfo")
@ApiOperation("code获取用户信息")
@ApiImplicitParams({
@ApiImplicitParam(type = "query", dataType = "String", name = "code", value = "微信code", required = true),
@ApiImplicitParam(type = "query", dataType = "String", name = "encryptedData", value = "encryptedData", required = true),
@ApiImplicitParam(type = "query", dataType = "String", name = "iv", value = "iv", required = true),
@ApiImplicitParam(type = "query", dataType = "Integer", name = "type", value = "1草莓 2五百里 3mdsk"),
})
public ResponseDto userInfo(
@RequestParam() String code,
@RequestParam() String encryptedData,
@RequestParam() String iv,
@RequestParam(defaultValue = "1") Integer type
) {
sweetLoginService.userInfo(code, encryptedData, iv, type);
return ResponseDto.success();
}
}
package com.liquidnet.service.sweet.service.impl;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.liquidnet.service.base.ResponseDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Objects;
/**
* <p>
* 登陆 服务实现类
* </p>
*
* @author liquidnet
* @since 2021-07-23
*/
@Service
public class SweetLoginServiceImpl {
@Value("${liquidnet.wechat.applet.strawberry.appid}")
private String strawberryAppid;
@Value("${liquidnet.wechat.applet.strawberry.secret}")
private String strawberrySecret;
@Value("${liquidnet.wechat.applet.five.appid}")
private String fiveAppid;
@Value("${liquidnet.wechat.applet.five.secret}")
private String fiveSecret;
@Value("${liquidnet.wechat.applet.mdsk.appid}")
private String mdskAppid;
@Value("${liquidnet.wechat.applet.mdsk.secret}")
private String mdskSecret;
public ResponseDto userInfo(String code, String encryptedData, String iv, Integer type) {
try {
String appId = "";
String appSecret = "";
switch (type) {
case 1:
appId = strawberryAppid;
appSecret = strawberrySecret;
break;
case 2:
appId = fiveAppid;
appSecret = fiveSecret;
break;
case 3:
appId = mdskAppid;
appSecret = mdskSecret;
break;
}
WxMaConfig wxMaConfig = wxMaConfig(appId, appSecret);
WxMaService wxMaService = wxMaService(wxMaConfig);
WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
if (null == sessionInfo) {
return ResponseDto.failure("login handler error");
}
// 解密用户信息
WxMaUserInfo wxUserInfo = wxMaService.getUserService().getUserInfo(sessionInfo.getSessionKey(),
encryptedData, iv);
if (null == wxUserInfo) {
return ResponseDto.failure("wxUser not exist");
}
// 解密手机号码信息
WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(sessionInfo.getSessionKey(),
encryptedData, iv);
if (Objects.isNull(wxMaPhoneNumberInfo) || StringUtils.isBlank(wxMaPhoneNumberInfo.getPhoneNumber())) {
return ResponseDto.failure("解密手机号码信息错误");
}
String unionId = sessionInfo.getUnionid();
String openId = sessionInfo.getOpenid();
return ResponseDto.success();
} catch (Exception e) {
return ResponseDto.failure();
}
}
private WxMaConfig wxMaConfig(String appId, String appSecret) {
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(appId);
config.setSecret(appSecret);
config.setMsgDataFormat("JSON");
return config;
}
private WxMaService wxMaService(WxMaConfig maConfig) {
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(maConfig);
return service;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment