Ver código fonte

微信登录

yangfeng 1 ano atrás
pai
commit
c9e62711f4

+ 5 - 0
system/system-biz/src/main/java/org/jeecg/modules/system/entity/SysUser.java

@@ -210,4 +210,9 @@ public class SysUser implements Serializable {
      * 讲师介绍
      */
     private String teacherIntroduce;
+
+    /**
+     * 微信openid
+     */
+    private String openId;
 }

+ 4 - 0
system/system-biz/src/main/java/org/jeecg/modules/system/service/ISysUserService.java

@@ -1,6 +1,7 @@
 package org.jeecg.modules.system.service;
 
 import com.alibaba.fastjson.JSONObject;
+import com.alipay.api.domain.AlipayMsaasMediarecogVoiceMediaaudioUploadModel;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -447,4 +448,7 @@ public interface ISysUserService extends IService<SysUser> {
      * @return
      */
     LoginUser getLoginUser(String token);
+
+    SysUser queryByOpenId(String openid);
 }
+

+ 10 - 0
system/system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysUserServiceImpl.java

@@ -104,6 +104,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
     @Lazy
     @Resource
     private RedisUtil redisUtil;
+    private String token;
 
     @Override
     public Result<IPage<SysUser>> queryPageList(HttpServletRequest req, QueryWrapper<SysUser> queryWrapper, Integer pageSize, Integer pageNo) {
@@ -1300,6 +1301,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
      */
     @Override
     public LoginUser getLoginUser(String token) {
+        this.token = token;
         if (StringUtils.isBlank(token)) {
             return null;
         }
@@ -1308,4 +1310,12 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
         return TokenUtils.getLoginUser(username, commonApi, redisUtil);
     }
 
+    @Override
+    public SysUser queryByOpenId(String openid) {
+        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper();
+        //用户所有角色
+        queryWrapper.eq(SysUser::getOpenId, openid);
+        return getOne(queryWrapper);
+    }
+
 }

+ 59 - 7
web/src/main/java/com/ynfy/app/api/v1/controller/ApiAuthController.java

@@ -1,8 +1,15 @@
 package com.ynfy.app.api.v1.controller;
 
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.aliyun.oss.common.utils.StringUtils;
 import com.ynfy.app.api.v1.annoation.IgnoreAuth;
+import com.ynfy.app.api.v1.util.ApiUserUtils;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.constant.CommonConstant;
 import org.jeecg.common.system.util.JwtUtil;
@@ -16,19 +23,15 @@ import org.jeecg.modules.system.model.SysLoginModel;
 import org.jeecg.modules.system.service.ISysDepartService;
 import org.jeecg.modules.system.service.ISysUserService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 @Api(tags = "API登录授权接口")
 @RestController
 @RequestMapping("/api/v1/auth")
+@Slf4j
 public class ApiAuthController {
 
     @Autowired
@@ -158,4 +161,53 @@ public class ApiAuthController {
         redisUtil.set(key, ++val, 10);
     }
 
+    /**
+     * 微信登录
+     */
+    @ApiOperation(value = "微信登录")
+    @IgnoreAuth
+    @GetMapping("loginByWx")
+    public Result<?> loginByWeixin(@RequestParam String code) {
+        //获取openid
+        String requestUrl = ApiUserUtils.getWebAccess(code);//通过自定义工具类组合出小程序需要的登录凭证 code
+        log.info("》》》组合token为:" + requestUrl);
+        JSONObject sessionData = JSON.parseObject(HttpUtil.get(requestUrl, CharsetUtil.CHARSET_UTF_8));
+
+        if (Objects.isNull(sessionData) || StringUtils.isNullOrEmpty(sessionData.getString("openid"))) {
+            return Result.error("登录失败");
+        }
+        String openId = sessionData.getString("openid");
+        SysUser wxUser = sysUserService.queryByOpenId(openId);
+        if (Objects.isNull(wxUser)) {
+            wxUser = new SysUser();
+            wxUser.setCreateTime(new Date());//设置创建时间
+            String salt = oConvertUtils.randomGen(8);
+            wxUser.setSalt(salt);
+            wxUser.setPassword("123456");
+            wxUser.setUsername(openId);
+            wxUser.setRealname("微信用户");
+            String passwordEncode = PasswordUtil.encrypt(wxUser.getUsername(), wxUser.getPassword(), salt);
+            wxUser.setPassword(passwordEncode);
+            wxUser.setStatus(1);
+            wxUser.setDelFlag(CommonConstant.DEL_FLAG_0);
+            wxUser.setOpenId(openId);
+            sysUserService.save(wxUser);
+        }
+        Result<JSONObject> result = new Result<JSONObject>();
+        Map<String, Object> map = new HashMap();
+        map.put("username", wxUser.getUsername());
+        map.put("realname", wxUser.getRealname());
+        map.put("userId", wxUser.getId());
+        String token = JwtUtil.sign(map, wxUser.getPassword());
+
+        JSONObject obj = new JSONObject();
+        obj.put("userInfo", wxUser);
+        //token 信息
+        obj.put("token", token);
+        result.setResult(obj);
+        result.setSuccess(true);
+        result.setCode(200);
+        return result;
+    }
+
 }

+ 59 - 0
web/src/main/java/com/ynfy/app/api/v1/util/ApiUserUtils.java

@@ -0,0 +1,59 @@
+package com.ynfy.app.api.v1.util;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "wx")
+public class ApiUserUtils {
+
+    private static String getCode;
+
+    private static String webAccessTokenhttps;
+
+    private static String appId;
+
+    private static String secret;
+
+    public static String getCode(String appid, String redirectUri, String scope) {
+        return String.format(getCode, appid, redirectUri, scope);
+    }
+
+    public static String getWebAccess(String code) {
+        return String.format(webAccessTokenhttps, appId, secret, code);
+    }
+
+    public static String getGetCode() {
+        return getCode;
+    }
+
+    public void setGetCode(String getCode) {
+        ApiUserUtils.getCode = getCode;
+    }
+
+
+    public static String getWebAccessTokenhttps() {
+        return webAccessTokenhttps;
+    }
+
+    public void setWebAccessTokenhttps(String webAccessTokenhttps) {
+        ApiUserUtils.webAccessTokenhttps = webAccessTokenhttps;
+    }
+
+    public static String getAppId() {
+        return appId;
+    }
+
+    public void setAppId(String appId) {
+        ApiUserUtils.appId = appId;
+    }
+
+    public static String getSecret() {
+        return secret;
+    }
+
+    public void setSecret(String secret) {
+        ApiUserUtils.secret = secret;
+    }
+}
+

+ 9 - 1
web/src/main/resources/application.yml

@@ -3,4 +3,12 @@ spring:
     name: exam-boot
     version: 1.0.0
   profiles:
-    active: dev
+    active: dev
+
+wx:
+  #小程序ID
+  appId: wxa72f873d39700d3e
+  #小程序密钥
+  secret: 87634c43863301b3f3b7f01bf90046f6
+  #获取Web_access_token https的请求地址
+  webAccessTokenhttps: https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code