add
This commit is contained in:
parent
b8aaa100b4
commit
6605fe5571
|
@ -99,6 +99,10 @@ export const vipRechargeApi = (data) => {
|
||||||
return request.post('/store/store/recharge', data);
|
return request.post('/store/store/recharge', data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const reVipRechargeApi = (data) => {
|
||||||
|
return request.post('/store/store/again_recharge', data);
|
||||||
|
}
|
||||||
|
|
||||||
export const rechargeCountApi = (data) => {
|
export const rechargeCountApi = (data) => {
|
||||||
return request.get('/store/store/recharge_count', data);
|
return request.get('/store/store/recharge_count', data);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
let BASE_URL
|
let BASE_URL
|
||||||
|
let WSS_URL
|
||||||
import store from "@/store/user.js"
|
import store from "@/store/user.js"
|
||||||
// 环境
|
// 环境
|
||||||
let env = "dev"
|
let env = "dev"
|
||||||
|
@ -8,12 +9,15 @@ let env = "dev"
|
||||||
switch (env) {
|
switch (env) {
|
||||||
case 'dev':
|
case 'dev':
|
||||||
BASE_URL = 'https://test-multi-store.lihaink.cn';
|
BASE_URL = 'https://test-multi-store.lihaink.cn';
|
||||||
|
WSS_URL = 'wss://test-multi-store.lihaink.cn/pull'
|
||||||
break;
|
break;
|
||||||
case 'liu':
|
case 'liu':
|
||||||
BASE_URL = 'http://192.168.1.201:8545';
|
BASE_URL = 'http://192.168.1.201:8545';
|
||||||
|
WSS_URL = 'wss://test-multi-store.lihaink.cn/pull'
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
BASE_URL = 'https://multi-store.lihaink.cn';
|
BASE_URL = 'https://multi-store.lihaink.cn';
|
||||||
|
WSS_URL = 'wss://test-multi-store.lihaink.cn/pull'
|
||||||
}
|
}
|
||||||
|
|
||||||
let HTTP_REQUEST_URL
|
let HTTP_REQUEST_URL
|
||||||
|
@ -21,6 +25,7 @@ let HEADER
|
||||||
|
|
||||||
let config = {
|
let config = {
|
||||||
HTTP_REQUEST_URL: BASE_URL,
|
HTTP_REQUEST_URL: BASE_URL,
|
||||||
|
WSS_URL: WSS_URL,
|
||||||
HEADER: {
|
HEADER: {
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
//#ifdef MP
|
//#ifdef MP
|
||||||
|
|
|
@ -21,8 +21,8 @@
|
||||||
<view class="li" v-for="(item,index) in lists" :key="index">
|
<view class="li" v-for="(item,index) in lists" :key="index">
|
||||||
<view class="li-top">
|
<view class="li-top">
|
||||||
<text>{{item.title}}</text>
|
<text>{{item.title}}</text>
|
||||||
<text style="font-weight: bold;" :style="{color:item.financial_pm?'#50C758':'red'
|
<text style="font-weight: bold;" :style="{color:!item.financial_pm?'#50C758':'red'
|
||||||
}">{{ item.financial_pm?'+':'-' }}{{item.number}}</text>
|
}">{{ !item.financial_pm?'+':'-' }}{{item.number}}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="li-top" style="margin-bottom: 0;">
|
<view class="li-top" style="margin-bottom: 0;">
|
||||||
<text style="font-size: 24rpx;color: grey;">{{item.create_time}}</text>
|
<text style="font-size: 24rpx;color: grey;">{{item.create_time}}</text>
|
||||||
|
@ -33,8 +33,8 @@
|
||||||
<view class="li" v-for="(item,index) in lists" :key="index">
|
<view class="li" v-for="(item,index) in lists" :key="index">
|
||||||
<view class="li-top">
|
<view class="li-top">
|
||||||
<text>{{item.order_sn}}</text>
|
<text>{{item.order_sn}}</text>
|
||||||
<text style="font-weight: bold;" :style="{color:item.status?'#50C758':'red'
|
<text style="font-weight: bold;" :style="{color:!item.status?'#50C758':'red'
|
||||||
}">{{ item.status?'+':'-' }}{{item.number}}</text>
|
}">{{ !item.status?'+':'-' }}{{item.number}}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="li-top" style="margin-bottom: 0;">
|
<view class="li-top" style="margin-bottom: 0;">
|
||||||
<text style="font-size: 24rpx;color: grey;">{{item.create_time}}</text>
|
<text style="font-size: 24rpx;color: grey;">{{item.create_time}}</text>
|
||||||
|
@ -68,10 +68,10 @@
|
||||||
name: '全部'
|
name: '全部'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: '支出'
|
name: '收入'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: '收入'
|
name: '支出'
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -81,7 +81,6 @@
|
||||||
getLists()
|
getLists()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const lists = ref([])
|
const lists = ref([])
|
||||||
let type = ref('')
|
let type = ref('')
|
||||||
const getLists = async (isPullDown = false) => {
|
const getLists = async (isPullDown = false) => {
|
||||||
|
|
|
@ -28,19 +28,19 @@
|
||||||
{{Role==1?'行业会员':'商户'}}开通报备
|
{{Role==1?'行业会员':'商户'}}开通报备
|
||||||
</view>
|
</view>
|
||||||
<up-form labelPosition="left" :model="formData" :borderBottom='false'>
|
<up-form labelPosition="left" :model="formData" :borderBottom='false'>
|
||||||
<up-form-item label="" prop="userInfo.name">
|
<up-form-item label="">
|
||||||
<up-input v-model="formData.real_name" border="none" prefixIcon="account" placeholder="请输入真实姓名"
|
<up-input v-model="formData.real_name" border="none" prefixIcon="account" placeholder="请输入真实姓名"
|
||||||
:customStyle="{background:'#F3F3F3',padding:'20rpx','border-radius':'30rpx'}"
|
:customStyle="{background:'#F3F3F3',padding:'20rpx','border-radius':'30rpx'}"
|
||||||
:placeholderStyle="{color:'#444444'}"
|
:placeholderStyle="{color:'#444444'}"
|
||||||
:prefixIconStyle="{'margin-right':'40rpx'}"></up-input>
|
:prefixIconStyle="{'margin-right':'40rpx'}"></up-input>
|
||||||
</up-form-item>
|
</up-form-item>
|
||||||
<up-form-item label="" prop="userInfo.name">
|
<up-form-item label="">
|
||||||
<up-input v-model="formData.mobile" border="none" prefixIcon="phone" placeholder="请输入电话号码"
|
<up-input v-model="formData.mobile" border="none" prefixIcon="phone" placeholder="请输入电话号码"
|
||||||
:customStyle="{background:'#F3F3F3',padding:'20rpx','border-radius':'30rpx'}"
|
:customStyle="{background:'#F3F3F3',padding:'20rpx','border-radius':'30rpx'}"
|
||||||
:placeholderStyle="{color:'#444444'}"
|
:placeholderStyle="{color:'#444444'}"
|
||||||
:prefixIconStyle="{'margin-right':'40rpx'}"></up-input>
|
:prefixIconStyle="{'margin-right':'40rpx'}"></up-input>
|
||||||
</up-form-item>
|
</up-form-item>
|
||||||
<up-form-item label="" prop="userInfo.name">
|
<up-form-item label="">
|
||||||
<view @click="showPop=true" style="width: 100%;">
|
<view @click="showPop=true" style="width: 100%;">
|
||||||
<up-input style="pointer-events: none" v-model="formData.address" border="none"
|
<up-input style="pointer-events: none" v-model="formData.address" border="none"
|
||||||
prefixIcon="map" readonly placeholder="点击选择地址" :customStyle="{background:'#F3F3F3',padding:'20rpx',
|
prefixIcon="map" readonly placeholder="点击选择地址" :customStyle="{background:'#F3F3F3',padding:'20rpx',
|
||||||
|
@ -50,7 +50,7 @@
|
||||||
</view>
|
</view>
|
||||||
</up-form-item>
|
</up-form-item>
|
||||||
|
|
||||||
<up-form-item label="" prop="userInfo.name" v-if='Role==1'>
|
<up-form-item label="" v-if='Role==1'>
|
||||||
<view @click="showPop1=true" style="width: 100%;">
|
<view @click="showPop1=true" style="width: 100%;">
|
||||||
<up-input style="pointer-events: none" v-model="formData.label_name" border="none"
|
<up-input style="pointer-events: none" v-model="formData.label_name" border="none"
|
||||||
prefixIcon="man-add" readonly placeholder="点击选择用户身份" :customStyle="{background:'#F3F3F3',padding:'20rpx',
|
prefixIcon="man-add" readonly placeholder="点击选择用户身份" :customStyle="{background:'#F3F3F3',padding:'20rpx',
|
||||||
|
@ -107,15 +107,21 @@
|
||||||
<uni-td style="font-size: 20rpx;" align="center" v-if='Role==1'>{{item.label_name}}</uni-td>
|
<uni-td style="font-size: 20rpx;" align="center" v-if='Role==1'>{{item.label_name}}</uni-td>
|
||||||
<uni-td style="font-size: 20rpx;" align="center">
|
<uni-td style="font-size: 20rpx;" align="center">
|
||||||
<view v-if="item.paid ==1 ||Role==4">已开通</view>
|
<view v-if="item.paid ==1 ||Role==4">已开通</view>
|
||||||
<view v-else @click="upadtaStatus(item)" style="color:#33B83A ;">未开通,查询</view>
|
<view v-else style="color:#33B83A ;">
|
||||||
|
<view @click="upadtaStatus(item)">
|
||||||
|
未开通,查询
|
||||||
|
</view>
|
||||||
|
<view @click="rePay(item)">
|
||||||
|
重新支付
|
||||||
|
</view>
|
||||||
|
</view>
|
||||||
</uni-td>
|
</uni-td>
|
||||||
</uni-tr>
|
</uni-tr>
|
||||||
</uni-table>
|
</uni-table>
|
||||||
</view>
|
</view>
|
||||||
</block>
|
</block>
|
||||||
</view>
|
</view>
|
||||||
|
<view style="height: 50rpx;" />
|
||||||
|
|
||||||
|
|
||||||
<!-- 地址选择器 -->
|
<!-- 地址选择器 -->
|
||||||
<up-popup :show="showPop" @close="showPop=false" @open="showPop=true" :round="10">
|
<up-popup :show="showPop" @close="showPop=false" @open="showPop=true" :round="10">
|
||||||
|
@ -169,6 +175,7 @@
|
||||||
reactive,
|
reactive,
|
||||||
ref
|
ref
|
||||||
} from 'vue';
|
} from 'vue';
|
||||||
|
import useUserStore from "@/store/user";
|
||||||
import {
|
import {
|
||||||
provinceListApi,
|
provinceListApi,
|
||||||
cityListApi,
|
cityListApi,
|
||||||
|
@ -180,6 +187,7 @@
|
||||||
import {
|
import {
|
||||||
vipRechargeApi,
|
vipRechargeApi,
|
||||||
rechargeCountApi,
|
rechargeCountApi,
|
||||||
|
reVipRechargeApi,
|
||||||
rechargeListsApi,
|
rechargeListsApi,
|
||||||
updataOrderApi,
|
updataOrderApi,
|
||||||
getStoreByPhone,
|
getStoreByPhone,
|
||||||
|
@ -187,11 +195,59 @@
|
||||||
getUserShip,
|
getUserShip,
|
||||||
getCreateLists
|
getCreateLists
|
||||||
} from "@/api/user.js"
|
} from "@/api/user.js"
|
||||||
|
import Push from "@/utils/push.js"
|
||||||
import {
|
import {
|
||||||
onPullDownRefresh,
|
onPullDownRefresh,
|
||||||
onLoad
|
onLoad
|
||||||
} from "@dcloudio/uni-app"
|
} from "@dcloudio/uni-app"
|
||||||
|
import {
|
||||||
|
config
|
||||||
|
} from "@/config/app.js"
|
||||||
|
|
||||||
|
|
||||||
|
// scoket
|
||||||
|
let timer = null
|
||||||
|
const userInfo = useUserStore().userInfo;
|
||||||
|
const connection = new Push({
|
||||||
|
url: config.WSS_URL, // websocket地址
|
||||||
|
app_key: '2ce3ce22329517213caa7dad261f5695',
|
||||||
|
});
|
||||||
|
|
||||||
|
// 浏览器监听user-1
|
||||||
|
const user_channel = connection.subscribe(`wechat_mmp_${userInfo.id}}`);
|
||||||
|
|
||||||
|
user_channel.on('message', function(data) {
|
||||||
|
console.log("收到消息--", data);
|
||||||
|
try {
|
||||||
|
if (timer) clearTimeout(timer);
|
||||||
|
uni.hideLoading()
|
||||||
|
currentTab.value = 2
|
||||||
|
formData.real_name = ''
|
||||||
|
formData.mobile = ''
|
||||||
|
formData.address = ''
|
||||||
|
formData.label_name = ''
|
||||||
|
formData.city = ''
|
||||||
|
formData.area = ''
|
||||||
|
formData.street = ''
|
||||||
|
formData.village = ''
|
||||||
|
formData.brigade = ''
|
||||||
|
currentAddressIndex.value = 0
|
||||||
|
tabsList.forEach(item => {
|
||||||
|
item.name = '请选择'
|
||||||
|
})
|
||||||
|
getLists()
|
||||||
|
getCount()
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 断线事件
|
||||||
|
user_channel.on('close', function() {
|
||||||
|
|
||||||
|
});
|
||||||
|
// scoket结束
|
||||||
|
|
||||||
|
|
||||||
const showPop1 = ref(false)
|
const showPop1 = ref(false)
|
||||||
const Role = ref('')
|
const Role = ref('')
|
||||||
|
@ -377,22 +433,28 @@
|
||||||
formData.recharge_type = 'INDUSTRYMEMBERS'
|
formData.recharge_type = 'INDUSTRYMEMBERS'
|
||||||
formData.user_ship = 1
|
formData.user_ship = 1
|
||||||
vipRechargeApi(formData).then(res => {
|
vipRechargeApi(formData).then(res => {
|
||||||
uni.$u.toast('操作成功');
|
uni.showLoading({
|
||||||
currentTab.value = 2
|
title: '支付中...'
|
||||||
formData.real_name = ''
|
});
|
||||||
formData.mobile = ''
|
timer = setTimeout(() => {
|
||||||
formData.address = ''
|
uni.hideLoading();
|
||||||
formData.label_name = ''
|
uni.$u.toast('支付超时');
|
||||||
formData.city = ''
|
// formData.real_name = ''
|
||||||
formData.area = ''
|
// formData.mobile = ''
|
||||||
formData.street = ''
|
// formData.address = ''
|
||||||
formData.village = ''
|
// formData.label_name = ''
|
||||||
formData.brigade = ''
|
// formData.city = ''
|
||||||
currentAddressIndex.value = 0
|
// formData.area = ''
|
||||||
|
// formData.street = ''
|
||||||
|
// formData.village = ''
|
||||||
|
// formData.brigade = ''
|
||||||
|
// currentAddressIndex.value = 0
|
||||||
|
// tabsList.forEach(item => {
|
||||||
|
// item.name = '请选择'
|
||||||
|
// })
|
||||||
|
}, 30000)
|
||||||
|
|
||||||
tabsList.forEach(item => {
|
// uni.$u.toast('操作成功');
|
||||||
item.name = '请选择'
|
|
||||||
})
|
|
||||||
|
|
||||||
})
|
})
|
||||||
getCount()
|
getCount()
|
||||||
|
@ -426,6 +488,24 @@
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const rePay = (item) => {
|
||||||
|
uni.scanCode({
|
||||||
|
success: function(res) {
|
||||||
|
reVipRechargeApi({
|
||||||
|
id: item.id,
|
||||||
|
auth_code: res.result
|
||||||
|
}).then(res => {
|
||||||
|
uni.$u.toast('操作成功');
|
||||||
|
getCount()
|
||||||
|
getLists()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// 邀请用户数
|
// 邀请用户数
|
||||||
const count = ref(0)
|
const count = ref(0)
|
||||||
const getCount = async () => {
|
const getCount = async () => {
|
||||||
|
@ -451,7 +531,6 @@
|
||||||
})
|
})
|
||||||
lists.value = res.data.lists
|
lists.value = res.data.lists
|
||||||
count.value = res.data.count
|
count.value = res.data.count
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -464,13 +543,15 @@
|
||||||
updataOrderApi({
|
updataOrderApi({
|
||||||
order_no: item.order_id,
|
order_no: item.order_id,
|
||||||
recharge: 1
|
recharge: 1
|
||||||
|
}).then(res => {
|
||||||
|
getCount()
|
||||||
|
getLists()
|
||||||
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onLoad(() => {
|
onLoad(() => {
|
||||||
getUserShip().then(res => {
|
getUserShip().then(res => {
|
||||||
console.log(res)
|
|
||||||
|
|
||||||
range.value = res.data.lists.map(item => {
|
range.value = res.data.lists.map(item => {
|
||||||
return {
|
return {
|
||||||
value: item.id,
|
value: item.id,
|
||||||
|
|
|
@ -53,7 +53,7 @@
|
||||||
"style": {
|
"style": {
|
||||||
"navigationBarTitleText": "个人中心",
|
"navigationBarTitleText": "个人中心",
|
||||||
"enablePullDownRefresh": true,
|
"enablePullDownRefresh": true,
|
||||||
"navigationStyle": "custom"
|
"navigationBarBackgroundColor": "#D3FDCA"
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
"path": "pages/charge/charge_record",
|
"path": "pages/charge/charge_record",
|
||||||
|
|
|
@ -23,8 +23,8 @@
|
||||||
</style>
|
</style>
|
||||||
<template>
|
<template>
|
||||||
<view>
|
<view>
|
||||||
<up-navbar title="我的" :autoBack="true" bgColor="transparent" :fixed="true">
|
<!-- <up-navbar title="我的" :autoBack="true" bgColor="transparent" :fixed="true">
|
||||||
</up-navbar>
|
</up-navbar> -->
|
||||||
<view class="user-info">
|
<view class="user-info">
|
||||||
<image class="bg" src="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/e3a7b202404261113002322.webp"
|
<image class="bg" src="https://lihai001.oss-cn-chengdu.aliyuncs.com/def/e3a7b202404261113002322.webp"
|
||||||
mode="widthFix"></image>
|
mode="widthFix"></image>
|
||||||
|
@ -198,7 +198,7 @@
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
position: relative;
|
position: relative;
|
||||||
width: 750rpx;
|
width: 750rpx;
|
||||||
height: 350rpx;
|
height: 250rpx;
|
||||||
|
|
||||||
.bg {
|
.bg {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
|
@ -212,7 +212,7 @@
|
||||||
box-sizing: border-box;
|
box-sizing: border-box;
|
||||||
padding: 50rpx 0 50rpx 50rpx;
|
padding: 50rpx 0 50rpx 50rpx;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 150rpx;
|
top: 50rpx;
|
||||||
left: 0;
|
left: 0;
|
||||||
z-index: 2;
|
z-index: 2;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
394
utils/push.js
394
utils/push.js
|
@ -1,6 +1,4 @@
|
||||||
|
function Push(options) {
|
||||||
|
|
||||||
export const Push = function Push(options) {
|
|
||||||
this.doNotConnect = 0;
|
this.doNotConnect = 0;
|
||||||
options = options || {};
|
options = options || {};
|
||||||
options.heartbeat = options.heartbeat || 25000;
|
options.heartbeat = options.heartbeat || 25000;
|
||||||
|
@ -17,7 +15,7 @@ export const Push = function Push(options) {
|
||||||
Push.prototype.checkoutPing = function() {
|
Push.prototype.checkoutPing = function() {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
_this.checkoutPingTimer && clearTimeout(_this.checkoutPingTimer);
|
_this.checkoutPingTimer && clearTimeout(_this.checkoutPingTimer);
|
||||||
_this.checkoutPingTimer = setTimeout(function () {
|
_this.checkoutPingTimer = setTimeout(function() {
|
||||||
_this.checkoutPingTimer = 0;
|
_this.checkoutPingTimer = 0;
|
||||||
if (_this.connection.state === 'connected') {
|
if (_this.connection.state === 'connected') {
|
||||||
_this.connection.send('{"event":"pusher:ping","data":{}}');
|
_this.connection.send('{"event":"pusher:ping","data":{}}');
|
||||||
|
@ -25,7 +23,7 @@ Push.prototype.checkoutPing = function() {
|
||||||
clearTimeout(_this.pingTimeoutTimer);
|
clearTimeout(_this.pingTimeoutTimer);
|
||||||
_this.pingTimeoutTimer = 0;
|
_this.pingTimeoutTimer = 0;
|
||||||
}
|
}
|
||||||
_this.pingTimeoutTimer = setTimeout(function () {
|
_this.pingTimeoutTimer = setTimeout(function() {
|
||||||
_this.connection.closeAndClean();
|
_this.connection.closeAndClean();
|
||||||
if (!_this.connection.doNotConnect) {
|
if (!_this.connection.doNotConnect) {
|
||||||
_this.connection.waitReconnect();
|
_this.connection.waitReconnect();
|
||||||
|
@ -35,19 +33,20 @@ Push.prototype.checkoutPing = function() {
|
||||||
}, this.config.heartbeat);
|
}, this.config.heartbeat);
|
||||||
};
|
};
|
||||||
|
|
||||||
Push.prototype.channel = function (name) {
|
Push.prototype.channel = function(name) {
|
||||||
return this.channels.find(name);
|
return this.channels.find(name);
|
||||||
};
|
};
|
||||||
Push.prototype.allChannels = function () {
|
Push.prototype.allChannels = function() {
|
||||||
return this.channels.all();
|
return this.channels.all();
|
||||||
};
|
};
|
||||||
Push.prototype.createConnection = function () {
|
Push.prototype.createConnection = function() {
|
||||||
if (this.connection) {
|
if (this.connection) {
|
||||||
throw Error('Connection already exist');
|
throw Error('Connection already exist');
|
||||||
}
|
}
|
||||||
var _this = this;
|
var _this = this;
|
||||||
var url = this.config.url;
|
var url = this.config.url;
|
||||||
function updateSubscribed () {
|
|
||||||
|
function updateSubscribed() {
|
||||||
for (var i in _this.channels) {
|
for (var i in _this.channels) {
|
||||||
_this.channels[i].subscribed = false;
|
_this.channels[i].subscribed = false;
|
||||||
}
|
}
|
||||||
|
@ -55,12 +54,12 @@ Push.prototype.createConnection = function () {
|
||||||
this.connection = new Connection({
|
this.connection = new Connection({
|
||||||
url: url,
|
url: url,
|
||||||
app_key: this.config.app_key,
|
app_key: this.config.app_key,
|
||||||
onOpen: function () {
|
onOpen: function() {
|
||||||
_this.connection.state ='connecting';
|
_this.connection.state = 'connecting';
|
||||||
_this.checkoutPing();
|
_this.checkoutPing();
|
||||||
},
|
},
|
||||||
onMessage: function(params) {
|
onMessage: function(params) {
|
||||||
if(_this.pingTimeoutTimer) {
|
if (_this.pingTimeoutTimer) {
|
||||||
clearTimeout(_this.pingTimeoutTimer);
|
clearTimeout(_this.pingTimeoutTimer);
|
||||||
_this.pingTimeoutTimer = 0;
|
_this.pingTimeoutTimer = 0;
|
||||||
}
|
}
|
||||||
|
@ -76,7 +75,8 @@ Push.prototype.createConnection = function () {
|
||||||
if (event === 'pusher:error') {
|
if (event === 'pusher:error') {
|
||||||
throw Error(params.data.message);
|
throw Error(params.data.message);
|
||||||
}
|
}
|
||||||
var data = JSON.parse(params.data), channel;
|
var data = JSON.parse(params.data),
|
||||||
|
channel;
|
||||||
if (event === 'pusher_internal:subscription_succeeded') {
|
if (event === 'pusher_internal:subscription_succeeded') {
|
||||||
channel = _this.channels[channel_name];
|
channel = _this.channels[channel_name];
|
||||||
channel.subscribed = true;
|
channel.subscribed = true;
|
||||||
|
@ -90,7 +90,7 @@ Push.prototype.createConnection = function () {
|
||||||
_this.subscribeAll();
|
_this.subscribeAll();
|
||||||
}
|
}
|
||||||
if (event.indexOf('pusher_internal') !== -1) {
|
if (event.indexOf('pusher_internal') !== -1) {
|
||||||
console.log("Event '"+event+"' not implement");
|
console.log("Event '" + event + "' not implement");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
channel = _this.channels[channel_name];
|
channel = _this.channels[channel_name];
|
||||||
|
@ -98,20 +98,20 @@ Push.prototype.createConnection = function () {
|
||||||
channel.emit(event, data);
|
channel.emit(event, data);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onClose: function () {
|
onClose: function() {
|
||||||
updateSubscribed();
|
updateSubscribed();
|
||||||
},
|
},
|
||||||
onError: function () {
|
onError: function() {
|
||||||
updateSubscribed();
|
updateSubscribed();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Push.prototype.disconnect = function () {
|
Push.prototype.disconnect = function() {
|
||||||
this.connection.doNotConnect = 1;
|
this.connection.doNotConnect = 1;
|
||||||
this.connection.close();
|
this.connection.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
Push.prototype.subscribeAll = function () {
|
Push.prototype.subscribeAll = function() {
|
||||||
if (this.connection.state !== 'connected') {
|
if (this.connection.state !== 'connected') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -121,15 +121,20 @@ Push.prototype.subscribeAll = function () {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Push.prototype.unsubscribe = function (channel_name) {
|
Push.prototype.unsubscribe = function(channel_name) {
|
||||||
if (this.channels[channel_name]) {
|
if (this.channels[channel_name]) {
|
||||||
delete this.channels[channel_name];
|
delete this.channels[channel_name];
|
||||||
if (this.connection.state === 'connected') {
|
if (this.connection.state === 'connected') {
|
||||||
this.connection.send(JSON.stringify({event:"pusher:unsubscribe", data:{channel:channel_name}}));
|
this.connection.send(JSON.stringify({
|
||||||
|
event: "pusher:unsubscribe",
|
||||||
|
data: {
|
||||||
|
channel: channel_name
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Push.prototype.unsubscribeAll = function () {
|
Push.prototype.unsubscribeAll = function() {
|
||||||
var channels = Object.keys(this.channels);
|
var channels = Object.keys(this.channels);
|
||||||
if (channels.length) {
|
if (channels.length) {
|
||||||
if (this.connection.state === 'connected') {
|
if (this.connection.state === 'connected') {
|
||||||
|
@ -140,7 +145,7 @@ Push.prototype.unsubscribeAll = function () {
|
||||||
}
|
}
|
||||||
this.channels = {};
|
this.channels = {};
|
||||||
};
|
};
|
||||||
Push.prototype.subscribe = function (channel_name) {
|
Push.prototype.subscribe = function(channel_name) {
|
||||||
if (this.channels[channel_name]) {
|
if (this.channels[channel_name]) {
|
||||||
return this.channels[channel_name];
|
return this.channels[channel_name];
|
||||||
}
|
}
|
||||||
|
@ -154,32 +159,41 @@ Push.prototype.subscribe = function (channel_name) {
|
||||||
};
|
};
|
||||||
Push.instances = [];
|
Push.instances = [];
|
||||||
|
|
||||||
function createChannel(channel_name, push)
|
function createChannel(channel_name, push) {
|
||||||
{
|
|
||||||
var channel = new Channel(push.connection, channel_name);
|
var channel = new Channel(push.connection, channel_name);
|
||||||
push.channels[channel_name] = channel;
|
push.channels[channel_name] = channel;
|
||||||
channel.subscribeCb = function () {
|
channel.subscribeCb = function() {
|
||||||
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:{channel:channel_name}}));
|
push.connection.send(JSON.stringify({
|
||||||
|
event: "pusher:subscribe",
|
||||||
|
data: {
|
||||||
|
channel: channel_name
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
channel.processSubscribe();
|
channel.processSubscribe();
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPrivateChannel(channel_name, push)
|
function createPrivateChannel(channel_name, push) {
|
||||||
{
|
|
||||||
var channel = new Channel(push.connection, channel_name);
|
var channel = new Channel(push.connection, channel_name);
|
||||||
push.channels[channel_name] = channel;
|
push.channels[channel_name] = channel;
|
||||||
channel.subscribeCb = function () {
|
channel.subscribeCb = function() {
|
||||||
__ajax({
|
__ajax({
|
||||||
url: push.config.auth,
|
url: push.config.auth,
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
data: {channel_name: channel_name, socket_id: push.connection.socket_id},
|
data: {
|
||||||
success: function (data) {
|
channel_name: channel_name,
|
||||||
|
socket_id: push.connection.socket_id
|
||||||
|
},
|
||||||
|
success: function(data) {
|
||||||
data = JSON.parse(data);
|
data = JSON.parse(data);
|
||||||
data.channel = channel_name;
|
data.channel = channel_name;
|
||||||
push.connection.send(JSON.stringify({event:"pusher:subscribe", data:data}));
|
push.connection.send(JSON.stringify({
|
||||||
|
event: "pusher:subscribe",
|
||||||
|
data: data
|
||||||
|
}));
|
||||||
},
|
},
|
||||||
error: function (e) {
|
error: function(e) {
|
||||||
throw Error(e);
|
throw Error(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -188,13 +202,12 @@ function createPrivateChannel(channel_name, push)
|
||||||
return channel;
|
return channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPresenceChannel(channel_name, push)
|
function createPresenceChannel(channel_name, push) {
|
||||||
{
|
|
||||||
return createPrivateChannel(channel_name, push);
|
return createPrivateChannel(channel_name, push);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*window.addEventListener('online', function(){
|
uni.onNetworkStatusChange(function(res) {
|
||||||
var con;
|
if (res.isConnected) {
|
||||||
for (var i in Push.instances) {
|
for (var i in Push.instances) {
|
||||||
con = Push.instances[i].connection;
|
con = Push.instances[i].connection;
|
||||||
con.reconnectInterval = 1;
|
con.reconnectInterval = 1;
|
||||||
|
@ -202,8 +215,8 @@ function createPresenceChannel(channel_name, push)
|
||||||
con.connect();
|
con.connect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});*/
|
}
|
||||||
|
});
|
||||||
|
|
||||||
function Connection(options) {
|
function Connection(options) {
|
||||||
this.dispatcher = new Dispatcher();
|
this.dispatcher = new Dispatcher();
|
||||||
|
@ -221,15 +234,87 @@ function Connection(options) {
|
||||||
this.connect();
|
this.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection.prototype.updateNetworkState = function(state){
|
Connection.prototype.updateNetworkState = function(state) {
|
||||||
var old_state = this.state;
|
var old_state = this.state;
|
||||||
this.state = state;
|
this.state = state;
|
||||||
if (old_state !== state) {
|
if (old_state !== state) {
|
||||||
this.emit('state_change', { previous: old_state, current: state });
|
this.emit('state_change', {
|
||||||
|
previous: old_state,
|
||||||
|
current: state
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Connection.prototype.connect = function () {
|
Connection.prototype.connect = function() {
|
||||||
|
this.doNotConnect = 0;
|
||||||
|
if (this.networkState == 'connecting' || this.networkState == 'established') {
|
||||||
|
console.log('networkState is ' + this.networkState + ' and do not need connect');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.reconnectTimer) {
|
||||||
|
clearTimeout(this.reconnectTimer);
|
||||||
|
this.reconnectTimer = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.closeAndClean();
|
||||||
|
|
||||||
|
var options = this.options;
|
||||||
|
var _this = this;
|
||||||
|
_this.updateNetworkState('connecting');
|
||||||
|
var cb = function() {
|
||||||
|
uni.onSocketOpen(function(res) {
|
||||||
|
_this.reconnectInterval = 1;
|
||||||
|
if (_this.doNotConnect) {
|
||||||
|
_this.updateNetworkState('closing');
|
||||||
|
uni.closeSocket();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_this.updateNetworkState('established');
|
||||||
|
if (options.onOpen) {
|
||||||
|
options.onOpen(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (options.onMessage) {
|
||||||
|
uni.onSocketMessage(options.onMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
uni.onSocketClose(function(res) {
|
||||||
|
_this.updateNetworkState('disconnected');
|
||||||
|
if (!_this.doNotConnect) {
|
||||||
|
_this.waitReconnect();
|
||||||
|
}
|
||||||
|
if (options.onClose) {
|
||||||
|
options.onClose(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
uni.onSocketError(function(res) {
|
||||||
|
_this.close();
|
||||||
|
if (!_this.doNotConnect) {
|
||||||
|
_this.waitReconnect();
|
||||||
|
}
|
||||||
|
if (options.onError) {
|
||||||
|
options.onError(res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
uni.connectSocket({
|
||||||
|
url: options.url,
|
||||||
|
fail: function(res) {
|
||||||
|
console.log('uni.connectSocket fail');
|
||||||
|
console.log(res);
|
||||||
|
_this.updateNetworkState('disconnected');
|
||||||
|
_this.waitReconnect();
|
||||||
|
},
|
||||||
|
success: function() {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
|
||||||
|
Connection.prototype.connect = function() {
|
||||||
this.doNotConnect = 0;
|
this.doNotConnect = 0;
|
||||||
if (this.state === 'connected') {
|
if (this.state === 'connected') {
|
||||||
console.log('networkState is "' + this.state + '" and do not need connect');
|
console.log('networkState is "' + this.state + '" and do not need connect');
|
||||||
|
@ -243,29 +328,28 @@ Connection.prototype.connect = function () {
|
||||||
this.closeAndClean();
|
this.closeAndClean();
|
||||||
|
|
||||||
var options = this.options;
|
var options = this.options;
|
||||||
var websocket = new WebSocket(options.url+'/app/'+options.app_key);
|
|
||||||
|
|
||||||
this.updateNetworkState('connecting');
|
this.updateNetworkState('connecting');
|
||||||
|
|
||||||
var _this = this;
|
var _this = this;
|
||||||
websocket.onopen = function (res) {
|
var cb = function() {
|
||||||
|
uni.onSocketOpen(function(res) {
|
||||||
_this.reconnectInterval = 1;
|
_this.reconnectInterval = 1;
|
||||||
if (_this.doNotConnect) {
|
if (_this.doNotConnect) {
|
||||||
_this.updateNetworkState('disconnected');
|
_this.updateNetworkState('disconnected');
|
||||||
websocket.close();
|
uni.closeSocket();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (options.onOpen) {
|
if (options.onOpen) {
|
||||||
options.onOpen(res);
|
options.onOpen(res);
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
if (options.onMessage) {
|
if (options.onMessage) {
|
||||||
websocket.onmessage = options.onMessage;
|
uni.onSocketMessage(options.onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
websocket.onclose = function (res) {
|
uni.onSocketClose(function(res) {
|
||||||
websocket.onmessage = websocket.onopen = websocket.onclose = websocket.onerror = null;
|
|
||||||
_this.updateNetworkState('disconnected');
|
_this.updateNetworkState('disconnected');
|
||||||
if (!_this.doNotConnect) {
|
if (!_this.doNotConnect) {
|
||||||
_this.waitReconnect();
|
_this.waitReconnect();
|
||||||
|
@ -273,9 +357,9 @@ Connection.prototype.connect = function () {
|
||||||
if (options.onClose) {
|
if (options.onClose) {
|
||||||
options.onClose(res);
|
options.onClose(res);
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
websocket.onerror = function (res) {
|
uni.onSocketError(function(res) {
|
||||||
_this.close();
|
_this.close();
|
||||||
if (!_this.doNotConnect) {
|
if (!_this.doNotConnect) {
|
||||||
_this.waitReconnect();
|
_this.waitReconnect();
|
||||||
|
@ -283,22 +367,31 @@ Connection.prototype.connect = function () {
|
||||||
if (options.onError) {
|
if (options.onError) {
|
||||||
options.onError(res);
|
options.onError(res);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
this.connection = websocket;
|
uni.connectSocket({
|
||||||
|
url: options.url + '/app/' + options.app_key,
|
||||||
|
fail: function(res) {
|
||||||
|
console.log('uni.connectSocket fail');
|
||||||
|
console.log(res);
|
||||||
|
_this.updateNetworkState('disconnected');
|
||||||
|
_this.waitReconnect();
|
||||||
|
},
|
||||||
|
success: function() {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
cb();
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection.prototype.closeAndClean = function () {
|
Connection.prototype.closeAndClean = function() {
|
||||||
if(this.connection) {
|
if (this.state === 'connected') {
|
||||||
var websocket = this.connection;
|
uni.closeSocket();
|
||||||
websocket.onmessage = websocket.onopen = websocket.onclose = websocket.onerror = null;
|
|
||||||
try {
|
|
||||||
websocket.close();
|
|
||||||
} catch (e) {}
|
|
||||||
this.updateNetworkState('disconnected');
|
|
||||||
}
|
}
|
||||||
|
this.updateNetworkState('disconnected');
|
||||||
};
|
};
|
||||||
|
|
||||||
Connection.prototype.waitReconnect = function () {
|
Connection.prototype.waitReconnect = function() {
|
||||||
if (this.state === 'connected' || this.state === 'connecting') {
|
if (this.state === 'connected' || this.state === 'connecting') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -308,7 +401,7 @@ Connection.prototype.waitReconnect = function () {
|
||||||
if (this.reconnectTimer) {
|
if (this.reconnectTimer) {
|
||||||
clearTimeout(this.reconnectTimer);
|
clearTimeout(this.reconnectTimer);
|
||||||
}
|
}
|
||||||
this.reconnectTimer = setTimeout(function(){
|
this.reconnectTimer = setTimeout(function() {
|
||||||
_this.connect();
|
_this.connect();
|
||||||
}, this.reconnectInterval);
|
}, this.reconnectInterval);
|
||||||
if (this.reconnectInterval < 1000) {
|
if (this.reconnectInterval < 1000) {
|
||||||
|
@ -329,17 +422,25 @@ Connection.prototype.send = function(data) {
|
||||||
console.trace('networkState is "' + this.state + '", can not send ' + data);
|
console.trace('networkState is "' + this.state + '", can not send ' + data);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.connection.send(data);
|
uni.sendSocketMessage({
|
||||||
|
data: data
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection.prototype.close = function(){
|
Connection.prototype.close = function() {
|
||||||
this.updateNetworkState('disconnected');
|
this.updateNetworkState('disconnected');
|
||||||
this.connection.close();
|
uni.closeSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
var __extends = (this && this.__extends) || function (d, b) {
|
var __extends = (this && this.__extends) || function(d, b) {
|
||||||
for (var p in b) if (b.hasOwnProperty(p)) {d[p] = b[p];}
|
for (var p in b)
|
||||||
function __() { this.constructor = d; }
|
if (b.hasOwnProperty(p)) {
|
||||||
|
d[p] = b[p];
|
||||||
|
}
|
||||||
|
|
||||||
|
function __() {
|
||||||
|
this.constructor = d;
|
||||||
|
}
|
||||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -357,14 +458,14 @@ function Channel(connection, channel_name) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Channel.prototype.processSubscribe = function () {
|
Channel.prototype.processSubscribe = function() {
|
||||||
if (this.connection.state !== 'connected') {
|
if (this.connection.state !== 'connected') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.subscribeCb();
|
this.subscribeCb();
|
||||||
};
|
};
|
||||||
|
|
||||||
Channel.prototype.processQueue = function () {
|
Channel.prototype.processQueue = function() {
|
||||||
if (this.connection.state !== 'connected' || !this.subscribed) {
|
if (this.connection.state !== 'connected' || !this.subscribed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -374,20 +475,25 @@ Channel.prototype.processQueue = function () {
|
||||||
this.queue = [];
|
this.queue = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
Channel.prototype.trigger = function (event, data) {
|
Channel.prototype.trigger = function(event, data) {
|
||||||
if (event.indexOf('client-') !== 0) {
|
if (event.indexOf('client-') !== 0) {
|
||||||
throw new Error("Event '" + event + "' should start with 'client-'");
|
throw new Error("Event '" + event + "' should start with 'client-'");
|
||||||
}
|
}
|
||||||
var _this = this;
|
var _this = this;
|
||||||
this.queue.push(function () {
|
this.queue.push(function() {
|
||||||
_this.connection.send(JSON.stringify({ event: event, data: data, channel: _this.channelName }));
|
_this.connection.send(JSON.stringify({
|
||||||
|
event: event,
|
||||||
|
data: data,
|
||||||
|
channel: _this.channelName
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
this.processQueue();
|
this.processQueue();
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////
|
////////////////
|
||||||
var Collections = (function () {
|
var Collections = (function() {
|
||||||
var exports = {};
|
var exports = {};
|
||||||
|
|
||||||
function extend(target) {
|
function extend(target) {
|
||||||
var sources = [];
|
var sources = [];
|
||||||
for (var _i = 1; _i < arguments.length; _i++) {
|
for (var _i = 1; _i < arguments.length; _i++) {
|
||||||
|
@ -399,8 +505,7 @@ var Collections = (function () {
|
||||||
if (extensions[property] && extensions[property].constructor &&
|
if (extensions[property] && extensions[property].constructor &&
|
||||||
extensions[property].constructor === Object) {
|
extensions[property].constructor === Object) {
|
||||||
target[property] = extend(target[property] || {}, extensions[property]);
|
target[property] = extend(target[property] || {}, extensions[property]);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
target[property] = extensions[property];
|
target[property] = extensions[property];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -409,13 +514,13 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.extend = extend;
|
exports.extend = extend;
|
||||||
|
|
||||||
function stringify() {
|
function stringify() {
|
||||||
var m = ["Push"];
|
var m = ["Push"];
|
||||||
for (var i = 0; i < arguments.length; i++) {
|
for (var i = 0; i < arguments.length; i++) {
|
||||||
if (typeof arguments[i] === "string") {
|
if (typeof arguments[i] === "string") {
|
||||||
m.push(arguments[i]);
|
m.push(arguments[i]);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
m.push(safeJSONStringify(arguments[i]));
|
m.push(safeJSONStringify(arguments[i]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -423,6 +528,7 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.stringify = stringify;
|
exports.stringify = stringify;
|
||||||
|
|
||||||
function arrayIndexOf(array, item) {
|
function arrayIndexOf(array, item) {
|
||||||
var nativeIndexOf = Array.prototype.indexOf;
|
var nativeIndexOf = Array.prototype.indexOf;
|
||||||
if (array === null) {
|
if (array === null) {
|
||||||
|
@ -440,6 +546,7 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.arrayIndexOf = arrayIndexOf;
|
exports.arrayIndexOf = arrayIndexOf;
|
||||||
|
|
||||||
function objectApply(object, f) {
|
function objectApply(object, f) {
|
||||||
for (var key in object) {
|
for (var key in object) {
|
||||||
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
||||||
|
@ -449,24 +556,27 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.objectApply = objectApply;
|
exports.objectApply = objectApply;
|
||||||
|
|
||||||
function keys(object) {
|
function keys(object) {
|
||||||
var keys = [];
|
var keys = [];
|
||||||
objectApply(object, function (_, key) {
|
objectApply(object, function(_, key) {
|
||||||
keys.push(key);
|
keys.push(key);
|
||||||
});
|
});
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.keys = keys;
|
exports.keys = keys;
|
||||||
|
|
||||||
function values(object) {
|
function values(object) {
|
||||||
var values = [];
|
var values = [];
|
||||||
objectApply(object, function (value) {
|
objectApply(object, function(value) {
|
||||||
values.push(value);
|
values.push(value);
|
||||||
});
|
});
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.values = values;
|
exports.values = values;
|
||||||
|
|
||||||
function apply(array, f, context) {
|
function apply(array, f, context) {
|
||||||
for (var i = 0; i < array.length; i++) {
|
for (var i = 0; i < array.length; i++) {
|
||||||
f.call(context || (window), array[i], i, array);
|
f.call(context || (window), array[i], i, array);
|
||||||
|
@ -474,6 +584,7 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.apply = apply;
|
exports.apply = apply;
|
||||||
|
|
||||||
function map(array, f) {
|
function map(array, f) {
|
||||||
var result = [];
|
var result = [];
|
||||||
for (var i = 0; i < array.length; i++) {
|
for (var i = 0; i < array.length; i++) {
|
||||||
|
@ -483,17 +594,19 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.map = map;
|
exports.map = map;
|
||||||
|
|
||||||
function mapObject(object, f) {
|
function mapObject(object, f) {
|
||||||
var result = {};
|
var result = {};
|
||||||
objectApply(object, function (value, key) {
|
objectApply(object, function(value, key) {
|
||||||
result[key] = f(value);
|
result[key] = f(value);
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.mapObject = mapObject;
|
exports.mapObject = mapObject;
|
||||||
|
|
||||||
function filter(array, test) {
|
function filter(array, test) {
|
||||||
test = test || function (value) {
|
test = test || function(value) {
|
||||||
return !!value;
|
return !!value;
|
||||||
};
|
};
|
||||||
var result = [];
|
var result = [];
|
||||||
|
@ -506,9 +619,10 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.filter = filter;
|
exports.filter = filter;
|
||||||
|
|
||||||
function filterObject(object, test) {
|
function filterObject(object, test) {
|
||||||
var result = {};
|
var result = {};
|
||||||
objectApply(object, function (value, key) {
|
objectApply(object, function(value, key) {
|
||||||
if ((test && test(value, key, object, result)) || Boolean(value)) {
|
if ((test && test(value, key, object, result)) || Boolean(value)) {
|
||||||
result[key] = value;
|
result[key] = value;
|
||||||
}
|
}
|
||||||
|
@ -517,15 +631,17 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.filterObject = filterObject;
|
exports.filterObject = filterObject;
|
||||||
|
|
||||||
function flatten(object) {
|
function flatten(object) {
|
||||||
var result = [];
|
var result = [];
|
||||||
objectApply(object, function (value, key) {
|
objectApply(object, function(value, key) {
|
||||||
result.push([key, value]);
|
result.push([key, value]);
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.flatten = flatten;
|
exports.flatten = flatten;
|
||||||
|
|
||||||
function any(array, test) {
|
function any(array, test) {
|
||||||
for (var i = 0; i < array.length; i++) {
|
for (var i = 0; i < array.length; i++) {
|
||||||
if (test(array[i], i, array)) {
|
if (test(array[i], i, array)) {
|
||||||
|
@ -536,6 +652,7 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.any = any;
|
exports.any = any;
|
||||||
|
|
||||||
function all(array, test) {
|
function all(array, test) {
|
||||||
for (var i = 0; i < array.length; i++) {
|
for (var i = 0; i < array.length; i++) {
|
||||||
if (!test(array[i], i, array)) {
|
if (!test(array[i], i, array)) {
|
||||||
|
@ -546,8 +663,9 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.all = all;
|
exports.all = all;
|
||||||
|
|
||||||
function encodeParamsObject(data) {
|
function encodeParamsObject(data) {
|
||||||
return mapObject(data, function (value) {
|
return mapObject(data, function(value) {
|
||||||
if (typeof value === "object") {
|
if (typeof value === "object") {
|
||||||
value = safeJSONStringify(value);
|
value = safeJSONStringify(value);
|
||||||
}
|
}
|
||||||
|
@ -556,16 +674,19 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.encodeParamsObject = encodeParamsObject;
|
exports.encodeParamsObject = encodeParamsObject;
|
||||||
|
|
||||||
function buildQueryString(data) {
|
function buildQueryString(data) {
|
||||||
var params = filterObject(data, function (value) {
|
var params = filterObject(data, function(value) {
|
||||||
return value !== undefined;
|
return value !== undefined;
|
||||||
});
|
});
|
||||||
return map(flatten(encodeParamsObject(params)), util_1["default"].method("join", "=")).join("&");
|
return map(flatten(encodeParamsObject(params)), util_1["default"].method("join", "=")).join("&");
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.buildQueryString = buildQueryString;
|
exports.buildQueryString = buildQueryString;
|
||||||
|
|
||||||
function decycleObject(object) {
|
function decycleObject(object) {
|
||||||
var objects = [], paths = [];
|
var objects = [],
|
||||||
|
paths = [];
|
||||||
return (function derez(value, path) {
|
return (function derez(value, path) {
|
||||||
var i, name, nu;
|
var i, name, nu;
|
||||||
switch (typeof value) {
|
switch (typeof value) {
|
||||||
|
@ -575,7 +696,9 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
for (i = 0; i < objects.length; i += 1) {
|
for (i = 0; i < objects.length; i += 1) {
|
||||||
if (objects[i] === value) {
|
if (objects[i] === value) {
|
||||||
return {$ref: paths[i]};
|
return {
|
||||||
|
$ref: paths[i]
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
objects.push(value);
|
objects.push(value);
|
||||||
|
@ -585,8 +708,7 @@ var Collections = (function () {
|
||||||
for (i = 0; i < value.length; i += 1) {
|
for (i = 0; i < value.length; i += 1) {
|
||||||
nu[i] = derez(value[i], path + '[' + i + ']');
|
nu[i] = derez(value[i], path + '[' + i + ']');
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
nu = {};
|
nu = {};
|
||||||
for (name in value) {
|
for (name in value) {
|
||||||
if (Object.prototype.hasOwnProperty.call(value, name)) {
|
if (Object.prototype.hasOwnProperty.call(value, name)) {
|
||||||
|
@ -604,11 +726,11 @@ var Collections = (function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.decycleObject = decycleObject;
|
exports.decycleObject = decycleObject;
|
||||||
|
|
||||||
function safeJSONStringify(source) {
|
function safeJSONStringify(source) {
|
||||||
try {
|
try {
|
||||||
return JSON.stringify(source);
|
return JSON.stringify(source);
|
||||||
}
|
} catch (e) {
|
||||||
catch (e) {
|
|
||||||
return JSON.stringify(decycleObject(source));
|
return JSON.stringify(decycleObject(source));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -617,25 +739,25 @@ var Collections = (function () {
|
||||||
return exports;
|
return exports;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var Dispatcher = (function () {
|
var Dispatcher = (function() {
|
||||||
function Dispatcher(failThrough) {
|
function Dispatcher(failThrough) {
|
||||||
this.callbacks = new CallbackRegistry();
|
this.callbacks = new CallbackRegistry();
|
||||||
this.global_callbacks = [];
|
this.global_callbacks = [];
|
||||||
this.failThrough = failThrough;
|
this.failThrough = failThrough;
|
||||||
}
|
}
|
||||||
Dispatcher.prototype.on = function (eventName, callback, context) {
|
Dispatcher.prototype.on = function(eventName, callback, context) {
|
||||||
this.callbacks.add(eventName, callback, context);
|
this.callbacks.add(eventName, callback, context);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Dispatcher.prototype.on_global = function (callback) {
|
Dispatcher.prototype.on_global = function(callback) {
|
||||||
this.global_callbacks.push(callback);
|
this.global_callbacks.push(callback);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Dispatcher.prototype.off = function (eventName, callback, context) {
|
Dispatcher.prototype.off = function(eventName, callback, context) {
|
||||||
this.callbacks.remove(eventName, callback, context);
|
this.callbacks.remove(eventName, callback, context);
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
Dispatcher.prototype.emit = function (eventName, data) {
|
Dispatcher.prototype.emit = function(eventName, data) {
|
||||||
var i;
|
var i;
|
||||||
for (i = 0; i < this.global_callbacks.length; i++) {
|
for (i = 0; i < this.global_callbacks.length; i++) {
|
||||||
this.global_callbacks[i](eventName, data);
|
this.global_callbacks[i](eventName, data);
|
||||||
|
@ -645,8 +767,7 @@ var Dispatcher = (function () {
|
||||||
for (i = 0; i < callbacks.length; i++) {
|
for (i = 0; i < callbacks.length; i++) {
|
||||||
callbacks[i].fn.call(callbacks[i].context || (window), data);
|
callbacks[i].fn.call(callbacks[i].context || (window), data);
|
||||||
}
|
}
|
||||||
}
|
} else if (this.failThrough) {
|
||||||
else if (this.failThrough) {
|
|
||||||
this.failThrough(eventName, data);
|
this.failThrough(eventName, data);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
|
@ -654,14 +775,14 @@ var Dispatcher = (function () {
|
||||||
return Dispatcher;
|
return Dispatcher;
|
||||||
}());
|
}());
|
||||||
|
|
||||||
var CallbackRegistry = (function () {
|
var CallbackRegistry = (function() {
|
||||||
function CallbackRegistry() {
|
function CallbackRegistry() {
|
||||||
this._callbacks = {};
|
this._callbacks = {};
|
||||||
}
|
}
|
||||||
CallbackRegistry.prototype.get = function (name) {
|
CallbackRegistry.prototype.get = function(name) {
|
||||||
return this._callbacks[prefix(name)];
|
return this._callbacks[prefix(name)];
|
||||||
};
|
};
|
||||||
CallbackRegistry.prototype.add = function (name, callback, context) {
|
CallbackRegistry.prototype.add = function(name, callback, context) {
|
||||||
var prefixedEventName = prefix(name);
|
var prefixedEventName = prefix(name);
|
||||||
this._callbacks[prefixedEventName] = this._callbacks[prefixedEventName] || [];
|
this._callbacks[prefixedEventName] = this._callbacks[prefixedEventName] || [];
|
||||||
this._callbacks[prefixedEventName].push({
|
this._callbacks[prefixedEventName].push({
|
||||||
|
@ -669,7 +790,7 @@ var CallbackRegistry = (function () {
|
||||||
context: context
|
context: context
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
CallbackRegistry.prototype.remove = function (name, callback, context) {
|
CallbackRegistry.prototype.remove = function(name, callback, context) {
|
||||||
if (!name && !callback && !context) {
|
if (!name && !callback && !context) {
|
||||||
this._callbacks = {};
|
this._callbacks = {};
|
||||||
return;
|
return;
|
||||||
|
@ -677,14 +798,14 @@ var CallbackRegistry = (function () {
|
||||||
var names = name ? [prefix(name)] : Collections.keys(this._callbacks);
|
var names = name ? [prefix(name)] : Collections.keys(this._callbacks);
|
||||||
if (callback || context) {
|
if (callback || context) {
|
||||||
this.removeCallback(names, callback, context);
|
this.removeCallback(names, callback, context);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
this.removeAllCallbacks(names);
|
this.removeAllCallbacks(names);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
CallbackRegistry.prototype.removeCallback = function (names, callback, context) {
|
CallbackRegistry.prototype.removeCallback = function(names, callback, context) {
|
||||||
Collections.apply(names, function (name) {
|
Collections.apply(names, function(name) {
|
||||||
this._callbacks[name] = Collections.filter(this._callbacks[name] || [], function (oning) {
|
this._callbacks[name] = Collections.filter(this._callbacks[name] || [], function(
|
||||||
|
oning) {
|
||||||
return (callback && callback !== oning.fn) ||
|
return (callback && callback !== oning.fn) ||
|
||||||
(context && context !== oning.context);
|
(context && context !== oning.context);
|
||||||
});
|
});
|
||||||
|
@ -693,59 +814,58 @@ var CallbackRegistry = (function () {
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
CallbackRegistry.prototype.removeAllCallbacks = function (names) {
|
CallbackRegistry.prototype.removeAllCallbacks = function(names) {
|
||||||
Collections.apply(names, function (name) {
|
Collections.apply(names, function(name) {
|
||||||
delete this._callbacks[name];
|
delete this._callbacks[name];
|
||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
return CallbackRegistry;
|
return CallbackRegistry;
|
||||||
}());
|
}());
|
||||||
|
|
||||||
function prefix(name) {
|
function prefix(name) {
|
||||||
return "_" + name;
|
return "_" + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
function __ajax(options){
|
function __ajax(options) {
|
||||||
options=options||{};
|
options = options || {};
|
||||||
options.type=(options.type||'GET').toUpperCase();
|
options.type = (options.type || 'GET').toUpperCase();
|
||||||
options.dataType=options.dataType||'json';
|
options.dataType = options.dataType || 'json';
|
||||||
params=formatParams(options.data);
|
var params = formatParams(options.data);
|
||||||
|
|
||||||
var xhr;
|
var xhr;
|
||||||
if(window.XMLHttpRequest){
|
if (window.XMLHttpRequest) {
|
||||||
xhr=new XMLHttpRequest();
|
xhr = new XMLHttpRequest();
|
||||||
}else{
|
} else {
|
||||||
xhr=ActiveXObject('Microsoft.XMLHTTP');
|
xhr = ActiveXObject('Microsoft.XMLHTTP');
|
||||||
}
|
}
|
||||||
|
|
||||||
xhr.onreadystatechange=function(){
|
xhr.onreadystatechange = function() {
|
||||||
if(xhr.readyState === 4){
|
if (xhr.readyState === 4) {
|
||||||
var status=xhr.status;
|
var status = xhr.status;
|
||||||
if(status>=200 && status<300){
|
if (status >= 200 && status < 300) {
|
||||||
options.success&&options.success(xhr.responseText,xhr.responseXML);
|
options.success && options.success(xhr.responseText, xhr.responseXML);
|
||||||
}else{
|
} else {
|
||||||
options.error&&options.error(status);
|
options.error && options.error(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(options.type==='GET'){
|
if (options.type === 'GET') {
|
||||||
xhr.open('GET',options.url+'?'+params,true);
|
xhr.open('GET', options.url + '?' + params, true);
|
||||||
xhr.send(null);
|
xhr.send(null);
|
||||||
}else if(options.type==='POST'){
|
} else if (options.type === 'POST') {
|
||||||
xhr.open('POST',options.url,true);
|
xhr.open('POST', options.url, true);
|
||||||
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
||||||
xhr.send(params);
|
xhr.send(params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatParams(data){
|
function formatParams(data) {
|
||||||
var arr=[];
|
var arr = [];
|
||||||
for(var name in data){
|
for (var name in data) {
|
||||||
arr.push(encodeURIComponent(name)+'='+encodeURIComponent(data[name]));
|
arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
|
||||||
}
|
}
|
||||||
return arr.join('&');
|
return arr.join('&');
|
||||||
}
|
}
|
||||||
|
|
||||||
// export const push = {
|
export default Push
|
||||||
// a:6
|
|
||||||
// }
|
|
Loading…
Reference in New Issue