Authored by 李奇

Merge remote-tracking branch 'origin/master' into feature/data-report

# Conflicts:
#	src/app.js
... ... @@ -178,7 +178,7 @@ class App extends Component {
console.log(error);
});
event.on('user-is-login', (loginedCallBack, loginSuccess) => {
event.one('user-is-login', (loginedCallBack, loginSuccess) => {
let userInfo = Taro.getStorageSync('userInfo');
if (userInfo && userInfo.uid) {
checkUidAndSessionKey(userInfo.uid, userInfo.session_key).then(result => {
... ... @@ -189,8 +189,7 @@ class App extends Component {
loginedCallBack();
}
} else {
event.on('my-user-login-success', () => {
event.off('my-user-login-success');
event.one('my-user-login-success', () => {
if (loginSuccess) {
loginSuccess();
}
... ... @@ -214,8 +213,7 @@ class App extends Component {
loginedCallBack();
}
} else {
event.on('my-user-login-success', () => {
event.off('my-user-login-success');
event.one('my-user-login-success', () => {
if (loginSuccess) {
loginSuccess();
}
... ... @@ -231,8 +229,7 @@ class App extends Component {
loginedCallBack();
}
} else {
event.on('my-user-login-success', () => {
event.off('my-user-login-success');
event.one('my-user-login-success', () => {
if (loginSuccess) {
loginSuccess();
}
... ... @@ -249,8 +246,7 @@ class App extends Component {
loginedCallBack();
}
} else {
event.on('my-user-login-success', () => {
event.off('my-user-login-success');
event.one('my-user-login-success', () => {
if (loginSuccess) {
loginSuccess();
}
... ... @@ -261,8 +257,7 @@ class App extends Component {
}
});
} else {
event.on('my-user-login-success', () => {
event.off('my-user-login-success');
event.one('my-user-login-success', () => {
if (loginSuccess) {
loginSuccess();
}
... ... @@ -273,7 +268,7 @@ class App extends Component {
}
});
event.on('judge-user-is-login', (isLogined, isLoginCallback) => {
event.one('judge-user-is-login', (isLogined, isLoginCallback) => {
let userInfo = Taro.getStorageSync('userInfo');
if (userInfo && userInfo.uid) {
checkUidAndSessionKey(userInfo.uid, userInfo.session_key).then(result => {
... ...
... ... @@ -83,7 +83,7 @@ export default class Login extends Component {
eventOnAll() {
const prefix = 'phonenumber';
event.on(this.addPrefix(prefix, USER_GET_PHONENUMBER_ERROR), (errMsg) => {
event.one(this.addPrefix(prefix, USER_GET_PHONENUMBER_ERROR), (errMsg) => {
if (errMsg === 'getPhoneNumber:fail user deny') {
errMsg = '获取手机号失败,请使用验证码登录';
}
... ... @@ -93,13 +93,13 @@ export default class Login extends Component {
icon: 'none'
});
});
event.on(this.addPrefix(prefix, USER_LOGIN_CALLBACK), (error) => {
event.one(this.addPrefix(prefix, USER_LOGIN_CALLBACK), (error) => {
console.log(error);
});
event.on(this.addPrefix(prefix, USER_LOGIN_SUCCESS), () => {
event.one(this.addPrefix(prefix, USER_LOGIN_SUCCESS), () => {
this.redirectAction();
});
event.on(this.addPrefix(prefix, USER_CHANGE_LOGIN_STATUS), (message) => {
event.one(this.addPrefix(prefix, USER_CHANGE_LOGIN_STATUS), (message) => {
const union_id = wx.getStorageSync('unionid');
this.setState({
inviteCode: this.$router.params.inviteCode ? this.$router.params.inviteCode : '',
... ...
... ... @@ -17,19 +17,19 @@ Page({
*/
onLoad: function (options) {
let that = this;
event.on('user-login-success', () => {
event.one('user-login-success', () => {
// 返回上一页
this.goReferer(); // 如果不需要绑定手机则返回前一页
});
event.on('change-login-status', params => {
event.one('change-login-status', params => {
that.setData({
loginText: params.text || '微信登录',
loginTips: params.tips || '',
loginButtonType: params.openType || ''
});
});
event.on('user-login-callback', this.loginCallback);
event.on('user-get-phonenumber-error', this.getPhonenumberError);
event.one('user-login-callback', this.loginCallback);
event.one('user-get-phonenumber-error', this.getPhonenumberError);
},
getPhonenumberError(error) {
... ... @@ -96,7 +96,8 @@ Page({
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
event.remove('user-login-callback', this.loginCallback);
event.remove('user-get-phonenumber-error', this.getPhonenumberError);
},
/**
... ...
... ... @@ -56,7 +56,7 @@ Page({
that.fetchOrderList(2,1);
}
});
event.on('refresh-order', () => {
event.one('refresh-order', () => {
// console.log("======refresh-order")
let currentType = that.data.activeIndex + 2;
this.fetchOrderList(currentType,1);
... ...
export default {
listeners: [],
on(type, handle) {
// first remove and add
this.off(type, handle);
if (typeof handle === 'function') {
console.debug(`listen event ${type}`);
this.listeners.push([type, handle]);
}
},
off(type, handle) {
let index = -1;
if (typeof handle === 'function' &&
this.listeners.some(([itype, ihandle]) => {
index++;
return itype === type && handle.name && ihandle.name === handle.name;
})
) {
this.listeners.splice(index, 1);
}
},
emit(type, ...params) {
this.listeners.forEach(([listenType, handle]) => type === listenType && handle(...params));
console.debug(`receive event ${type}: ${JSON.stringify(params)}`);
},
removeAllListeners() {
this.listeners = [];
}
};
/**
* description: 参考借鉴 Node.js 的 EventEmitter
* 对象管理 event 事件
* @func on 监听
* @func emit 触发
* @func once 监听只执行一次
* @func remove 移除某个类型的某个事件
* @func removeAll 移除某个类型的全部事件/所有类型的全部事件
*/
class EventEmitter {
constructor() {
this._events = Object.create(null);
this._eventsCount = 0;
}
/**
* emit 触发事件
* @param type 事件名
* @param ...args 参数
*/
emit(type) {
let events, handler, len, isFn;
events = this._events;
handler = events[type];
console.log(events);
if (!handler)
return false;
isFn = typeof handler === 'function';
len = arguments.length;
// 优化性能
switch(len) {
case 0:
throw new Error('"emit" not have arguments');
break;
case 1:
emitNone(handler, isFn, this);
break;
case 2:
emitOne(handler, isFn, this, arguments[1]);
break;
case 3:
emitTwo(handler, isFn, this, arguments[1], arguments[2]);
break;
case 4:
emitThree(handler, isFn, this, arguments[1], arguments[2], arguments[3]);
break;
default:
args = new Array(len - 1);
for (let i = 1; i < len; i++) {
args[i - 1] = arguments[i];
}
emitMany(handler, isFn, this, args);
}
return true;
}
one(type, listener,) {
let events;
if (typeof listener !== 'function')
throw new Error('"listener" argument must be a function');
events = this._events;
console.log(events);
if (typeof events === 'undefined') {
events = this._events = Object.create(null);
this._eventsCount = 0;
}
events[type] = listener;
++this._eventsCount;
return this;
}
_addListener(type, listener, prepend) {
let events;
let existing;
if (typeof listener !== 'function')
throw new Error('"listener" argument must be a function');
events = this._events;
console.log(events);
if (typeof events === 'undefined') {
events = this._events = Object.create(null);
this._eventsCount = 0;
} else {
existing = events[type];
}
if (!existing) {
existing = events[type] = listener;
++this._eventsCount;
} else {
if (typeof existing === 'function') {
existing = events[type] = prepend ? [listener, existing] : [existing, listener];
} else if (prepend) {
existing.unshift(listener);
} else {
existing.push(listener);
}
}
return this;
}
/**
* on 监听事件
* @param type 事件名
* @param listener 监听函数
*/
on(type, listener) {
return this._addListener(type, listener, false);
}
/**
* once 监听只执行一次事件
* @param type 事件名
* @param listener 只执行一次的监听函数
*/
once(type, listener) {
if (typeof listener !== 'function')
throw new Error('"listener" argument must be a function');
this.on(type, _onceWrap(this, type, listener));
return this;
}
/**
* prepend 优先监听事件
* @param type 事件名
* @param listener 要优先的监听函数
*/
prepend(type, listener) {
return this._addListener(type, listener, true);
}
/**
* prependOnce 优先只执行一次的监听事件
* @param type 事件名
* @param listener 要优先且只执行一次的监听函数
*/
prependOnce(type, listener) {
if (typeof listener !== 'function')
throw new TypeError('"listener" argument must be a function');
this.prepend(type, _onceWrap(this, type, listener));
return this;
}
/**
* remove 移除的监听事件
* @param type 事件名
* @param listener 要移除的监听函数(可选)
*/
remove(type, listener) {
let list, events, position, originalListener;
if (typeof listener !== 'function')
throw new TypeError('"listener" argument must be a function');
events = this._events;
if (!events) {
return this;
}
list = events[type];
if (!list) {
return this;
}
if (list === listener || list.listener === listener) {
if (--this._eventsCount === 0) {
this._events = Object.create(null);
} else {
delete events[type];
}
} else if (typeof list !== 'function') {
position = -1;
for (let i = list.length - 1; i >= 0; i--) {
if (list[i] === listener || list[i].listener === listener) {
originalListener = list[i].listener;
position = i;
break;
}
}
if (position < 0) {
return this;
}
if (position === 0) {
list.shift();
} else {
spliceOne(list, position);
}
if (list.length === 1) {
events[type] = list[0];
}
}
return this;
}
/**
* removeAll 移除某事件名的全部监听事件/移除全部事件名的事件
* @param type 事件名
*/
removeAll(type) {
let listeners, events;
events = this._events;
if (!events)
return this;
if (arguments.length === 0) {
var keys = Object.keys(events);
var key;
for (let i = 0; i < keys.length; ++i) {
key = keys[i];
this.removeAll(key);
}
this._events = Object.create(null);
this._eventsCount = 0;
return this;
}
listeners = events[type];
if (typeof listeners === 'function') {
this.remove(type, listeners);
} else if (listeners !== undefined) {
for (let i = listeners.length - 1; i >= 0; i--) {
this.remove(type, listeners[i]);
}
}
return this;
}
eventNames() {
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
}
}
const emitNone = (handler, isFn, self) => {
if (isFn) {
handler.call(self);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self);
}
}
const emitOne = (handler, isFn, self, arg1) => {
if (isFn) {
handler.call(self, arg1);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self, arg1);
}
}
const emitTwo = (handler, isFn, self, arg1, arg2) => {
if (isFn) {
handler.call(self, arg1, arg2);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self, arg1, arg2);
}
}
const emitThree = (handler, isFn, self, arg1, arg2, arg3) => {
if (isFn) {
handler.call(self, arg1, arg2, arg3);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].call(self, arg1, arg2, arg3);
}
}
const emitMany = (handler, isFn, self, args) => {
if (isFn) {
handler.apply(self, args);
} else {
let len = handler.length;
let listeners = arrayClone(handler, len);
for (let i = 0; i < len; ++i)
listeners[i].apply(self, args);
}
}
const spliceOne = (list, index) => {
for (; index + 1 < list.length; index++)
list[index] = list[index + 1];
list.pop();
}
function onceWrapper(...args) {
if (!this.fired) {
this.target.remove(this.type, this.wrapFn);
this.fired = true;
Reflect.apply(this.listener, this.target, args);
}
}
const _onceWrap = (target, type, listener) => {
var state = { fired: false, wrapFn: undefined, target, type, listener };
var wrapped = onceWrapper.bind(state);
wrapped.listener = listener;
state.wrapFn = wrapped;
return wrapped;
}
function arrayClone(arr, n) {
var copy = new Array(n);
for (var i = 0; i < n; ++i)
copy[i] = arr[i];
return copy;
}
const event = new EventEmitter();
export default event;
\ No newline at end of file
... ...