mirror of
https://gitee.com/elegant_wings/dbd-meeting.git
synced 2025-06-21 12:29:36 +08:00
192 lines
7.2 KiB
Java
192 lines
7.2 KiB
Java
package com.ics.controller.mobile;
|
||
|
||
|
||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||
import com.alibaba.fastjson.JSONObject;
|
||
import com.ics.admin.service.IIcsCustomerStaffService;
|
||
import com.ics.common.constant.Constants;
|
||
import com.ics.common.core.controller.BaseController;
|
||
import com.ics.common.core.domain.IcsCustomerStaff;
|
||
import com.ics.common.core.domain.R;
|
||
import com.ics.common.utils.MessageUtils;
|
||
import com.ics.common.utils.StringUtils;
|
||
import com.ics.common.utils.bean.BeanUtils;
|
||
import com.ics.system.domain.User;
|
||
import com.ics.system.log.publish.PublishFactory;
|
||
import com.ics.system.service.IAccessTokenService;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.data.redis.core.RedisTemplate;
|
||
import org.springframework.web.bind.annotation.GetMapping;
|
||
import org.springframework.web.bind.annotation.PostMapping;
|
||
import org.springframework.web.bind.annotation.RequestBody;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
import org.wf.jwtp.annotation.RequiresPermissions;
|
||
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.concurrent.TimeUnit;
|
||
|
||
|
||
/**
|
||
* 微信用户直接登录
|
||
*
|
||
* @author jack
|
||
*/
|
||
@Slf4j
|
||
@RestController
|
||
public class WxLoginAPIController extends BaseController {
|
||
|
||
|
||
|
||
@Autowired
|
||
private IIcsCustomerStaffService icsCustomerStaffService;
|
||
|
||
@Autowired
|
||
private IAccessTokenService tokenService;
|
||
|
||
@Autowired
|
||
private WxMaService wxMaService;
|
||
|
||
@Autowired
|
||
private RedisTemplate<String, String> redisTemplate;
|
||
|
||
|
||
String smallWxAccessTokenKey = "smallWxAccessToken";
|
||
String smallWxUserPassword = "123456";
|
||
|
||
|
||
|
||
// 微信小程序登录后,新建一个token ,然后在新增LoginUser 这个注解,然后在拦截器中获取token,然后获取用户信息
|
||
|
||
@PostMapping("/weixin/login")
|
||
public R login(@RequestBody Map<String, String> paramMap) {
|
||
// 参数
|
||
String jsCode = paramMap.get("jsCode");
|
||
// 必填
|
||
if (org.apache.commons.lang3.StringUtils.isBlank(jsCode)) {
|
||
throw new RuntimeException("请传递jsCode");
|
||
}
|
||
// 小程序登录
|
||
JSONObject sessionObject = SmallWxOkHttp.code2Session(jsCode);
|
||
String openid = sessionObject.getString("openid");
|
||
String unionid = sessionObject.getString("unionid");
|
||
//
|
||
R ajax = R.ok();
|
||
// 校验用户是否存在
|
||
IcsCustomerStaff sysUser = icsCustomerStaffService.selectUserByOpenid(openid);
|
||
// 用户存在直接获取token
|
||
if (sysUser != null) {
|
||
String phonenumber = sysUser.getMobile();
|
||
User user = new User();
|
||
PublishFactory.recordLoginInfo(sysUser.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
||
BeanUtils.copyBeanProp(user, sysUser);
|
||
Map<String, Object> token = tokenService.createToken(user);
|
||
ajax.put(Constants.TOKEN, token);
|
||
// ajax.put(Constants.TOKEN, token);
|
||
ajax.put("user", sysUser);
|
||
ajax.put("openid", openid);
|
||
return ajax;
|
||
} else { // 用户不存在返回openid进行注册登录
|
||
ajax.put("openid", openid);
|
||
return ajax;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
@PostMapping("/social_user_login/login")
|
||
public R social(@RequestBody Map<String, String> paramMap) {
|
||
try {
|
||
// 参数
|
||
String code = paramMap.get("code");
|
||
String openid = paramMap.get("openid");
|
||
// 必填
|
||
if (StringUtils.isBlank(code)) {
|
||
throw new RuntimeException("请传递code");
|
||
}
|
||
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);
|
||
IcsCustomerStaff customerStaff = icsCustomerStaffService.selectByPhone(phoneNumber);
|
||
if(customerStaff !=null){
|
||
|
||
if (StringUtils.isBlank(customerStaff.getOpenid())){
|
||
customerStaff.setOpenid(openid);
|
||
icsCustomerStaffService.updateIcsCustomerStaff(customerStaff);
|
||
}
|
||
|
||
|
||
User user = new User();
|
||
PublishFactory.recordLoginInfo(customerStaff.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
||
BeanUtils.copyBeanProp(user,customerStaff);
|
||
Map<String, Object> token = tokenService.createToken(user);
|
||
R ajax = R.ok();
|
||
ajax.put(Constants.TOKEN, token);
|
||
ajax.put("user", user);
|
||
ajax.put("openid", openid);
|
||
return ajax;
|
||
}else {
|
||
// 新增用户信息
|
||
icsCustomerStaff.setUsername(phoneNumber);
|
||
icsCustomerStaff.setDataType(Constants.CUSTOMER_VISIT);
|
||
icsCustomerStaff.setOpenid(openid);
|
||
icsCustomerStaff.setGender("0");
|
||
icsCustomerStaff.setStatus("0");
|
||
int i = icsCustomerStaffService.insertIcsCustomerStaff(icsCustomerStaff);
|
||
Map<String, Object> token = tokenService.createStaffToken(icsCustomerStaff);
|
||
R ajax = R.ok();
|
||
ajax.put(Constants.TOKEN, token);
|
||
ajax.put("user", icsCustomerStaff);
|
||
ajax.put("openid", openid);
|
||
return ajax;
|
||
}
|
||
} catch (Exception e) {
|
||
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;
|
||
}
|
||
|
||
@RequiresPermissions("member:center:view")
|
||
@GetMapping ("/wx/getUserInfo")
|
||
public R login( Long userId) {
|
||
try {
|
||
//检查是否是否存在
|
||
IcsCustomerStaff icsCustomerStaff = new IcsCustomerStaff();
|
||
icsCustomerStaff.setId(userId);
|
||
IcsCustomerStaff customerStaff = icsCustomerStaffService.selectIcsCustomerStaffById(userId);
|
||
if(null !=customerStaff){
|
||
return R.ok().put("data",customerStaff);
|
||
}else {
|
||
return R.error("获取微信用户数据失败");
|
||
}
|
||
} catch (Exception e) {
|
||
return R.error("获取微信用户数据失败");
|
||
}
|
||
}
|
||
|
||
|
||
}
|