通用设置
一、查询通用设置
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 { |
| |
| |
| 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 { |
| |
| |
| 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; |
| |
| |
| 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; |
| |
| |
| public Settings selectSettings(Long userId) { |
| QueryWrapper<Settings> qw = new QueryWrapper<>(); |
| qw.eq("user_id", userId); |
| Settings settings = settingsMapper.selectOne(qw); |
| return settings; |
| } |
| |
| |
| @Override |
| public void save(Settings settings) { |
| settingsMapper.insert(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; |
| |
| |
| |
| |
| |
| |
| |
| |
| @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(); |
| |
| |
| Long userId = ThreadLocalUtils.getUserId(); |
| settingsVo.setId(userId); |
| |
| String userPhone = ThreadLocalUtils.getUserPhone(); |
| settingsVo.setPhone(userPhone); |
| |
| |
| Question question =questionApi.queryQuestion(userId); |
| String txt = question == null ? "你喜欢java吗?" : question.getTxt(); |
| settingsVo.setStrangerQuestion(txt); |
| |
| |
| |
| Settings settings =settingApi.selectSettings(userId); |
| |
| if(settings != null){ |
| settingsVo.setGonggaoNotification(settings.getGonggaoNotification()); |
| settingsVo.setPinglunNotification(settings.getPinglunNotification()); |
| settingsVo.setLikeNotification(settings.getLikeNotification()); |
| } |
| return settingsVo; |
| } |
| |
二、陌生人问题
对数据库表进行操作:如果存在数据,更新数据库。如果不存在数据,保存数据库表数据
SettingsController
| |
| |
| |
| |
| |
| |
| |
| @PostMapping("/questions") |
| public ResponseEntity setQuestion(@RequestBody Map map){ |
| |
| |
| String content = (String) map.get("content"); |
| |
| settingsService.setQuestion(content); |
| return ResponseEntity.ok(null); |
| } |
SettingsService
| |
| |
| public void setQuestion(String content) { |
| |
| |
| Long userId = ThreadLocalUtils.getUserId(); |
| |
| 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
| |
| |
| |
| |
| |
| |
| |
| |
| @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"); |
| |
| Long userId = ThreadLocalUtils.getUserId(); |
| |
| 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
| |
| |
| |
| |
| |
| |
| |
| |
| @GetMapping("/blacklist") |
| public ResponseEntity blackList(@RequestParam(defaultValue = "1") int page, |
| @RequestParam(defaultValue = "10") int pageSize) { |
| |
| PageResult pageResult = settingsService.queryBlacklist(page,pageSize); |
| |
| return ResponseEntity.ok(pageResult); |
| } |
SettingsService
| |
| public PageResult queryBlacklist(int page, int pageSize) { |
| |
| Long userId = ThreadLocalUtils.getUserId(); |
| |
| IPage<UserInfo> blacklist =blackListApi.queryBlacklist(userId,page,pageSize); |
| |
| 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
| |
| |
| |
| |
| |
| |
| |
| |
| @DeleteMapping("/blacklist/{uid}") |
| public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId){ |
| |
| |
| settingsService.deleteBlackList(blackUserId); |
| return ResponseEntity.ok(null); |
| } |
SettingsService
| |
| public void deleteBlackList(Long blackUserId) { |
| |
| Long userId = ThreadLocalUtils.getUserId(); |
| blackListApi.deleteBlack(userId,blackUserId); |
| } |
五、修改手机号码
SettingsController
| |
| |
| |
| |
| |
| @PostMapping("/phone/sendVerificationCode") |
| public ResponseEntity updateUserPhone(){ |
| String randomCode = settingsService.updateUserPhone(); |
| System.out.println("【探花交友】验证码:"+randomCode+",验证码五分钟有效,请勿泄露"); |
| |
| return ResponseEntity.ok(null); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @PostMapping("/phone/checkVerificationCode") |
| public ResponseEntity checkVerificationCode(@RequestBody Map map){ |
| |
| String verificationCode = (String) map.get("verificationCode"); |
| |
| Map map1 = settingsService.verificationCode(verificationCode); |
| |
| return ResponseEntity.ok(map1); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| @PostMapping("phone") |
| public ResponseEntity saveNewPhone(@RequestBody Map map){ |
| |
| |
| settingsService.savaUserPhone(map); |
| return ResponseEntity.ok(null); |
| } |
| |
SettingsService
| |
| |
| public String updateUserPhone() { |
| |
| String userPhone = ThreadLocalUtils.getUserPhone(); |
| |
| String randomCode = RandomStringUtils.randomNumeric(6); |
| |
| |
| redisTemplate.opsForValue().set("Code_"+userPhone, randomCode, Duration.ofMinutes(5)); |
| |
| return randomCode; |
| } |
| |
| |
| |
| |
| public Map verificationCode(String verificationCode) { |
| |
| |
| String userPhone = ThreadLocalUtils.getUserPhone(); |
| |
| String randomCode = redisTemplate.opsForValue().get("Code_" + userPhone); |
| |
| |
| 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) { |
| |
| |
| String newPhone = (String) map.get("phone"); |
| |
| Long userId = ThreadLocalUtils.getUserId(); |
| |
| User user = new User(); |
| user.setId(userId); |
| user.setMobile(newPhone); |
| |
| |
| userApi.updateUserPhone(user); |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理