Quellcode durchsuchen

add(backend) [标签 用户 1.0]

wangxiao vor 4 Jahren
Ursprung
Commit
9336dfe604

+ 77 - 0
operation-backend/src/main/java/com/idiot/operationbackend/controller/FansController.java

@@ -0,0 +1,77 @@
+package com.idiot.operationbackend.controller;
+
+
+import com.idiot.operationbackend.entity.AccountFans;
+import com.idiot.operationbackend.service.facade.AccountFansService;
+import com.idiot.operationbackend.service.facade.WeChatService;
+import com.idiot.operationbackend.support.CustomException;
+import com.idiot.operationbackend.support.JsonResult;
+import com.idiot.operationbackend.util.JwtTokenUtil;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Objects;
+
+/**
+ * @author wang xiao
+ * @date Created in 17:55 2020/9/17
+ */
+@RestController
+@RequestMapping("/fans")
+@Api(value = "FansController", tags ="粉丝管理")
+public class FansController {
+
+
+    @Autowired
+    private AccountFansService fansService;
+
+
+    @Autowired
+    private WeChatService weChatService;
+
+    private final Logger logger = LoggerFactory.getLogger(FansController.class);
+
+
+    @GetMapping("/{accountId}")
+    @ApiOperation(value = "查询公众号粉丝列表")
+    public ResponseEntity<JsonResult<Object>> getPageFans (@RequestHeader String token,
+                                                   @PathVariable String accountId,
+                                                   @RequestParam Integer page,
+                                                   @RequestParam Integer pageSize,
+                                                   @RequestParam(required = false) Integer sex,
+                                                   @RequestParam(required = false) String city,
+                                                   @RequestParam(required = false) String tagId,
+                                                   @RequestParam(required = false) String province,
+                                                   @RequestParam(required = false) String label,
+                                                   @RequestParam(required = false) String startTime,
+                                                   @RequestParam(required = false) String endTime) {
+        String userId = JwtTokenUtil.getUserId(token);
+        logger.info("用户:{}查询公众号:{}粉丝数据",userId,accountId);
+        return ResponseEntity.ok(JsonResult.success(
+                fansService.pageFans(accountId,sex,city,province,tagId,label,startTime,endTime,page,pageSize)));
+    }
+
+
+
+    @PostMapping("/addRemark/{fansId}")
+    @ApiOperation(value = "粉丝添加/修改备注")
+    public ResponseEntity<JsonResult<Boolean>> addRemark(@RequestParam String remark,@RequestParam String fansId) {
+        if (StringUtils.isEmpty(remark)){
+            throw new CustomException(500,"备注不能为空!");
+        }
+        AccountFans fans = fansService.getById(fansId);
+        if (Objects.isNull(fans)){
+            throw new CustomException(500,"选择用户不能为空!");
+        }
+        boolean ifUp = weChatService.updateRemark(fans.getAccountId(),fans.getOpenId(),remark);
+        return ResponseEntity.ok(JsonResult.success(ifUp));
+    }
+
+
+}

+ 62 - 0
operation-backend/src/main/java/com/idiot/operationbackend/controller/TagController.java

@@ -0,0 +1,62 @@
+package com.idiot.operationbackend.controller;
+
+import com.idiot.operationbackend.entity.AccountTag;
+import com.idiot.operationbackend.service.facade.AccountTagService;
+import com.idiot.operationbackend.service.facade.WeChatService;
+import com.idiot.operationbackend.support.CustomException;
+import com.idiot.operationbackend.support.JsonResult;
+import com.idiot.operationbackend.util.JwtTokenUtil;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * @author wang xiao
+ * @date Created in 19:51 2020/9/17
+ */
+@RestController
+@RequestMapping("/tag")
+@Api(value = "TagController", tags ="标签管理")
+public class TagController {
+
+    @Autowired
+    private AccountTagService tagService;
+
+    @Autowired
+    private WeChatService weChatService;
+
+    private final Logger logger = LoggerFactory.getLogger(TagController.class);
+
+    @GetMapping("/list")
+    @ApiOperation(value = "查询公众号标签列表")
+    public ResponseEntity<JsonResult<List<AccountTag>>> getTagList (@RequestHeader String token,
+                                                                    @RequestParam String accountId) {
+        if (StringUtils.isEmpty(accountId)) {
+            throw new CustomException(500,"请选择公众号!");
+        }
+        String userId =  JwtTokenUtil.getUserId(token);
+        logger.info("用户:{}查询公众号:{}标签",userId,accountId);
+        return ResponseEntity.ok(JsonResult.success(tagService.queryAccountTag(accountId)));
+    }
+
+    @PostMapping("/addTag")
+    @ApiOperation(value = "添加标签")
+    public ResponseEntity<JsonResult<Boolean>> addTag (@RequestHeader String token,
+                                                       @RequestParam String name,
+                                                       @RequestParam String accountId) {
+        if (StringUtils.isEmpty(name)) {
+            throw new CustomException(500,"请填写标签内容!");
+        }
+        String userId =  JwtTokenUtil.getUserId(token);
+        logger.info("用户:{}查询公众号:{}添加标签",userId,accountId);
+        return ResponseEntity.ok(JsonResult.success(weChatService.createTag(accountId,name)));
+    }
+
+}

+ 14 - 0
operation-backend/src/main/java/com/idiot/operationbackend/entity/AccountTag.java

@@ -2,6 +2,9 @@ package com.idiot.operationbackend.entity;
 
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.idiot.operationbackend.support.Constants;
+
+import java.time.LocalDateTime;
 
 /**
  * 公众号标签
@@ -24,6 +27,17 @@ public class AccountTag {
 
     private String createTime;
 
+    public AccountTag() {
+    }
+
+    public AccountTag(Integer wxId, String accountId, String name) {
+        this.wxId = wxId;
+        this.accountId = accountId;
+        this.name = name;
+        this.createTime = LocalDateTime.now().format(Constants.DATE_TIME_FORMATTER);
+        this.fansCount = 0;
+    }
+
     public String getId() {
         return id;
     }

+ 30 - 0
operation-backend/src/main/java/com/idiot/operationbackend/service/facade/AccountFansService.java

@@ -1,5 +1,6 @@
 package com.idiot.operationbackend.service.facade;
 
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.idiot.operationbackend.entity.AccountFans;
 import com.idiot.operationbackend.vo.NumberStatData;
@@ -57,4 +58,33 @@ public interface AccountFansService extends IService<AccountFans> {
      */
     List<NumberStatData> statByFansProperty (String accountId,String startDate,String endDate);
 
+
+    /**
+     *  粉丝列表
+     * @author wangxiao
+     * @date 19:18 2020/9/17
+     * @param accountId
+     * @param sex
+     * @param city
+     * @param province
+     * @param tagId
+     * @param label
+     * @param startTime
+     * @param endTime
+     * @param page
+     * @param pageSize
+     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.idiot.operationbackend.entity.AccountFans>
+     */
+    Page<AccountFans> pageFans(String accountId,Integer sex, String city,String province,String tagId,String label,String startTime,String endTime ,int page,int pageSize );
+
+    /**
+     *  更新备注
+     * @author wangxiao
+     * @date 19:42 2020/9/17
+     * @param accountId
+     * @param openId
+     * @param remark
+     * @return boolean
+     */
+    boolean updateFansRemark(String accountId, String openId, String remark);
 }

+ 21 - 0
operation-backend/src/main/java/com/idiot/operationbackend/service/facade/AccountTagService.java

@@ -50,4 +50,25 @@ public interface AccountTagService extends IService<AccountTag> {
      * @return boolean
      */
     boolean delAccountTag (List<AccountTag> accountTags);
+
+
+    /**
+     *  查询标签列表
+     * @author wangxiao
+     * @date 19:59 2020/9/17
+     * @param accountId
+     * @return java.util.List<com.idiot.operationbackend.entity.AccountTag>
+     */
+    List<AccountTag> getTagByAccountId (String accountId);
+
+    /**
+     *  创建标签
+     * @author wangxiao
+     * @date 20:25 2020/9/17
+     * @param label
+     * @param accountId
+     * @param wxId
+     * @return boolean
+     */
+    boolean addTag(String label,String accountId,int wxId);
 }

+ 20 - 0
operation-backend/src/main/java/com/idiot/operationbackend/service/facade/WeChatService.java

@@ -230,6 +230,26 @@ public interface WeChatService {
    */
   String getArticleSummary(String accountId,String startDate,String endDate );
 
+  /**
+   *  添加备注
+   * @author wangxiao
+   * @date 19:38 2020/9/17
+   * @param accountId
+   * @param openId
+   * @param remark
+   * @return java.lang.String
+   */
+  boolean updateRemark(String accountId,String openId,String remark);
+
+  /**
+   *  创建标签
+   * @author wangxiao
+   * @date 20:05 2020/9/17
+   * @param accountId
+   * @param tagLabel
+   * @return boolean
+   */
+  boolean createTag (String accountId,String tagLabel);
 
   /**
    * 不支持的消息

+ 49 - 0
operation-backend/src/main/java/com/idiot/operationbackend/service/impl/AccountFansServiceImpl.java

@@ -1,7 +1,10 @@
 package com.idiot.operationbackend.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.idiot.operationbackend.entity.AccountFans;
 import com.idiot.operationbackend.mappers.AccountFansMapper;
@@ -11,6 +14,7 @@ import com.idiot.operationbackend.vo.NumberStatData;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @author wang xiao
@@ -21,6 +25,7 @@ public class AccountFansServiceImpl extends ServiceImpl<AccountFansMapper, Accou
         implements AccountFansService {
 
 
+
     @Override
     public AccountFans queryByAccountIdAndOpenId(String accountId, String openId) {
         return getOne(Wrappers.<AccountFans>lambdaQuery().eq(AccountFans::getAccountId,accountId)
@@ -61,4 +66,48 @@ public class AccountFansServiceImpl extends ServiceImpl<AccountFansMapper, Accou
         }
         return numberStatData;
     }
+
+
+    @Override
+    public Page<AccountFans> pageFans(String accountId, Integer sex, String city, String province, String tagId, String label, String startTime, String endTime, int page, int pageSize) {
+        Page<AccountFans> queryPage = new Page<>(page,pageSize);
+        LambdaQueryWrapper<AccountFans> queryWrapper = Wrappers.lambdaQuery();
+        queryWrapper.eq(AccountFans::getAccountId,accountId);
+        if (Objects.nonNull(sex)) {
+            queryWrapper.eq(AccountFans::getSex,sex);
+        }
+        if (StringUtils.isNotBlank(city)) {
+            queryWrapper.eq(AccountFans::getCity,city);
+        }
+        if (StringUtils.isNotBlank(province)) {
+            queryWrapper.eq(AccountFans::getProvince,province);
+        }
+        if (StringUtils.isNotBlank(label)) {
+            queryWrapper.eq(AccountFans::getNickName,label).or()
+                    .eq(AccountFans::getRemark,label);
+        }
+        if (StringUtils.isNotBlank(startTime)) {
+           long start =  Constants.toLocalDateTime(startTime).toEpochSecond(Constants.DEFAULT_ZONE);
+           queryWrapper.ge(AccountFans::getSubscribeTime,start);
+        }
+        if (StringUtils.isNotBlank(endTime)) {
+            long end =  Constants.toLocalDateTime(endTime).plusDays(1).toEpochSecond(Constants.DEFAULT_ZONE);
+            queryWrapper.le(AccountFans::getSubscribeTime,end);
+        }
+        if (StringUtils.isNotBlank(tagId)) {
+            queryWrapper.last(" AND locate('"+tagId+"' , tag_id_list) ORDER BY subscribe_time DESC");
+        }else {
+            queryWrapper.orderByDesc(AccountFans::getSubscribeTime);
+        }
+        return page(queryPage,queryWrapper);
+    }
+
+    @Override
+    public boolean updateFansRemark(String accountId, String openId, String remark) {
+        return update(Wrappers.<AccountFans>lambdaUpdate()
+                .eq(AccountFans::getAccountId,accountId)
+                .eq(AccountFans::getOpenId,openId)
+                .set(AccountFans::getRemark,remark)
+        );
+    }
 }

+ 8 - 2
operation-backend/src/main/java/com/idiot/operationbackend/service/impl/AccountTagServiceImpl.java

@@ -69,7 +69,13 @@ public class AccountTagServiceImpl  extends ServiceImpl<AccountTagMapper,Account
         return accountTags.stream().anyMatch(e->wxId.equals(e.getWxId()));
     }
 
+    @Override
+    public List<AccountTag> getTagByAccountId(String accountId) {
+        return list(Wrappers.<AccountTag>lambdaQuery().eq(AccountTag::getAccountId,accountId));
+    }
 
-
-
+    @Override
+    public boolean addTag(String label, String accountId, int wxId) {
+        return save(new AccountTag(wxId,accountId,label));
+    }
 }

+ 48 - 5
operation-backend/src/main/java/com/idiot/operationbackend/service/impl/WeChatServiceImpl.java

@@ -32,10 +32,7 @@ import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 import java.util.concurrent.TimeUnit;
 
 
@@ -451,7 +448,7 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
 
     @Override
     public String getArticleSummary(String accountId, String startDate, String endDate) {
-        String requestUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token=ACCESS_TOKEN";
+        String requestUrl = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token=%s";
         String accessToken = getAuthorizerAccessToken(accountId);
         requestUrl = String.format(requestUrl,accessToken);
         HttpHeaders headers = new HttpHeaders();
@@ -467,4 +464,50 @@ public class WeChatServiceImpl implements WeChatService, InitializingBean {
                 LocalDateTime.now().toString(),jsonStr);
         return jsonStr;
     }
+
+    @Override
+    public boolean updateRemark(String accountId, String openId, String remark) {
+        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s";
+        String accessToken = getAuthorizerAccessToken(accountId);
+        requestUrl = String.format(requestUrl,accessToken);
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        logger.info("微信设置备注,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
+        MultiValueMap<String,String> params = new LinkedMultiValueMap<>(2);
+        params.add("openid",openId);
+        params.add("remark",remark);
+        HttpEntity<MultiValueMap<String, String>>  entity = new HttpEntity<> (params,headers);
+        ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
+        String jsonStr = respStr.getBody();
+        logger.info("微信设置备注,accountId:{}----end,时间:{},微信返回{}",accountId,LocalDateTime.now().toString(),jsonStr);
+        if (0 ==JSONObject.parseObject(jsonStr).getInteger("errcode")) {
+            return fansService.updateFansRemark(accountId,openId,remark);
+        }
+        return false;
+    }
+
+    @Override
+    public boolean createTag(String accountId, String tagLabel) {
+
+        String requestUrl = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=%s";
+        String accessToken = getAuthorizerAccessToken(accountId);
+        requestUrl =  String.format(requestUrl,accessToken);
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        MultiValueMap<String,Object> params = new LinkedMultiValueMap<>(2);
+        HashMap<String,String> map = new HashMap<>(2);
+        map.put("name",tagLabel);
+        params.add("tag",map);
+        HttpEntity<MultiValueMap<String, Object>>  entity = new HttpEntity<> (params,headers);
+        logger.info("微信创建标签,accountId:{}----start,时间:{}",accountId, LocalDateTime.now().toString());
+        ResponseEntity<String> respStr = restTemplate.exchange(requestUrl,HttpMethod.POST,entity,String.class);
+        String jsonStr = respStr.getBody();
+        logger.info("微信创建标签,accountId:{}----end,时间:{},微信返回{}",accountId,LocalDateTime.now().toString(),jsonStr);
+        JSONObject temp = JSONObject.parseObject(jsonStr).getJSONObject("tag");
+        if (null != temp) {
+            Integer wxId = temp.getInteger("id");
+            return tagService.addTag(tagLabel,accountId,wxId);
+        }
+        return false;
+    }
 }