|
@@ -3,12 +3,28 @@ 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 cn.hutool.json.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.ylx.common.annotation.Log;
|
|
|
+import com.ylx.common.constant.Constants;
|
|
|
import com.ylx.common.core.domain.R;
|
|
|
+import com.ylx.common.core.domain.model.WxLoginUser;
|
|
|
import com.ylx.common.enums.BusinessType;
|
|
|
+import com.ylx.common.utils.MessageUtils;
|
|
|
+import com.ylx.framework.manager.AsyncManager;
|
|
|
+import com.ylx.framework.manager.factory.AsyncFactory;
|
|
|
+import com.ylx.framework.web.service.WxTokenService;
|
|
|
+import com.ylx.massage.domain.CouponReceive;
|
|
|
+import com.ylx.massage.domain.TWxUser;
|
|
|
+import com.ylx.massage.service.CouponReceiveService;
|
|
|
+import com.ylx.massage.service.TWxUserService;
|
|
|
import com.ylx.massage.utils.WeChatUtil;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -23,6 +39,7 @@ import static com.ylx.massage.utils.OtherUtil.verification;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@RestController
|
|
|
+@Api(tags = {"微信公众号"})
|
|
|
@RequestMapping("/weChat")
|
|
|
public class WeChatController {
|
|
|
|
|
@@ -47,6 +64,18 @@ public class WeChatController {
|
|
|
@Resource
|
|
|
private WeChatUtil weChatUtil;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private TWxUserService wxUserService;
|
|
|
+
|
|
|
+ @Resource(name = "commonAsyncExecutor")
|
|
|
+ private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CouponReceiveService couponReceiveService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private WxTokenService wxTokenService;
|
|
|
+
|
|
|
/**
|
|
|
* 微信Token验证
|
|
|
*
|
|
@@ -102,16 +131,19 @@ public class WeChatController {
|
|
|
*
|
|
|
* @param state 状态参数
|
|
|
*/
|
|
|
+ @ApiOperation("获取微信code")
|
|
|
+ @Log(title = "获取微信code", businessType = BusinessType.OTHER)
|
|
|
@GetMapping("/getCode")
|
|
|
- public void weiXinLogin(String state) {
|
|
|
- QrConfig config = new QrConfig(300, 300);
|
|
|
+ public String weiXinLogin(String state) {
|
|
|
+// QrConfig config = new QrConfig(300, 300);
|
|
|
// 设置边距,即二维码和背景之间的边距
|
|
|
- config.setMargin(1);
|
|
|
+// config.setMargin(1);
|
|
|
// 生成二维码到文件,也可以到流
|
|
|
String code = weChatUtil.getCode(state);
|
|
|
log.info("code:{}", code);
|
|
|
// QrCodeUtil.generate(code, config,
|
|
|
// FileUtil.file(IMG_PATH));
|
|
|
+ return code;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -121,13 +153,12 @@ public class WeChatController {
|
|
|
* @return 访问令牌
|
|
|
*/
|
|
|
@GetMapping("/getAccessToken")
|
|
|
- public String getAccessToken(@RequestParam String code) {
|
|
|
+ public R 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));
|
|
|
|
|
|
// 如果用户历史上已经完成授权
|
|
@@ -139,34 +170,37 @@ public class WeChatController {
|
|
|
// 如果用户是第一次进行微信公众号授权
|
|
|
// 进行这一步时用户应点击了同意授权按钮
|
|
|
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());
|
|
|
+ JSONObject jsonObject = new JSONObject(userInfoJsom);
|
|
|
+ // 将用户信息保存到数据库中
|
|
|
+ LambdaQueryWrapper<TWxUser> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ objectLambdaQueryWrapper.eq(TWxUser::getcOpenid, openid);
|
|
|
+ TWxUser user = wxUserService.getOne(objectLambdaQueryWrapper);
|
|
|
+ if (user == null) {
|
|
|
+ user = new TWxUser();
|
|
|
+ user.setcOpenid(openid);
|
|
|
+ user.setcNickName(jsonObject.get("nickname").toString());
|
|
|
+ user.setcIcon(jsonObject.get("headimgurl").toString());
|
|
|
+// user.setcPhone(phoneNumber);
|
|
|
+ wxUserService.save(user);
|
|
|
+ //异步 添加新人优惠卷
|
|
|
+ TWxUser finalUser = user;
|
|
|
+ threadPoolTaskExecutor.submit(() -> couponReceiveService.submit(new CouponReceive().setOpenid(finalUser.getcOpenid()).setCouponId("1")));
|
|
|
+ user.setId(user.getId());
|
|
|
+ }
|
|
|
|
|
|
- // 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 userInfoJsom;
|
|
|
+ WxLoginUser wxUser = new WxLoginUser();
|
|
|
+ BeanUtils.copyProperties(user, wxUser);
|
|
|
+ // 生成并返回令牌
|
|
|
+ String token = wxTokenService.createToken(wxUser);
|
|
|
+ if (token == null || token.isEmpty()) {
|
|
|
+ return R.fail("生成令牌失败");
|
|
|
+ }
|
|
|
+ wxUser.setToken(token);
|
|
|
+ // 返回用户信息
|
|
|
+ // 记录登录信息
|
|
|
+ AsyncManager.me().execute(AsyncFactory.recordLogininfor(wxUser.getCOpenid(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
+ return R.ok(wxUser);
|
|
|
}
|
|
|
|
|
|
/**
|