package com.ics.controller.mobile; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.ics.admin.domain.Customer; import com.ics.admin.domain.Park; import com.ics.admin.domain.meeting.*; import com.ics.admin.service.ICustomerService; import com.ics.admin.service.IIcsCustomerStaffService; import com.ics.admin.service.IParkService; import com.ics.admin.service.meeting.*; 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.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** * 微信用户直接登录 * * @author jack */ @Slf4j @RestController public class WxLoginAPIController extends BaseController { @Autowired private IIcsCustomerStaffService icsCustomerStaffService; @Autowired private IAccessTokenService tokenService; @Autowired private WxMaService wxMaService; @Autowired private RedisTemplate redisTemplate; @Autowired private IParkService parkService; @Autowired private IRoomEquipmentService roomEquipmentService; @Autowired private IDetailEquipmentService detailEquipmentService; @Autowired private IUserEquipmentService userEquipmentService; @Autowired private IEquipmentService equipmentService; @Autowired private ICustomerService customerService; @Autowired private IStaffCustomerService staffCustomerService; @Autowired private IStaffOpenService staffOpenService; String smallWxAccessTokenKey = "smallWxAccessToken"; String smallWxUserPassword = "123456"; // 微信小程序登录后,新建一个token ,然后在新增LoginUser 这个注解,然后在拦截器中获取token,然后获取用户信息 @PostMapping("/weixin/login") public R login(@RequestBody Map paramMap) { // 参数 String jsCode = paramMap.get("jsCode"); String appId = paramMap.get("appId"); String appSecret = paramMap.get("appSecret"); // 必填 if (org.apache.commons.lang3.StringUtils.isBlank(jsCode)) { throw new RuntimeException("请传递jsCode"); } // 小程序登录 JSONObject sessionObject = SmallWxOkHttp.code2Session(jsCode, appId, appSecret); String openid = sessionObject.getString("openid"); // R ajax = R.ok(); // 校验用户是否存在 StaffOpen staffOpen = staffOpenService.selectStaffOpenByOpenId(openid); // 用户存在直接获取token if (staffOpen != null) { IcsCustomerStaff sysUser = icsCustomerStaffService.selectIcsCustomerStaffById(staffOpen.getStaffId()); if (null != sysUser) { Long parkId = sysUser.getParkId(); if (parkId != null) { Park park = parkService.selectParkById(parkId); sysUser.setParkName(park.getName()); } User user = new User(); PublishFactory.recordLoginInfo(sysUser.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); BeanUtils.copyBeanProp(user, sysUser); Map token = tokenService.createToken(user); ajax.put(Constants.TOKEN, token); ajax.put("user", sysUser); ajax.put("openid", openid); return ajax; } ajax.put("openid", openid); return ajax; } else { // 用户不存在返回openid进行注册登录 ajax.put("openid", openid); return ajax; } } @PostMapping("/changyang_user_login/login") public R userLogin(@RequestBody Map paramMap) { // 参数 String code = paramMap.get("code"); String openid = paramMap.get("openid"); String appId = paramMap.get("appId"); String appSecret = paramMap.get("appSecret"); String parkId = paramMap.get("parkId"); String tenantId = paramMap.get("tenantId"); // 必填 if (StringUtils.isBlank(code)) { throw new RuntimeException("请传递code"); } if (StringUtils.isBlank(openid)) { throw new RuntimeException("请传递openid"); } // 获取微信小程序 AccessToken String smallWxAccessToken = getSmallWxAccessToken(appId, appSecret); // 获取手机号 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 customerStaff1 = icsCustomerStaffService.selectUserByMobile(phoneNumber); //如果手机号等于null 就新增用户 if (null == customerStaff1) { Assert.isTrue(false, "外部人员注册,请联系管理员"); return R.error("外部人员注册,请联系管理员"); } else { List staffCustomers = staffCustomerService.selectStaffCustomerByStaffIdAndParkId(customerStaff1.getId(),parkId); if (CollUtil.isEmpty(staffCustomers)) { Assert.isTrue(false, "外部人员注册,请联系管理员"); return R.error("外部人员注册,请联系管理员"); } StaffOpen staffOpen = staffOpenService.selectStaffOpenByOpenId(openid); if (null == staffOpen) { StaffOpen staffOpen1 = new StaffOpen(); staffOpen1.setOpenid(openid); staffOpen1.setStaffId(customerStaff1.getId()); staffOpen1.setParkId(Long.valueOf(parkId)); staffOpen1.setTenantId(Long.valueOf(tenantId)); staffOpenService.insertStaffOpen(staffOpen1); } icsCustomerStaffService.updateIcsCustomerStaff(customerStaff1); User user = new User(); PublishFactory.recordLoginInfo(customerStaff1.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); BeanUtils.copyBeanProp(user, customerStaff1); Map token = tokenService.createToken(user); R ajax = R.ok(); ajax.put(Constants.TOKEN, token); ajax.put("user", user); ajax.put("openid", openid); return ajax; } } /** * 郑州授权登录 * * @param paramMap * @return */ @PostMapping("/social_user_login/login") public R social(@RequestBody Map paramMap) { try { // 参数 String code = paramMap.get("code"); String openid = paramMap.get("openid"); String appId = paramMap.get("appId"); String appSecret = paramMap.get("appSecret"); String parkId = paramMap.get("parkId"); String tenantId = paramMap.get("tenantId"); // 必填 if (StringUtils.isBlank(code)) { throw new RuntimeException("请传递code"); } if (StringUtils.isBlank(openid)) { throw new RuntimeException("请传递openid"); } // 获取微信小程序 AccessToken String smallWxAccessToken = getSmallWxAccessToken(appId, appSecret); // 获取手机号 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); //todo 需要根据手机号和对应的openId 查询对应的用户。 IcsCustomerStaff customerStaff = icsCustomerStaffService.selectUserByMobile(phoneNumber); if (customerStaff != null) { StaffOpen staffOpen = staffOpenService.selectStaffOpenByStaffIdAndOpenId(customerStaff.getId(),openid); if (staffOpen != null) { // 根据 用户绑定的企业 绑定对应的设备 ArrayList ids = new ArrayList<>(); Customer customer = customerService.selectCustomerById(customerStaff.getIcsCustomerId()); if (null != customer) { String roomId = customer.getRoomId(); List roomIds = StrUtil.split(roomId, ','); List collect = roomIds.stream().map(Long::valueOf).collect(Collectors.toList()); //获取了房间集合,循环对应集合, for (Long id : collect) { List roomEquipment = roomEquipmentService.selectByRoomId(id); if (CollUtil.isNotEmpty(roomEquipment)) { for (RoomEquipment equipment : roomEquipment) { ids.add(equipment.getEquipmentId()); } } List 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); } } }else { StaffOpen staffOpen1 = new StaffOpen(); staffOpen1.setOpenid(openid); staffOpen1.setStaffId(customerStaff.getId()); // staffOpen1.setParkId(Long.valueOf(parkId)); // staffOpen1.setTenantId(Long.valueOf(tenantId)); staffOpenService.insertStaffOpen(staffOpen1); } if (parkId != null) { Park park = parkService.selectParkById(Long.valueOf(parkId)); customerStaff.setParkName(park.getName()); } User user = new User(); PublishFactory.recordLoginInfo(customerStaff.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")); BeanUtils.copyBeanProp(user, customerStaff); Map token = tokenService.createToken(user); R ajax = R.ok(); ajax.put(Constants.TOKEN, token); ajax.put("user", user); ajax.put("openid", openid); return ajax; } else { // 新增用户信息 // todo 现根据手机号查询对应的数据。如果存在则不新增,如果不存在则新增 icsCustomerStaff.setUsername(phoneNumber); icsCustomerStaff.setGender("0"); icsCustomerStaff.setStatus("0"); int i = icsCustomerStaffService.insertIcsCustomerStaff(icsCustomerStaff); StaffOpen staffOpen1 = new StaffOpen(); staffOpen1.setOpenid(openid); staffOpen1.setStaffId(icsCustomerStaff.getId()); // staffOpen1.setParkId(Long.valueOf(parkId)); // staffOpen1.setTenantId(Long.valueOf(tenantId)); staffOpenService.insertStaffOpen(staffOpen1); Map token = tokenService.createStaffToken(icsCustomerStaff); R ajax = R.ok(); ajax.put(Constants.TOKEN, token); ajax.put("user", icsCustomerStaff); ajax.put("openid", openid); // IcsCustomerStaff customerStaff1 = icsCustomerStaffService.selectUserByMobile(phoneNumber); // //如果手机号等于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 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 token = tokenService.createToken(user); // R ajax = R.ok(); // ajax.put(Constants.TOKEN, token); // ajax.put("user", user); // ajax.put("openid", openid); return ajax; } } catch (Exception e) { log.error(e.getMessage(), e); return R.error("获取微信用户数据失败"); } } /** * 获取微信小程序AccessToken */ public String getSmallWxAccessToken(String appId, String appSecret) { // 从缓存获取微信小程序 AccessToken String smallWxAccessToken = redisTemplate.opsForValue().get(smallWxAccessTokenKey); if (StringUtils.isBlank(smallWxAccessToken)) { smallWxAccessToken = SmallWxOkHttp.getAccessToken(appId, appSecret); redisTemplate.opsForValue().set(appId + smallWxAccessTokenKey, appId + smallWxAccessToken, 7200, TimeUnit.SECONDS); } return smallWxAccessToken; } @RequiresPermissions("member:center:view") @GetMapping("/wx/getUserInfo") public R login(Long userId, Long parkId) { try { //检查是否是否存在 IcsCustomerStaff customerStaff = icsCustomerStaffService.selectIcsCustomerStaffById(userId); if (null != customerStaff) { StaffCustomer staffCustomer = new StaffCustomer(); staffCustomer.setStaffId(userId); List staffCustomers = staffCustomerService.selectStaffCustomerByStaffId(userId); if (CollUtil.isNotEmpty(staffCustomers)) { for (StaffCustomer staffCustomer1 : staffCustomers) { Customer customer = customerService.selectCustomerByIdAndParkId(staffCustomer1.getIcsCustomerId(), parkId); if (null != customer) { customerStaff.setCustomerName(customer.getName()); customerStaff.setIcsCustomerId(customer.getId()); break; } } } return R.ok().put("data", customerStaff); } else { return R.error("获取微信用户数据失败"); } } catch (Exception e) { return R.error("获取微信用户数据失败"); } } }