feat: 新功能开发,监测历史记录。微信和物联网平台,模块搭建。
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package com.intc.weixin.controller;
|
||||
|
||||
import com.intc.common.core.domain.R;
|
||||
import com.intc.common.web.core.BaseController;
|
||||
import com.intc.weixin.service.WxMaService;
|
||||
import com.intc.weixin.service.WxMpService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 微信对接控制器
|
||||
*
|
||||
* @author intc
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/weixin")
|
||||
@Tag(name = "微信对接管理", description = "微信对接相关接口")
|
||||
public class WeixinController extends BaseController {
|
||||
|
||||
@Autowired(required = false)
|
||||
private WxMpService wxMpService;
|
||||
|
||||
@Autowired(required = false)
|
||||
private WxMaService wxMaService;
|
||||
|
||||
@Operation(summary = "测试接口")
|
||||
@GetMapping("/test")
|
||||
public R<String> test() {
|
||||
return R.ok("微信模块测试成功!");
|
||||
}
|
||||
|
||||
@Operation(summary = "获取公众号用户信息")
|
||||
@GetMapping("/mp/user/{openId}")
|
||||
public R<WxMpUser> getMpUserInfo(
|
||||
@Parameter(description = "用户openId") @PathVariable String openId) {
|
||||
try {
|
||||
if (wxMpService == null) {
|
||||
return R.fail("公众号配置未启用");
|
||||
}
|
||||
WxMpUser userInfo = wxMpService.getUserInfo(openId);
|
||||
return R.ok(userInfo);
|
||||
} catch (WxErrorException e) {
|
||||
log.error("获取用户信息失败", e);
|
||||
return R.fail("获取用户信息失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "生成公众号二维码")
|
||||
@GetMapping("/mp/qrcode")
|
||||
public R<String> createMpQrCode(
|
||||
@Parameter(description = "场景值") @RequestParam String scene) {
|
||||
try {
|
||||
if (wxMpService == null) {
|
||||
return R.fail("公众号配置未启用");
|
||||
}
|
||||
String qrCodeUrl = wxMpService.createQrCode(scene);
|
||||
return R.ok(qrCodeUrl);
|
||||
} catch (WxErrorException e) {
|
||||
log.error("生成二维码失败", e);
|
||||
return R.fail("生成二维码失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "小程序登录")
|
||||
@GetMapping("/ma/login")
|
||||
public R<String> maLogin(
|
||||
@Parameter(description = "登录code") @RequestParam String code) {
|
||||
try {
|
||||
if (wxMaService == null) {
|
||||
return R.fail("小程序配置未启用");
|
||||
}
|
||||
String openId = wxMaService.code2Session(code);
|
||||
return R.ok(openId);
|
||||
} catch (WxErrorException e) {
|
||||
log.error("小程序登录失败", e);
|
||||
return R.fail("小程序登录失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "生成小程序码")
|
||||
@GetMapping("/ma/qrcode")
|
||||
public R<String> createMaQrCode(
|
||||
@Parameter(description = "场景值") @RequestParam String scene,
|
||||
@Parameter(description = "页面路径") @RequestParam(required = false) String page) {
|
||||
try {
|
||||
if (wxMaService == null) {
|
||||
return R.fail("小程序配置未启用");
|
||||
}
|
||||
byte[] qrCodeBytes = wxMaService.getUnlimitedQrCode(scene, page);
|
||||
// 这里可以将字节数组上传到OSS或者直接返回给前端
|
||||
return R.ok("二维码生成成功,大小: " + qrCodeBytes.length + " bytes");
|
||||
} catch (WxErrorException e) {
|
||||
log.error("生成小程序码失败", e);
|
||||
return R.fail("生成小程序码失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user