目录

一、资金记录

(一)后端

(二)前端

二、个人中心

(一)后端

(二)前端

三、整合rabbitmq

(一)linux下rabbitmq 的安装

(二)整合rabbitmq发送短信

1、MQ服务器设置

2、创建rabbit-mq模块

3、service-base中创建SmsDTO

4、service-core中发送消息

5、service-sms中监听消息


一、资金记录

(一)后端

TransFlowController

@Api(tags = "资金记录")@RestController@RequestMapping("/api/core/transFlow")@Slf4jpublic class TransFlowController {@Resourceprivate TransFlowService transFlowService;@ApiOperation("获取列表")@GetMapping("/list")public R list(HttpServletRequest request) {String token = request.getHeader("token");Long userId = JwtUtils.getUserId(token);List list = transFlowService.selectByUserId(userId);return R.ok().data("list", list);}}

接口:TransFlowService

List selectByUserId(Long userId);

实现:TransFlowServiceImpl

@Overridepublic List selectByUserId(Long userId) {QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("user_id", userId).orderByDesc("id");return baseMapper.selectList(queryWrapper);}

(二)前端

脚本pages/user/fund.vue

fetchTransFlowList() {this.$axios.$get('/api/core/transFlow/list').then((response) => {this.transFlowList = response.data.list})},

二、个人中心

(一)后端

创建VO

@Data@ApiModel(description = "首页用户信息")public class UserIndexVO {@ApiModelProperty(value = "用户id")private Long userId;@ApiModelProperty(value = "用户姓名")private String name;@ApiModelProperty(value = "用户昵称")private String nickName;@ApiModelProperty(value = "1:出借人 2:借款人")private Integer userType;@ApiModelProperty(value = "用户头像")private String headImg;@ApiModelProperty(value = "绑定状态(0:未绑定,1:绑定成功 -1:绑定失败)")private Integer bindStatus;@ApiModelProperty(value = "帐户可用余额")private BigDecimal amount;@ApiModelProperty(value = "冻结金额")private BigDecimal freezeAmount;@ApiModelProperty(value = "上次登录时间")private LocalDateTime lastLoginTime;}

UserInfoController

@ApiOperation("获取个人空间用户信息")@GetMapping("/auth/getIndexUserInfo")public R getIndexUserInfo(HttpServletRequest request) {String token = request.getHeader("token");Long userId = JwtUtils.getUserId(token);UserIndexVO userIndexVO = userInfoService.getIndexUserInfo(userId);return R.ok().data("userIndexVO", userIndexVO);}

接口:UserInfoService

UserIndexVO getIndexUserInfo(Long userId);

实现:UserInfoServiceImpl

@Overridepublic UserIndexVO getIndexUserInfo(Long userId) {//用户信息UserInfo userInfo = baseMapper.selectById(userId);//账户信息QueryWrapper userAccountQueryWrapper = new QueryWrapper();userAccountQueryWrapper.eq("user_id", userId);UserAccount userAccount = userAccountMapper.selectOne(userAccountQueryWrapper);//登录信息QueryWrapper userLoginRecordQueryWrapper = new QueryWrapper();userLoginRecordQueryWrapper.eq("user_id", userId).orderByDesc("id").last("limit 1");UserLoginRecord userLoginRecord = userLoginRecordMapper.selectOne(userLoginRecordQueryWrapper);result.put("userLoginRecord", userLoginRecord);//组装结果数据UserIndexVO userIndexVO = new UserIndexVO();userIndexVO.setUserId(userInfo.getId());userIndexVO.setUserType(userInfo.getUserType());userIndexVO.setName(userInfo.getName());userIndexVO.setNickName(userInfo.getNickName());userIndexVO.setHeadImg(userInfo.getHeadImg());userIndexVO.setBindStatus(userInfo.getBindStatus());userIndexVO.setAmount(userAccount.getAmount());userIndexVO.setFreezeAmount(userAccount.getFreezeAmount());userIndexVO.setLastLoginTime(userLoginRecord.getCreateTime());return userIndexVO;}

(二)前端

pages/user/index.vue

fetchUserData() {this.$axios.$get('/api/core/userInfo/auth/getIndexUserInfo').then((response) => {this.userIndexVO = response.data.userIndexVO})},

三、整合rabbitmq

(一)linux下rabbitmq 的安装

rpm -ivh erlang-21.3.8.9-1.el7.x86_64.rpm

rpm -ivh socat-1.7.3.2-1.el6.lux.x86_64.rpm

rpm -ivh rabbitmq-server-3.8.1-1.el7.noarch.rpm

以上按顺序安装

启用管理插件

rabbitmq-plugins enable rabbitmq_management

启动rabbitmq

systemctl start rabbitmq-server.service

查看后台进程

ps -ef | grep rabbitmq

因为linux下开启rabbitmq,默认的guest账户只能在本地访问,因此我们要新建用户

//添加管理员账户和密码

rabbitmqctl add_user admin 123456

// 分配账号角色
rabbitmqctl set_user_tags admin administrator

// 修改密码(要修改一次密码,否则无法登录)

rabbitmqctl change_password admin admin

// 查看用户列表

rabbitmqctllist_users

(二)整合rabbitmq发送短信

1、MQ服务器设置

创建用户 srbuser,配置管理员权限

创建虚拟机 /srbhost,在Set permission中为新建的用户分配访问虚拟机/srbhost 和 /的权限

2、创建rabbit-mq模块

srb下创建rabbit-mq模块并引入依赖

org.springframework.bootspring-boot-starter-amqpcom.alibabafastjsonorg.projectlomboklombok

MQConfig

@Configurationpublic class MQConfig {@Beanpublic MessageConverter messageConverter(){//json字符串转换器return new Jackson2JsonMessageConverter();}}

MQConst

public class MQConst {public static final String EXCHANGE_TOPIC_SMS = "exchange.topic.sms";//交换机public static final String ROUTING_SMS_ITEM = "routing.sms.item";//路由public static final String QUEUE_SMS_ITEM= "queue.sms.item";//消息队列}

MQService

@Service@Slf4jpublic class MQService {@Resourceprivate AmqpTemplate amqpTemplate;/** *发送消息 * @param exchange 交换机 * @param routingKey 路由 * @param message 消息 */public boolean sendMessage(String exchange, String routingKey, Object message) {log.info("发送消息...........");amqpTemplate.convertAndSend(exchange, routingKey, message);return true;}}

3、service-base中创建SmsDTO

@Data@ApiModel(description = "短信")public class SmsDTO {@ApiModelProperty(value = "手机号")private String mobile;@ApiModelProperty(value = "消息内容")private String message;}

4、service-core中发送消息

引入依赖

com.atguigurabbit-mq0.0.1-SNAPSHOT

yml配置

#spring:rabbitmq:host: 192.168.100.103port: 5672virtual-host: /srbhostusername: srbuserpassword: 123456

根据bindCode获取手机号

UserInfoService接口:

String getMobileByBindCode(String bindCode);

UserInfoServiceImpl实现:

@Overridepublic String getMobileByBindCode(String bindCode) {QueryWrapper userInfoQueryWrapper = new QueryWrapper();userInfoQueryWrapper.eq("bind_code", bindCode);UserInfo userInfo = baseMapper.selectOne(userInfoQueryWrapper);return userInfo.getMobile();}

向MQ发送信息:UserAccountServiceImpl 充值回调方法的最后添加发送消息的代码:

@Resourceprivate UserInfoService userInfoService;@Resourceprivate MQService mqService;@Transactional(rollbackFor = Exception.class)@Overridepublic void notify(Map paramMap) {log.info("充值成功:" + JSONObject.toJSONString(paramMap));......//发消息log.info("发消息");String mobile = userInfoService.getMobileByBindCode(bindCode);SmsDTO smsDTO = new SmsDTO();smsDTO.setMobile(mobile);smsDTO.setMessage("充值成功");mqService.sendMessage(MQConst.EXCHANGE_TOPIC_SMS, MQConst.ROUTING_SMS_ITEM, smsDTO);}

5、service-sms中监听消息

引入依赖

com.atguigurabbit-mq0.0.1-SNAPSHOT

yml配置

#spring:rabbitmq:host: 192.168.100.103port: 5672virtual-host: /srbhostusername: srbuserpassword: 123456

创建MQ监听器

package com.atguigu.srb.sms.receiver;@Component@Slf4jpublic class SmsReceiver {@Resourceprivate SmsService smsService;@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MQConst.QUEUE_SMS_ITEM, durable = "true"),exchange = @Exchange(value = MQConst.EXCHANGE_TOPIC_SMS),key = {MQConst.ROUTING_SMS_ITEM}))public void send(SmsDTO smsDTO) throws IOException {log.info("SmsReceiver 消息监听");Map param = new HashMap();param.put("code", smsDTO.getMessage());smsService.send(smsDTO.getMobile(), SmsProperties.TEMPLATE_CODE, param);}}

距尚融宝第一篇博客发布已经过去了一个半月,终于完成了尚融宝这个项目,这个项目主要是在业务上难以理解,后面的投资和放款,还款也涉及多张表的改动,需要保持头脑清晰,理清思路。

接下来继续沉淀,学习一下新的知识,复习旧知识夯实自己的基础。

山高路远,有缘再会!