对话框推荐使用yui这个框架,我们设计的消息提示模式,是根据给定一个消息的id,参数,来获取匹配的提示消息,比如MESSAGE.I.1 ,就表示获取MESSAGE这个大机能下面的id为1的消息,而且消息类型是 I(INFO),表示是提示类型的。
代码下载地址
https://gitee.com/yellowcong/shior-dmeo/tree/master/test_cas
插件
插件名 | 下载地址 |
---|---|
layer | http://layer.layui.com/hello.html |
layui | http://www.layui.com/ |
实现效果
我们通过下面的两种方式进行访问,就可以获取到对话框的信息了,而且这事 通过数据库 获取的,而不是固定写死的
//不带模板匹配的消息
sys.showMessage("SYSTEM.I.1")
//带参数的消息
sys.showMessage("SYSTEM.E.2",["zhansan","3"])
带参数的效果
表设计
CREATE TABLE `sys_message` (
`message_kbn` char(1) NOT NULL COMMENT '消息类型/E(ERROR)/I(INFO)/Q(QUESION)/S(SUCCESS)',
`kino_id` varchar(32) NOT NULL COMMENT '机能名称/MESSAGE/USER/PASSAGE',
`message_id` tinyint(4) NOT NULL COMMENT '消息id/1.. 排序增长',
`message` varchar(250) NOT NULL COMMENT '消息内容',
`userId` varchar(32) DEFAULT NULL COMMENT '用户id',
`userNm` varchar(32) DEFAULT NULL COMMENT '用户名称',
`addDate` date DEFAULT NULL COMMENT '添加日期',
PRIMARY KEY (`message_kbn`,`kino_id`,`message_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
插入几条测试数据
insert into `sys_message`(`kino_id`,`message_kbn`,`message_id`,`message`,`userId`,`userNm`,`addDate`) values ('SYSTEM','E',2,'用户{0},您登录的次数超过{1}次,请稍后重试',NULL,NULL,NULL),('SYSTEM','I',1,'您打开的页面太多,请先关闭?',NULL,NULL,NULL);
后台设计
框架采用的是Srping+SpringMVC+mybatis,数据库采用的是Mysql,权限控制采用的是shiro
类 | 功能 |
---|---|
Message | 数据库映射类 |
MessageMapper | Dao层 |
MessageMapper.xml | Dao层 |
MessageService | service层 |
MessageServiceImpl | service层 |
MessageController | 控制层 |
Message
package com.yellowcong.shiro.model;
import java.io.Serializable;
import java.util.Date;
/**
* @author
*/
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 消息类型/E(ERROR)/I(INFO)/Q(QUESION)/S(SUCCESS)
*/
private String messageKbn;
/**
* 机能名称/MESSAGE/USER/PASSAGE
*/
private String kinoId;
/**
* 消息id/1.. 排序增长
*/
private Byte messageId;
/**
* 消息内容
*/
private String message;
/**
* 用户id
*/
private String userid;
/**
* 用户名称
*/
private String usernm;
/**
* 添加日期
*/
private Date adddate;
public String getMessageKbn() {
return messageKbn;
}
public void setMessageKbn(String messageKbn) {
this.messageKbn = messageKbn;
}
public String getKinoId() {
return kinoId;
}
public void setKinoId(String kinoId) {
this.kinoId = kinoId;
}
public Byte getMessageId() {
return messageId;
}
public void setMessageId(Byte messageId) {
this.messageId = messageId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsernm() {
return usernm;
}
public void setUsernm(String usernm) {
this.usernm = usernm;
}
public Date getAdddate() {
return adddate;
}
public void setAdddate(Date adddate) {
this.adddate = adddate;
}
}
MessageMapper
package com.yellowcong.shiro.dao;
import org.apache.ibatis.annotations.Param;
import com.yellowcong.shiro.model.Message;
/**
* 创建日期:2017/12/22<br/>
* 创建时间:17:17:01<br/>
* 创建用户:yellowcong<br/>
* 机能概要:
*/
public interface MessageMapper {
/**
* 创建日期:2017年12月31日<br/>
* 创建时间:下午5:18:34<br/>
* 创建用户:yellowcong<br/>
* 机能概要:根据消息类型来获取id
* @param type 消息类型/E(ERROR)/I(INFO)/Q(QUESION)/S(SUCCESS)
* @param kinoId 机能名称/MESSAGE/USER/PASSAGE
* @param messageId 消息id 123
* @return
*/
Message getMsg(@Param("kionId")String kinoId,@Param("messageKbn") String messageKbn,@Param("messageId") String messageId);
}
MessageMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yellowcong.shiro.dao.MessageMapper">
<resultMap id="Message" type="com.yellowcong.shiro.model.Message">
<result column="message_kbn" jdbcType="CHAR" property="messageKbn" />
<result column="kino_id" jdbcType="VARCHAR" property="kinoId" />
<result column="message_id" jdbcType="TINYINT" property="messageId" />
<result column="message" jdbcType="VARCHAR" property="message" />
<result column="userId" jdbcType="VARCHAR" property="userid" />
<result column="userNm" jdbcType="VARCHAR" property="usernm" />
<result column="addDate" jdbcType="DATE" property="adddate" />
</resultMap>
<select id="getMsg" resultMap="Message">
SELECT * FROM sys_message a WHERE a.kino_id= #{kionId} AND a.message_kbn = #{messageKbn} AND a.message_id = #{messageId}
</select>
</mapper>
MessageService
package com.yellowcong.service;
import com.yellowcong.shiro.model.Message;
/**
* 创建日期:2017年12月31日
* 创建时间:下午5:24:34
* 创建者 :yellowcong
* 机能概要:
*/
public interface MessageService {
/**
* 创建日期:2017年12月31日<br/>
* 创建时间:下午5:27:17<br/>
* 创建用户:yellowcong<br/>
* 机能概要:通过id来获取Message
* @param id,例如 W.MESSAGE.1
* @param args 多个参数
* @return
*/
public Message getMsg(String id,String... args);
}
MessageServiceImpl
package com.yellowcong.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yellowcong.service.MessageService;
import com.yellowcong.shiro.dao.MessageMapper;
import com.yellowcong.shiro.model.Message;
/**
* 创建日期:2017年12月31日
* 创建时间:下午5:25:25
* 创建者 :yellowcong
* 机能概要:
*/
@Service("messageService")
public class MessageServiceImpl implements MessageService{
@Autowired
private MessageMapper messageMapper;
public Message getMsg(String id,String... args) {
//MESSAGE.E.1
//获取Message机能中的 消息信息
String [] ids = id.split("\\.");
String kinoId = ids[0];
String messageKbn = ids[1];
String messageId = ids[2];
Message msg = this.messageMapper.getMsg(kinoId,messageKbn, messageId);
//当消息没有找到的情况
if(msg == null){
msg = new Message();
msg.setMessageKbn("E");
msg.setMessage(id+"没有找到,请确认数据库中存在此消息。");
return msg;
}
//获取到msg之后 ,替换msg里面的匹配的数据
//用户{0},您以登录。
if(args != null && args.length >0){
String messageStr = msg.getMessage();
for(int i=0;i<args.length;i++){
messageStr = messageStr.replace("{"+i+"}", args[i]);
}
msg.setMessage(messageStr);
}
return msg;
}
}
控制层
控制层,只提供了一个方法,就是通过id来获取到消息
package com.yellowcong.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.yellowcong.service.MessageService;
import com.yellowcong.shiro.model.Message;
import com.yellowcong.shiro.utils.ControllerUtils;
/**
* 创建日期:2017年12月31日<br/>
* 创建时间:下午5:55:49 <br/>
* 创建者 :yellowcong <br/>
* 机能概要:
*/
@RequestMapping("/msg")
@Controller
@Scope("prototype")
public class MessageController {
@Autowired
private MessageService messageService;
@RequestMapping("/getMsg")
public void getMessage(String id,String args,HttpServletResponse response){
String [] arrs = null;
if(args != null && !StringUtils.isEmpty(args)){
arrs = args.split(",");
}
//从数据库中获取数据
Message msg = this.messageService.getMsg(id, arrs);
//写数据到客户端
Map<String,String> map = new HashMap<String, String>();
map.put("type", msg.getMessageKbn());
map.put("text", msg.getMessage());
ControllerUtils.writeJSON(response, map);
}
}
前端代码
基于yui提供的框架上,进行消息提示的修改。
//---------------------------------------------------------
//共同js处理
//---------------------------------------------------------
var sys = new Object();
//---------------------------------------------------------
//获取服务器短的消息
//id 消息的id SYSTEM.I.1 SYSTEM.E.2
//消息模板匹配 单个消息 ,直接 传递 字符串,多个消息传递数组
//---------------------------------------------------------
sys.showMessage = function(id,args){
if(args == null|| args == undefined){
args = null;
}else if(utils.isArray(args)){
//当是数组的时候,直接加入 逗号进行分割
args = args.join(",");
}
var data = {id:id,args:args};
// menus的 url
var url = utils.contextPath() + "/msg/getMsg";
$.ajax({
type : "get",// 使用提交的方法 post、get
url : url,// 提交的地址
data : data,// 数据
async : false, // 配置是否异步操作
dataType : "json",// 返回数据类型的格式
}).done(function(result) {
//获取code 的id
var iconCd = getIconCode(result.type);
layer.open({title: "系统提示",icon:iconCd,shadeClose: true,content: result.text});
});
}
//获取icon的类型
function getIconCode(type){
//icon 类型
//1 --> SUCESS
//2 --> EROR
//3 --> Question
//7 --> INFO
var iconCode = -1;
switch(type){
case "S":
iconCode = 1;
break;
case "E":
iconCode = 2;
break;
case "Q":
iconCode = 3;
break;
case "I":
iconCode = 7;
break;
}
return iconCode;
}