13.通用设置
通用设置
一、查询通用设置
1.1搭建服务提供者环境
实体类
BlackList
package com.tanhua.model.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//黑名单表
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BlackList extends BasePojo {
private Long id;
private Long userId;
private Long blackUserId;
}
Question
package com.tanhua.model.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//陌生人问题表
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Question extends BasePojo {
private Long id;
private Long userId;
//问题内容
private String txt;
}
Settings
package com.tanhua.model.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//通知开关设置表
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Settings extends BasePojo {
private Long id;
private Long userId;
private Boolean likeNotification;
private Boolean pinglunNotification;
private Boolean gonggaoNotification;
}
mapper
BlackListMapper
package com.tanhua.dubbo.mappers;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.BlackList;
public interface BlackListMapper extends BaseMapper<BlackList> {
}
QuestionMapper
package com.tanhua.dubbo.mappers;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.Question;
public interface QuestionMapper extends BaseMapper<Question> {
}
SettingsMapper
package com.tanhua.dubbo.mappers;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.Settings;
public interface SettingsMapper extends BaseMapper<Settings> {
}
api接口
BlackListApi
package com.tanhua.dubbo.api;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.model.domain.UserInfo;
public interface BlackListApi {
//分页查询用户黑名单列表
IPage<UserInfo> queryBlacklist(Long userId, int page, int pageSize);
//取消黑名单,从数据库中删除黑名单信息
void deleteBlack(Long userId, Long blackUserId);
}
QuestionApi
package com.tanhua.dubbo.api;
import com.tanhua.model.domain.Question;
public interface QuestionApi {
//根据用户id查询用户陌生人人问题
Question queryQuestion(Long userId);
//保存陌生人问题到数据库
void saveQuestion(Question question);
//修改数据库的陌生人问题
void updateQuestion(Question question);
}
SettingApi
package com.tanhua.dubbo.api;
import com.tanhua.model.domain.Settings;
public interface SettingApi {
//根据用户id查询Settings对象,通知开关
Settings selectSettings(Long userId);
//保存
void save(Settings settings);
//更新
void update(Settings settings);
}
api接口实现类
BlackListApiImpl
package com.tanhua.dubbo.api;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class BlackListApiImpl implements BlackListApi{
}
QuestionApiImpl
package com.tanhua.dubbo.api;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.QuestionMapper;
import com.tanhua.model.domain.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
@DubboService
public class QuestionApiImpl implements QuestionApi {
@Autowired
private QuestionMapper questionMapper;
//根据id查询Question,陌生人问题
public Question queryQuestion(Long id) {
QueryWrapper<Question> qw = new QueryWrapper<>();
qw.eq("user_id", id);
Question question = questionMapper.selectOne(qw);
return question;
}
// //保存陌生人问题到数据库
@Override
public void saveQuestion(Question question) {
questionMapper.insert(question);
}
// //更新数据库的陌生人问题
@Override
public void updateQuestion(Question question) {
questionMapper.updateById(question);
}
}
SettingApiImpl
package com.tanhua.dubbo.api;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.SettingsMapper;
import com.tanhua.model.domain.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
@DubboService
public class SettingApiImpl implements SettingApi{
@Autowired
private SettingsMapper settingsMapper;
//根据用户id查询Settings
public Settings selectSettings(Long userId) {
QueryWrapper<Settings> qw = new QueryWrapper<>();
qw.eq("user_id", userId);
Settings settings = settingsMapper.selectOne(qw);
return settings;
}
//保存Settings到数据库
@Override
public void save(Settings settings) {
settingsMapper.insert(settings);
}
//根据userid修改数据库中的settings
@Override
public void update(Settings settings) {
settingsMapper.updateById(settings);
}
}
vo对象
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SettingsVo implements Serializable {
private Long id;
private String strangerQuestion = "";//陌生人问题
private String phone;//手机号码
private Boolean likeNotification = true;
private Boolean pinglunNotification = true;
private Boolean gonggaoNotification = true;
}
1.2服务消费者
SettingsController
@RestController
@RequestMapping("/users")
public class SettingsController {
@Autowired
private SettingsService settingsService;
/**
*
* 读取用户通用设置
* 1.请求路径:users/settings
* 2.请求参数:请求头中的token,Authorization
* 3.响应参数:SettingsVo
*/
@GetMapping("/settings")
public ResponseEntity settings(){
SettingsVo settingsVo = settingsService.settings();
return ResponseEntity.ok(settingsVo);
}
}
SettingsService
@Service
public class SettingsService {
@DubboReference
private QuestionApi questionApi;
@DubboReference
private SettingApi settingApi;
//查询通用设置
public SettingsVo settings() {
SettingsVo settingsVo = new SettingsVo();
//1.获取用户id
Long userId = ThreadLocalUtils.getUserId();
settingsVo.setId(userId);
//2.获取用户手机号码
String userPhone = ThreadLocalUtils.getUserPhone();
settingsVo.setPhone(userPhone);
//3.查询陌生人问题
Question question =questionApi.queryQuestion(userId);//根据用户id查询Question对象
String txt = question == null ? "你喜欢java吗?" : question.getTxt();//三元运算符
settingsVo.setStrangerQuestion(txt);
//4.查询通用设置通知开关
Settings settings =settingApi.selectSettings(userId);
if(settings != null){
settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
settingsVo.setPinglunNotification(settings.getPinglunNotification());
settingsVo.setLikeNotification(settings.getLikeNotification());
}
return settingsVo;
}
二、陌生人问题
对数据库表进行操作:如果存在数据,更新数据库。如果不存在数据,保存数据库表数据
SettingsController
/**
*设置陌生人问题:如果没有查询出来问题,保存;
* 如果查询出来问题,则是修改
* 请求路径:/users/questions
* 请求方式:post;
* 请求参数:content(问题),用map集合接收
*/
@PostMapping("/questions")
public ResponseEntity setQuestion(@RequestBody Map map){
//1.获取问题正文参数
String content = (String) map.get("content");
//2.调用业务层,完成问题保存或更新
settingsService.setQuestion(content);
return ResponseEntity.ok(null);
}
SettingsService
//设置陌生人问题
public void setQuestion(String content) {
//1.获取用户id
Long userId = ThreadLocalUtils.getUserId();
//2.根据用户id查询陌生人问题
Question question = questionApi.queryQuestion(userId);
if(question == null){
//不存在,保存数据
question =new Question();
question.setUserId(userId);
question.setTxt(content);
questionApi.saveQuestion(question);
}else{
//存在
question.setTxt(content);
questionApi.updateQuestion(question);
}
}
三、通知设置
SettingsController
/**
* 通知设置:
* 请求路径:/users/notifications/setting
*请求参数: likeNotification boolean
* pinglunNotification boolean
* gonggaoNotification Boolean
* 使用map集合接收
*/
@PostMapping("/notifications/setting")
public ResponseEntity setNotification(@RequestBody Map map){
//调用业务层完成业务更新
settingsService.setNotification(map);
return ResponseEntity.ok(null);
}
SettingsService
//通知设置
public void setNotification(Map map) {
Boolean likeNotification = (Boolean) map.get("likeNotification");
Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
//1.获取用户id
Long userId = ThreadLocalUtils.getUserId();
//2.调用api根据用户id查询Settings
Settings settings = settingApi.selectSettings(userId);
if(settings == null){
//不存在,帮他保存到数据库
settings =new Settings();
settings.setUserId(userId);
settings.setLikeNotification(likeNotification);
settings.setPinglunNotification(pinglunNotification);
settings.setGonggaoNotification(gonggaoNotification);
settingApi.save(settings);
}else{
//存在,更新
settings.setLikeNotification(likeNotification);
settings.setPinglunNotification(pinglunNotification);
settings.setGonggaoNotification(gonggaoNotification);
settingApi.update(settings);
}
}
四、黑名单管理
1.1黑名单列表分页查询
vo对象
package com.tanhua.domain.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult implements Serializable {
private Integer counts = 0;//总记录数
private Integer pagesize;//页大小
private Integer pages = 0;//总页数
private Integer page;//当前页码
private List<?> items = Collections.emptyList(); //列表
public PageResult(Integer page,Integer pagesize,
int counts,List list) {
this.page = page;
this.pagesize = pagesize;
this.items = list;
this.counts = counts;
this.pages = counts % pagesize == 0 ? counts / pagesize : counts / pagesize + 1;
}
}
MybatisPlusConfig
tanhua-dubbo-db`引导类开启mybatis-plus分页插件支持
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
SettingsController
/**
*
* 黑名单翻页列表
* 请求路径:/users/blacklist
* 请求方式:get
* 请求参数:page,当前页数;pagesize,页尺寸
* 响应数据:PageResult
*/
@GetMapping("/blacklist")
public ResponseEntity blackList(@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int pageSize) {
//1.调用业务层完成业务逻辑,分页查询
PageResult pageResult = settingsService.queryBlacklist(page,pageSize);
//2.构造返回数据
return ResponseEntity.ok(pageResult);
}
SettingsService
//黑名单列表分页查询
public PageResult queryBlacklist(int page, int pageSize) {
//1.获取用户id
Long userId = ThreadLocalUtils.getUserId();
//2.调用api根据用户id查询用户的黑名单列表
IPage<UserInfo> blacklist =blackListApi.queryBlacklist(userId,page,pageSize);
//3.从Ipage对象获取 PageResult对象所需的数据
PageResult pageResult = new PageResult(page,pageSize, (int) blacklist.getTotal(),blacklist.getRecords());
return pageResult;
}
UserInfoMapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {
@Select("select * from tb_user_info where id in (\n" +
" SELECT black_user_id FROM tb_black_list where user_id=#{userId}\n" +
")")
IPage<UserInfo> findBlackList(@Param("pages") Page pages, @Param("userId") Long userId);
}
1.2取消黑名单
SettingsController
/**
* 取消黑名单
* 请求路径:/users/blacklist/:uid
* 请求方式:delete
* 请求参数:路径参数,uid
* 响应数据:状态码200
*/
@DeleteMapping("/blacklist/{uid}")
public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId){
//1.调用业务层完成业务逻辑
settingsService.deleteBlackList(blackUserId);
return ResponseEntity.ok(null);
}
SettingsService
//取消黑名单
public void deleteBlackList(Long blackUserId) {
//1.获取用户id
Long userId = ThreadLocalUtils.getUserId();
blackListApi.deleteBlack(userId,blackUserId);
}
五、修改手机号码
SettingsController
/**
* 修改手机号之获取验证码;
* 请求路径:/users/phone/sendVerificationCode
* 请求方式:post
*/
@PostMapping("/phone/sendVerificationCode")
public ResponseEntity updateUserPhone(){
String randomCode = settingsService.updateUserPhone();
System.out.println("【探花交友】验证码:"+randomCode+",验证码五分钟有效,请勿泄露");
return ResponseEntity.ok(null);
}
/**
*校验验证码:
* 请求路径:/users/phone/checkVerificationCode
* 请求方式:post
* 请求参数:verificationCode,验证码
* 响应数据:verification,boolean,是否验证通过
*/
@PostMapping("/phone/checkVerificationCode")
public ResponseEntity checkVerificationCode(@RequestBody Map map){
//1.获取验证码
String verificationCode = (String) map.get("verificationCode");
//2.调用业务层次校验验证码
Map map1 = settingsService.verificationCode(verificationCode);
//3.响应数据
return ResponseEntity.ok(map1);
}
/**
* 修改手机号码之保存手机号码
* 请求路径:/users/phone
* 请求方式:post
* 请求参数:phone,新手机号码
* 响应数据:null
*/
@PostMapping("phone")
public ResponseEntity saveNewPhone(@RequestBody Map map){
//直接调用业务层修改手机号码
settingsService.savaUserPhone(map);
return ResponseEntity.ok(null);
}
SettingsService
//修改手机获取验证码
public String updateUserPhone() {
//1.获取手机号码
String userPhone = ThreadLocalUtils.getUserPhone();
//2.生成六位随机数字
String randomCode = RandomStringUtils.randomNumeric(6);
//3.把随机数缓存进Redis
redisTemplate.opsForValue().set("Code_"+userPhone, randomCode, Duration.ofMinutes(5));
return randomCode;
}
//校验验证码:
public Map verificationCode(String verificationCode) {
//获取手机号码
String userPhone = ThreadLocalUtils.getUserPhone();
//1.从Redis中获取验证码
String randomCode = redisTemplate.opsForValue().get("Code_" + userPhone);
//2.校验验证码
if(StringUtils.isEmpty(verificationCode) || !randomCode.equals(verificationCode)){
throw new BusinessException(ErrorResult.loginError());
}
Boolean result = true;
Map resultMap = new HashMap();
resultMap.put("verification", result);
return resultMap;
}
//保存新手机号码
public void savaUserPhone(Map map) {
//1.获取要保存的新手机号码
String newPhone = (String) map.get("phone");
//2.获取用户id
Long userId = ThreadLocalUtils.getUserId();
User user = new User();
user.setId(userId);
user.setMobile(newPhone);
//3.调用api根据用户id修改手机号码
userApi.updateUserPhone(user);
}