一. 通用设置
1.1 需求分析
1. 需求分析
通用设置,包含探花交友APP基本的软件设置功能。包含:
设置陌生人问题:当平台其他用户想进行在线交流时需要回答陌生人问题。
通用设置:包含一些APP通知设置
黑名单:对于不感兴趣的用户设置黑名单屏蔽骚扰
2. 数据库表
通用设置
CREATE TABLE `tb_settings` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL,
`like_notification` tinyint(4) DEFAULT '1' COMMENT '推送喜欢通知',
`pinglun_notification` tinyint(4) DEFAULT '1' COMMENT '推送评论通知',
`gonggao_notification` tinyint(4) DEFAULT '1' COMMENT '推送公告通知',
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='设置表';
问题表
CREATE TABLE `tb_question` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL COMMENT '用户id',
`txt` varchar(200) DEFAULT NULL COMMENT '问题内容',
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8
黑名单
CREATE TABLE `tb_black_list` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL,
`black_user_id` bigint(20) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='黑名单';
3. 搭建提供者环境
实体类
(1) Settings
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Settings extends BasePojo {
private Long id;
private Long userId;
private Boolean likeNotification;
private Boolean pinglunNotification;
private Boolean gonggaoNotification;
}
(2)Question
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Question extends BasePojo {
private Long id;
private Long userId;
//问题内容
private String txt;
}
(3)BlackList
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BlackList extends BasePojo {
private Long id;
private Long userId;
private Long blackUserId;
}
mapper接口
(1)SettingsMapper
public interface SettingsMapper extends BaseMapper<Settings> {
}
(2)QuestionMapper
public interface QuestionMapper extends BaseMapper<Question> {
}
(3)BlackListMapper
public interface BlackListMapper extends BaseMapper<BlackList> {
}
api接口
(1) SettingApi
public interface SettingsApi {
}
(2)QuestionApi
import com.tanhua.domain.db.Question;
public interface QuestionApi {
}
(3)BlackListApi
public interface BlackListApi {
}
api服务实现类
(1)SettingServiceImpl
@Service
public class SettingsApiImpl implements SettingsApi {
@Autowired
private SettingsMapper settingsMapper;
}
(2)QuestionServiceImpl
@Service
public class QuestionApiImpl implements QuestionApi {
@Autowired
private QuestionMapper questionMapper;
}
(3)BlackListServiceImpl
@Service
public class BlackListApiImpl implements BlackListApi {
@Autowired
private BlackListMapper blackListMapper;
}
1.2 查询通用设置
代码流程
1. 接口文档
2. 代码实现
步骤
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;
}
SettingsController
在tanhua-server
工程创建SettingsController
完成代码编写
@RestController
@RequestMapping("/users")
public class SettingsController {
@Autowired
private SettingsService settingsService;
/**
* 查询通用设置
*/
@GetMapping("/settings")
public ResponseEntity settings() {
SettingsVo vo = settingsService.settings();
return ResponseEntity.ok(vo);
}
}
SettingService
在tanhua-server
工程创建SettingService
完成代码编写
@Service
public class SettingsService {
@DubboReference
private QuestionApi questionApi;
@DubboReference
private SettingsApi settingsApi;
@DubboReference
private BlackListApi blackListApi;
//查询通用设置
public SettingsVo settings() {
SettingsVo vo = new SettingsVo();
//1、获取用户id
Long userId = UserHolder.getUserId();
vo.setId(userId);
//2、获取用户的手机号码
vo.setPhone(UserHolder.getMobile());
//3、获取用户的陌生人问题
Question question = questionApi.findByUserId(userId);
String txt = question == null ? "你喜欢java吗?" : question.getTxt();
vo.setStrangerQuestion(txt);
//4、获取用户的APP通知开关数据
Settings settings = settingsApi.findByUserId(userId);
if(settings != null) {
vo.setGonggaoNotification(settings.getGonggaoNotification());
vo.setPinglunNotification(settings.getPinglunNotification());
vo.setLikeNotification(settings.getLikeNotification());
}
return vo;
}
}
QuestionApi
在tanhua-dubbo中的QuestionApi
和QuestionApiImpl
补充方法
@Override
public Question findByUserId(Long userId) {
QueryWrapper<Question> qw = new QueryWrapper<>();
qw.eq("user_id",userId);
return questionMapper.selectOne(qw);
}
SettingApi
在tanhua-dubbo中的SettingApi
和SettingApiImpl
补充方法
//根据用户id查询
public Settings findByUserId