Browse Source

增加 全局事件总线

master
lihenian 5 years ago
parent
commit
60ded2d86a
  1. 1
      .eslintrc.js
  2. 33
      project.config.json
  3. 2
      subpages/home/pages/newsDetail/newsDetail.js
  4. 54
      utils/eventBus.js
  5. 4
      utils/filter.wxs

1
.eslintrc.js

@ -36,6 +36,7 @@ module.exports = {
"allow": ["log", "warn", "error", "info"] // "allow" 是个字符串数组,包含允许使用的console 对象的方法 "allow": ["log", "warn", "error", "info"] // "allow" 是个字符串数组,包含允许使用的console 对象的方法
} }
], ],
"no-debugger": "off",
//禁止空格和 tab 的混合缩进 //禁止空格和 tab 的混合缩进
"no-mixed-spaces-and-tabs": [ "no-mixed-spaces-and-tabs": [
"error", "smart-tabs" "error", "smart-tabs"

33
project.config.json

@ -17,6 +17,7 @@
"checkInvalidKey": true, "checkInvalidKey": true,
"checkSiteMap": true, "checkSiteMap": true,
"uploadWithSourceMap": true, "uploadWithSourceMap": true,
"compileHotReLoad": false,
"babelSetting": { "babelSetting": {
"ignore": [], "ignore": [],
"disablePlugins": [], "disablePlugins": [],
@ -24,8 +25,7 @@
}, },
"useIsolateContext": true, "useIsolateContext": true,
"useCompilerModule": false, "useCompilerModule": false,
"userConfirmedUseCompilerModuleSwitch": false, "userConfirmedUseCompilerModuleSwitch": false
"compileHotReLoad": false
}, },
"compileType": "miniprogram", "compileType": "miniprogram",
"libVersion": "2.8.2", "libVersion": "2.8.2",
@ -59,7 +59,7 @@
"list": [] "list": []
}, },
"miniprogram": { "miniprogram": {
"current": 0, "current": -1,
"list": [ "list": [
{ {
"id": 0, "id": 0,
@ -82,32 +82,11 @@
"query": "scene=gridLeader", "query": "scene=gridLeader",
"scene": 1011 "scene": 1011
}, },
{
"id": 3,
"name": "pages/index/index",
"pathName": "pages/index/index",
"query": "",
"scene": null
},
{
"id": 4,
"name": "pages/indexNew/indexNew",
"pathName": "pages/indexNew/indexNew",
"query": "",
"scene": null
},
{
"id": -1,
"name": "完善信息",
"pathName": "pages/complete/complete",
"query": "",
"scene": null
},
{ {
"id": -1, "id": -1,
"name": "subpages/associationNew/pages/eventlist/eventlist", "name": "subpages/home/pages/newsDetail/newsDetail",
"pathName": "subpages/associationNew/pages/eventlist/eventlist", "pathName": "subpages/home/pages/newsDetail/newsDetail",
"query": "partyGroupId=4048a5a044a047d9a3dc135a0d756eee&topicType=1", "query": "id=7c8bc749ff4b6380bf1d902c0bde0ba1",
"scene": null "scene": null
} }
] ]

2
subpages/home/pages/newsDetail/newsDetail.js

@ -196,7 +196,7 @@ Page({
}, 1000) }, 1000)
}, },
// 加积分 // 加积分
browsePoints (){ browsePoints () {
let that = this let that = this
const para = { const para = {
newsId: that.data.detailId newsId: that.data.detailId

54
utils/eventBus.js

@ -0,0 +1,54 @@
/**
* EventBus 全局事件总线
* on(msgName, func) 订阅消息 msgName订阅的事件名称 func 事件回调
* once(msgName, func) 仅订阅一次消息 后订阅的会替换前面订阅的消息
* emit(msgName,data) 发布消息 msgName消息名称 data-数据
* off(msgName) 移除消息
*/
class EventBus {
constructor () {
this.msgQueues = {}
}
// 将消息 绑定到 消息队列中
on (msgName, func) {
if (this.msgQueues[msgName]) {
if (typeof this.msgQueues[msgName] === "function") {
this.msgQueues[msgName] = [this.msgQueues[msgName], func]
} else {
this.msgQueues[msgName] = [...this.msgQueues[msgName], func]
}
} else {
this.msgQueues[msgName] = func;
}
}
// 消息队列中仅保存一个消息
once (msgName, func) {
this.msgQueues[msgName] = func
}
// 发送消息
emit (msgName, data = "") {
if (!this.msgQueues[msgName]) {
return
}
if (typeof this.msgQueues[msgName] === "function") {
this.msgQueues[msgName](data)
} else {
this.msgQueues[msgName].forEach(fn => fn(data))
}
}
// 移除消息
off (msgName) {
if (!this.msgQueues[msgName]) {
return
}
delete this.msgQueues[msgName]
}
}
export default new EventBus()

4
utils/filter.wxs

@ -1,6 +1,6 @@
var formatTime = function (strDate, format = "yyyy-MM-dd hh:mm:ss") { var formatTime = function (strDate, format = "yyyy-MM-dd hh:mm:ss") {
// 解决ios出现NaN问题 // 解决ios出现NaN问题
var realDate = strDate ? getDate(strDate.replace(getRegExp('-', 'g'), '/')) : getDate(); var realDate = strDate ? getDate(strDate.replace(getRegExp("-", "g"), "/")) : getDate();
var regYear = getRegExp("(y+)", "i"); var regYear = getRegExp("(y+)", "i");
var date = [ var date = [
["M+", realDate.getMonth() + 1], ["M+", realDate.getMonth() + 1],
@ -13,7 +13,7 @@ var formatTime = function (strDate, format = "yyyy-MM-dd hh:mm:ss") {
]; ];
var reg1 = regYear.exec(format); var reg1 = regYear.exec(format);
if (reg1) { if (reg1) {
format = format.replace(reg1[1], (realDate.getFullYear() + '').substring(4 - reg1[1].length)); format = format.replace(reg1[1], (realDate.getFullYear() + "").substring(4 - reg1[1].length));
} }
for (var i = 0; i < date.length; i++) { for (var i = 0; i < date.length; i++) {
var reg2 = getRegExp("(" + date[i][0] + ")").exec(format); var reg2 = getRegExp("(" + date[i][0] + ")").exec(format);

Loading…
Cancel
Save