|
@@ -7,6 +7,7 @@ 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.controller.BaseController;
|
|
|
import com.ylx.common.core.domain.R;
|
|
|
import com.ylx.common.core.domain.model.WxLoginUser;
|
|
|
import com.ylx.common.enums.BusinessType;
|
|
@@ -41,16 +42,14 @@ import static com.ylx.massage.utils.OtherUtil.verification;
|
|
|
@RestController
|
|
|
@Api(tags = {"微信公众号"})
|
|
|
@RequestMapping("/weChat")
|
|
|
-public class WeChatController {
|
|
|
+public class WeChatController extends BaseController {
|
|
|
|
|
|
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";
|
|
|
+
|
|
|
/**
|
|
|
* 二维码保存路径
|
|
|
*/
|
|
@@ -159,13 +158,6 @@ public class WeChatController {
|
|
|
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>";
|
|
|
-// }
|
|
|
|
|
|
// 如果用户是第一次进行微信公众号授权
|
|
|
// 进行这一步时用户应点击了同意授权按钮
|
|
@@ -181,6 +173,7 @@ public class WeChatController {
|
|
|
user.setcOpenid(openid);
|
|
|
user.setcNickName(jsonObject.get("nickname").toString());
|
|
|
user.setcIcon(jsonObject.get("headimgurl").toString());
|
|
|
+ user.setcSessionKey(refreshToken);
|
|
|
// user.setcPhone(phoneNumber);
|
|
|
wxUserService.save(user);
|
|
|
//异步 添加新人优惠卷
|
|
@@ -203,37 +196,38 @@ public class WeChatController {
|
|
|
return R.ok(wxUser);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 刷新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");
|
|
|
-// }
|
|
|
+ public String refreshToken() {
|
|
|
|
|
|
+ WxLoginUser wxLoginUser = this.getWxLoginUser();
|
|
|
+ TWxUser user = wxUserService.getById(wxLoginUser.getId());
|
|
|
+ if (user == null){
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
// 发送get请求获取 RefreshToken
|
|
|
- Map<?, ?> result = weChatUtil.refreshToken("");//weChatUser.getRefreshToken()
|
|
|
+ Map<?, ?> result = weChatUtil.refreshToken(user.getcSessionKey());
|
|
|
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());
|
|
|
-
|
|
|
+ user.setcSessionKey(refreshToken);
|
|
|
// 存储数据库
|
|
|
-// weChatUserService.updateById(weChatUserUpdate);
|
|
|
+ wxUserService.updateById(user);
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+ @ApiOperation("获取公众号二维码")
|
|
|
+ @RequestMapping(value = "getwxQrCode", method = RequestMethod.GET)
|
|
|
+ public String getWxQrCodeUtil(@RequestParam String openId) {
|
|
|
+
|
|
|
+ String token = this.refreshToken();
|
|
|
+ Map<?, ?> url = weChatUtil.getUrl(token, openId);
|
|
|
+ return url.toString();
|
|
|
|
|
|
- return R.ok(accessToken);
|
|
|
}
|
|
|
}
|