5 роки тому
батько
коміт
3d650c037a

+ 7 - 0
pom.xml

@@ -87,6 +87,13 @@
             <version>3.8.1</version>
         </dependency>
 
+        <!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations -->
+        <dependency>
+            <groupId>io.swagger</groupId>
+            <artifactId>swagger-annotations</artifactId>
+            <version>1.5.15</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 1 - 1
src/main/java/com/hhsx/minigame/constant/Constant.java

@@ -8,7 +8,7 @@ package com.hhsx.minigame.constant;
  */
 public class Constant {
     public static final String MICROBLOG_APPKEY = "2141531565";//微博的appkey
-    public static final String MICROBLOG_APPSECRET = "f39462b939a56ce6d846bf83e04c20b5";//微博的appsecret
+    public static final String MICROBLOG_ACCESSTOKEN = "f39462b939a56ce6d846bf83e04c20b5";//微博的accesstoken
     public static final String WECHAT_APPID = "2141531565";//微信的appid
     public static final String WECHAT_APPSECRET = "f39462b939a56ce6d846bf83e04c20b5";//微信的appsecret
 

+ 30 - 2
src/main/java/com/hhsx/minigame/controller/NewsController.java

@@ -1,17 +1,45 @@
 package com.hhsx.minigame.controller;
 
 
+import com.hhsx.minigame.entity.vo.NewsVO;
+import com.hhsx.minigame.service.NewsService;
+import com.hhsx.minigame.utils.HttpRespMsg;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.ResponseBody;
 
 /**
  * @author 吴涛涛
  * @since 2019-09-17
  */
-@RestController
+@Controller
 @RequestMapping("/news")
 public class NewsController {
 
+    @Autowired
+    NewsService newsService;
+
+    /**
+     * 添加分享信息
+     *
+     * 传递的参数:
+     * type: 0 -微信 1 -微博
+     * type=0时,所需其他参数: message:寄语 openid:用户openid
+     *
+     * type=1时,所需其他参数:message:寄语 uid:微博身份唯一凭证
+     * @return
+     */
+    @ApiOperation(value = "添加分享信息", notes = "用户登录方法")
+    @RequestMapping("/login")
+    @ResponseBody
+    public HttpRespMsg addUserNews(NewsVO newsVO){
+        HttpRespMsg msg =  newsService.addUserNews(newsVO);
+        return msg;
+
+    }
+
 }
 

+ 6 - 3
src/main/java/com/hhsx/minigame/entity/News.java

@@ -6,6 +6,10 @@ import com.baomidou.mybatisplus.extension.activerecord.Model;
 import com.baomidou.mybatisplus.annotation.TableId;
 import java.time.LocalDateTime;
 import com.baomidou.mybatisplus.annotation.TableField;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+
 import java.io.Serializable;
 
 /**
@@ -43,13 +47,12 @@ public class News extends Model<News> {
      * 创建时间
      */
     @TableField("indate")
-    private LocalDateTime indate;
-
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")    private LocalDateTime indate;
     /**
      * 获取链接的来源,0-微信 1-微博
      */
     @TableField("type")
-    private Integer type;
+    private Integer type = 0;
 
     /**
      * 寄语

+ 19 - 0
src/main/java/com/hhsx/minigame/entity/vo/NewsVO.java

@@ -0,0 +1,19 @@
+package com.hhsx.minigame.entity.vo;
+
+import com.hhsx.minigame.entity.News;
+import lombok.Data;
+
+/**
+ * Author: 吴涛涛 cuiyi@itany.com
+ * Date : 2019 - 09 - 17 11:09
+ * Description:<描述>
+ * Version: 1.0
+ */
+@Data
+public class NewsVO extends News {
+
+    private String openId;//用户openid
+
+    private String uid;//微博身份唯一凭证
+
+}

+ 3 - 0
src/main/java/com/hhsx/minigame/service/NewsService.java

@@ -2,6 +2,8 @@ package com.hhsx.minigame.service;
 
 import com.hhsx.minigame.entity.News;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.hhsx.minigame.entity.vo.NewsVO;
+import com.hhsx.minigame.utils.HttpRespMsg;
 
 /**
  * <p>
@@ -13,4 +15,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface NewsService extends IService<News> {
 
+    HttpRespMsg addUserNews(NewsVO newsVO);
 }

+ 36 - 0
src/main/java/com/hhsx/minigame/service/impl/NewsServiceImpl.java

@@ -1,11 +1,18 @@
 package com.hhsx.minigame.service.impl;
 
 import com.hhsx.minigame.entity.News;
+import com.hhsx.minigame.entity.vo.NewsVO;
 import com.hhsx.minigame.mapper.NewsMapper;
 import com.hhsx.minigame.service.NewsService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.hhsx.minigame.utils.AccessToken;
+import com.hhsx.minigame.utils.HttpRespMsg;
+import com.hhsx.minigame.utils.WechatUserNews;
+import com.hhsx.minigame.utils.WechatAndMicroblogUtil;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+
 /**
  * <p>
  *  服务实现类
@@ -17,4 +24,33 @@ import org.springframework.stereotype.Service;
 @Service
 public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
 
+    @Resource
+    NewsMapper newsMapper;
+
+    @Override
+    public HttpRespMsg addUserNews(NewsVO newsVO) {
+        HttpRespMsg msg = new HttpRespMsg();
+        if(newsVO.getType()==0){
+            //微信授权的链接
+            try {
+                String newAccessToken = AccessToken.getNewAccessToken();
+                WechatUserNews wechatUserNews = WechatAndMicroblogUtil.getWechatUserNews(newsVO.getOpenId(), newAccessToken);
+                News news = new News();
+                news.setNickName(wechatUserNews.getNickname());
+                news.setHeaderPic(wechatUserNews.getHeadimgurl());
+                news.setMessage(newsVO.getMessage());
+                news.setType(newsVO.getType());
+                newsMapper.insert(news);
+                msg.data = news;
+                return msg;
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+
+        }else if(newsVO.getType()==1){
+            //微博授权的链接
+        }
+
+        return null;
+    }
 }

+ 58 - 0
src/main/java/com/hhsx/minigame/utils/MicroblogUsersNews.java

@@ -0,0 +1,58 @@
+package com.hhsx.minigame.utils;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * Author: 吴涛涛 cuiyi@itany.com
+ * Date : 2019 - 09 - 17 11:39
+ * Description:<描述>
+ *     新浪微博用户基本详细返回的实体类封装
+ * Version: 1.0
+ */
+@Data
+public class MicroblogUsersNews {
+
+    private String subscribe;//用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息,1表示用户已关注。
+
+    private String uid;//用户的标识,对当前公众号唯一
+
+    private String nickname;//用户的昵称
+
+    private String sex;//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
+
+    private String city;//用户所在城市
+
+    private String country;//用户所在国家
+
+    private String province;//用户所在省份
+
+    private String language;//用户的语言,简体中文为zh_CN
+
+    private String headimgurl;//用户头像地址(中图),50×50像素
+
+    private String headimgurl_large;//用户头像地址(大图),180×180像素
+
+    private String headimgurl_hd;//用户头像地址(高清),高清头像原图
+
+    private Integer follow;//该用户是否关注access_token中的uid,1:是,0:否
+
+    private Long subscribe_time;//用户关注时间,为时间戳。如果用户曾多次关注,则取最后关注时间
+
+    private String unionid;//只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
+
+    private String remark;//公众号运营者对粉丝的备注,公众号运营者可在微信公众平台用户管理界面对粉丝添加备注
+
+    private String groupid;//用户所在的分组ID(兼容旧的用户分组接口)
+
+    private List<Integer> tagid_list;//用户被打上的标签ID列表
+
+    private String subscribe_scene;//返回用户关注的渠道来源,ADD_SCENE_SEARCH 公众号搜索,ADD_SCENE_ACCOUNT_MIGRATION 公众号迁移,ADD_SCENE_PROFILE_CARD 名片分享,ADD_SCENE_QR_CODE 扫描二维码,ADD_SCENE_PROFILE_ LINK 图文页内名称点击,ADD_SCENE_PROFILE_ITEM 图文页右上角菜单,ADD_SCENE_PAID 支付后关注,ADD_SCENE_OTHERS 其他
+
+    private String qr_scene;//二维码扫码场景(开发者自定义)
+
+    private String qr_scene_str;//二维码扫码场景描述(开发者自定义)
+
+
+}

+ 3 - 5
src/main/java/com/hhsx/minigame/utils/WechatUtil.java

@@ -12,9 +12,9 @@ import java.util.Date;
  * Description:<描述>
  * Version: 1.0
  */
-public class WechatUtil {
+public class WechatAndMicroblogUtil {
 
-    public static WechatUserNews getWechatUserNews(String openId,String lang,String access_token)throws Exception{
+    public static WechatUserNews getWechatUserNews(String openId,String access_token)throws Exception{
         WechatUserNews wechatUserNews = new WechatUserNews();
         String url ="https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openId+"&lang=zh_CN";
         String resp = HttpKit.get(url, true);
@@ -23,11 +23,9 @@ public class WechatUtil {
         //获取值赋值给全局变量
         if (!json.containsKey("errcode")) {
             wechatUserNews.setHeadimgurl(json.getString("headimgurl"));;
-            String newExpiresIn = json.getString("expires_in");
+            wechatUserNews.setNickname(json.getString("nickname"));
 //            System.out.println("获取新access_token==>" + oldAccessToken);
         }
         return wechatUserNews;
     }
-
-
 }

+ 1 - 0
src/main/java/com/hhsx/minigame/utils/WechatUserNews.java

@@ -8,6 +8,7 @@ import java.util.List;
  * Author: 吴涛涛 cuiyi@itany.com
  * Date : 2019 - 09 - 17 10:25
  * Description:<描述>
+ *     微信用户基本详细返回的实体类封装
  * Version: 1.0
  */
 @Data