feat: 地图实时显示设备位置,功能代码提交。

This commit is contained in:
tianyongbao
2025-10-19 12:34:21 +08:00
parent 2bb1281a99
commit 3c8f10a7a4
15 changed files with 220 additions and 399 deletions

View File

@@ -1,58 +1,78 @@
package com.intc.weixin.controller;
import com.intc.weixin.utils.CheckUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import com.intc.common.core.web.domain.AjaxResult;
import com.intc.weixin.entity.WxUser;
import com.intc.weixin.service.WxUserService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import com.intc.weixin.entity.WxOpenDataDTO;
import com.intc.weixin.utils.ThirdSessionHolder;
/**
* 业务模块
*
* @author tianyongbao
* @date 2025/1/8
*/
/**
* 服务器验证
* 微信小程序接口
*
* @author tianyongbao
* @date 2025/1/8
*/
@RestController
@AllArgsConstructor
@RequestMapping("/api")
@Api(tags = "对接微信模块")
@RequestMapping("/weixin/api")
@Api(tags = "微信小程序接口")
public class WeChatController {
/**
* 功能描述: 签名校验
*/
@GetMapping("/wx")
@ApiOperation(value = "签名校验", notes = "签名校验")
public void login(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
request.setCharacterEncoding("UTF-8");
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
PrintWriter out = null;
private final WxUserService wxUserService;
@PostMapping("/ma/wxuser/login")
@ApiOperation(value = "微信小程序登录", notes = "通过 appId + jsCode 进行登录,返回 thirdSession")
public AjaxResult loginMini(@RequestParam("appId") String appId,
@RequestParam("jsCode") String jsCode) {
if (StringUtils.isBlank(appId) || StringUtils.isBlank(jsCode)) {
return AjaxResult.error("appId 或 jsCode 不能为空");
}
try {
out = response.getWriter();
if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
out.write(echostr);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
WxUser wxUser = wxUserService.loginMa(appId, jsCode);
return AjaxResult.success(wxUser);
} catch (WxErrorException e) {
return AjaxResult.error(e.getMessage());
} catch (Exception e) {
return AjaxResult.error("登录失败,请稍后重试");
}
}
@PostMapping("/ma/wxuser/decrypt-save")
@ApiOperation(value = "小程序用户信息解密并保存", notes = "通过 encryptedData + iv 解密用户信息,并保存到数据库")
public AjaxResult decryptAndSaveMaUser(@RequestParam("appId") String appId,
@RequestParam("encryptedData") String encryptedData,
@RequestParam("iv") String iv,
@RequestParam(value = "rawData", required = false) String rawData,
@RequestParam(value = "signature", required = false) String signature) {
if (StringUtils.isBlank(appId) || StringUtils.isBlank(encryptedData) || StringUtils.isBlank(iv)) {
return AjaxResult.error("appId、encryptedData 或 iv 不能为空");
}
try {
// 从 ThirdSession 中获取用户ID与会话密钥
String userId = ThirdSessionHolder.getWxUserId();
String sessionKey = ThirdSessionHolder.getThirdSession().getSessionKey();
WxOpenDataDTO dto = new WxOpenDataDTO();
dto.setAppId(appId);
dto.setUserId(userId);
dto.setEncryptedData(encryptedData);
dto.setIv(iv);
dto.setRawData(rawData);
dto.setSignature(signature);
dto.setSessionKey(sessionKey);
WxUser wxUser = wxUserService.saveOrUptateWxUser(dto);
return AjaxResult.success(wxUser);
} catch (Exception e) {
return AjaxResult.error("解密失败或保存异常,请稍后重试");
}
}
}