Ver código fonte

新增了多小程序的功能

shuhao 4 meses atrás
pai
commit
68fe511067

+ 10 - 4
admin/src/main/java/com/flyer/foster/controller/app/AppUserController.java

@@ -4,7 +4,9 @@ import com.flyer.foster.consts.WechatConst;
 import com.flyer.foster.dto.app.AppSubscribeRespDTO;
 import com.flyer.foster.dto.app.AppUserQueryDTO;
 import com.flyer.foster.dto.app.AppUserUpdateDTO;
+import com.flyer.foster.entity.AppInfo;
 import com.flyer.foster.entity.AppSubscribe;
+import com.flyer.foster.service.IAppInfoService;
 import com.flyer.foster.service.IAppSubscribeService;
 import com.flyer.foster.service.IAppUserService;
 import com.flyer.foster.util.WeChatApiUtil;
@@ -38,9 +40,12 @@ public class AppUserController {
     @Autowired
     private IAppSubscribeService iAppSubscribeService;
 
+    @Autowired
+    private IAppInfoService iAppInfoService;
+
     @RequestMapping("/login")
-    public R login(@RequestBody AppUserQueryDTO dto) {
-        return R.ok().result(iAppUserService.login(dto));
+    public R login(@RequestBody AppUserQueryDTO dto, @RequestHeader(value = "appid") String appid) {
+        return R.ok().result(iAppUserService.login(dto, appid));
     }
 
     /**
@@ -64,8 +69,9 @@ public class AppUserController {
      * @return
      */
     @PostMapping("/send-sub-msg")
-    public R sendSubscribeMessage() {
-        String accessToken = weChatApiUtil.getAccessToken(WechatConst.APP_ID, WechatConst.APP_SECRET);
+    public R sendSubscribeMessage(@RequestHeader(value = "appid") String appid) {
+        AppInfo appInfo = iAppInfoService.getByAppId(appid);
+        String accessToken = weChatApiUtil.getAccessToken(appInfo.getAppId(), appInfo.getAppSecret());
 
         // 获取已经订阅用户信息
         List<AppSubscribeRespDTO> subscribeUserInfoList = iAppSubscribeService.getSubscribeUserInfo();

+ 2 - 0
admin/src/main/java/com/flyer/foster/dto/GardenAddDTO.java

@@ -70,4 +70,6 @@ public class GardenAddDTO {
     private String coverUrl4;
 
     private String coverUrl4Content;
+
+    private String appid;
 }

+ 5 - 0
admin/src/main/java/com/flyer/foster/dto/GardenUpdateDTO.java

@@ -62,6 +62,11 @@ public class GardenUpdateDTO {
     private String baseMapUrl;
 
     /**
+     * appid
+     */
+    private String appid;
+
+    /**
      * 二维码
      */
     private String qrCode;

+ 3 - 0
admin/src/main/java/com/flyer/foster/entity/AppInfo.java

@@ -1,5 +1,7 @@
 package com.flyer.foster.entity;
 
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import java.io.Serializable;
 import lombok.Getter;
@@ -23,6 +25,7 @@ public class AppInfo implements Serializable {
     /**
      * 微信小程序appid
      */
+    @TableId(value = "app_id", type = IdType.INPUT)
     private String appId;
 
     /**

+ 2 - 0
admin/src/main/java/com/flyer/foster/entity/Garden.java

@@ -116,6 +116,8 @@ public class Garden implements Serializable {
      */
     private Integer tenantId;
 
+    private String appid;
+
     /**
      * 乐观锁
      */

+ 2 - 0
admin/src/main/java/com/flyer/foster/service/IAppInfoService.java

@@ -13,4 +13,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IAppInfoService extends IService<AppInfo> {
 
+    AppInfo getByAppId(String appId);
+
 }

+ 1 - 1
admin/src/main/java/com/flyer/foster/service/IAppUserService.java

@@ -16,7 +16,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IAppUserService extends IService<AppUser> {
 
-    AppUserRespDTO login(AppUserQueryDTO dto);
+    AppUserRespDTO login(AppUserQueryDTO dto, String appid);
 
     boolean updateUser(AppUserUpdateDTO updateDTO);
 }

+ 4 - 0
admin/src/main/java/com/flyer/foster/service/impl/AppInfoServiceImpl.java

@@ -17,4 +17,8 @@ import org.springframework.stereotype.Service;
 @Service
 public class AppInfoServiceImpl extends ServiceImpl<IAppInfoMapper, AppInfo> implements IAppInfoService {
 
+    @Override
+    public AppInfo getByAppId(String appId) {
+        return lambdaQuery().eq(AppInfo::getAppId, appId).one();
+    }
 }

+ 9 - 2
admin/src/main/java/com/flyer/foster/service/impl/AppUserServiceImpl.java

@@ -9,17 +9,20 @@ import com.flyer.foster.consts.WechatConst;
 import com.flyer.foster.dto.app.AppUserQueryDTO;
 import com.flyer.foster.dto.app.AppUserRespDTO;
 import com.flyer.foster.dto.app.AppUserUpdateDTO;
+import com.flyer.foster.entity.AppInfo;
 import com.flyer.foster.entity.AppUser;
 import com.flyer.foster.entity.Garden;
 import com.flyer.foster.mapper.IAppUserMapper;
 import com.flyer.foster.pojo.Code2SessionResp;
 import com.flyer.foster.pojo.StpAppUtil;
+import com.flyer.foster.service.IAppInfoService;
 import com.flyer.foster.service.IAppUserService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.flyer.foster.service.IGardenService;
 import com.flyer.foster.util.WeChatApiUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.web.bind.annotation.RequestHeader;
 
 import java.time.LocalDateTime;
 
@@ -51,10 +54,14 @@ public class AppUserServiceImpl extends ServiceImpl<IAppUserMapper, AppUser> imp
     @Autowired
     private IGardenService iGardenService;
 
+    @Autowired
+    private IAppInfoService iAppInfoService;
+
     @Override
-    public AppUserRespDTO login(AppUserQueryDTO dto) {
+    public AppUserRespDTO login(AppUserQueryDTO dto, String appid) {
+        AppInfo appInfo = iAppInfoService.getByAppId(appid);
         // 查找用户信息
-        Code2SessionResp code2Session = weChatApiUtil.getCode2Session(dto.getCode(), WechatConst.APP_ID, WechatConst.APP_SECRET);
+        Code2SessionResp code2Session = weChatApiUtil.getCode2Session(dto.getCode(), appInfo.getAppId(), appInfo.getAppSecret());
         String openid = code2Session.getOpenid();
         // 根据gardenId获取租户id
         Garden garden = iGardenService.getById(dto.getGardenId());

+ 15 - 4
admin/src/main/java/com/flyer/foster/service/impl/GardenServiceImpl.java

@@ -9,9 +9,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.flyer.exception.BusinessException;
 import com.flyer.foster.consts.WechatConst;
 import com.flyer.foster.dto.*;
+import com.flyer.foster.entity.AppInfo;
 import com.flyer.foster.entity.Area;
 import com.flyer.foster.entity.Garden;
 import com.flyer.foster.mapper.IGardenMapper;
+import com.flyer.foster.service.IAppInfoService;
 import com.flyer.foster.service.IAreaService;
 import com.flyer.foster.service.IGardenService;
 import com.flyer.foster.util.HttpUtils;
@@ -47,6 +49,9 @@ public class GardenServiceImpl extends ServiceImpl<IGardenMapper, Garden> implem
     @Autowired
     private QiniuUploadService qiniuUploadService;
 
+    @Autowired
+    private IAppInfoService iAppInfoService;
+
     @Override
     public IPage<GardenRespDTO> getPageList(IPage<GardenRespDTO> page, GardenQueryDTO queryDTO) {
         IPage<GardenRespDTO> pageList = baseMapper.getPageList(page, queryDTO);
@@ -70,7 +75,7 @@ public class GardenServiceImpl extends ServiceImpl<IGardenMapper, Garden> implem
         BeanUtil.copyProperties(addDTO, garden);
         this.save(garden);
         // 生成二维码
-        String qrCode = this.generateQRCode(garden.getId());
+        String qrCode = this.generateQRCode(garden.getId(), addDTO.getAppid());
 
         return this.lambdaUpdate()
                 .set(Garden::getQrCode, qrCode)
@@ -79,11 +84,11 @@ public class GardenServiceImpl extends ServiceImpl<IGardenMapper, Garden> implem
 
     /**
      * 生成小程序二维码
-     *
      * @param gardenId 果园id
      */
-    private String generateQRCode(Integer gardenId) {
-        WxAccessToken wxAccessToken = WxAccessToken.getInstance(weChatApiUtil, WechatConst.APP_ID, WechatConst.APP_SECRET);
+    private String generateQRCode(Integer gardenId, String appId) {
+        AppInfo appInfo = iAppInfoService.getByAppId(appId);
+        WxAccessToken wxAccessToken = WxAccessToken.getInstance(weChatApiUtil, appInfo.getAppId(), appInfo.getAppSecret());
         StringBuilder sb2 = new StringBuilder(200);
         sb2.append("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=");
         sb2.append(wxAccessToken.getAccess_token());
@@ -117,11 +122,17 @@ public class GardenServiceImpl extends ServiceImpl<IGardenMapper, Garden> implem
         if (garden == null) {
             throw new BusinessException("无效的果园id");
         }
+        // 生成二维码
+        String qrCode = this.generateQRCode(garden.getId(), updateDTO.getAppid());
+        System.out.println("------------qrCode-------------");
+        System.out.println(qrCode);
+        System.out.println("------------qrCode-------------");
         return this.lambdaUpdate()
                 .set(StrUtil.isNotBlank(updateDTO.getName()), Garden::getName, updateDTO.getName())
                 .set(updateDTO.getWorldMapId() != null, Garden::getWorldMapId, updateDTO.getWorldMapId())
                 .set(StrUtil.isNotBlank(updateDTO.getWkt()), Garden::getWkt, updateDTO.getWkt())
                 .set(StrUtil.isNotBlank(updateDTO.getIcon()), Garden::getIcon, updateDTO.getIcon())
+                .set(StrUtil.isNotBlank(updateDTO.getAppid()), Garden::getAppid, updateDTO.getAppid())
                 .set(StrUtil.isNotBlank(updateDTO.getDayBg()), Garden::getDayBg, updateDTO.getDayBg())
                 .set(StrUtil.isNotBlank(updateDTO.getNightBg()), Garden::getNightBg, updateDTO.getNightBg())
                 .set(updateDTO.getBirdseyeGardenId() != null, Garden::getBirdseyeGardenId, updateDTO.getBirdseyeGardenId())

+ 14 - 10
admin/src/main/java/com/flyer/foster/util/WxAccessToken.java

@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject;
 import lombok.Data;
 
 import java.time.LocalDateTime;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * 获取 WX 单例 AccessToken
@@ -11,27 +13,29 @@ import java.time.LocalDateTime;
 @Data
 public class WxAccessToken {
 
-    private static WxAccessToken instance;
+    private static Map<String , WxAccessToken> instance = new HashMap<>();
 
 
     public static WxAccessToken getInstance(WeChatApiUtil weChatApiUtil, String appId, String secret){
-        if(instance != null && !instance.isExpires()){
-            return instance;
+        WxAccessToken wxAccessToken = instance.get(appId);
+        if(wxAccessToken != null && !wxAccessToken.isExpires(wxAccessToken)){
+            return wxAccessToken;
         }
         JSONObject obj= weChatApiUtil.getAccessToken1(appId, secret);
-        instance = new WxAccessToken();
-        instance.expires_in = obj.getInteger("expires_in");
-        instance.expires = LocalDateTime.now().plusSeconds(instance.expires_in - 100);
-        instance.access_token = obj.getString("access_token");
-        return instance;
+        wxAccessToken = new WxAccessToken();
+        wxAccessToken.expires_in = obj.getInteger("expires_in");
+        wxAccessToken.expires = LocalDateTime.now().plusSeconds(wxAccessToken.expires_in - 100);
+        wxAccessToken.access_token = obj.getString("access_token");
+        instance.put(appId, wxAccessToken);
+        return wxAccessToken;
     }
 
     /**
      * 判断当期token是否过期
      * @return
      */
-    private boolean isExpires(){
-        return LocalDateTime.now().isAfter(instance.expires);
+    private boolean isExpires(WxAccessToken wxAccessToken){
+        return LocalDateTime.now().isAfter(wxAccessToken.expires);
     }