dbd-meeting/ics-web/src/main/java/com/ics/controller/mobile/WxLoginAPIController.java

139 lines
5.3 KiB
Java
Raw Normal View History

2024-01-23 16:42:27 +08:00
package com.ics.controller.mobile;
import cn.binarywang.wx.miniapp.api.WxMaService;
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.util.WxMaConfigHolder;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ics.admin.domain.IcsCustomerStaff;
import com.ics.admin.service.IIcsCustomerStaffService;
2024-01-23 16:42:27 +08:00
import com.ics.common.constant.Constants;
import com.ics.common.core.domain.R;
import com.ics.common.utils.DateUtils;
import com.ics.common.utils.MessageUtils;
import com.ics.common.utils.RandomUtil;
import com.ics.common.utils.StringUtils;
import com.ics.common.utils.bean.BeanUtils;
2024-01-23 16:42:27 +08:00
import com.ics.service.IWxAppLoginService;
import com.ics.system.domain.User;
import com.ics.system.domain.form.LoginRequest;
import com.ics.system.log.publish.PublishFactory;
import com.ics.system.service.IAccessTokenService;
import com.ics.system.service.IUserService;
import com.ics.system.util.PasswordUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
2024-01-23 16:42:27 +08:00
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
2024-01-23 16:42:27 +08:00
import java.util.Objects;
import java.util.concurrent.TimeUnit;
2024-01-23 16:42:27 +08:00
/**
* 微信用户直接登录
*
* @author jack
*/
@Slf4j
@RestController
public class WxLoginAPIController {
@Autowired
private IIcsCustomerStaffService icsCustomerStaffService;
2024-01-23 16:42:27 +08:00
@Autowired
private IAccessTokenService tokenService;
@Autowired
private WxMaService wxMaService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
String smallWxAccessTokenKey = "smallWxAccessToken";
String smallWxUserPassword = "123456";
2024-01-23 16:42:27 +08:00
@PostMapping("/social_user_login/login")
public R social(@RequestBody Map<String, String> paramMap) {
2024-01-23 16:42:27 +08:00
try {
// 参数
String code = paramMap.get("code");
String openid = paramMap.get("openid");
// 必填
if (StringUtils.isBlank(code)) {
throw new RuntimeException("请传递code");
2024-01-23 16:42:27 +08:00
}
if (StringUtils.isBlank(openid)) {
throw new RuntimeException("请传递openid");
}
// 获取微信小程序 AccessToken
String smallWxAccessToken = getSmallWxAccessToken();
// 获取手机号
JSONObject jsonObj = SmallWxOkHttp.getPhoneNumber(code, openid, smallWxAccessToken);
JSONObject phoneInfo = jsonObj.getJSONObject("phone_info");
// 手机号
String phoneNumber = phoneInfo.getString("phoneNumber");
IcsCustomerStaff icsCustomerStaff = new IcsCustomerStaff();
icsCustomerStaff.setMobile(phoneNumber);
List<IcsCustomerStaff> list = icsCustomerStaffService.selectIcsCustomerStaffList(icsCustomerStaff);
if(list.size()>0){
User user = new User();
PublishFactory.recordLoginInfo(list.get(0).getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
BeanUtils.copyBeanProp(user,list.get(0));
return R.ok(tokenService.createToken(user));
}else {
return R.error("登录失败");
}
} catch (Exception e) {
2024-01-23 16:42:27 +08:00
log.error(e.getMessage(), e);
return R.error("获取微信用户数据失败");
}
}
/**
* 获取微信小程序AccessToken
*/
public String getSmallWxAccessToken() {
// 从缓存获取微信小程序 AccessToken
String smallWxAccessToken = redisTemplate.opsForValue().get(smallWxAccessTokenKey);
if (StringUtils.isBlank(smallWxAccessToken)) {
smallWxAccessToken = SmallWxOkHttp.getAccessToken();
redisTemplate.opsForValue().set(smallWxAccessTokenKey, smallWxAccessToken, 7200, TimeUnit.SECONDS);
}
return smallWxAccessToken;
}
2024-01-23 16:42:27 +08:00
@PostMapping("/wx/login")
public R login(@RequestBody String mobile) {
2024-01-23 16:42:27 +08:00
try {
//检查是否是否存在
IcsCustomerStaff icsCustomerStaff = new IcsCustomerStaff();
icsCustomerStaff.setMobile(mobile);
List<IcsCustomerStaff> list = icsCustomerStaffService.selectIcsCustomerStaffList(icsCustomerStaff);
if(list.size()>0){
User user = new User();
PublishFactory.recordLoginInfo(list.get(0).getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
BeanUtils.copyBeanProp(user,list.get(0));
return R.ok(tokenService.createToken(user));
}else {
return R.error("登录失败");
2024-01-23 16:42:27 +08:00
}
} catch (Exception e) {
log.error("调用微信服务器出现异常", e);
return R.error("获取微信用户数据失败");
}
}
}