小程序登录接口
This commit is contained in:
		
							
								
								
									
										5
									
								
								guigu-ssyx-parent/service/service-user/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								guigu-ssyx-parent/service/service-user/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
FROM openjdk:8-jdk-alpine
 | 
			
		||||
LABEL authors="yovinchen"
 | 
			
		||||
VOLUME /tmp
 | 
			
		||||
ADD ./target/service-user.jar service-user.jar
 | 
			
		||||
ENTRYPOINT ["java","-jar","/service-user.jar", "&"]
 | 
			
		||||
							
								
								
									
										20
									
								
								guigu-ssyx-parent/service/service-user/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								guigu-ssyx-parent/service/service-user/pom.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
<?xml version="1.0" encoding="UTF-8"?>
 | 
			
		||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
			
		||||
         xmlns="http://maven.apache.org/POM/4.0.0"
 | 
			
		||||
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 | 
			
		||||
    <modelVersion>4.0.0</modelVersion>
 | 
			
		||||
    <parent>
 | 
			
		||||
        <groupId>com.atguigu</groupId>
 | 
			
		||||
        <artifactId>service</artifactId>
 | 
			
		||||
        <version>1.0-SNAPSHOT</version>
 | 
			
		||||
    </parent>
 | 
			
		||||
 | 
			
		||||
    <artifactId>service-user</artifactId>
 | 
			
		||||
 | 
			
		||||
    <properties>
 | 
			
		||||
        <maven.compiler.source>8</maven.compiler.source>
 | 
			
		||||
        <maven.compiler.target>8</maven.compiler.target>
 | 
			
		||||
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 | 
			
		||||
    </properties>
 | 
			
		||||
 | 
			
		||||
</project>
 | 
			
		||||
@@ -0,0 +1,22 @@
 | 
			
		||||
package com.atguigu.ssyx;
 | 
			
		||||
 | 
			
		||||
import org.springframework.boot.SpringApplication;
 | 
			
		||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
 | 
			
		||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 | 
			
		||||
import org.springframework.cloud.openfeign.EnableFeignClients;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: ServiceUserApplication
 | 
			
		||||
 * Package: com.atguigu.ssyx
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 15:39
 | 
			
		||||
 */
 | 
			
		||||
@SpringBootApplication
 | 
			
		||||
@EnableDiscoveryClient
 | 
			
		||||
@EnableFeignClients
 | 
			
		||||
public class ServiceUserApplication {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        SpringApplication.run(ServiceUserApplication.class, args);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,41 @@
 | 
			
		||||
package com.atguigu.ssyx.user.controller;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.common.result.Result;
 | 
			
		||||
import com.atguigu.ssyx.model.user.User;
 | 
			
		||||
import com.atguigu.ssyx.user.service.LoginWxService;
 | 
			
		||||
import com.atguigu.ssyx.user.service.UserService;
 | 
			
		||||
import io.swagger.annotations.ApiOperation;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: WeixinApiController
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.controller
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:23
 | 
			
		||||
 */
 | 
			
		||||
@RestController
 | 
			
		||||
@RequestMapping("/api/user/weixin")
 | 
			
		||||
public class WeixinApiController {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private LoginWxService loginWxService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserService userService;
 | 
			
		||||
 | 
			
		||||
    //用户微信授权登录
 | 
			
		||||
    @ApiOperation(value = "微信登录获取openid(小程序)")
 | 
			
		||||
    @GetMapping("/wxLogin/{code}")
 | 
			
		||||
    public Result loginWx(@PathVariable String code) {
 | 
			
		||||
        return Result.ok(loginWxService.loginWx(code));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/auth/updateUser")
 | 
			
		||||
    @ApiOperation(value = "更新用户昵称与头像")
 | 
			
		||||
    public Result updateUser(@RequestBody User user) {
 | 
			
		||||
        userService.updateUser(user);
 | 
			
		||||
        return Result.ok(null);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,17 @@
 | 
			
		||||
package com.atguigu.ssyx.user.mapper;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.model.user.Leader;
 | 
			
		||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 | 
			
		||||
import org.springframework.stereotype.Repository;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: LeaderMapper
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.mapper
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:57
 | 
			
		||||
 */
 | 
			
		||||
@Repository
 | 
			
		||||
public interface LeaderMapper extends BaseMapper<Leader> {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,17 @@
 | 
			
		||||
package com.atguigu.ssyx.user.mapper;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.model.user.UserDelivery;
 | 
			
		||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 | 
			
		||||
import org.springframework.stereotype.Repository;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: UserDeliveryMapper
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.mapper
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:57
 | 
			
		||||
 */
 | 
			
		||||
@Repository
 | 
			
		||||
public interface UserDeliveryMapper extends BaseMapper<UserDelivery> {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -0,0 +1,17 @@
 | 
			
		||||
package com.atguigu.ssyx.user.mapper;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.model.user.User;
 | 
			
		||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 | 
			
		||||
import org.springframework.stereotype.Repository;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: UserMapper
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.mapper
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:50
 | 
			
		||||
 */
 | 
			
		||||
@Repository
 | 
			
		||||
public interface UserMapper extends BaseMapper<User> {
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,100 @@
 | 
			
		||||
package com.atguigu.ssyx.user.service;
 | 
			
		||||
 | 
			
		||||
import com.alibaba.fastjson.JSONObject;
 | 
			
		||||
import com.atguigu.ssyx.common.constant.RedisConst;
 | 
			
		||||
import com.atguigu.ssyx.common.exception.SsyxException;
 | 
			
		||||
import com.atguigu.ssyx.common.result.ResultCodeEnum;
 | 
			
		||||
import com.atguigu.ssyx.common.utils.JwtHelper;
 | 
			
		||||
import com.atguigu.ssyx.enums.UserType;
 | 
			
		||||
import com.atguigu.ssyx.model.user.User;
 | 
			
		||||
import com.atguigu.ssyx.user.utils.ConstantPropertiesUtil;
 | 
			
		||||
import com.atguigu.ssyx.user.utils.HttpClientUtils;
 | 
			
		||||
import com.atguigu.ssyx.vo.user.LeaderAddressVo;
 | 
			
		||||
import com.atguigu.ssyx.vo.user.UserLoginVo;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.data.redis.core.RedisTemplate;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: LoginWxService
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.service
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 17:08
 | 
			
		||||
 */
 | 
			
		||||
@Service
 | 
			
		||||
public class LoginWxService {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserService userService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RedisTemplate redisTemplate;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public Map<String, Object> loginWx(String code) {
 | 
			
		||||
        //1 得到微信返回code临时票据值
 | 
			
		||||
        //2 拿着code + 小程序id + 小程序秘钥 请求微信接口服务
 | 
			
		||||
        //// 使用HttpClient工具请求
 | 
			
		||||
        //小程序id
 | 
			
		||||
        String wxOpenAppId = ConstantPropertiesUtil.WX_OPEN_APP_ID;
 | 
			
		||||
        //小程序秘钥
 | 
			
		||||
        String wxOpenAppSecret = ConstantPropertiesUtil.WX_OPEN_APP_SECRET;
 | 
			
		||||
        //get请求
 | 
			
		||||
        //拼接请求地址+参数
 | 
			
		||||
        /// 地址?name=value&name1=value1
 | 
			
		||||
        String url = "https://api.weixin.qq.com/sns/jscode2session" + "?appid=%s" + "&secret=%s" + "&js_code=%s" + "&grant_type=authorization_code";
 | 
			
		||||
        String tokenUrl = String.format(url, wxOpenAppId, wxOpenAppSecret, code);
 | 
			
		||||
        //HttpClient发送get请求
 | 
			
		||||
        String result = null;
 | 
			
		||||
        try {
 | 
			
		||||
            result = HttpClientUtils.get(tokenUrl);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw new SsyxException(ResultCodeEnum.FETCH_ACCESSTOKEN_FAILD);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //3 请求微信接口服务,返回两个值 session_key 和 openid
 | 
			
		||||
        //// openId是你微信唯一标识
 | 
			
		||||
        JSONObject jsonObject = JSONObject.parseObject(result);
 | 
			
		||||
        String session_key = jsonObject.getString("session_key");
 | 
			
		||||
        String openid = jsonObject.getString("openid");
 | 
			
		||||
 | 
			
		||||
        //4 添加微信用户信息到数据库里面
 | 
			
		||||
        //// 操作user表
 | 
			
		||||
        //// 判断是否是第一次使用微信授权登录:如何判断?openId
 | 
			
		||||
        User user = userService.getUserByOpenId(openid);
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            user = new User();
 | 
			
		||||
            user.setOpenId(openid);
 | 
			
		||||
            user.setNickName(openid);
 | 
			
		||||
            user.setPhotoUrl("");
 | 
			
		||||
            user.setUserType(UserType.USER);
 | 
			
		||||
            user.setIsNew(0);
 | 
			
		||||
            userService.save(user);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //5 根据userId查询提货点和团长信息
 | 
			
		||||
        ////提货点  user表  user_delivery表
 | 
			
		||||
        ////团长    leader表
 | 
			
		||||
        LeaderAddressVo leaderAddressVo = userService.getLeaderAddressByUserId(user.getId());
 | 
			
		||||
 | 
			
		||||
        //6 使用JWT工具根据userId和userName生成token字符串
 | 
			
		||||
        String token = JwtHelper.createToken(user.getId(), user.getNickName());
 | 
			
		||||
 | 
			
		||||
        //7 获取当前登录用户信息,放到Redis里面,设置有效时间
 | 
			
		||||
        UserLoginVo userLoginVo = userService.getUserLoginVo(user.getId());
 | 
			
		||||
        redisTemplate.opsForValue()
 | 
			
		||||
                     .set(RedisConst.USER_LOGIN_KEY_PREFIX + user.getId(), userLoginVo, RedisConst.USERKEY_TIMEOUT, TimeUnit.DAYS);
 | 
			
		||||
 | 
			
		||||
        //8 需要数据封装到map返回
 | 
			
		||||
        Map<String, Object> map = new HashMap<>();
 | 
			
		||||
        map.put("user", user);
 | 
			
		||||
        map.put("token", token);
 | 
			
		||||
        map.put("leaderAddressVo", leaderAddressVo);
 | 
			
		||||
        return map;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,46 @@
 | 
			
		||||
package com.atguigu.ssyx.user.service;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.model.user.User;
 | 
			
		||||
import com.atguigu.ssyx.vo.user.LeaderAddressVo;
 | 
			
		||||
import com.atguigu.ssyx.vo.user.UserLoginVo;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.service.IService;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: UserService
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.service
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:33
 | 
			
		||||
 */
 | 
			
		||||
public interface UserService extends IService<User> {
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据微信openid获取用户信息
 | 
			
		||||
     *
 | 
			
		||||
     * @param openid
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    User getUserByOpenId(String openid);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 根据userId查询提货点和团长信息
 | 
			
		||||
     *
 | 
			
		||||
     * @param id
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    LeaderAddressVo getLeaderAddressByUserId(Long id);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 获取当前登录用户信息
 | 
			
		||||
     *
 | 
			
		||||
     * @param id
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    UserLoginVo getUserLoginVo(Long id);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 更新用户昵称与头像
 | 
			
		||||
     *
 | 
			
		||||
     * @param user
 | 
			
		||||
     */
 | 
			
		||||
    void updateUser(User user);
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,97 @@
 | 
			
		||||
package com.atguigu.ssyx.user.service.impl;
 | 
			
		||||
 | 
			
		||||
import com.atguigu.ssyx.common.auth.AuthContextHolder;
 | 
			
		||||
import com.atguigu.ssyx.model.user.Leader;
 | 
			
		||||
import com.atguigu.ssyx.model.user.User;
 | 
			
		||||
import com.atguigu.ssyx.model.user.UserDelivery;
 | 
			
		||||
import com.atguigu.ssyx.user.mapper.LeaderMapper;
 | 
			
		||||
import com.atguigu.ssyx.user.mapper.UserDeliveryMapper;
 | 
			
		||||
import com.atguigu.ssyx.user.mapper.UserMapper;
 | 
			
		||||
import com.atguigu.ssyx.user.service.UserService;
 | 
			
		||||
import com.atguigu.ssyx.vo.user.LeaderAddressVo;
 | 
			
		||||
import com.atguigu.ssyx.vo.user.UserLoginVo;
 | 
			
		||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
			
		||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
			
		||||
import org.springframework.beans.BeanUtils;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: UserServiceImpl
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.service.impl
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:34
 | 
			
		||||
 */
 | 
			
		||||
@Service
 | 
			
		||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserDeliveryMapper userDeliveryMapper;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private LeaderMapper leaderMapper;
 | 
			
		||||
 | 
			
		||||
    //// 判断是否是第一次使用微信授权登录:如何判断?openId
 | 
			
		||||
    @Override
 | 
			
		||||
    public User getUserByOpenId(String openid) {
 | 
			
		||||
        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openid));
 | 
			
		||||
        return user;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //5 根据userId查询提货点和团长信息
 | 
			
		||||
    @Override
 | 
			
		||||
    public LeaderAddressVo getLeaderAddressByUserId(Long userId) {
 | 
			
		||||
        //根据userId查询用户默认的团长id
 | 
			
		||||
        UserDelivery userDelivery = userDeliveryMapper.selectOne(new LambdaQueryWrapper<UserDelivery>().eq(UserDelivery::getUserId, userId)
 | 
			
		||||
                                                                                                       .eq(UserDelivery::getIsDefault, 1));
 | 
			
		||||
        if (userDelivery == null) {
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
        //拿着上面查询团长id查询leader表查询团长其他信息
 | 
			
		||||
        Leader leader = leaderMapper.selectById(userDelivery.getLeaderId());
 | 
			
		||||
        //封装数据到LeaderAddressVo
 | 
			
		||||
        LeaderAddressVo leaderAddressVo = new LeaderAddressVo();
 | 
			
		||||
        BeanUtils.copyProperties(leader, leaderAddressVo);
 | 
			
		||||
        leaderAddressVo.setUserId(userId);
 | 
			
		||||
        leaderAddressVo.setLeaderId(leader.getId());
 | 
			
		||||
        leaderAddressVo.setLeaderName(leader.getName());
 | 
			
		||||
        leaderAddressVo.setLeaderPhone(leader.getPhone());
 | 
			
		||||
        leaderAddressVo.setWareId(userDelivery.getWareId());
 | 
			
		||||
        leaderAddressVo.setStorePath(leader.getStorePath());
 | 
			
		||||
        return leaderAddressVo;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //7 获取当前登录用户信息,
 | 
			
		||||
    @Override
 | 
			
		||||
    public UserLoginVo getUserLoginVo(Long id) {
 | 
			
		||||
        User user = baseMapper.selectById(id);
 | 
			
		||||
        UserLoginVo userLoginVo = new UserLoginVo();
 | 
			
		||||
        userLoginVo.setUserId(id);
 | 
			
		||||
        userLoginVo.setNickName(user.getNickName());
 | 
			
		||||
        userLoginVo.setPhotoUrl(user.getPhotoUrl());
 | 
			
		||||
        userLoginVo.setIsNew(user.getIsNew());
 | 
			
		||||
        userLoginVo.setOpenId(user.getOpenId());
 | 
			
		||||
 | 
			
		||||
        UserDelivery userDelivery = userDeliveryMapper.selectOne(new LambdaQueryWrapper<UserDelivery>().eq(UserDelivery::getUserId, id)
 | 
			
		||||
                                                                                                       .eq(UserDelivery::getIsDefault, 1));
 | 
			
		||||
        if (userDelivery != null) {
 | 
			
		||||
            userLoginVo.setLeaderId(userDelivery.getLeaderId());
 | 
			
		||||
            userLoginVo.setWareId(userDelivery.getWareId());
 | 
			
		||||
        } else {
 | 
			
		||||
            userLoginVo.setLeaderId(1L);
 | 
			
		||||
            userLoginVo.setWareId(1L);
 | 
			
		||||
        }
 | 
			
		||||
        return userLoginVo;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateUser(User user) {
 | 
			
		||||
        //获取当前登录用户id
 | 
			
		||||
        User user1 = baseMapper.selectById(AuthContextHolder.getUserId());
 | 
			
		||||
        //把昵称更新为微信用户
 | 
			
		||||
        user1.setNickName(user.getNickName()
 | 
			
		||||
                              .replaceAll("[ue000-uefff]", "*"));
 | 
			
		||||
        user1.setPhotoUrl(user.getPhotoUrl());
 | 
			
		||||
        baseMapper.updateById(user1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,29 @@
 | 
			
		||||
package com.atguigu.ssyx.user.utils;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.InitializingBean;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: ConstantPropertiesUtil
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.utils
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:19
 | 
			
		||||
 */
 | 
			
		||||
@Component
 | 
			
		||||
public class ConstantPropertiesUtil implements InitializingBean {
 | 
			
		||||
 | 
			
		||||
    public static String WX_OPEN_APP_ID;
 | 
			
		||||
    public static String WX_OPEN_APP_SECRET;
 | 
			
		||||
    @Value("${wx.open.app_id}")
 | 
			
		||||
    private String appId;
 | 
			
		||||
    @Value("${wx.open.app_secret}")
 | 
			
		||||
    private String appSecret;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void afterPropertiesSet() throws Exception {
 | 
			
		||||
        WX_OPEN_APP_ID = appId;
 | 
			
		||||
        WX_OPEN_APP_SECRET = appSecret;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,317 @@
 | 
			
		||||
package com.atguigu.ssyx.user.utils;
 | 
			
		||||
 | 
			
		||||
import org.apache.commons.io.IOUtils;
 | 
			
		||||
import org.apache.commons.lang.StringUtils;
 | 
			
		||||
import org.apache.http.Consts;
 | 
			
		||||
import org.apache.http.HttpEntity;
 | 
			
		||||
import org.apache.http.HttpResponse;
 | 
			
		||||
import org.apache.http.NameValuePair;
 | 
			
		||||
import org.apache.http.client.HttpClient;
 | 
			
		||||
import org.apache.http.client.config.RequestConfig;
 | 
			
		||||
import org.apache.http.client.config.RequestConfig.Builder;
 | 
			
		||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
 | 
			
		||||
import org.apache.http.client.methods.HttpGet;
 | 
			
		||||
import org.apache.http.client.methods.HttpPost;
 | 
			
		||||
import org.apache.http.conn.ConnectTimeoutException;
 | 
			
		||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 | 
			
		||||
import org.apache.http.conn.ssl.SSLContextBuilder;
 | 
			
		||||
import org.apache.http.conn.ssl.TrustStrategy;
 | 
			
		||||
import org.apache.http.conn.ssl.X509HostnameVerifier;
 | 
			
		||||
import org.apache.http.entity.ContentType;
 | 
			
		||||
import org.apache.http.entity.StringEntity;
 | 
			
		||||
import org.apache.http.impl.client.CloseableHttpClient;
 | 
			
		||||
import org.apache.http.impl.client.HttpClients;
 | 
			
		||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 | 
			
		||||
import org.apache.http.message.BasicNameValuePair;
 | 
			
		||||
 | 
			
		||||
import javax.net.ssl.SSLContext;
 | 
			
		||||
import javax.net.ssl.SSLException;
 | 
			
		||||
import javax.net.ssl.SSLSession;
 | 
			
		||||
import javax.net.ssl.SSLSocket;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.SocketTimeoutException;
 | 
			
		||||
import java.security.GeneralSecurityException;
 | 
			
		||||
import java.security.cert.CertificateException;
 | 
			
		||||
import java.security.cert.X509Certificate;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * ClassName: HttpClientUtils
 | 
			
		||||
 * Package: com.atguigu.ssyx.user.utils
 | 
			
		||||
 *
 | 
			
		||||
 * @author yovinchen
 | 
			
		||||
 * @Create 2023/9/22 16:20
 | 
			
		||||
 */
 | 
			
		||||
public class HttpClientUtils {
 | 
			
		||||
 | 
			
		||||
    public static final int connTimeout = 10000;
 | 
			
		||||
    public static final int readTimeout = 10000;
 | 
			
		||||
    public static final String charset = "UTF-8";
 | 
			
		||||
    private static HttpClient client = null;
 | 
			
		||||
 | 
			
		||||
    static {
 | 
			
		||||
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
 | 
			
		||||
        cm.setMaxTotal(128);
 | 
			
		||||
        cm.setDefaultMaxPerRoute(128);
 | 
			
		||||
        client = HttpClients.custom()
 | 
			
		||||
                            .setConnectionManager(cm)
 | 
			
		||||
                            .build();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String postParameters(String url, String parameterStr) throws Exception {
 | 
			
		||||
        return post(url, parameterStr, "application/x-www-form-urlencoded", charset, connTimeout, readTimeout);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String postParameters(String url, String parameterStr, String charset, Integer connTimeout, Integer readTimeout) throws Exception {
 | 
			
		||||
        return post(url, parameterStr, "application/x-www-form-urlencoded", charset, connTimeout, readTimeout);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String postParameters(String url, Map<String, String> params) throws Exception {
 | 
			
		||||
        return postForm(url, params, null, connTimeout, readTimeout);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String postParameters(String url, Map<String, String> params, Integer connTimeout, Integer readTimeout) throws Exception {
 | 
			
		||||
        return postForm(url, params, null, connTimeout, readTimeout);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String get(String url) throws Exception {
 | 
			
		||||
        return get(url, charset, null, null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String get(String url, String charset) throws Exception {
 | 
			
		||||
        return get(url, charset, connTimeout, readTimeout);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送一个 Post 请求, 使用指定的字符集编码.
 | 
			
		||||
     *
 | 
			
		||||
     * @param url
 | 
			
		||||
     * @param body        RequestBody
 | 
			
		||||
     * @param mimeType    例如 application/xml "application/x-www-form-urlencoded" a=1&b=2&c=3
 | 
			
		||||
     * @param charset     编码
 | 
			
		||||
     * @param connTimeout 建立链接超时时间,毫秒.
 | 
			
		||||
     * @param readTimeout 响应超时时间,毫秒.
 | 
			
		||||
     * @return ResponseBody, 使用指定的字符集编码.
 | 
			
		||||
     * @throws ConnectTimeoutException 建立链接超时异常
 | 
			
		||||
     * @throws SocketTimeoutException  响应超时
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    public static String post(String url, String body, String mimeType, String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception {
 | 
			
		||||
        HttpClient client = null;
 | 
			
		||||
        HttpPost post = new HttpPost(url);
 | 
			
		||||
        String result = "";
 | 
			
		||||
        try {
 | 
			
		||||
            if (StringUtils.isNotBlank(body)) {
 | 
			
		||||
                HttpEntity entity = new StringEntity(body, ContentType.create(mimeType, charset));
 | 
			
		||||
                post.setEntity(entity);
 | 
			
		||||
            }
 | 
			
		||||
            // 设置参数
 | 
			
		||||
            Builder customReqConf = RequestConfig.custom();
 | 
			
		||||
            if (connTimeout != null) {
 | 
			
		||||
                customReqConf.setConnectTimeout(connTimeout);
 | 
			
		||||
            }
 | 
			
		||||
            if (readTimeout != null) {
 | 
			
		||||
                customReqConf.setSocketTimeout(readTimeout);
 | 
			
		||||
            }
 | 
			
		||||
            post.setConfig(customReqConf.build());
 | 
			
		||||
 | 
			
		||||
            HttpResponse res;
 | 
			
		||||
            if (url.startsWith("https")) {
 | 
			
		||||
                // 执行 Https 请求.
 | 
			
		||||
                client = createSSLInsecureClient();
 | 
			
		||||
                res = client.execute(post);
 | 
			
		||||
            } else {
 | 
			
		||||
                // 执行 Http 请求.
 | 
			
		||||
                client = HttpClientUtils.client;
 | 
			
		||||
                res = client.execute(post);
 | 
			
		||||
            }
 | 
			
		||||
            result = IOUtils.toString(res.getEntity()
 | 
			
		||||
                                         .getContent(), charset);
 | 
			
		||||
        } finally {
 | 
			
		||||
            post.releaseConnection();
 | 
			
		||||
            if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) {
 | 
			
		||||
                ((CloseableHttpClient) client).close();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 提交form表单
 | 
			
		||||
     *
 | 
			
		||||
     * @param url
 | 
			
		||||
     * @param params
 | 
			
		||||
     * @param connTimeout
 | 
			
		||||
     * @param readTimeout
 | 
			
		||||
     * @return
 | 
			
		||||
     * @throws ConnectTimeoutException
 | 
			
		||||
     * @throws SocketTimeoutException
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    public static String postForm(String url, Map<String, String> params, Map<String, String> headers, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception {
 | 
			
		||||
 | 
			
		||||
        HttpClient client = null;
 | 
			
		||||
        HttpPost post = new HttpPost(url);
 | 
			
		||||
        try {
 | 
			
		||||
            if (params != null && !params.isEmpty()) {
 | 
			
		||||
                List<NameValuePair> formParams = new ArrayList<NameValuePair>();
 | 
			
		||||
                Set<Entry<String, String>> entrySet = params.entrySet();
 | 
			
		||||
                for (Entry<String, String> entry : entrySet) {
 | 
			
		||||
                    formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
 | 
			
		||||
                }
 | 
			
		||||
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, Consts.UTF_8);
 | 
			
		||||
                post.setEntity(entity);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (headers != null && !headers.isEmpty()) {
 | 
			
		||||
                for (Entry<String, String> entry : headers.entrySet()) {
 | 
			
		||||
                    post.addHeader(entry.getKey(), entry.getValue());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            // 设置参数
 | 
			
		||||
            Builder customReqConf = RequestConfig.custom();
 | 
			
		||||
            if (connTimeout != null) {
 | 
			
		||||
                customReqConf.setConnectTimeout(connTimeout);
 | 
			
		||||
            }
 | 
			
		||||
            if (readTimeout != null) {
 | 
			
		||||
                customReqConf.setSocketTimeout(readTimeout);
 | 
			
		||||
            }
 | 
			
		||||
            post.setConfig(customReqConf.build());
 | 
			
		||||
            HttpResponse res = null;
 | 
			
		||||
            if (url.startsWith("https")) {
 | 
			
		||||
                // 执行 Https 请求.
 | 
			
		||||
                client = createSSLInsecureClient();
 | 
			
		||||
                res = client.execute(post);
 | 
			
		||||
            } else {
 | 
			
		||||
                // 执行 Http 请求.
 | 
			
		||||
                client = HttpClientUtils.client;
 | 
			
		||||
                res = client.execute(post);
 | 
			
		||||
            }
 | 
			
		||||
            return IOUtils.toString(res.getEntity()
 | 
			
		||||
                                       .getContent(), "UTF-8");
 | 
			
		||||
        } finally {
 | 
			
		||||
            post.releaseConnection();
 | 
			
		||||
            if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) {
 | 
			
		||||
                ((CloseableHttpClient) client).close();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 发送一个 GET 请求
 | 
			
		||||
     *
 | 
			
		||||
     * @param url
 | 
			
		||||
     * @param charset
 | 
			
		||||
     * @param connTimeout 建立链接超时时间,毫秒.
 | 
			
		||||
     * @param readTimeout 响应超时时间,毫秒.
 | 
			
		||||
     * @return
 | 
			
		||||
     * @throws ConnectTimeoutException 建立链接超时
 | 
			
		||||
     * @throws SocketTimeoutException  响应超时
 | 
			
		||||
     * @throws Exception
 | 
			
		||||
     */
 | 
			
		||||
    public static String get(String url, String charset, Integer connTimeout, Integer readTimeout) throws ConnectTimeoutException, SocketTimeoutException, Exception {
 | 
			
		||||
 | 
			
		||||
        HttpClient client = null;
 | 
			
		||||
        HttpGet get = new HttpGet(url);
 | 
			
		||||
        String result = "";
 | 
			
		||||
        try {
 | 
			
		||||
            // 设置参数
 | 
			
		||||
            Builder customReqConf = RequestConfig.custom();
 | 
			
		||||
            if (connTimeout != null) {
 | 
			
		||||
                customReqConf.setConnectTimeout(connTimeout);
 | 
			
		||||
            }
 | 
			
		||||
            if (readTimeout != null) {
 | 
			
		||||
                customReqConf.setSocketTimeout(readTimeout);
 | 
			
		||||
            }
 | 
			
		||||
            get.setConfig(customReqConf.build());
 | 
			
		||||
 | 
			
		||||
            HttpResponse res = null;
 | 
			
		||||
 | 
			
		||||
            if (url.startsWith("https")) {
 | 
			
		||||
                // 执行 Https 请求.
 | 
			
		||||
                client = createSSLInsecureClient();
 | 
			
		||||
                res = client.execute(get);
 | 
			
		||||
            } else {
 | 
			
		||||
                // 执行 Http 请求.
 | 
			
		||||
                client = HttpClientUtils.client;
 | 
			
		||||
                res = client.execute(get);
 | 
			
		||||
            }
 | 
			
		||||
            result = IOUtils.toString(res.getEntity()
 | 
			
		||||
                                         .getContent(), charset);
 | 
			
		||||
        } finally {
 | 
			
		||||
            get.releaseConnection();
 | 
			
		||||
            if (url.startsWith("https") && client != null && client instanceof CloseableHttpClient) {
 | 
			
		||||
                ((CloseableHttpClient) client).close();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 从 response 里获取 charset
 | 
			
		||||
     *
 | 
			
		||||
     * @param ressponse
 | 
			
		||||
     * @return
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("unused")
 | 
			
		||||
    private static String getCharsetFromResponse(HttpResponse ressponse) {
 | 
			
		||||
        // Content-Type:text/html; charset=GBK
 | 
			
		||||
        if (ressponse.getEntity() != null && ressponse.getEntity()
 | 
			
		||||
                                                      .getContentType() != null && ressponse.getEntity()
 | 
			
		||||
                                                                                            .getContentType()
 | 
			
		||||
                                                                                            .getValue() != null) {
 | 
			
		||||
            String contentType = ressponse.getEntity()
 | 
			
		||||
                                          .getContentType()
 | 
			
		||||
                                          .getValue();
 | 
			
		||||
            if (contentType.contains("charset=")) {
 | 
			
		||||
                return contentType.substring(contentType.indexOf("charset=") + 8);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 创建 SSL连接
 | 
			
		||||
     *
 | 
			
		||||
     * @return
 | 
			
		||||
     * @throws GeneralSecurityException
 | 
			
		||||
     */
 | 
			
		||||
    private static CloseableHttpClient createSSLInsecureClient() throws GeneralSecurityException {
 | 
			
		||||
        try {
 | 
			
		||||
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
 | 
			
		||||
                                                               public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
 | 
			
		||||
                                                                   return true;
 | 
			
		||||
                                                               }
 | 
			
		||||
                                                           })
 | 
			
		||||
                                                           .build();
 | 
			
		||||
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() {
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public boolean verify(String arg0, SSLSession arg1) {
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public void verify(String host, SSLSocket ssl) throws IOException {
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public void verify(String host, X509Certificate cert) throws SSLException {
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
            return HttpClients.custom()
 | 
			
		||||
                              .setSSLSocketFactory(sslsf)
 | 
			
		||||
                              .build();
 | 
			
		||||
        } catch (GeneralSecurityException e) {
 | 
			
		||||
            throw e;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,36 @@
 | 
			
		||||
server:
 | 
			
		||||
  port: 8206
 | 
			
		||||
 | 
			
		||||
mybatis-plus:
 | 
			
		||||
  configuration:
 | 
			
		||||
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 | 
			
		||||
  type-enums-package: com.atguigu.ssyx.enums
 | 
			
		||||
 | 
			
		||||
spring:
 | 
			
		||||
  datasource:
 | 
			
		||||
    type: com.zaxxer.hikari.HikariDataSource
 | 
			
		||||
    driver-class-name: com.mysql.cj.jdbc.Driver
 | 
			
		||||
    url: jdbc:mysql://82.157.68.223:3306/shequ-user?characterEncoding=utf-8&useSSL=false
 | 
			
		||||
    username: shequ-user
 | 
			
		||||
    password: shequ-user
 | 
			
		||||
  jackson:
 | 
			
		||||
    date-format: yyyy-MM-dd HH:mm:ss
 | 
			
		||||
    time-zone: GMT+8
 | 
			
		||||
  redis:
 | 
			
		||||
    host: 82.157.68.223
 | 
			
		||||
    port: 6379
 | 
			
		||||
    database: 0
 | 
			
		||||
    timeout: 1800000
 | 
			
		||||
    password:
 | 
			
		||||
    lettuce:
 | 
			
		||||
      pool:
 | 
			
		||||
        max-active: 20 #最大连接数
 | 
			
		||||
        max-wait: -1    #最大阻塞等待时间(负数表示没限制)
 | 
			
		||||
        max-idle: 5    #最大空闲
 | 
			
		||||
        min-idle: 0     #最小空闲
 | 
			
		||||
wx:
 | 
			
		||||
  open:
 | 
			
		||||
    # 小程序微信公众平台appId
 | 
			
		||||
    app_id: wx2edefe049c6a37b9
 | 
			
		||||
    # 小程序微信公众平台api秘钥
 | 
			
		||||
    app_secret: 1cb2b7aec97a28f0cdf4b74defa1792a
 | 
			
		||||
@@ -0,0 +1,11 @@
 | 
			
		||||
spring:
 | 
			
		||||
  application:
 | 
			
		||||
    name: service-user
 | 
			
		||||
  profiles:
 | 
			
		||||
    active: dev
 | 
			
		||||
  cloud:
 | 
			
		||||
    nacos:
 | 
			
		||||
      discovery:
 | 
			
		||||
        server-addr: 82.157.68.223:8848
 | 
			
		||||
        username: nacos
 | 
			
		||||
        password: nacos
 | 
			
		||||
		Reference in New Issue
	
	Block a user