2024-01-23 16:42:27 +08:00
|
|
|
|
package com.ics.controller.mobile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
2024-04-08 11:15:03 +08:00
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
2024-01-23 16:42:27 +08:00
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
2024-04-08 11:15:03 +08:00
|
|
|
|
import com.ics.admin.domain.Customer;
|
2024-04-03 14:28:17 +08:00
|
|
|
|
import com.ics.admin.domain.Park;
|
2024-04-08 11:15:03 +08:00
|
|
|
|
import com.ics.admin.domain.meeting.DetailEquipment;
|
|
|
|
|
import com.ics.admin.domain.meeting.RoomEquipment;
|
2024-04-09 10:02:49 +08:00
|
|
|
|
import com.ics.admin.domain.meeting.StaffCustomer;
|
2024-04-08 11:15:03 +08:00
|
|
|
|
import com.ics.admin.domain.meeting.UserEquipment;
|
|
|
|
|
import com.ics.admin.service.ICustomerService;
|
2024-02-25 16:43:09 +08:00
|
|
|
|
import com.ics.admin.service.IIcsCustomerStaffService;
|
2024-04-03 14:28:17 +08:00
|
|
|
|
import com.ics.admin.service.IParkService;
|
2024-04-09 10:02:49 +08:00
|
|
|
|
import com.ics.admin.service.meeting.*;
|
2024-01-23 16:42:27 +08:00
|
|
|
|
import com.ics.common.constant.Constants;
|
2024-02-26 15:53:08 +08:00
|
|
|
|
import com.ics.common.core.controller.BaseController;
|
2024-03-13 15:15:35 +08:00
|
|
|
|
import com.ics.common.core.domain.IcsCustomerStaff;
|
2024-01-23 16:42:27 +08:00
|
|
|
|
import com.ics.common.core.domain.R;
|
|
|
|
|
import com.ics.common.utils.MessageUtils;
|
|
|
|
|
import com.ics.common.utils.StringUtils;
|
2024-02-25 16:43:09 +08:00
|
|
|
|
import com.ics.common.utils.bean.BeanUtils;
|
2024-01-23 16:42:27 +08:00
|
|
|
|
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;
|
2024-02-25 16:43:09 +08:00
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
2024-03-29 08:55:47 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
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.RestController;
|
2024-03-29 08:55:47 +08:00
|
|
|
|
import org.wf.jwtp.annotation.RequiresPermissions;
|
2024-01-23 16:42:27 +08:00
|
|
|
|
|
2024-04-08 11:15:03 +08:00
|
|
|
|
import java.util.ArrayList;
|
2024-02-25 16:43:09 +08:00
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2024-04-08 11:15:03 +08:00
|
|
|
|
import java.util.stream.Collectors;
|
2024-01-23 16:42:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 微信用户直接登录
|
|
|
|
|
*
|
|
|
|
|
* @author jack
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
2024-02-26 15:53:08 +08:00
|
|
|
|
public class WxLoginAPIController extends BaseController {
|
|
|
|
|
|
|
|
|
|
|
2024-01-23 16:42:27 +08:00
|
|
|
|
@Autowired
|
2024-02-25 16:43:09 +08:00
|
|
|
|
private IIcsCustomerStaffService icsCustomerStaffService;
|
2024-01-23 16:42:27 +08:00
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IAccessTokenService tokenService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private WxMaService wxMaService;
|
|
|
|
|
|
2024-02-25 16:43:09 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
2024-04-03 14:28:17 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
private IParkService parkService;
|
|
|
|
|
|
2024-04-08 11:15:03 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
private IRoomEquipmentService roomEquipmentService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IDetailEquipmentService detailEquipmentService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IUserEquipmentService userEquipmentService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IEquipmentService equipmentService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ICustomerService customerService;
|
|
|
|
|
|
2024-04-09 10:02:49 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
private IStaffCustomerService staffCustomerService;
|
|
|
|
|
|
2024-02-25 16:43:09 +08:00
|
|
|
|
|
|
|
|
|
String smallWxAccessTokenKey = "smallWxAccessToken";
|
|
|
|
|
String smallWxUserPassword = "123456";
|
|
|
|
|
|
2024-02-26 15:53:08 +08:00
|
|
|
|
|
2024-03-19 11:18:09 +08:00
|
|
|
|
// 微信小程序登录后,新建一个token ,然后在新增LoginUser 这个注解,然后在拦截器中获取token,然后获取用户信息
|
2024-03-13 15:15:35 +08:00
|
|
|
|
|
2024-02-26 15:53:08 +08:00
|
|
|
|
@PostMapping("/weixin/login")
|
|
|
|
|
public R login(@RequestBody Map<String, String> paramMap) {
|
|
|
|
|
// 参数
|
|
|
|
|
String jsCode = paramMap.get("jsCode");
|
2024-04-25 16:50:37 +08:00
|
|
|
|
String appId = paramMap.get("appId");
|
|
|
|
|
String appSecret = paramMap.get("appSecret");
|
2024-02-26 15:53:08 +08:00
|
|
|
|
// 必填
|
|
|
|
|
if (org.apache.commons.lang3.StringUtils.isBlank(jsCode)) {
|
|
|
|
|
throw new RuntimeException("请传递jsCode");
|
|
|
|
|
}
|
|
|
|
|
// 小程序登录
|
2024-04-25 16:50:37 +08:00
|
|
|
|
JSONObject sessionObject = SmallWxOkHttp.code2Session(jsCode,appId,appSecret);
|
2024-02-26 15:53:08 +08:00
|
|
|
|
String openid = sessionObject.getString("openid");
|
|
|
|
|
String unionid = sessionObject.getString("unionid");
|
|
|
|
|
//
|
|
|
|
|
R ajax = R.ok();
|
|
|
|
|
// 校验用户是否存在
|
|
|
|
|
IcsCustomerStaff sysUser = icsCustomerStaffService.selectUserByOpenid(openid);
|
|
|
|
|
// 用户存在直接获取token
|
|
|
|
|
if (sysUser != null) {
|
2024-04-03 14:28:17 +08:00
|
|
|
|
Long parkId = sysUser.getParkId();
|
2024-04-08 11:15:03 +08:00
|
|
|
|
if (parkId != null) {
|
2024-04-03 14:28:17 +08:00
|
|
|
|
Park park = parkService.selectParkById(parkId);
|
|
|
|
|
sysUser.setParkName(park.getName());
|
|
|
|
|
}
|
2024-02-26 15:53:08 +08:00
|
|
|
|
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);
|
2024-04-25 16:50:37 +08:00
|
|
|
|
|
2024-02-26 15:53:08 +08:00
|
|
|
|
return ajax;
|
|
|
|
|
} else { // 用户不存在返回openid进行注册登录
|
|
|
|
|
ajax.put("openid", openid);
|
|
|
|
|
return ajax;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-01-23 16:42:27 +08:00
|
|
|
|
@PostMapping("/social_user_login/login")
|
2024-02-25 16:43:09 +08:00
|
|
|
|
public R social(@RequestBody Map<String, String> paramMap) {
|
2024-01-23 16:42:27 +08:00
|
|
|
|
try {
|
2024-02-25 16:43:09 +08:00
|
|
|
|
// 参数
|
|
|
|
|
String code = paramMap.get("code");
|
|
|
|
|
String openid = paramMap.get("openid");
|
2024-04-25 16:50:37 +08:00
|
|
|
|
String appId = paramMap.get("appId");
|
|
|
|
|
String appSecret = paramMap.get("appSecret");
|
2024-02-25 16:43:09 +08:00
|
|
|
|
// 必填
|
|
|
|
|
if (StringUtils.isBlank(code)) {
|
|
|
|
|
throw new RuntimeException("请传递code");
|
2024-01-23 16:42:27 +08:00
|
|
|
|
}
|
2024-02-25 16:43:09 +08:00
|
|
|
|
if (StringUtils.isBlank(openid)) {
|
|
|
|
|
throw new RuntimeException("请传递openid");
|
|
|
|
|
}
|
|
|
|
|
// 获取微信小程序 AccessToken
|
2024-04-25 16:50:37 +08:00
|
|
|
|
String smallWxAccessToken = getSmallWxAccessToken(appId,appSecret);
|
2024-02-25 16:43:09 +08:00
|
|
|
|
// 获取手机号
|
|
|
|
|
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);
|
2024-04-25 16:50:37 +08:00
|
|
|
|
//todo 需要根据手机号和对应的openId 查询对应的用户。
|
|
|
|
|
IcsCustomerStaff customerStaff = icsCustomerStaffService.selectByPhoneAndOpenId(phoneNumber,openid);
|
2024-04-08 11:15:03 +08:00
|
|
|
|
if (customerStaff != null) {
|
|
|
|
|
if (StringUtils.isBlank(customerStaff.getOpenid())) {
|
|
|
|
|
// 根据 用户绑定的企业 绑定对应的设备
|
|
|
|
|
ArrayList<Long> ids = new ArrayList<>();
|
|
|
|
|
Customer customer = customerService.selectCustomerById(customerStaff.getIcsCustomerId());
|
|
|
|
|
if (null != customer) {
|
|
|
|
|
String roomId = customer.getRoomId();
|
|
|
|
|
List<String> roomIds = StrUtil.split(roomId, ',');
|
|
|
|
|
List<Long> collect = roomIds.stream().map(Long::valueOf).collect(Collectors.toList());
|
|
|
|
|
//获取了房间集合,循环对应集合,
|
|
|
|
|
for (Long id : collect) {
|
2024-04-25 16:50:37 +08:00
|
|
|
|
List<RoomEquipment> roomEquipment = roomEquipmentService.selectByRoomId(id);
|
|
|
|
|
if (CollUtil.isNotEmpty(roomEquipment)) {
|
|
|
|
|
for (RoomEquipment equipment : roomEquipment) {
|
|
|
|
|
ids.add(equipment.getEquipmentId());
|
|
|
|
|
}
|
2024-04-08 11:15:03 +08:00
|
|
|
|
}
|
|
|
|
|
List<DetailEquipment> detailEquipments = detailEquipmentService.selectByRoomId(id);
|
|
|
|
|
if (CollUtil.isNotEmpty(detailEquipments)) {
|
|
|
|
|
for (DetailEquipment detailEquipment : detailEquipments) {
|
|
|
|
|
ids.add(detailEquipment.getEquipmentId());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (CollUtil.isNotEmpty(ids)) {
|
|
|
|
|
for (Long id : ids) {
|
|
|
|
|
UserEquipment userEquipment = new UserEquipment();
|
|
|
|
|
userEquipment.setEquipmentId(id);
|
|
|
|
|
userEquipment.setUserId(customerStaff.getId());
|
|
|
|
|
userEquipment.setStartTime(customer.getStartDate());
|
|
|
|
|
userEquipment.setEndDate(customer.getEndDate());
|
|
|
|
|
userEquipmentService.insertUserEquipment(userEquipment);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-29 08:55:47 +08:00
|
|
|
|
customerStaff.setOpenid(openid);
|
|
|
|
|
icsCustomerStaffService.updateIcsCustomerStaff(customerStaff);
|
|
|
|
|
}
|
2024-04-03 14:28:17 +08:00
|
|
|
|
Long parkId = customerStaff.getParkId();
|
2024-04-08 11:15:03 +08:00
|
|
|
|
if (parkId != null) {
|
2024-04-03 14:28:17 +08:00
|
|
|
|
Park park = parkService.selectParkById(parkId);
|
|
|
|
|
customerStaff.setParkName(park.getName());
|
|
|
|
|
}
|
2024-03-29 08:55:47 +08:00
|
|
|
|
|
2024-02-25 16:43:09 +08:00
|
|
|
|
User user = new User();
|
2024-03-29 08:55:47 +08:00
|
|
|
|
PublishFactory.recordLoginInfo(customerStaff.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
2024-04-08 11:15:03 +08:00
|
|
|
|
BeanUtils.copyBeanProp(user, customerStaff);
|
2024-02-26 15:53:08 +08:00
|
|
|
|
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;
|
2024-04-08 11:15:03 +08:00
|
|
|
|
} else {
|
2024-02-26 15:53:08 +08:00
|
|
|
|
// 新增用户信息
|
2024-04-25 16:50:37 +08:00
|
|
|
|
// todo 现根据手机号查询对应的数据。如果存在则不新增,如果不存在则新增
|
|
|
|
|
|
2024-05-20 17:01:37 +08:00
|
|
|
|
IcsCustomerStaff customerStaff1 = icsCustomerStaffService.selectUserByMobile(phoneNumber);
|
2024-04-25 16:50:37 +08:00
|
|
|
|
//如果手机号等于null 就新增用户
|
|
|
|
|
if (null == customerStaff1) {
|
|
|
|
|
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;
|
|
|
|
|
}else {
|
|
|
|
|
String openid1 = customerStaff1.getOpenid();
|
|
|
|
|
customerStaff1.setOpenid(openid1 + ","+openid);
|
|
|
|
|
icsCustomerStaffService.updateIcsCustomerStaff(customerStaff1);
|
|
|
|
|
User user = new User();
|
|
|
|
|
PublishFactory.recordLoginInfo(customerStaff1.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
|
|
|
|
BeanUtils.copyBeanProp(user, customerStaff1);
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-02-25 16:43:09 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
2024-01-23 16:42:27 +08:00
|
|
|
|
log.error(e.getMessage(), e);
|
|
|
|
|
return R.error("获取微信用户数据失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-04-08 11:15:03 +08:00
|
|
|
|
|
2024-02-25 16:43:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取微信小程序AccessToken
|
|
|
|
|
*/
|
2024-04-25 16:50:37 +08:00
|
|
|
|
public String getSmallWxAccessToken(String appId, String appSecret) {
|
2024-02-25 16:43:09 +08:00
|
|
|
|
// 从缓存获取微信小程序 AccessToken
|
|
|
|
|
String smallWxAccessToken = redisTemplate.opsForValue().get(smallWxAccessTokenKey);
|
|
|
|
|
if (StringUtils.isBlank(smallWxAccessToken)) {
|
2024-04-25 16:50:37 +08:00
|
|
|
|
smallWxAccessToken = SmallWxOkHttp.getAccessToken(appId, appSecret);
|
2024-02-25 16:43:09 +08:00
|
|
|
|
redisTemplate.opsForValue().set(smallWxAccessTokenKey, smallWxAccessToken, 7200, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
return smallWxAccessToken;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 08:55:47 +08:00
|
|
|
|
@RequiresPermissions("member:center:view")
|
2024-04-08 11:15:03 +08:00
|
|
|
|
@GetMapping("/wx/getUserInfo")
|
2024-04-25 16:50:37 +08:00
|
|
|
|
public R login(Long userId, Long parkId) {
|
2024-01-23 16:42:27 +08:00
|
|
|
|
try {
|
2024-02-25 16:43:09 +08:00
|
|
|
|
//检查是否是否存在
|
2024-04-09 10:02:49 +08:00
|
|
|
|
|
2024-03-30 15:17:31 +08:00
|
|
|
|
IcsCustomerStaff customerStaff = icsCustomerStaffService.selectIcsCustomerStaffById(userId);
|
2024-04-08 11:15:03 +08:00
|
|
|
|
if (null != customerStaff) {
|
2024-04-09 10:02:49 +08:00
|
|
|
|
StaffCustomer staffCustomer = new StaffCustomer();
|
|
|
|
|
staffCustomer.setStaffId(userId);
|
|
|
|
|
List<StaffCustomer> staffCustomers = staffCustomerService.selectStaffCustomerByStaffId(userId);
|
2024-04-25 16:50:37 +08:00
|
|
|
|
if (CollUtil.isNotEmpty(staffCustomers)) {
|
2024-04-09 10:02:49 +08:00
|
|
|
|
for (StaffCustomer staffCustomer1 : staffCustomers) {
|
|
|
|
|
Customer customer = customerService.selectCustomerByIdAndParkId(staffCustomer1.getIcsCustomerId(), parkId);
|
2024-04-25 16:50:37 +08:00
|
|
|
|
if (null != customer) {
|
2024-04-09 10:02:49 +08:00
|
|
|
|
customerStaff.setCustomerName(customer.getName());
|
|
|
|
|
customerStaff.setIcsCustomerId(customer.getId());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-08 11:15:03 +08:00
|
|
|
|
return R.ok().put("data", customerStaff);
|
|
|
|
|
} else {
|
2024-02-26 15:53:08 +08:00
|
|
|
|
return R.error("获取微信用户数据失败");
|
2024-01-23 16:42:27 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return R.error("获取微信用户数据失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-26 15:53:08 +08:00
|
|
|
|
|
|
|
|
|
|
2024-01-23 16:42:27 +08:00
|
|
|
|
}
|