|
@@ -0,0 +1,205 @@
|
|
|
+package com.ylx.web.controller.massage;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.extra.qrcode.QrCodeUtil;
|
|
|
+import cn.hutool.extra.qrcode.QrConfig;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ylx.common.annotation.Log;
|
|
|
+import com.ylx.common.core.domain.R;
|
|
|
+import com.ylx.common.enums.BusinessType;
|
|
|
+import com.ylx.massage.utils.WeChatUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static com.ylx.massage.utils.OtherUtil.verification;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author b16mt
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/weChat")
|
|
|
+public class WeChatController{
|
|
|
+
|
|
|
+ private final static String TOKEN = "abcd1234";
|
|
|
+ private final static String ENCODING = "UTF-8";
|
|
|
+ private final static String ACCESS_TOKEN = "access_token";
|
|
|
+ private final static String REFRESH_TOKEN = "refresh_token";
|
|
|
+ private final static String OPEN_ID = "openid";
|
|
|
+ private final static String NICK_NAME = "nickname";
|
|
|
+ private final static String SEX = "sex";
|
|
|
+ private final static String HEAD_IMG_URL = "headimgurl";
|
|
|
+ /**
|
|
|
+ * 二维码保存路径
|
|
|
+ */
|
|
|
+ private final static String IMG_PATH = "D:\\Users\\code.png";
|
|
|
+
|
|
|
+// private final WeChatUserService weChatUserService;
|
|
|
+
|
|
|
+// public WeChatController(WeChatUserService weChatUserService) {
|
|
|
+// this.weChatUserService = weChatUserService;
|
|
|
+// }
|
|
|
+ @Resource
|
|
|
+ private WeChatUtil weChatUtil;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信Token验证
|
|
|
+ *
|
|
|
+ * @param signature 微信加密签名
|
|
|
+ * @param timestamp 时间戳
|
|
|
+ * @param nonce 随机数
|
|
|
+ * @param echostr 随机字符串
|
|
|
+ * @param response HTTP响应对象
|
|
|
+ * @throws Exception 如果处理过程中出现错误
|
|
|
+ */
|
|
|
+ @GetMapping("/verifyToken")
|
|
|
+ public void verifyToken(@RequestParam(value = "signature") String signature,
|
|
|
+ @RequestParam(value = "timestamp") String timestamp,
|
|
|
+ @RequestParam(value = "nonce") String nonce,
|
|
|
+ @RequestParam(value = "echostr") String echostr, HttpServletResponse response) throws Exception {
|
|
|
+
|
|
|
+ log.info("11111111111111111111111111111111111111111111");
|
|
|
+ // 参数排序
|
|
|
+ String[] params = new String[] { timestamp, nonce, TOKEN };
|
|
|
+ Arrays.sort(params);
|
|
|
+
|
|
|
+ // 校验成功则响应 echostr,失败则不响应
|
|
|
+ if (verification(params, signature) && echostr != null) {
|
|
|
+ response.setCharacterEncoding(ENCODING);
|
|
|
+ response.getWriter().write(echostr);
|
|
|
+ response.getWriter().flush();
|
|
|
+ response.getWriter().close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/verifyToken")
|
|
|
+ @Log(title = "公众号pverifyToken", businessType = BusinessType.OTHER)
|
|
|
+ public void pverifyToken(@RequestParam(value = "signature") String signature,
|
|
|
+ @RequestParam(value = "timestamp") String timestamp,
|
|
|
+ @RequestParam(value = "nonce") String nonce) {
|
|
|
+
|
|
|
+ log.info("11111111111111111111111111111111111111111111");
|
|
|
+ // 参数排序
|
|
|
+ String[] params = new String[] { timestamp, nonce, TOKEN };
|
|
|
+ Arrays.sort(params);
|
|
|
+
|
|
|
+// {
|
|
|
+// "signature": "63c8f3e1fb23d0d18f6d7ddf8019dc8a8737a04b",
|
|
|
+// "openid": "oPYgb6qoOUXYIQLbB7f6zOF40kwk",
|
|
|
+// "nonce": "1376426761",
|
|
|
+// "timestamp": "1717569593"
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取微信code
|
|
|
+ *
|
|
|
+ * @param state 状态参数
|
|
|
+ */
|
|
|
+ @GetMapping("/getCode")
|
|
|
+ public void weiXinLogin(String state) {
|
|
|
+ QrConfig config = new QrConfig(300, 300);
|
|
|
+ // 设置边距,即二维码和背景之间的边距
|
|
|
+ config.setMargin(1);
|
|
|
+ // 生成二维码到文件,也可以到流
|
|
|
+
|
|
|
+
|
|
|
+ QrCodeUtil.generate(weChatUtil.getCode(state), config,
|
|
|
+ FileUtil.file(IMG_PATH));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取token和userInfo
|
|
|
+ *
|
|
|
+ * @param code 微信授权码
|
|
|
+ * @return 访问令牌
|
|
|
+ */
|
|
|
+ @GetMapping("/getAccessToken")
|
|
|
+ public String getAccessToken(@RequestParam String code) {
|
|
|
+ // 发送get请求获取 AccessToken
|
|
|
+ Map<?, ?> result = weChatUtil.getAccessToken(code);
|
|
|
+ String accessToken = result.get(ACCESS_TOKEN).toString();
|
|
|
+ String refreshToken = result.get(REFRESH_TOKEN).toString();
|
|
|
+ String openid = result.get(OPEN_ID).toString();
|
|
|
+
|
|
|
+// WeChatUser user = weChatUserService.getOne(new LambdaUpdateWrapper<WeChatUser>().eq(WeChatUser::getOpenid, openid));
|
|
|
+
|
|
|
+ // 如果用户历史上已经完成授权
|
|
|
+// if (user != null) {
|
|
|
+// log.info("该用户已授权");
|
|
|
+// return "<h1>你已经授权过啦~</h1>";
|
|
|
+// }
|
|
|
+
|
|
|
+ // 如果用户是第一次进行微信公众号授权
|
|
|
+ // 进行这一步时用户应点击了同意授权按钮
|
|
|
+ String userInfoJsom = weChatUtil.getUserInfo(accessToken, openid);
|
|
|
+
|
|
|
+ // 解析JSON数据
|
|
|
+// JSONObject jsonObject = new JSONObject(userInfoJsom);
|
|
|
+
|
|
|
+ // 设置相关实体属性
|
|
|
+// WeChatUser weChatUser = new WeChatUser();
|
|
|
+// weChatUser.setAccessToken(accessToken);
|
|
|
+// weChatUser.setRefreshToken(refreshToken);
|
|
|
+// weChatUser.setCreateDate(LocalDateTime.now());
|
|
|
+// weChatUser.setUpdateDate(LocalDateTime.now());
|
|
|
+
|
|
|
+ // TODO 这里需要绑定系统真实的用户id
|
|
|
+// weChatUser.setUserId(StpUtil.getLoginIdAsLong());
|
|
|
+// weChatUser.setCreateUser(StpUtil.getLoginIdAsLong());
|
|
|
+// weChatUser.setUpdateUser(StpUtil.getLoginIdAsLong());
|
|
|
+// weChatUser.setUserId(1L);
|
|
|
+// weChatUser.setCreateUser(1L);
|
|
|
+// weChatUser.setUpdateUser(1L);
|
|
|
+
|
|
|
+// weChatUser.setOpenid(openid);
|
|
|
+// weChatUser.setNickname(jsonObject.getStr(NICK_NAME));
|
|
|
+// weChatUser.setSex(jsonObject.getStr(SEX));
|
|
|
+// weChatUser.setAvatar(jsonObject.getStr(HEAD_IMG_URL));
|
|
|
+
|
|
|
+ // 存储用户信息
|
|
|
+// weChatUserService.save(weChatUser);
|
|
|
+
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新token,微信提供的token是有限时间的,但是对于财务报销系统仅需授权一次的情况下一般不需要进行更新
|
|
|
+ *
|
|
|
+ * @return accessToken
|
|
|
+ */
|
|
|
+ @GetMapping("/refreshToken")
|
|
|
+ public R<String> refreshToken() {
|
|
|
+ // TODO 这里需要绑定系统真实的用户id
|
|
|
+// Long userId = StpUtil.getLoginIdAsLong();
|
|
|
+ //Long userId = 1L;
|
|
|
+
|
|
|
+// WeChatUser weChatUser = weChatUserService.getOne(new LambdaUpdateWrapper<WeChatUser>().eq(WeChatUser::getUserId, userId));
|
|
|
+// if (weChatUser == null){
|
|
|
+// return Result.error("error");
|
|
|
+// }
|
|
|
+
|
|
|
+ // 发送get请求获取 RefreshToken
|
|
|
+ Map<?, ?> result = weChatUtil.refreshToken("");//weChatUser.getRefreshToken()
|
|
|
+ String accessToken = result.get(ACCESS_TOKEN).toString();
|
|
|
+ String refreshToken = result.get(REFRESH_TOKEN).toString();
|
|
|
+
|
|
|
+ // 更新用户信息
|
|
|
+// WeChatUser weChatUserUpdate = new WeChatUser();
|
|
|
+// weChatUserUpdate.setId(weChatUser.getId());
|
|
|
+// weChatUserUpdate.setAccessToken(accessToken);
|
|
|
+// weChatUserUpdate.setRefreshToken(refreshToken);
|
|
|
+// weChatUserUpdate.setUpdateDate(LocalDateTime.now());
|
|
|
+
|
|
|
+ // 存储数据库
|
|
|
+// weChatUserService.updateById(weChatUserUpdate);
|
|
|
+
|
|
|
+ return R.ok(accessToken);
|
|
|
+ }
|
|
|
+}
|