5 лет назад
Родитель
Сommit
85a89080ff

+ 14 - 0
pom.xml

@@ -94,6 +94,20 @@
             <version>1.5.15</version>
         </dependency>
 
+        <!-- https://mvnrepository.com/artifact/com.belerweb/weibo4j-oauth2 -->
+        <dependency>
+            <groupId>com.belerweb</groupId>
+            <artifactId>weibo4j-oauth2</artifactId>
+            <version>2.1.1-beta2-3</version>
+        </dependency>
+
+        <!-- thymeleaf -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-thymeleaf</artifactId>
+        </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_ACCESSTOKEN = "f39462b939a56ce6d846bf83e04c20b5";//微博的accesstoken
+    public static final String MICROBLOG_APPSECRET = "f39462b939a56ce6d846bf83e04c20b5";//微博的accesstoken
     public static final String WECHAT_APPID = "2141531565";//微信的appid
     public static final String WECHAT_APPSECRET = "f39462b939a56ce6d846bf83e04c20b5";//微信的appsecret
 

+ 28 - 0
src/main/java/com/hhsx/minigame/controller/NewsController.java

@@ -6,11 +6,17 @@ 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.beans.factory.annotation.Value;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+
 /**
  * @author 吴涛涛
  * @since 2019-09-17
@@ -21,6 +27,8 @@ public class NewsController {
 
     @Autowired
     NewsService newsService;
+    @Value("${callbackURL}")
+    private String callbackURL;
 
     /**
      * 添加分享信息
@@ -40,6 +48,26 @@ public class NewsController {
         return msg;
 
     }
+    /**
+     * 微博授权登录
+     *
+     * 传递的参数:
+     * type: 0 -微信 1 -微博
+     * type=0时,所需其他参数: message:寄语 openid:用户openid
+     *
+     * type=1时,所需其他参数:message:寄语 uid:微博身份唯一凭证
+     * @return
+     */
+    @ApiOperation(value = "微博授权登录", notes = "微博授权登录方法")
+    @RequestMapping("/microblogLogin")
+    @ResponseBody
+    public HttpRespMsg microblogLogin(NewsVO newsVO) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
+        HttpRespMsg msg =  newsService.login(newsVO,callbackURL);
+        return msg;
+
+    }
+
+
 
 }
 

+ 91 - 0
src/main/java/com/hhsx/minigame/controller/UserController.java

@@ -0,0 +1,91 @@
+package com.hhsx.minigame.controller;
+
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.hhsx.minigame.constant.Constant;
+import com.hhsx.minigame.entity.User;
+import com.hhsx.minigame.service.UserService;
+import com.hhsx.minigame.utils.HttpKit;
+import com.hhsx.minigame.utils.HttpRespMsg;
+import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringEscapeUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.util.Date;
+
+/**
+ * <p>
+ * 前端控制器
+ * </p>
+ *
+ * @author 吴涛涛
+ * @since 2019-09-18
+ */
+@RestController
+@RequestMapping("/user")
+public class UserController {
+
+    @Autowired
+    private UserService userService;
+
+    /**
+     * 微信授权登录
+     * 参数:
+     * type:授权类型,0-微信,1-微博
+     * code:平台返回的code值
+     *
+     * @return
+     */
+    @ApiOperation("微信/微博网页授权")
+    @RequestMapping(value = "weiXinLogin")
+    @ResponseBody
+    public Object weiXinLogin(@RequestParam String code, Integer type,
+                              HttpServletResponse response) throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
+        HttpRespMsg msg = new HttpRespMsg();
+        if (type == 0) {
+            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Constant.WECHAT_APPID + "&secret=" + Constant.WECHAT_APPSECRET + "&code=" + code + "&grant_type=authorization_code";
+            String resp = HttpKit.get(url, true);
+            resp = StringEscapeUtils.unescapeJava(resp);
+            System.out.println(resp);
+            JSONObject json = (JSONObject) JSON.parse(resp);
+            if (!json.containsKey("errcode")) {
+                String openId = json.getString("openid");
+                User user = new User();
+                user.setType(type);
+                user.setVoucherId(openId);
+                QueryWrapper<User> qw = new QueryWrapper<>();
+                qw.eq("voucher_id", openId).eq("type", type);
+                if (userService.count(qw) == 0) {
+                    userService.save(user);
+                } else {
+                    //列表中已包含当前用户,
+                    user = userService.getOne(qw);
+                }
+                msg.data = user;
+            } else {
+                msg.setError(json.getString("errmsg"));
+            }
+        }else if(type == 1){
+//            String url = "https://api.weibo.com/oauth2/authorize?client_id="+Constant.MICROBLOG_APPKEY +"&response_type=code&redirect_uri="+callbackURL;
+
+        }
+
+        response.setContentType("application/json");
+        response.setCharacterEncoding("UTF-8");
+        return msg;
+    }
+
+}
+

+ 95 - 0
src/main/java/com/hhsx/minigame/entity/User.java

@@ -0,0 +1,95 @@
+package com.hhsx.minigame.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+import com.baomidou.mybatisplus.annotation.TableId;
+import java.time.LocalDateTime;
+import com.baomidou.mybatisplus.annotation.TableField;
+import java.io.Serializable;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author 吴涛涛
+ * @since 2019-09-18
+ */
+@TableName("mini_user")
+public class User extends Model<User> {
+
+    private static final long serialVersionUID=1L;
+
+    /**
+     * 用户表主键
+     */
+    @TableId(value = "id", type = IdType.AUTO)
+    private Integer id;
+
+    /**
+     * 授权类型0-微信 1-微博
+     */
+    @TableField("type")
+    private Integer type;
+
+    /**
+     * 授权id type=0:openid  type=1:uid
+     */
+    @TableField("voucher_id")
+    private String voucherId;
+
+    /**
+     * 授权时间
+     */
+    @TableField("indate")
+    private LocalDateTime indate;
+
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getType() {
+        return type;
+    }
+
+    public void setType(Integer type) {
+        this.type = type;
+    }
+
+    public String getVoucherId() {
+        return voucherId;
+    }
+
+    public void setVoucherId(String voucherId) {
+        this.voucherId = voucherId;
+    }
+
+    public LocalDateTime getIndate() {
+        return indate;
+    }
+
+    public void setIndate(LocalDateTime indate) {
+        this.indate = indate;
+    }
+
+    @Override
+    protected Serializable pkVal() {
+        return this.id;
+    }
+
+    @Override
+    public String toString() {
+        return "User{" +
+        "id=" + id +
+        ", type=" + type +
+        ", voucherId=" + voucherId +
+        ", indate=" + indate +
+        "}";
+    }
+}

+ 16 - 0
src/main/java/com/hhsx/minigame/mapper/UserMapper.java

@@ -0,0 +1,16 @@
+package com.hhsx.minigame.mapper;
+
+import com.hhsx.minigame.entity.User;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author 吴涛涛
+ * @since 2019-09-18
+ */
+public interface UserMapper extends BaseMapper<User> {
+
+}

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

@@ -5,6 +5,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
 import com.hhsx.minigame.entity.vo.NewsVO;
 import com.hhsx.minigame.utils.HttpRespMsg;
 
+import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+
 /**
  * <p>
  *  服务类
@@ -16,4 +21,6 @@ import com.hhsx.minigame.utils.HttpRespMsg;
 public interface NewsService extends IService<News> {
 
     HttpRespMsg addUserNews(NewsVO newsVO);
+
+    HttpRespMsg login(NewsVO newsVO,String callbackURL) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException;
 }

+ 16 - 0
src/main/java/com/hhsx/minigame/service/UserService.java

@@ -0,0 +1,16 @@
+package com.hhsx.minigame.service;
+
+import com.hhsx.minigame.entity.User;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author 吴涛涛
+ * @since 2019-09-18
+ */
+public interface UserService extends IService<User> {
+
+}

+ 22 - 6
src/main/java/com/hhsx/minigame/service/impl/NewsServiceImpl.java

@@ -1,17 +1,22 @@
 package com.hhsx.minigame.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.hhsx.minigame.constant.Constant;
 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 com.hhsx.minigame.utils.*;
+import org.apache.commons.lang3.StringEscapeUtils;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
 
 /**
  * <p>
@@ -33,8 +38,7 @@ public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements Ne
         if(newsVO.getType()==0){
             //微信授权的链接
             try {
-                String newAccessToken = AccessToken.getNewAccessToken();
-                WechatUserNews wechatUserNews = WechatAndMicroblogUtil.getWechatUserNews(newsVO.getOpenId(), newAccessToken);
+                WechatUserNews wechatUserNews = WechatAndMicroblogUtil.getWechatUserNews(newsVO.getOpenId(), AccessToken.getNewAccessToken());
                 News news = new News();
                 news.setNickName(wechatUserNews.getNickname());
                 news.setHeaderPic(wechatUserNews.getHeadimgurl());
@@ -49,8 +53,20 @@ public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements Ne
 
         }else if(newsVO.getType()==1){
             //微博授权的链接
+
+
         }
 
         return null;
     }
+
+    //微博授权登录
+    @Override
+    public HttpRespMsg login(NewsVO newsVO,String callbackURL) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
+        String authorizationURL = "https://api.weibo.com/oauth2/authorize?client_id="+Constant.MICROBLOG_APPKEY +"&response_type=code&redirect_uri="+callbackURL;
+        String resp = HttpKit.get(authorizationURL, true);
+        resp = StringEscapeUtils.unescapeJava(resp);
+        JSONObject json = (JSONObject) JSON.parse(resp);
+        return null;
+    }
 }

+ 20 - 0
src/main/java/com/hhsx/minigame/service/impl/UserServiceImpl.java

@@ -0,0 +1,20 @@
+package com.hhsx.minigame.service.impl;
+
+import com.hhsx.minigame.entity.User;
+import com.hhsx.minigame.mapper.UserMapper;
+import com.hhsx.minigame.service.UserService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author 吴涛涛
+ * @since 2019-09-18
+ */
+@Service
+public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
+
+}

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

@@ -205,7 +205,7 @@ public class CodeGenerator {
         //若想要生成的实体类继承某个Controller,则可打开下面注释。写上需要继承的Controller的位置即可
 //        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
         //此处user是表名,多个英文逗号分割
-        strategy.setInclude("mini_news");
+        strategy.setInclude("mini_user");
 //        strategy.setExclude();//数据库表全生成
 //        strategy.setInclude(scanner("user").split(","));//表名,多个英文逗号分割
         strategy.setControllerMappingHyphenStyle(true);

+ 14 - 0
src/main/java/com/hhsx/minigame/utils/Test.java

@@ -0,0 +1,14 @@
+package com.hhsx.minigame.utils;
+
+/**
+ * Author: 吴涛涛 cuiyi@itany.com
+ * Date : 2019 - 09 - 17 16:31
+ * Description:<描述>
+ * Version: 1.0
+ */
+public class Test {
+
+    public static void main(String[] args) {
+
+    }
+}

+ 2 - 2
src/main/resources/application.properties

@@ -35,8 +35,8 @@ spring.thymeleaf.prefix=classpath:/static/
 #spring.redis.host=localhost
 #spring.redis.port=6379
 ######################################################################################################
-## 文件上传路径
-#upload.path=D:/mould/upload/
+# 微博授权登录发放code的回调地址
+callbackURL=http://www.mini.game/index.html
 #######################################################################################################
 ## 文件下载路径
 #download.path=D:/mould/download/

+ 18 - 0
src/main/resources/mapper/UserMapper.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.hhsx.minigame.mapper.UserMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.hhsx.minigame.entity.User">
+        <id column="id" property="id" />
+        <result column="type" property="type" />
+        <result column="voucher_id" property="voucherId" />
+        <result column="indate" property="indate" />
+    </resultMap>
+
+    <!-- 通用查询结果列 -->
+    <sql id="Base_Column_List">
+        id, type, voucher_id, indate
+    </sql>
+
+</mapper>