From 15bef06adb847a22c6bad1f4d98497bab5b37d4e Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 31 Mar 2020 17:41:46 +0800 Subject: [PATCH 01/13] =?UTF-8?q?:bug:=20=E5=BE=AE=E4=BF=A1=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E5=90=8E=E8=B7=B3=E8=BD=AC=E8=BF=94=E5=9B=9E=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/mp/controller/WxoAuthController.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java index 7e77d71..f2f6094 100644 --- a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java +++ b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java @@ -8,7 +8,6 @@ import me.chanjar.weixin.common.util.http.URIUtil; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; import me.chanjar.weixin.mp.bean.result.WxMpUser; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -28,16 +27,22 @@ public class WxoAuthController { @Value("${resource.domain}") private String domain; + @Value("${server.servlet.context-path}") + private String contextPath; + @GetMapping public String wxAuth(@PathVariable String appId, @RequestParam(name = "redirectUrl") String redirectUrl) { + StringBuilder baseUrl; wxMpService.switchoverTo(appId); + // 测试号 if ("wxa49093339a5a822b".equals(appId)) { - domain = "http://oae.nat300.top/vertical"; + baseUrl = new StringBuilder("http://1wx.rymcu.com").append(contextPath); } else { - domain += "/vertical-console"; + baseUrl = new StringBuilder(domain).append(contextPath); } - StringBuilder accessTokenUrl = new StringBuilder(domain).append("/wx/oauth/" + appId + "/getAccessToken?redirectUrl=").append(URIUtil.encodeURIComponent(domain + redirectUrl)); + StringBuilder accessTokenUrl = baseUrl.append("/wx/oauth/" + appId + "/getAccessToken?redirectUrl=").append(URIUtil.encodeURIComponent(redirectUrl)); String oauth2Url = wxMpService.oauth2buildAuthorizationUrl(accessTokenUrl.toString(), WxConsts.OAuth2Scope.SNSAPI_BASE, null); + return "redirect:" + oauth2Url; } From 1d557e75eeea89bf4e56d42da121d0aafa364c43 Mon Sep 17 00:00:00 2001 From: ronger Date: Fri, 3 Apr 2020 17:35:07 +0800 Subject: [PATCH 02/13] =?UTF-8?q?:sparkles:=20=E7=89=B9=E6=AE=8A=E6=97=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 + .../com/rymcu/vertical/entity/SpecialDay.java | 44 ++++++ .../com/rymcu/vertical/entity/WxUser.java | 49 ++++++ .../vertical/mapper/SpecialDayMapper.java | 10 ++ .../rymcu/vertical/mapper/WxUserMapper.java | 10 ++ .../vertical/service/SpecialDayService.java | 10 ++ .../rymcu/vertical/service/WxUserService.java | 14 ++ .../service/impl/SpecialDayServiceImpl.java | 20 +++ .../service/impl/WxUserServiceImpl.java | 62 ++++++++ .../web/api/admin/AdminController.java | 29 ++-- .../wx/miniapp/config/WxMaConfiguration.java | 147 ++++++++++++++++++ .../wx/miniapp/config/WxMaProperties.java | 45 ++++++ .../controller/WxMaMediaController.java | 80 ++++++++++ .../controller/WxMaUserController.java | 93 +++++++++++ .../vertical/wx/miniapp/utils/JsonUtils.java | 28 ++++ .../wx/mp/controller/WxMenuController.java | 54 +++---- .../wx/mp/controller/WxoAuthController.java | 4 + src/main/java/mapper/SpecialDayMapper.xml | 17 ++ src/main/java/mapper/WxUserMapper.xml | 21 +++ 19 files changed, 697 insertions(+), 45 deletions(-) create mode 100644 src/main/java/com/rymcu/vertical/entity/SpecialDay.java create mode 100644 src/main/java/com/rymcu/vertical/entity/WxUser.java create mode 100644 src/main/java/com/rymcu/vertical/mapper/SpecialDayMapper.java create mode 100644 src/main/java/com/rymcu/vertical/mapper/WxUserMapper.java create mode 100644 src/main/java/com/rymcu/vertical/service/SpecialDayService.java create mode 100644 src/main/java/com/rymcu/vertical/service/WxUserService.java create mode 100644 src/main/java/com/rymcu/vertical/service/impl/SpecialDayServiceImpl.java create mode 100644 src/main/java/com/rymcu/vertical/service/impl/WxUserServiceImpl.java create mode 100644 src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java create mode 100644 src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaProperties.java create mode 100644 src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaMediaController.java create mode 100644 src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaUserController.java create mode 100644 src/main/java/com/rymcu/vertical/wx/miniapp/utils/JsonUtils.java create mode 100644 src/main/java/mapper/SpecialDayMapper.xml create mode 100644 src/main/java/mapper/WxUserMapper.xml diff --git a/pom.xml b/pom.xml index bd2ce30..16c8bbc 100644 --- a/pom.xml +++ b/pom.xml @@ -180,6 +180,11 @@ weixin-java-open 3.7.0 + + com.github.binarywang + java-emoji-converter + 1.0.0 + diff --git a/src/main/java/com/rymcu/vertical/entity/SpecialDay.java b/src/main/java/com/rymcu/vertical/entity/SpecialDay.java new file mode 100644 index 0000000..7c8dafb --- /dev/null +++ b/src/main/java/com/rymcu/vertical/entity/SpecialDay.java @@ -0,0 +1,44 @@ +package com.rymcu.vertical.entity; + +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.io.Serializable; +import java.util.Date; + +/** + * @author ronger + */ +@Data +@Table(name="vertical_special_day") +public class SpecialDay implements Serializable,Cloneable{ + /** */ + @Id + @Column(name = "id") + @GeneratedValue(generator = "JDBC") + private Integer idSpecialDay; + /** 名称 */ + private String specialDayName; + /** 权重/优先级,小数优秀 */ + private Integer weights; + /** 开始时间 */ + private Date startTime; + /** 过期时间 */ + private Date expirationTime; + /** 是否重复 */ + private Integer repeat; + /** 重复周期 */ + private Integer repeatCycle; + /** 0:天1:周2:月3:年 */ + private Integer repeatCycleUnit; + /** 创建时间 */ + private Date createdTime; + /** 图片路径 */ + private String imgUrl; + /** 执行全局样式 */ + private String cssStyle; + +} diff --git a/src/main/java/com/rymcu/vertical/entity/WxUser.java b/src/main/java/com/rymcu/vertical/entity/WxUser.java new file mode 100644 index 0000000..e9d4103 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/entity/WxUser.java @@ -0,0 +1,49 @@ +package com.rymcu.vertical.entity; + +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author ronger + */ +@Data +@Table(name = "vertical_wx_user") +public class WxUser { + + @Id + @Column(name = "id") + @GeneratedValue(generator = "JDBC") + private Integer idWxUser; + + private Boolean subscribe; + + private String openId; + + private String nickname; + + private String sexDesc; + + private Integer sex; + + private String language; + + private String city; + + private String province; + + private String country; + + private String headImgUrl; + + private Long subscribeTime; + + private String unionId; + + private String appId; + + private String actToken; +} diff --git a/src/main/java/com/rymcu/vertical/mapper/SpecialDayMapper.java b/src/main/java/com/rymcu/vertical/mapper/SpecialDayMapper.java new file mode 100644 index 0000000..9e9a964 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/mapper/SpecialDayMapper.java @@ -0,0 +1,10 @@ +package com.rymcu.vertical.mapper; + +import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.entity.SpecialDay; + +/** + * @author ronger + */ +public interface SpecialDayMapper extends Mapper { +} diff --git a/src/main/java/com/rymcu/vertical/mapper/WxUserMapper.java b/src/main/java/com/rymcu/vertical/mapper/WxUserMapper.java new file mode 100644 index 0000000..9e941d3 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/mapper/WxUserMapper.java @@ -0,0 +1,10 @@ +package com.rymcu.vertical.mapper; + +import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.entity.WxUser; + +/** + * @author ronger + */ +public interface WxUserMapper extends Mapper { +} diff --git a/src/main/java/com/rymcu/vertical/service/SpecialDayService.java b/src/main/java/com/rymcu/vertical/service/SpecialDayService.java new file mode 100644 index 0000000..e5a8ff0 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/SpecialDayService.java @@ -0,0 +1,10 @@ +package com.rymcu.vertical.service; + +import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.entity.SpecialDay; + +/** + * @author ronger + */ +public interface SpecialDayService extends Service { +} diff --git a/src/main/java/com/rymcu/vertical/service/WxUserService.java b/src/main/java/com/rymcu/vertical/service/WxUserService.java new file mode 100644 index 0000000..59e8527 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/WxUserService.java @@ -0,0 +1,14 @@ +package com.rymcu.vertical.service; + +import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.entity.WxUser; +import me.chanjar.weixin.mp.bean.result.WxMpUser; + +/** + * @author ronger + */ +public interface WxUserService extends Service { + + WxUser saveUser(WxMpUser wxMpUser, String appId); + +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/SpecialDayServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/SpecialDayServiceImpl.java new file mode 100644 index 0000000..b959b36 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/impl/SpecialDayServiceImpl.java @@ -0,0 +1,20 @@ +package com.rymcu.vertical.service.impl; + +import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.entity.SpecialDay; +import com.rymcu.vertical.mapper.SpecialDayMapper; +import com.rymcu.vertical.service.SpecialDayService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * @author ronger + */ +@Service +public class SpecialDayServiceImpl extends AbstractService implements SpecialDayService { + + @Resource + private SpecialDayMapper specialDayMapper; + +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/WxUserServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/WxUserServiceImpl.java new file mode 100644 index 0000000..3309f0d --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/impl/WxUserServiceImpl.java @@ -0,0 +1,62 @@ +package com.rymcu.vertical.service.impl; + +import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.entity.WxUser; +import com.rymcu.vertical.mapper.WxUserMapper; +import com.rymcu.vertical.service.WxUserService; +import me.chanjar.weixin.mp.bean.result.WxMpUser; +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +/** + * @author ronger + */ +@Service +public class WxUserServiceImpl extends AbstractService implements WxUserService { + + @Resource + private WxUserMapper wxUserMapper; + + @Override + public WxUser saveUser(WxMpUser wxMpUser, String appId) { + WxUser searchWxUser = new WxUser(); + if (StringUtils.isBlank(wxMpUser.getUnionId())) { + searchWxUser.setUnionId(wxMpUser.getUnionId()); + } else { + searchWxUser.setAppId(appId); + searchWxUser.setOpenId(searchWxUser.getOpenId()); + } + List wxUsers = wxUserMapper.select(searchWxUser); + WxUser wxUser; + if (wxUsers.isEmpty()) { + wxUser = new WxUser(); + wxUser.setAppId(appId); + wxUser = copyWxUser(wxMpUser,wxUser); + wxUserMapper.insertSelective(wxUser); + } else { + wxUser = wxUsers.get(0); + wxUser = copyWxUser(wxMpUser,wxUser); + wxUserMapper.updateByPrimaryKeySelective(wxUser); + } + return wxUser; + } + + private WxUser copyWxUser(WxMpUser wxMpUser, WxUser wxUser) { + wxUser.setNickname(wxMpUser.getNickname()); + wxUser.setHeadImgUrl(wxMpUser.getHeadImgUrl()); + wxUser.setCountry(wxMpUser.getCountry()); + wxUser.setProvince(wxMpUser.getProvince()); + wxUser.setCity(wxMpUser.getCity()); + wxUser.setSex(wxMpUser.getSex()); + wxUser.setSubscribe(wxMpUser.getSubscribe()); + wxUser.setSubscribeTime(wxMpUser.getSubscribeTime()); + wxUser.setUnionId(wxMpUser.getUnionId()); + wxUser.setOpenId(wxMpUser.getOpenId()); + wxUser.setLanguage(wxMpUser.getLanguage()); + wxUser.setSexDesc(wxMpUser.getSexDesc()); + return wxUser; + } +} diff --git a/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java b/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java index 2249a76..643dc9b 100644 --- a/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java +++ b/src/main/java/com/rymcu/vertical/web/api/admin/AdminController.java @@ -6,14 +6,8 @@ import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.dto.admin.TopicTagDTO; import com.rymcu.vertical.dto.admin.UserRoleDTO; -import com.rymcu.vertical.entity.Role; -import com.rymcu.vertical.entity.Tag; -import com.rymcu.vertical.entity.Topic; -import com.rymcu.vertical.entity.User; -import com.rymcu.vertical.service.RoleService; -import com.rymcu.vertical.service.TagService; -import com.rymcu.vertical.service.TopicService; -import com.rymcu.vertical.service.UserService; +import com.rymcu.vertical.entity.*; +import com.rymcu.vertical.service.*; import com.rymcu.vertical.util.Utils; import org.apache.commons.lang.StringUtils; import org.springframework.web.bind.annotation.*; @@ -39,6 +33,8 @@ public class AdminController { private TopicService topicService; @Resource private TagService tagService; + @Resource + private SpecialDayService specialDayService; @GetMapping("/users") public GlobalResult> users(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows){ @@ -47,10 +43,7 @@ public class AdminController { PageInfo pageInfo = new PageInfo<>(list); Map map = new HashMap(2); map.put("users", pageInfo.getList()); - Map pagination = new HashMap<>(3); - pagination.put("pageSize",pageInfo.getPageSize()); - pagination.put("total",pageInfo.getTotal()); - pagination.put("currentPage",pageInfo.getPageNum()); + Map pagination = Utils.getPagination(pageInfo); map.put("pagination", pagination); return GlobalResultGenerator.genSuccessResult(map); } @@ -204,4 +197,16 @@ public class AdminController { return GlobalResultGenerator.genSuccessResult(map); } + @GetMapping("/special-days") + public GlobalResult specialDays(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) { + PageHelper.startPage(page, rows); + List list = specialDayService.findAll(); + PageInfo pageInfo = new PageInfo<>(list); + Map map = new HashMap<>(2); + map.put("specialDays", pageInfo.getList()); + Map pagination = Utils.getPagination(pageInfo); + map.put("pagination", pagination); + return GlobalResultGenerator.genSuccessResult(map); + } + } diff --git a/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java b/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java new file mode 100644 index 0000000..e4498d0 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaConfiguration.java @@ -0,0 +1,147 @@ +package com.rymcu.vertical.wx.miniapp.config; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; +import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage; +import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; +import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; +import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; +import cn.binarywang.wx.miniapp.message.WxMaMessageHandler; +import cn.binarywang.wx.miniapp.message.WxMaMessageRouter; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; +import me.chanjar.weixin.common.error.WxErrorException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.PostConstruct; +import java.io.File; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @author Binary Wang + */ +@Configuration +@EnableConfigurationProperties(WxMaProperties.class) +public class WxMaConfiguration { + private WxMaProperties properties; + + private static Map routers = Maps.newHashMap(); + private static Map maServices = Maps.newHashMap(); + + @Autowired + public WxMaConfiguration(WxMaProperties properties) { + this.properties = properties; + } + + public static WxMaService getMaService(String appid) { + WxMaService wxService = maServices.get(appid); + if (wxService == null) { + throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置,请核实!", appid)); + } + + return wxService; + } + + public static WxMaMessageRouter getRouter(String appid) { + return routers.get(appid); + } + + @PostConstruct + public void init() { + List configs = this.properties.getConfigs(); + if (configs == null) { + throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!"); + } + + maServices = configs.stream() + .map(a -> { + WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); + config.setAppid(a.getAppId()); + config.setSecret(a.getSecret()); + config.setToken(a.getToken()); + config.setAesKey(a.getAesKey()); + config.setMsgDataFormat(a.getMsgDataFormat()); + + WxMaService service = new WxMaServiceImpl(); + service.setWxMaConfig(config); + routers.put(a.getAppId(), this.newRouter(service)); + return service; + }).collect(Collectors.toMap(s -> s.getWxMaConfig().getAppid(), a -> a)); + } + + private WxMaMessageRouter newRouter(WxMaService service) { + final WxMaMessageRouter router = new WxMaMessageRouter(service); + router + .rule().handler(logHandler).next() + .rule().async(false).content("模板").handler(templateMsgHandler).end() + .rule().async(false).content("文本").handler(textHandler).end() + .rule().async(false).content("图片").handler(picHandler).end() + .rule().async(false).content("二维码").handler(qrcodeHandler).end(); + return router; + } + + private final WxMaMessageHandler templateMsgHandler = (wxMessage, context, service, sessionManager) -> { + service.getMsgService().sendTemplateMsg(WxMaTemplateMessage.builder() + .templateId("此处更换为自己的模板id") + .formId("自己替换可用的formid") + .data(Lists.newArrayList( + new WxMaTemplateData("keyword1", "339208499", "#173177"))) + .toUser(wxMessage.getFromUser()) + .build()); + return null; + }; + + private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> { + System.out.println("收到消息:" + wxMessage.toString()); + service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson()) + .toUser(wxMessage.getFromUser()).build()); + return null; + }; + + private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> { + service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息") + .toUser(wxMessage.getFromUser()).build()); + return null; + }; + + private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> { + try { + WxMediaUploadResult uploadResult = service.getMediaService() + .uploadMedia("image", "png", + ClassLoader.getSystemResourceAsStream("tmp.png")); + service.getMsgService().sendKefuMsg( + WxMaKefuMessage + .newImageBuilder() + .mediaId(uploadResult.getMediaId()) + .toUser(wxMessage.getFromUser()) + .build()); + } catch (WxErrorException e) { + e.printStackTrace(); + } + + return null; + }; + + private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> { + try { + final File file = service.getQrcodeService().createQrcode("123", 430); + WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file); + service.getMsgService().sendKefuMsg( + WxMaKefuMessage + .newImageBuilder() + .mediaId(uploadResult.getMediaId()) + .toUser(wxMessage.getFromUser()) + .build()); + } catch (WxErrorException e) { + e.printStackTrace(); + } + + return null; + }; + +} diff --git a/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaProperties.java b/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaProperties.java new file mode 100644 index 0000000..4ad9dc5 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/wx/miniapp/config/WxMaProperties.java @@ -0,0 +1,45 @@ +package com.rymcu.vertical.wx.miniapp.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.List; + +/** + * @author Binary Wang + */ +@Data +@ConfigurationProperties(prefix = "wx.miniapp") +public class WxMaProperties { + + private List configs; + + @Data + public static class Config { + /** + * 设置微信小程序的appid + */ + private String appId; + + /** + * 设置微信小程序的Secret + */ + private String secret; + + /** + * 设置微信小程序消息服务器配置的token + */ + private String token; + + /** + * 设置微信小程序消息服务器配置的EncodingAESKey + */ + private String aesKey; + + /** + * 消息格式,XML或者JSON + */ + private String msgDataFormat; + } + +} diff --git a/src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaMediaController.java b/src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaMediaController.java new file mode 100644 index 0000000..132e995 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaMediaController.java @@ -0,0 +1,80 @@ +package com.rymcu.vertical.wx.miniapp.controller; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.constant.WxMaConstants; +import com.google.common.collect.Lists; +import com.google.common.io.Files; +import com.rymcu.vertical.wx.miniapp.config.WxMaConfiguration; +import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; +import me.chanjar.weixin.common.error.WxErrorException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + *
+ *  小程序临时素材接口
+ *  Created by BinaryWang on 2017/6/16.
+ * 
+ * + * @author Binary Wang + */ +@RestController +@RequestMapping("/wx/media/{appId}") +public class WxMaMediaController { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * 上传临时素材 + * + * @return 素材的media_id列表,实际上如果有的话,只会有一个 + */ + @PostMapping("/upload") + public List uploadMedia(@PathVariable String appId, HttpServletRequest request) throws WxErrorException { + final WxMaService wxService = WxMaConfiguration.getMaService(appId); + + CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); + + if (!resolver.isMultipart(request)) { + return Lists.newArrayList(); + } + + MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; + Iterator it = multiRequest.getFileNames(); + List result = Lists.newArrayList(); + while (it.hasNext()) { + try { + MultipartFile file = multiRequest.getFile(it.next()); + File newFile = new File(Files.createTempDir(), file.getOriginalFilename()); + this.logger.info("filePath is :" + newFile.toString()); + file.transferTo(newFile); + WxMediaUploadResult uploadResult = wxService.getMediaService().uploadMedia(WxMaConstants.KefuMsgType.IMAGE, newFile); + this.logger.info("media_id : " + uploadResult.getMediaId()); + result.add(uploadResult.getMediaId()); + } catch (IOException e) { + this.logger.error(e.getMessage(), e); + } + } + + return result; + } + + /** + * 下载临时素材 + */ + @GetMapping("/download/{mediaId}") + public File getMedia(@PathVariable String appId, @PathVariable String mediaId) throws WxErrorException { + final WxMaService wxService = WxMaConfiguration.getMaService(appId); + + return wxService.getMediaService().getMedia(mediaId); + } +} diff --git a/src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaUserController.java b/src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaUserController.java new file mode 100644 index 0000000..1eaf208 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/wx/miniapp/controller/WxMaUserController.java @@ -0,0 +1,93 @@ +package com.rymcu.vertical.wx.miniapp.controller; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; +import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; +import com.rymcu.vertical.wx.miniapp.config.WxMaConfiguration; +import com.rymcu.vertical.wx.miniapp.utils.JsonUtils; +import me.chanjar.weixin.common.error.WxErrorException; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 微信小程序用户接口 + * + * @author Binary Wang + */ +@RestController +@RequestMapping("/wx/user/{appId}") +public class WxMaUserController { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + /** + * 登陆接口 + */ + @GetMapping("/login") + public String login(@PathVariable String appId, String code) { + if (StringUtils.isBlank(code)) { + return "empty jscode"; + } + + final WxMaService wxService = WxMaConfiguration.getMaService(appId); + + try { + WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code); + this.logger.info(session.getSessionKey()); + this.logger.info(session.getOpenid()); + //TODO 可以增加自己的逻辑,关联业务相关数据 + return JsonUtils.toJson(session); + } catch (WxErrorException e) { + this.logger.error(e.getMessage(), e); + return e.toString(); + } + } + + /** + *
+     * 获取用户信息接口
+     * 
+ */ + @GetMapping("/info") + public String info(@PathVariable String appId, String sessionKey, + String signature, String rawData, String encryptedData, String iv) { + final WxMaService wxService = WxMaConfiguration.getMaService(appId); + + // 用户信息校验 + if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) { + return "user check failed"; + } + + // 解密用户信息 + WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv); + + return JsonUtils.toJson(userInfo); + } + + /** + *
+     * 获取用户绑定手机号信息
+     * 
+ */ + @GetMapping("/phone") + public String phone(@PathVariable String appId, String sessionKey, String signature, + String rawData, String encryptedData, String iv) { + final WxMaService wxService = WxMaConfiguration.getMaService(appId); + + // 用户信息校验 + if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) { + return "user check failed"; + } + + // 解密 + WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); + + return JsonUtils.toJson(phoneNoInfo); + } + +} diff --git a/src/main/java/com/rymcu/vertical/wx/miniapp/utils/JsonUtils.java b/src/main/java/com/rymcu/vertical/wx/miniapp/utils/JsonUtils.java new file mode 100644 index 0000000..d274870 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/wx/miniapp/utils/JsonUtils.java @@ -0,0 +1,28 @@ +package com.rymcu.vertical.wx.miniapp.utils; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +/** + * @author Binary Wang + */ +public class JsonUtils { + private static final ObjectMapper JSON = new ObjectMapper(); + + static { + JSON.setSerializationInclusion(Include.NON_NULL); + JSON.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE); + } + + public static String toJson(Object obj) { + try { + return JSON.writeValueAsString(obj); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + + return null; + } +} diff --git a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxMenuController.java b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxMenuController.java index 40ad886..3feb574 100644 --- a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxMenuController.java +++ b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxMenuController.java @@ -46,9 +46,9 @@ public class WxMenuController { public String menuCreateSample(@PathVariable String appId) throws WxErrorException, MalformedURLException { WxMenu menu = new WxMenu(); WxMenuButton button1 = new WxMenuButton(); - button1.setType(MenuButtonType.CLICK); - button1.setName("今日歌曲"); - button1.setKey("V1001_TODAY_MUSIC"); + button1.setType(MenuButtonType.VIEW); + button1.setName("官方网站"); + button1.setUrl("https://rymcu.com"); // WxMenuButton button2 = new WxMenuButton(); // button2.setType(WxConsts.BUTTON_MINIPROGRAM); @@ -58,7 +58,7 @@ public class WxMenuController { // button2.setUrl("http://mp.weixin.qq.com"); WxMenuButton button3 = new WxMenuButton(); - button3.setName("菜单"); + button3.setName("学习教程"); menu.getButtons().add(button1); // menu.getButtons().add(button2); @@ -66,38 +66,26 @@ public class WxMenuController { WxMenuButton button31 = new WxMenuButton(); button31.setType(MenuButtonType.VIEW); - button31.setName("搜索"); - button31.setUrl("http://www.soso.com/"); + button31.setName("51单片机"); + button31.setUrl("https://rymcu.com/article/27"); - WxMenuButton button32 = new WxMenuButton(); - button32.setType(MenuButtonType.VIEW); - button32.setName("视频"); - button32.setUrl("http://v.qq.com/"); - - WxMenuButton button33 = new WxMenuButton(); - button33.setType(MenuButtonType.CLICK); - button33.setName("赞一下我们"); - button33.setKey("V1001_GOOD"); - - WxMenuButton button34 = new WxMenuButton(); - button34.setType(MenuButtonType.VIEW); - button34.setName("获取用户信息"); - - ServletRequestAttributes servletRequestAttributes = - (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - if (servletRequestAttributes != null) { - HttpServletRequest request = servletRequestAttributes.getRequest(); - URL requestURL = new URL(request.getRequestURL().toString()); - String url = this.wxService.switchoverTo(appId).oauth2buildAuthorizationUrl( - String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appId), - WxConsts.OAuth2Scope.SNSAPI_USERINFO, null); - button34.setUrl(url); - } +// WxMenuButton button34 = new WxMenuButton(); +// button34.setType(MenuButtonType.VIEW); +// button34.setName("获取用户信息"); +// +// ServletRequestAttributes servletRequestAttributes = +// (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); +// if (servletRequestAttributes != null) { +// HttpServletRequest request = servletRequestAttributes.getRequest(); +// URL requestURL = new URL(request.getRequestURL().toString()); +// String url = this.wxService.switchoverTo(appId).oauth2buildAuthorizationUrl( +// String.format("%s://%s/wx/redirect/%s/greet", requestURL.getProtocol(), requestURL.getHost(), appId), +// WxConsts.OAuth2Scope.SNSAPI_USERINFO, null); +// button34.setUrl(url); +// } button3.getSubButtons().add(button31); - button3.getSubButtons().add(button32); - button3.getSubButtons().add(button33); - button3.getSubButtons().add(button34); +// button3.getSubButtons().add(button34); this.wxService.switchover(appId); return this.wxService.getMenuService().menuCreate(menu); diff --git a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java index f2f6094..e5c3c4d 100644 --- a/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java +++ b/src/main/java/com/rymcu/vertical/wx/mp/controller/WxoAuthController.java @@ -1,5 +1,6 @@ package com.rymcu.vertical.wx.mp.controller; +import com.rymcu.vertical.service.WxUserService; import com.rymcu.vertical.util.ContextHolderUtils; import me.chanjar.weixin.common.api.WxConsts; import me.chanjar.weixin.common.bean.WxJsapiSignature; @@ -23,6 +24,8 @@ public class WxoAuthController { @Resource private WxMpService wxMpService; + @Resource + private WxUserService wxUserService; @Value("${resource.domain}") private String domain; @@ -56,6 +59,7 @@ public class WxoAuthController { } WxMpUser wxMpUser =wxMpService.getUserService().userInfo(oAuth2AccessToken.getOpenId()); + wxUserService.saveUser(wxMpUser,appId); ContextHolderUtils.getSession2().setAttribute("wxUser", wxMpUser); return "redirect:" + redirectUrl; } diff --git a/src/main/java/mapper/SpecialDayMapper.xml b/src/main/java/mapper/SpecialDayMapper.xml new file mode 100644 index 0000000..d4977b7 --- /dev/null +++ b/src/main/java/mapper/SpecialDayMapper.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/mapper/WxUserMapper.xml b/src/main/java/mapper/WxUserMapper.xml new file mode 100644 index 0000000..228a592 --- /dev/null +++ b/src/main/java/mapper/WxUserMapper.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 74f5878910af932d226cc05c4a5bef7109c7364c Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 7 Apr 2020 17:32:20 +0800 Subject: [PATCH 03/13] =?UTF-8?q?:sparkles:=20=E4=BD=9C=E5=93=81=E9=9B=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rymcu/vertical/entity/Portfolio.java | 34 +++++++++++++++++++ .../vertical/mapper/PortfolioMapper.java | 10 ++++++ src/main/java/mapper/PortfolioMapper.xml | 13 +++++++ 3 files changed, 57 insertions(+) create mode 100644 src/main/java/com/rymcu/vertical/entity/Portfolio.java create mode 100644 src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java create mode 100644 src/main/java/mapper/PortfolioMapper.xml diff --git a/src/main/java/com/rymcu/vertical/entity/Portfolio.java b/src/main/java/com/rymcu/vertical/entity/Portfolio.java new file mode 100644 index 0000000..5d37ca6 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/entity/Portfolio.java @@ -0,0 +1,34 @@ +package com.rymcu.vertical.entity; + +import lombok.Data; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.util.Date; + +/** + * @author ronger + */ +@Data +@Table(name = "vertical_portfolio") +public class Portfolio { + /** 主键 */ + @Id + @GeneratedValue(generator = "JDBC") + @Column(name = "id") + private Integer idPortfolio; + /** 作品集头像 */ + private String headImgUrl; + /** 作品集名称 */ + private String name; + /** 作品集作者 */ + private Integer portfolioAuthorId; + /** 作品集介绍 */ + private String description; + /** 创建时间 */ + private Date createdTime; + /** 更新时间 */ + private Date updatedTime; +} diff --git a/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java b/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java new file mode 100644 index 0000000..fbe5c7c --- /dev/null +++ b/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java @@ -0,0 +1,10 @@ +package com.rymcu.vertical.mapper; + +import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.entity.Portfolio; + +/** + * @author ronger + */ +public interface PortfolioMapper extends Mapper { +} diff --git a/src/main/java/mapper/PortfolioMapper.xml b/src/main/java/mapper/PortfolioMapper.xml new file mode 100644 index 0000000..54dac69 --- /dev/null +++ b/src/main/java/mapper/PortfolioMapper.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file From 8d431d5835fd2109ccef82c6cf6567c79fd039ce Mon Sep 17 00:00:00 2001 From: x ronger Date: Wed, 8 Apr 2020 00:08:46 +0800 Subject: [PATCH 04/13] =?UTF-8?q?:sparkles:=20=E4=BD=9C=E5=93=81=E9=9B=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vertical/service/PortfolioService.java | 10 ++++++++++ .../service/impl/PortfolioServiceImpl.java | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/main/java/com/rymcu/vertical/service/PortfolioService.java create mode 100644 src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java diff --git a/src/main/java/com/rymcu/vertical/service/PortfolioService.java b/src/main/java/com/rymcu/vertical/service/PortfolioService.java new file mode 100644 index 0000000..6d0b9b0 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/PortfolioService.java @@ -0,0 +1,10 @@ +package com.rymcu.vertical.service; + +import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.entity.Portfolio; + +/** + * @author ronger + */ +public interface PortfolioService extends Service { +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java new file mode 100644 index 0000000..2630d6c --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java @@ -0,0 +1,20 @@ +package com.rymcu.vertical.service.impl; + +import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.entity.Portfolio; +import com.rymcu.vertical.mapper.PortfolioMapper; +import com.rymcu.vertical.service.PortfolioService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * @author ronger + */ +@Service +public class PortfolioServiceImpl extends AbstractService implements PortfolioService { + + @Resource + private PortfolioMapper portfolioMapper; + +} From 686cbb925eeadce062836816406a94a3e3ff3c6b Mon Sep 17 00:00:00 2001 From: ronger Date: Wed, 8 Apr 2020 12:05:02 +0800 Subject: [PATCH 05/13] =?UTF-8?q?:art:=20=E9=80=9A=E8=BF=87=20UserService?= =?UTF-8?q?=20=E7=BB=9F=E4=B8=80=E6=9F=A5=E8=AF=A2=E4=BD=9C=E8=80=85?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/rymcu/vertical/dto/Author.java | 4 ++-- .../java/com/rymcu/vertical/mapper/ArticleMapper.java | 8 -------- src/main/java/com/rymcu/vertical/mapper/UserMapper.java | 7 +++++++ src/main/java/com/rymcu/vertical/service/UserService.java | 8 ++++++++ .../rymcu/vertical/service/impl/ArticleServiceImpl.java | 2 +- .../com/rymcu/vertical/service/impl/UserServiceImpl.java | 6 ++++++ src/main/java/mapper/ArticleMapper.xml | 8 -------- src/main/java/mapper/UserMapper.xml | 8 ++++++++ 8 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/dto/Author.java b/src/main/java/com/rymcu/vertical/dto/Author.java index 1f6a567..9b40ee3 100644 --- a/src/main/java/com/rymcu/vertical/dto/Author.java +++ b/src/main/java/com/rymcu/vertical/dto/Author.java @@ -8,12 +8,12 @@ import lombok.Data; @Data public class Author { - private String idUser; + private Integer idUser; private String userNickname; private String userAvatarURL; - private Integer userArticleCount; + private String userArticleCount; } diff --git a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java index 959525f..4135199 100644 --- a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java @@ -3,7 +3,6 @@ package com.rymcu.vertical.mapper; import com.rymcu.vertical.core.mapper.Mapper; import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.ArticleTagDTO; -import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.ArticleContent; import org.apache.ibatis.annotations.Param; @@ -23,13 +22,6 @@ public interface ArticleMapper extends Mapper
{ */ List selectArticles(@Param("searchText") String searchText, @Param("tag") String tag); - /** - * 根据用户 ID 获取作者信息 - * @param id - * @return - */ - Author selectAuthor(@Param("id") Integer id); - /** * 根据文章 ID 查询文章 * @param id diff --git a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java index 6096eab..ce95279 100644 --- a/src/main/java/com/rymcu/vertical/mapper/UserMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/UserMapper.java @@ -1,6 +1,7 @@ package com.rymcu.vertical.mapper; import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.dto.UserDTO; import com.rymcu.vertical.dto.UserInfoDTO; import com.rymcu.vertical.entity.User; @@ -106,4 +107,10 @@ public interface UserMapper extends Mapper { * @return */ Integer checkNicknameByIdUser(@Param("idUser") Integer idUser, @Param("nickname") String nickname); + /** + * 根据用户 ID 获取作者信息 + * @param id + * @return + */ + Author selectAuthor(@Param("id") Integer id); } \ No newline at end of file diff --git a/src/main/java/com/rymcu/vertical/service/UserService.java b/src/main/java/com/rymcu/vertical/service/UserService.java index e817abc..f2fb61b 100644 --- a/src/main/java/com/rymcu/vertical/service/UserService.java +++ b/src/main/java/com/rymcu/vertical/service/UserService.java @@ -1,6 +1,7 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.dto.UserDTO; import com.rymcu.vertical.dto.UserInfoDTO; import com.rymcu.vertical.entity.User; @@ -100,4 +101,11 @@ public interface UserService extends Service { * @return */ Integer findRoleWeightsByUser(Integer idUser); + + /** + * 查询作者信息 + * @param idUser + * @return + */ + Author selectAuthor(Integer idUser); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 2a077fc..8a6a80e 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -264,7 +264,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } private ArticleDTO genArticle(ArticleDTO article, Integer type) { - Author author = articleMapper.selectAuthor(article.getArticleAuthorId()); + Author author = userService.selectAuthor(article.getArticleAuthorId()); article.setArticleAuthor(author); article.setTimeAgo(Utils.getTimeAgo(article.getUpdatedTime())); List tags = articleMapper.selectTags(article.getIdArticle()); diff --git a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java index 8180eb5..dc8ef02 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/UserServiceImpl.java @@ -2,6 +2,7 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.core.service.redis.RedisService; +import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.dto.TokenUser; import com.rymcu.vertical.dto.UserDTO; import com.rymcu.vertical.dto.UserInfoDTO; @@ -207,4 +208,9 @@ public class UserServiceImpl extends AbstractService implements UserServic public Integer findRoleWeightsByUser(Integer idUser) { return userMapper.selectRoleWeightsByUser(idUser); } + + @Override + public Author selectAuthor(Integer idUser) { + return userMapper.selectAuthor(idUser); + } } diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index f294d88..407ec77 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -38,11 +38,6 @@ - - - - - @@ -78,9 +73,6 @@ - select count(*) from vertical_user where nickname = #{nickname} and id != #{idUser} + \ No newline at end of file From 27db7a83f7357c6e5285073f3bcd7c5cd01cf248 Mon Sep 17 00:00:00 2001 From: ronger Date: Wed, 8 Apr 2020 17:53:32 +0800 Subject: [PATCH 06/13] =?UTF-8?q?:sparkles:=20=E4=BD=9C=E5=93=81=E9=9B=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rymcu/vertical/dto/PortfolioDTO.java | 35 +++++++++++ .../rymcu/vertical/mapper/ArticleMapper.java | 7 +++ .../vertical/mapper/PortfolioMapper.java | 24 ++++++++ .../vertical/service/ArticleService.java | 9 +++ .../vertical/service/PortfolioService.java | 26 +++++++++ .../service/impl/ArticleServiceImpl.java | 9 +++ .../service/impl/PortfolioServiceImpl.java | 58 +++++++++++++++++++ .../vertical/service/impl/TagServiceImpl.java | 2 +- .../web/api/common/CommonApiController.java | 26 +++++++-- .../api/portfolio/PortfolioController.java | 45 ++++++++++++++ .../vertical/web/api/user/UserController.java | 21 +++++++ src/main/java/mapper/ArticleMapper.xml | 4 ++ src/main/java/mapper/PortfolioMapper.xml | 17 ++++++ 13 files changed, 278 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java create mode 100644 src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java diff --git a/src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java b/src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java new file mode 100644 index 0000000..772f46a --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java @@ -0,0 +1,35 @@ +package com.rymcu.vertical.dto; + +import lombok.Data; + +import java.util.Date; + +/** + * @author ronger + */ +@Data +public class PortfolioDTO { + + private Integer idPortfolio; + /** 作品集头像 */ + private String headImgUrl; + /** 作品集作者 */ + private Integer portfolioAuthorId; + /** 作品集作者 */ + private String portfolioAuthorName; + /** 作品集作者头像 */ + private String portfolioAuthorAvatarUrl; + /** 作品集名称 */ + private String name; + /** 作品集介绍 */ + private String description; + /** 更新时间 */ + private Date updatedTime; + /** 过去时长 */ + private String timeAgo; + + private Author portfolioAuthor; + + private Integer articleNumber; + +} diff --git a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java index 4135199..31eed56 100644 --- a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java @@ -111,4 +111,11 @@ public interface ArticleMapper extends Mapper
{ * @return */ Integer deleteUnusedArticleTag(@Param("idArticleTag") Integer idArticleTag); + + /** + * 查询作品集下文章 + * @param idPortfolio + * @return + */ + List selectArticlesByIdPortfolio(@Param("idPortfolio") Integer idPortfolio); } diff --git a/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java b/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java index fbe5c7c..23a6d20 100644 --- a/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java @@ -1,10 +1,34 @@ package com.rymcu.vertical.mapper; import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.dto.PortfolioDTO; import com.rymcu.vertical.entity.Portfolio; +import org.apache.ibatis.annotations.Param; + +import java.util.List; /** * @author ronger */ public interface PortfolioMapper extends Mapper { + /** + * 查询用户作品集 + * @param idUser + * @return + */ + List selectUserPortfoliosByIdUser(@Param("idUser") Integer idUser); + + /** + * 查询作品集 + * @param id + * @return + */ + PortfolioDTO selectPortfolioDTOById(@Param("id") Integer id); + + /** + * 统计作品集下文章数 + * @param idPortfolio + * @return + */ + Integer selectCountArticleNumber(@Param("idPortfolio") Integer idPortfolio); } diff --git a/src/main/java/com/rymcu/vertical/service/ArticleService.java b/src/main/java/com/rymcu/vertical/service/ArticleService.java index 30325a3..2ab3a97 100644 --- a/src/main/java/com/rymcu/vertical/service/ArticleService.java +++ b/src/main/java/com/rymcu/vertical/service/ArticleService.java @@ -78,13 +78,22 @@ public interface ArticleService extends Service
{ /** * 获取分享链接数据 * @param id + * @throws BaseApiException * @return */ Map share(Integer id) throws BaseApiException; /** * 查询草稿文章类别 + * @throws BaseApiException * @return */ List findDrafts() throws BaseApiException; + + /** + * 查询作品集下文章 + * @param idPortfolio + * @return + */ + List findArticlesByIdPortfolio(Integer idPortfolio); } diff --git a/src/main/java/com/rymcu/vertical/service/PortfolioService.java b/src/main/java/com/rymcu/vertical/service/PortfolioService.java index 6d0b9b0..0d516bd 100644 --- a/src/main/java/com/rymcu/vertical/service/PortfolioService.java +++ b/src/main/java/com/rymcu/vertical/service/PortfolioService.java @@ -1,10 +1,36 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.dto.PortfolioDTO; +import com.rymcu.vertical.dto.UserDTO; import com.rymcu.vertical.entity.Portfolio; +import com.rymcu.vertical.web.api.exception.BaseApiException; + +import java.util.List; +import java.util.Map; /** * @author ronger */ public interface PortfolioService extends Service { + + /** + * 查询用户作品集 + * @param userDTO + * @return + */ + List findUserPortfoliosByUser(UserDTO userDTO); + + /** 查询作品集 + * @param id + * @return + */ + PortfolioDTO findPortfolioDTOById(Integer id); + + /** + * 保持/更新作品集 + * @param portfolio + * @return + */ + Portfolio postPortfolio(Portfolio portfolio) throws BaseApiException; } diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 8a6a80e..d3dcc44 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -263,6 +263,15 @@ public class ArticleServiceImpl extends AbstractService
implements Arti return list; } + @Override + public List findArticlesByIdPortfolio(Integer idPortfolio) { + List list = articleMapper.selectArticlesByIdPortfolio(idPortfolio); + list.forEach(article->{ + genArticle(article,0); + }); + return list; + } + private ArticleDTO genArticle(ArticleDTO article, Integer type) { Author author = userService.selectAuthor(article.getArticleAuthorId()); article.setArticleAuthor(author); diff --git a/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java index 2630d6c..51db999 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java @@ -1,12 +1,23 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.dto.Author; +import com.rymcu.vertical.dto.PortfolioDTO; +import com.rymcu.vertical.dto.UserDTO; import com.rymcu.vertical.entity.Portfolio; +import com.rymcu.vertical.entity.User; import com.rymcu.vertical.mapper.PortfolioMapper; import com.rymcu.vertical.service.PortfolioService; +import com.rymcu.vertical.service.UserService; +import com.rymcu.vertical.util.UserUtils; +import com.rymcu.vertical.web.api.exception.BaseApiException; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * @author ronger @@ -16,5 +27,52 @@ public class PortfolioServiceImpl extends AbstractService implements @Resource private PortfolioMapper portfolioMapper; + @Resource + private UserService userService; + @Override + public List findUserPortfoliosByUser(UserDTO userDTO) { + List list = portfolioMapper.selectUserPortfoliosByIdUser(userDTO.getIdUser()); + Author author = new Author(); + author.setIdUser(userDTO.getIdUser()); + author.setUserAvatarURL(userDTO.getAvatarUrl()); + author.setUserNickname(userDTO.getNickname()); + list.forEach(portfolioDTO -> { + genPortfolioAuthor(portfolioDTO,author); + }); + return list; + } + + @Override + public PortfolioDTO findPortfolioDTOById(Integer id) { + PortfolioDTO portfolio = portfolioMapper.selectPortfolioDTOById(id); + Author author = userService.selectAuthor(portfolio.getPortfolioAuthorId()); + genPortfolioAuthor(portfolio,author); + Integer articleNumber = portfolioMapper.selectCountArticleNumber(portfolio.getIdPortfolio()); + portfolio.setArticleNumber(articleNumber); + return portfolio; + } + + @Override + public Portfolio postPortfolio(Portfolio portfolio) throws BaseApiException { + User user = UserUtils.getWxCurrentUser(); + if (portfolio.getIdPortfolio() == null || portfolio.getIdPortfolio() == 0) { + portfolio.setPortfolioAuthorId(user.getIdUser()); + portfolio.setCreatedTime(new Date()); + portfolio.setUpdatedTime(portfolio.getCreatedTime()); + portfolioMapper.insertSelective(portfolio); + } else { + portfolio.setUpdatedTime(new Date()); + portfolioMapper.updateByPrimaryKeySelective(portfolio); + } + return portfolio; + } + + private PortfolioDTO genPortfolioAuthor(PortfolioDTO portfolioDTO, Author author) { + portfolioDTO.setPortfolioAuthorAvatarUrl(author.getUserAvatarURL()); + portfolioDTO.setPortfolioAuthorName(author.getUserNickname()); + portfolioDTO.setPortfolioAuthorId(author.getIdUser()); + portfolioDTO.setPortfolioAuthor(author); + return portfolioDTO; + } } diff --git a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index 16b25fd..1d0352d 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -107,7 +107,7 @@ public class TagServiceImpl extends AbstractService implements TagService { @Override @Transactional(rollbackFor = Exception.class) public Map saveTag(Tag tag) { - Integer result = 0; + Integer result; Map map = new HashMap(1); if (tag.getIdTag() == null) { diff --git a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java index b26ea25..311fc80 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java @@ -6,13 +6,11 @@ import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.core.result.GlobalResultMessage; import com.rymcu.vertical.core.service.log.annotation.VisitLogger; -import com.rymcu.vertical.dto.ArticleDTO; -import com.rymcu.vertical.dto.ForgetPasswordDTO; -import com.rymcu.vertical.dto.TokenUser; -import com.rymcu.vertical.dto.UserRegisterInfoDTO; +import com.rymcu.vertical.dto.*; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.service.ArticleService; import com.rymcu.vertical.service.JavaMailService; +import com.rymcu.vertical.service.PortfolioService; import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.util.Utils; @@ -38,6 +36,8 @@ public class CommonApiController { private UserService userService; @Resource private ArticleService articleService; + @Resource + private PortfolioService portfolioService; @ApiOperation(value = "获取邮件验证码") @GetMapping("/get-email-code") @@ -120,4 +120,22 @@ public class CommonApiController { Map map = userService.forgetPassword(forgetPassword.getCode(), forgetPassword.getPassword()); return GlobalResultGenerator.genSuccessResult(map); } + + @GetMapping("/portfolio/{id}") + @VisitLogger + public GlobalResult> portfolio(@PathVariable Integer id){ + PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(id); + Map map = new HashMap<>(1); + map.put("portfolio", portfolioDTO); + return GlobalResultGenerator.genSuccessResult(map); + } + + @GetMapping("/portfolio/{id}/articles") + public GlobalResult articles(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows, @PathVariable Integer id) { + PageHelper.startPage(page, rows); + List list = articleService.findArticlesByIdPortfolio(id); + PageInfo pageInfo = new PageInfo(list); + Map map = Utils.getArticlesGlobalResult(pageInfo); + return GlobalResultGenerator.genSuccessResult(map); + } } diff --git a/src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java b/src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java new file mode 100644 index 0000000..10cbfc7 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java @@ -0,0 +1,45 @@ +package com.rymcu.vertical.web.api.portfolio; + +import com.rymcu.vertical.core.result.GlobalResult; +import com.rymcu.vertical.core.result.GlobalResultGenerator; +import com.rymcu.vertical.dto.PortfolioDTO; +import com.rymcu.vertical.entity.Portfolio; +import com.rymcu.vertical.service.PortfolioService; +import com.rymcu.vertical.web.api.exception.BaseApiException; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/portfolio") +public class PortfolioController { + + @Resource + private PortfolioService portfolioService; + + @GetMapping("/detail/{id}") + public GlobalResult detail(@PathVariable Integer id) { + PortfolioDTO portfolio = portfolioService.findPortfolioDTOById(id); + Map map = new HashMap<>(1); + map.put("portfolio", portfolio); + return GlobalResultGenerator.genSuccessResult(map); + } + + @PostMapping("/post") + public GlobalResult add(@RequestBody Portfolio portfolio) throws BaseApiException { + portfolio = portfolioService.postPortfolio(portfolio); + return GlobalResultGenerator.genSuccessResult(portfolio); + } + + @PutMapping("/post") + public GlobalResult update(@RequestBody Portfolio portfolio) throws BaseApiException { + portfolio = portfolioService.postPortfolio(portfolio); + return GlobalResultGenerator.genSuccessResult(portfolio); + } + +} diff --git a/src/main/java/com/rymcu/vertical/web/api/user/UserController.java b/src/main/java/com/rymcu/vertical/web/api/user/UserController.java index 206578f..10f218c 100644 --- a/src/main/java/com/rymcu/vertical/web/api/user/UserController.java +++ b/src/main/java/com/rymcu/vertical/web/api/user/UserController.java @@ -5,13 +5,16 @@ import com.github.pagehelper.PageInfo; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.PortfolioDTO; import com.rymcu.vertical.dto.UserDTO; import com.rymcu.vertical.service.ArticleService; +import com.rymcu.vertical.service.PortfolioService; import com.rymcu.vertical.service.UserService; import com.rymcu.vertical.util.Utils; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,6 +29,8 @@ public class UserController { private UserService userService; @Resource private ArticleService articleService; + @Resource + private PortfolioService portfolioService; @GetMapping("/{nickname}") public GlobalResult detail(@PathVariable String nickname){ @@ -46,4 +51,20 @@ public class UserController { return GlobalResultGenerator.genSuccessResult(map); } + @GetMapping("/{nickname}/portfolios") + public GlobalResult userPortfolios(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "12") Integer rows, @PathVariable String nickname){ + UserDTO userDTO = userService.findUserDTOByNickname(nickname); + if (userDTO == null){ + return GlobalResultGenerator.genErrorResult("用户不存在!"); + } + PageHelper.startPage(page, rows); + List list = portfolioService.findUserPortfoliosByUser(userDTO); + PageInfo pageInfo = new PageInfo(list); + Map map = new HashMap(2); + map.put("portfolios", list); + Map pagination = Utils.getPagination(pageInfo); + map.put("pagination", pagination); + return GlobalResultGenerator.genSuccessResult(map); + } + } diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index 407ec77..ffeddf3 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -101,4 +101,8 @@ + \ No newline at end of file diff --git a/src/main/java/mapper/PortfolioMapper.xml b/src/main/java/mapper/PortfolioMapper.xml index 54dac69..e78ece3 100644 --- a/src/main/java/mapper/PortfolioMapper.xml +++ b/src/main/java/mapper/PortfolioMapper.xml @@ -10,4 +10,21 @@ + + + + + + + + + + + \ No newline at end of file From 5b84fa9358cf49528e325fd277eaa81677ff60ae Mon Sep 17 00:00:00 2001 From: ronger Date: Thu, 9 Apr 2020 18:50:13 +0800 Subject: [PATCH 07/13] =?UTF-8?q?:art:=20=E5=B1=9E=E6=80=A7=E5=90=8D?= =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rymcu/vertical/dto/PortfolioDTO.java | 4 ++-- .../com/rymcu/vertical/entity/Portfolio.java | 5 +++-- src/main/java/mapper/PortfolioMapper.xml | 16 ++++++++-------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java b/src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java index 772f46a..2b6b486 100644 --- a/src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/PortfolioDTO.java @@ -20,9 +20,9 @@ public class PortfolioDTO { /** 作品集作者头像 */ private String portfolioAuthorAvatarUrl; /** 作品集名称 */ - private String name; + private String portfolioTitle; /** 作品集介绍 */ - private String description; + private String portfolioDescription; /** 更新时间 */ private Date updatedTime; /** 过去时长 */ diff --git a/src/main/java/com/rymcu/vertical/entity/Portfolio.java b/src/main/java/com/rymcu/vertical/entity/Portfolio.java index 5d37ca6..c18877a 100644 --- a/src/main/java/com/rymcu/vertical/entity/Portfolio.java +++ b/src/main/java/com/rymcu/vertical/entity/Portfolio.java @@ -20,13 +20,14 @@ public class Portfolio { @Column(name = "id") private Integer idPortfolio; /** 作品集头像 */ + @Column(name = "portfolio_head_img_url") private String headImgUrl; /** 作品集名称 */ - private String name; + private String portfolioTitle; /** 作品集作者 */ private Integer portfolioAuthorId; /** 作品集介绍 */ - private String description; + private String portfolioDescription; /** 创建时间 */ private Date createdTime; /** 更新时间 */ diff --git a/src/main/java/mapper/PortfolioMapper.xml b/src/main/java/mapper/PortfolioMapper.xml index e78ece3..9ecb377 100644 --- a/src/main/java/mapper/PortfolioMapper.xml +++ b/src/main/java/mapper/PortfolioMapper.xml @@ -3,26 +3,26 @@ - - + + - + - - + + - + + + \ No newline at end of file diff --git a/src/main/java/mapper/PortfolioMapper.xml b/src/main/java/mapper/PortfolioMapper.xml index 9ecb377..6fc9147 100644 --- a/src/main/java/mapper/PortfolioMapper.xml +++ b/src/main/java/mapper/PortfolioMapper.xml @@ -18,6 +18,9 @@ + + insert into vertical_portfolio_article (id_vertical_portfolio, id_vertical_article, sort_no) values (#{idPortfolio}, #{idArticle}, #{maxSortNo}) + @@ -27,4 +30,10 @@ + + \ No newline at end of file From 8b2c7f93b5974665a37b6b9b09922a00ed6c954b Mon Sep 17 00:00:00 2001 From: x ronger Date: Mon, 13 Apr 2020 23:41:48 +0800 Subject: [PATCH 09/13] =?UTF-8?q?:sparkles:=20=E4=BD=9C=E5=93=81=E9=9B=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ---- .../com/rymcu/vertical/dto/ArticleDTO.java | 2 ++ .../vertical/dto/PortfolioArticleDTO.java | 2 ++ .../com/rymcu/vertical/entity/Portfolio.java | 2 ++ .../vertical/mapper/PortfolioMapper.java | 12 ++++++++- .../vertical/service/PortfolioService.java | 10 +++++++- .../service/impl/PortfolioServiceImpl.java | 25 +++++++++++++++++-- .../web/api/common/CommonApiController.java | 2 +- .../api/portfolio/PortfolioController.java | 10 ++++++-- src/main/java/mapper/ArticleMapper.xml | 5 ++-- src/main/java/mapper/PortfolioMapper.xml | 14 ++++++++++- 11 files changed, 74 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 16c8bbc..bd2ce30 100644 --- a/pom.xml +++ b/pom.xml @@ -180,11 +180,6 @@ weixin-java-open 3.7.0 - - com.github.binarywang - java-emoji-converter - 1.0.0 - diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java index 50cedc3..aaa0759 100644 --- a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java @@ -53,4 +53,6 @@ public class ArticleDTO { private List articleComments; private List portfolios; + + private Integer sortNo; } diff --git a/src/main/java/com/rymcu/vertical/dto/PortfolioArticleDTO.java b/src/main/java/com/rymcu/vertical/dto/PortfolioArticleDTO.java index e50c381..adb755a 100644 --- a/src/main/java/com/rymcu/vertical/dto/PortfolioArticleDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/PortfolioArticleDTO.java @@ -18,4 +18,6 @@ public class PortfolioArticleDTO { private String portfolioTitle; + private Integer sortNo; + } diff --git a/src/main/java/com/rymcu/vertical/entity/Portfolio.java b/src/main/java/com/rymcu/vertical/entity/Portfolio.java index c18877a..3cd2482 100644 --- a/src/main/java/com/rymcu/vertical/entity/Portfolio.java +++ b/src/main/java/com/rymcu/vertical/entity/Portfolio.java @@ -28,6 +28,8 @@ public class Portfolio { private Integer portfolioAuthorId; /** 作品集介绍 */ private String portfolioDescription; + /** 作品集介绍 Html */ + private String portfolioDescriptionHtml; /** 创建时间 */ private Date createdTime; /** 更新时间 */ diff --git a/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java b/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java index c259b09..f9a11fb 100644 --- a/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/PortfolioMapper.java @@ -21,9 +21,10 @@ public interface PortfolioMapper extends Mapper { /** * 查询作品集 * @param id + * @param type * @return */ - PortfolioDTO selectPortfolioDTOById(@Param("id") Integer id); + PortfolioDTO selectPortfolioDTOById(@Param("id") Integer id, @Param("type") Integer type); /** * 统计作品集下文章数 @@ -55,4 +56,13 @@ public interface PortfolioMapper extends Mapper { * @return */ Integer selectMaxSortNo(@Param("idPortfolio") Integer idPortfolio); + + /** + * 更新文章排序号 + * @param idPortfolio + * @param idArticle + * @param sortNo + * @return + */ + Integer updateArticleSortNo(@Param("idPortfolio") Integer idPortfolio, @Param("idArticle") Integer idArticle, @Param("sortNo") Integer sortNo); } diff --git a/src/main/java/com/rymcu/vertical/service/PortfolioService.java b/src/main/java/com/rymcu/vertical/service/PortfolioService.java index 9291e5c..ffd5241 100644 --- a/src/main/java/com/rymcu/vertical/service/PortfolioService.java +++ b/src/main/java/com/rymcu/vertical/service/PortfolioService.java @@ -24,9 +24,10 @@ public interface PortfolioService extends Service { /** 查询作品集 * @param idPortfolio + * @param type * @return */ - PortfolioDTO findPortfolioDTOById(Integer idPortfolio); + PortfolioDTO findPortfolioDTOById(Integer idPortfolio, Integer type); /** * 保持/更新作品集 @@ -54,4 +55,11 @@ public interface PortfolioService extends Service { * @return */ Map bindArticle(PortfolioArticleDTO portfolioArticle); + + /** + * 更新文章排序号 + * @param portfolioArticle + * @return + */ + Map updateArticleSortNo(PortfolioArticleDTO portfolioArticle); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java index 3fc3a32..0cb5cb4 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java @@ -48,8 +48,8 @@ public class PortfolioServiceImpl extends AbstractService implements } @Override - public PortfolioDTO findPortfolioDTOById(Integer idPortfolio) { - PortfolioDTO portfolio = portfolioMapper.selectPortfolioDTOById(idPortfolio); + public PortfolioDTO findPortfolioDTOById(Integer idPortfolio, Integer type) { + PortfolioDTO portfolio = portfolioMapper.selectPortfolioDTOById(idPortfolio,type); Author author = userService.selectAuthor(portfolio.getPortfolioAuthorId()); genPortfolioAuthor(portfolio,author); Integer articleNumber = portfolioMapper.selectCountArticleNumber(portfolio.getIdPortfolio()); @@ -106,6 +106,27 @@ public class PortfolioServiceImpl extends AbstractService implements return map; } + @Override + public Map updateArticleSortNo(PortfolioArticleDTO portfolioArticle) { + Map map = new HashMap(1); + if (portfolioArticle.getIdPortfolio() == null || portfolioArticle.getIdPortfolio() == 0) { + map.put("message", "作品集数据异常!"); + } + if (portfolioArticle.getIdArticle() == null || portfolioArticle.getIdArticle() == 0) { + map.put("message", "文章数据异常!"); + } + if (portfolioArticle.getSortNo() == null) { + map.put("message", "排序号不能为空!"); + } + Integer result = portfolioMapper.updateArticleSortNo(portfolioArticle.getIdPortfolio(),portfolioArticle.getIdArticle(),portfolioArticle.getSortNo()); + if (result > 0) { + map.put("message", "更新成功!"); + } else { + map.put("message", "更新失败!"); + } + return map; + } + private PortfolioDTO genPortfolioAuthor(PortfolioDTO portfolioDTO, Author author) { portfolioDTO.setPortfolioAuthorAvatarUrl(author.getUserAvatarURL()); portfolioDTO.setPortfolioAuthorName(author.getUserNickname()); diff --git a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java index 311fc80..722b5bc 100644 --- a/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java +++ b/src/main/java/com/rymcu/vertical/web/api/common/CommonApiController.java @@ -124,7 +124,7 @@ public class CommonApiController { @GetMapping("/portfolio/{id}") @VisitLogger public GlobalResult> portfolio(@PathVariable Integer id){ - PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(id); + PortfolioDTO portfolioDTO = portfolioService.findPortfolioDTOById(id,1); Map map = new HashMap<>(1); map.put("portfolio", portfolioDTO); return GlobalResultGenerator.genSuccessResult(map); diff --git a/src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java b/src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java index 0f0f53a..425b681 100644 --- a/src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java +++ b/src/main/java/com/rymcu/vertical/web/api/portfolio/PortfolioController.java @@ -24,8 +24,8 @@ public class PortfolioController { private PortfolioService portfolioService; @GetMapping("/detail/{id}") - public GlobalResult detail(@PathVariable Integer id) { - PortfolioDTO portfolio = portfolioService.findPortfolioDTOById(id); + public GlobalResult detail(@PathVariable Integer id,@RequestParam(defaultValue = "0") Integer type) { + PortfolioDTO portfolio = portfolioService.findPortfolioDTOById(id, type); Map map = new HashMap<>(1); map.put("portfolio", portfolio); return GlobalResultGenerator.genSuccessResult(map); @@ -55,4 +55,10 @@ public class PortfolioController { return GlobalResultGenerator.genSuccessResult(map); } + @PutMapping("/update-article-sort-no") + public GlobalResult updateArticleSortNo(@RequestBody PortfolioArticleDTO portfolioArticle) { + Map map = portfolioService.updateArticleSortNo(portfolioArticle); + return GlobalResultGenerator.genSuccessResult(map); + } + } diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index f2c10f5..526982c 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -37,6 +37,7 @@ + @@ -108,8 +109,8 @@ select art.*,su.nickname,su.avatar_url from vertical_article art left join vertical_user su on art.article_author_id = su.id where article_status = '1' and art.article_author_id = #{idUser} order by updated_time desc select id, portfolio_head_img_url, portfolio_title, portfolio_author_id, portfolio_description, updated_time from vertical_portfolio where portfolio_author_id = #{idUser} select id, portfolio_head_img_url, portfolio_title, portfolio_author_id, portfolio_description, updated_time from vertical_portfolio where portfolio_author_id = #{idUser} From 23bf6780dff175a241500383c04a6b45a4d5b9c8 Mon Sep 17 00:00:00 2001 From: x ronger Date: Thu, 23 Apr 2020 18:37:01 +0800 Subject: [PATCH 12/13] =?UTF-8?q?:bug:=20=E8=8D=89=E7=A8=BF=E9=A2=84?= =?UTF-8?q?=E8=A7=88=E4=B8=8D=E6=98=BE=E7=A4=BA=E6=AD=A3=E6=96=87=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/rymcu/vertical/service/impl/ArticleServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index c289f46..8e93a8d 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -285,9 +285,9 @@ public class ArticleServiceImpl extends AbstractService
implements Arti List tags = articleMapper.selectTags(article.getIdArticle()); article.setTags(tags); ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); - if (type == 1){ + if (type.equals(1) || type.equals(0)){ article.setArticleContent(articleContent.getArticleContentHtml()); - } else if (type == 2) { + } else if (type.equals(2)) { article.setArticleContent(articleContent.getArticleContent()); } From e1aa873e3313b2656d996fdd82189bddf0f6845d Mon Sep 17 00:00:00 2001 From: ronger Date: Wed, 6 May 2020 19:03:30 +0800 Subject: [PATCH 13/13] =?UTF-8?q?:art:=20=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ArticleServiceImpl.java | 60 ++++++++++--------- src/main/java/mapper/ArticleMapper.xml | 20 +++---- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 8e93a8d..271c40d 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -146,14 +146,6 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } newArticle.setArticleTitle(articleTitle); newArticle.setArticleTags(articleTags); - if(StringUtils.isNotBlank(articleContentHtml)){ - Integer length = articleContentHtml.length(); - if(length > MAX_PREVIEW){ - length = 200; - } - String articlePreviewContent = articleContentHtml.substring(0,length); - newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); - } newArticle.setArticleStatus(article.getArticleStatus()); newArticle.setUpdatedTime(new Date()); articleMapper.updateArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); @@ -175,6 +167,15 @@ public class ArticleServiceImpl extends AbstractService
implements Arti newArticle.setArticlePermalink(domain + "/draft/" + newArticle.getIdArticle()); newArticle.setArticleLink("/draft/" + newArticle.getIdArticle()); } + + if(StringUtils.isNotBlank(articleContentHtml)){ + Integer length = articleContentHtml.length(); + if(length > MAX_PREVIEW){ + length = MAX_PREVIEW; + } + String articlePreviewContent = articleContentHtml.substring(0,length); + newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); + } articleMapper.updateByPrimaryKeySelective(newArticle); map.put("id", newArticle.getIdArticle()); @@ -279,31 +280,36 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } private ArticleDTO genArticle(ArticleDTO article, Integer type) { - Author author = userService.selectAuthor(article.getArticleAuthorId()); + Integer ARTICLE_LIST = 0; + Integer ARTICLE_VIEW = 1; + Integer ARTICLE_EDIT = 2; + Author author = genAuthor(article); article.setArticleAuthor(author); article.setTimeAgo(Utils.getTimeAgo(article.getUpdatedTime())); List tags = articleMapper.selectTags(article.getIdArticle()); article.setTags(tags); - ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); - if (type.equals(1) || type.equals(0)){ - article.setArticleContent(articleContent.getArticleContentHtml()); - } else if (type.equals(2)) { - article.setArticleContent(articleContent.getArticleContent()); - } - - if(StringUtils.isBlank(article.getArticlePreviewContent())){ - Integer length = articleContent.getArticleContentHtml().length(); - if(length > MAX_PREVIEW){ - length = 200; + if (!type.equals(ARTICLE_LIST)) { + ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); + if (type.equals(ARTICLE_VIEW)){ + article.setArticleContent(articleContent.getArticleContentHtml()); + // 获取评论列表数据 + List commentDTOList = commentService.getArticleComments(article.getIdArticle()); + article.setArticleComments(commentDTOList); + // 获取所属作品集列表数据 + List portfolioArticleDTOList = articleMapper.selectPortfolioArticles(article.getIdArticle()); + article.setPortfolios(portfolioArticleDTOList); + } else if (type.equals(ARTICLE_EDIT)) { + article.setArticleContent(articleContent.getArticleContent()); } - String articlePreviewContent = articleContent.getArticleContentHtml().substring(0,length); - article.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); } - List commentDTOList = commentService.getArticleComments(article.getIdArticle()); - article.setArticleComments(commentDTOList); - - List portfolioArticleDTOList = articleMapper.selectPortfolioArticles(article.getIdArticle()); - article.setPortfolios(portfolioArticleDTOList); return article; } + + private Author genAuthor(ArticleDTO article) { + Author author = new Author(); + author.setUserNickname(article.getArticleAuthorName()); + author.setUserAvatarURL(article.getArticleAuthorAvatarUrl()); + author.setIdUser(article.getArticleAuthorId()); + return author; + } } diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index 526982c..3aac306 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -78,10 +78,10 @@ delete from vertical_tag_article where id = #{idArticleTag}