|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|