1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

微信小程序 提审时隐藏功能;微信小程序添加微信快捷登录、随便看看(登的user用户)

This commit is contained in:
2023-04-17 02:36:04 +08:00
parent 62ed92029c
commit 0238251ab0
29 changed files with 1205 additions and 624 deletions

View File

@@ -82,6 +82,16 @@
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- OpenFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- 分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>

View File

@@ -3,11 +3,13 @@ package com.cxyxiaomo.epp.user;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
// 启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserProvider {
public static void main(String[] args) {
SpringApplication.run(UserProvider.class, args);

View File

@@ -15,6 +15,7 @@ import com.cxyxiaomo.epp.common.pojo.Role;
import com.cxyxiaomo.epp.common.pojo.User;
import com.cxyxiaomo.epp.common.response.Res;
import com.cxyxiaomo.epp.common.vo.UserVO;
import com.cxyxiaomo.epp.user.rpc.WeChatTokenServiceFeign;
import com.cxyxiaomo.epp.user.service.RoleService;
import com.cxyxiaomo.epp.user.service.UserService;
import com.github.pagehelper.PageHelper;
@@ -25,10 +26,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.*;
@Controller
@RequestMapping("/user")
@@ -40,6 +38,9 @@ public class UserController {
@Resource
private RoleService roleService;
@Resource
private WeChatTokenServiceFeign weChatTokenService;
/**
* 用户登录
*
@@ -50,6 +51,12 @@ public class UserController {
@PostMapping("/login")
@ResponseBody
public Res login(@RequestParam("username") String username, @RequestParam("password") String password) {
// 微信小程序 随便看看
if (Objects.equals(username, "#fastLogin#") && Objects.equals(password, "#fastLogin#")) {
username = "user";
password = "user";
}
User user = userService.getUserByUsername(username);
if (user != null) {
String passwordHash = DigestUtils.sha512Hex(password);
@@ -65,6 +72,49 @@ public class UserController {
}
}
/**
* 微信小程序用户登录
*
* @param code
* @return
*/
@PostMapping("/wxlogin")
@ResponseBody
public Res wxlogin(@RequestParam("code") String code) {
// 微信小程序 登录页面 随便看看
if (Objects.isNull(code)) {
return Res.error("参数错误");
}
String jsonString = weChatTokenService.getOpenIdFromApi(code);
JSONObject jsonObject = JSONObject.parseObject(jsonString);
String openId = jsonObject.getString("data");
if (Objects.isNull(openId) || "".equals(openId)) {
return Res.error("微信接口异常,请重试");
}
User user = userService.getUserByWxcode(openId);
if (user == null) {
// 创建一个新用户
user = new User();
user.setId(null);
user.setUsername(UUID.randomUUID().toString().replace("-", "").substring(0, 10));
user.setPassword(UUID.randomUUID().toString());
user.setRealname("微信用户" + user.getUsername().substring(0, 5));
user.setRoleId(3);
user.setPermission("1");
user.setWxcode(openId);
userService.addUser(user);
// 添加之后 user.getId() 可以获取到用户id
// System.out.println("userId: " + user.getId());
}
// 完成登录
HashMap<String, Object> map = new HashMap<>();
map.put("userInfo", UserVO.convertFrom(user));
return Res.success(map);
}
/**
* 通过用户ID获取用户信息
*

View File

@@ -19,6 +19,8 @@ public interface UserDao {
User getUserByUsername(String username);
User getUserByWxcode(String wxcode);
public List<User> getUserList(UserVO userVO);
public boolean deleteUserById(Integer userId);

View File

@@ -0,0 +1,16 @@
package com.cxyxiaomo.epp.user.rpc;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("microservice-provider-access")
public interface WeChatTokenServiceFeign {
/**
* 返回值、参数要对应,方法名随意
* 在 OpenFeign 中方法参数前如果没有注解,默认添加 @RequestBody 注解,最多只能存在一个不带注解的参数
* 普通表单参数必须添加 @Requestparam 注解。如果变量名和参数名称对应可以不写name
*/
@GetMapping("/access/wechat/rpc/getOpenIdFromApi")
String getOpenIdFromApi(@RequestParam("code") String code);
}

View File

@@ -18,6 +18,10 @@ public class UserService {
return userDao.getUserByUsername(username);
}
public User getUserByWxcode(String wxcode) {
return userDao.getUserByWxcode(wxcode);
}
public User getUserById(Integer id) {
return userDao.getUserById(id);
}

View File

@@ -3,11 +3,11 @@
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cxyxiaomo.epp.user.dao.UserDao">
<insert id="addUser" parameterType="com.cxyxiaomo.epp.common.pojo.User">
<insert id="addUser" parameterType="com.cxyxiaomo.epp.common.pojo.User" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO user (username, `password`, realname, id_number, phone_number, role_id, building_id, doorplate,
permission, permission_time)
permission, permission_time, wx_code)
VALUES (#{username}, #{password}, #{realname}, #{idNumber}, #{phoneNumber}, #{roleId}, #{buildingId},
#{doorplate}, #{permission}, #{permissionTime})
#{doorplate}, #{permission}, #{permissionTime}, #{wxcode})
</insert>
<update id="updateUser" parameterType="com.cxyxiaomo.epp.common.pojo.User">
UPDATE user
@@ -42,6 +42,9 @@
<if test="permissionTime != null">
permission_time = #{permissionTime},
</if>
<if test="wxcode != null and wxcode != ''">
wx_code = #{doorplate},
</if>
</set>
WHERE id = #{id}
</update>
@@ -55,6 +58,11 @@
from user
where username = #{username}
</select>
<select id="getUserByWxcode" resultType="com.cxyxiaomo.epp.common.pojo.User">
select *
from user
where wx_code = #{wxcode}
</select>
<select id="getUserList" resultType="com.cxyxiaomo.epp.common.pojo.User">
select *
from user