Browse Source

Merge branch 'master' of http://git.liuniu946.com/lhl/nzhbsr

lhl 2 years ago
parent
commit
ca9b817fdf
3 changed files with 239 additions and 6 deletions
  1. 9 0
      api/index.js
  2. 69 6
      pages/index/index.vue
  3. 161 0
      utils/socket.js

+ 9 - 0
api/index.js

@@ -34,3 +34,12 @@ export function loadIndexs(data) {
 	});
 }
 
+// 获取首页信息
+export function geLevertade(data) {
+	return request({
+		url: '/api/Levertade/ctmarket',
+		method: 'get',
+		data
+	});
+}
+

+ 69 - 6
pages/index/index.vue

@@ -72,27 +72,82 @@
 </template>
 
 <script>
+	import {
+		scoketNew,
+		scoketOpen
+	} from '@/utils/socket.js';
+	import {
+		geLevertade
+	} from '@/api/index.js';
 	export default {
 		data() {
 			return {
-
+				scoket: '',
+				instId: 'IOTA-USDT', //请求的产品id
+				spList:[],//需要查询的列表
+				listOBj:{}//保存实际列表对象
 			};
 		},
 		computed: {
 
 		},
 		onLoad: function(option) {
+			
 
 		},
 		onShow: function() {
-
-
+			this.geLevertade()
+		},
+		onHide() {
+			this.closeScoket()
 		},
 		//下拉刷新
 		onPullDownRefresh() {
 
 		},
 		methods: {
+			swiperChange(){
+				
+			},
+			// 开始请求长连接
+			onScoket(){
+				const that = this;
+				that.scoket = scoketNew("wss://wsaws.okx.com:8443/ws/v5/public");
+				that.scoket.scoketOpen().then((res) => {
+					const requestList=that.spList.map((e)=>{
+						return{
+							"channel": "tickers",
+							"instId": e.coinname.toUpperCase()+"-USDT"
+						}
+					})
+					that.scoket.scoketSend({
+						"op": "subscribe",
+						"args": requestList
+					}).then((res) => {
+						console.log(res, '发送成功');
+					})
+					that.scoket.scoketMessage((res)=>{
+						try{
+						that.listOBj[res.arg.instId] = res.data
+						}catch(e){
+							console.log(res,'报错');
+						}
+					})
+				})
+			},
+			closeScoket(){
+				that.scoket.scoketClose();
+			},
+			// 获取查询列表
+			geLevertade(){
+				const that = this;
+				geLevertade().then((e)=>{
+					that.spList = e.list;
+					// 开启长连接
+					that.onScoket()
+					
+				})
+			},
 			// 轮播图跳转
 			bannerNavToUrl(item) {
 				// #ifdef H5
@@ -117,9 +172,7 @@
 		width: 726rpx;
 		height: 273rpx;
 
-		.carousel-item {
-			
-		}
+		.carousel-item {}
 
 		// margin: 20rpx 0 0;
 		image {
@@ -128,46 +181,56 @@
 			height: 273rpx;
 		}
 	}
+
 	.gnh-wrap {
 		margin: 20rpx 0;
 		width: 750rpx;
 		height: 315rpx;
 		background-color: #fff;
 		border-radius: 50rpx 50rpx 0 0;
+
 		.gnh-top {
 			justify-content: space-around;
 			height: 156rpx;
+
 			.top-item {
 				flex-direction: column;
 				justify-content: center;
 				align-items: center;
+
 				image {
 					width: 66rpx;
 					height: 75rpx;
 				}
+
 				view {
 					font-weight: 500;
 					color: #525C6E;
 				}
 			}
 		}
+
 		.gnh-btm {
 			justify-content: space-around;
+
 			.btm-item {
 				width: 33%;
 				text-align: center;
 				padding-top: 20rpx;
+
 				.btm-item-name {
 					font-size: 24rpx;
 					font-weight: 500;
 					color: #525C6E;
 				}
+
 				.btm-item-val {
 					font-size: 26rpx;
 					font-weight: 500;
 					color: #DD3745;
 					padding: 20rpx 0;
 				}
+
 				.btm-item-bl {
 					font-size: 24rpx;
 					font-weight: 500;

+ 161 - 0
utils/socket.js

@@ -0,0 +1,161 @@
+/**
+ * @param {string} url
+ * @param {Function} success
+ * @param {Function} fail  
+ * @description 创建新的scoket链接
+ */
+
+export function scoketNew(url, success = function() {}, fail = function() {}) {
+	const scoket = uni.connectSocket({
+		url,
+		success,
+		fail
+	});
+	return {
+		scoket,
+		scoketOpen,
+		scoketSend,
+		scoketClose,
+		scoketMessage
+	}
+}
+
+/**
+ * @param {Object} scoket 会话对象
+ */
+export function scoketOpen() {
+	let that = this;
+	return new Promise((resolve, rejact) => {
+		that.scoket.onOpen(res => {
+			resolve(res)
+		})
+	})
+}
+// 发送消息
+/**
+ * @param {Object} data 发送的数据
+ */
+export function scoketSend(data) {
+	let that = this;
+	return new Promise((resolve, rejact) => {
+		that.scoket.send({
+			data: JSON.stringify(data),
+			success: res => {
+				resolve(res)
+			},
+			fail: res => {
+				rejact(res)
+			},
+		});
+	})
+
+}
+/**
+ * @@description  关闭会话链接
+ */
+export function scoketClose() {
+	this.scoket.close({
+		success(res) {
+			console.log('关闭成功', res);
+		},
+		fail(err) {
+			console.log('关闭失败', err);
+		}
+	})
+}
+/**
+ * @@description  监听会话
+ */
+export function scoketMessage(fun) {
+	this.scoket.onMessage(res => {
+		const data = JSON.parse(res.data);
+		if (data.code) {
+			if (+data.code == 60018) {
+				console.log('scoket返回错误',data);
+			}
+		} else {
+			fun(data)
+		}
+	});
+}
+
+
+
+// this.socketTask.onOpen(res => {
+// 	console.log('WebSocket连接正常打开中...!');
+// 	this.socketOpen = true;
+// 	// 注:只有连接正常打开中 ,才能正常收到消息
+// 	this.socketTask.onMessage(res => {
+// 		this.client_id = JSON.parse(res.data).client_id;
+// 		console.log(JSON.parse(res.data));
+// 		if (JSON.parse(res.data).code == 10501) {
+// 			console.log('服务器问题');
+// 			uni.showLoading({
+// 				title: '断线重连'
+// 			});
+// 			// this.closeSocket();
+// 		} else if (JSON.parse(res.data).code == 0) {
+// 			this.$api.msg(JSON.parse(res.data).msg);
+// 		} else {
+// 			console.log('连接成功');
+// 			// ls.content.indexOf('http')
+// 			let newmessage = JSON.parse(res.data);
+// 			let add_time = getTime(newmessage.timestamp)
+
+// 			newmessage = {
+// 				avatar: newmessage.avatar,
+// 				content: newmessage.content,
+// 				nickname: newmessage.nickname,
+// 				order_id: newmessage.order_id,
+// 				type: newmessage.m_type,
+// 				uid: newmessage.uid,
+// 				add_time: add_time
+// 			}
+
+// 			if (newmessage.type == 5) {
+// 				newmessage.content = newmessage.content.split(',');
+// 				newmessage.content[0].split('?')
+// 			}
+
+
+// 			if (newmessage.order_id == this.order_id) {
+// 				// let ques_name = this.que_url
+// 				this.MsgList = this.MsgList.concat(newmessage);
+// 				this.MsgList = this.MsgList.map(e => {
+// 					e.add_time = Date.parse(e.add_time).toString();
+// 					e.add_time = e.add_time.substr(0, 10);
+// 					return e;
+// 				});
+// 				for (let i = 0; i < this.MsgList.length; i++) {
+// 					let length = this.MsgList.length;
+// 					//当前记录时间
+// 					let newTime = this.MsgList[i].add_time;
+// 					//上一条记录时间
+// 					if (i + 1 < length) {
+// 						let lastTime = this.MsgList[i + 1].add_time;
+// 						let time = (this.MsgList[i + 1].add_time) - (this.MsgList[i].add_time);
+// 						if (time > 300) {
+// 							this.MsgList[i].time = true;
+// 						}
+// 					}
+// 				}
+// 				this.MsgList = this.MsgList.map(e => {
+// 					e.add_time = getTime(e.add_time);
+// 					return e;
+// 				});
+// 				console.log(334, this.MsgList)
+// 			}
+// 			this.$nextTick(function() {
+// 				this.scrollToBottom();
+// 			});
+// 		}
+// 	});
+// 	let join = '{"type":"handshake","role":"user","uid":' + this.userInfo.uid + '}';
+// 	console.log(join);
+// 	this.socketTask.send({
+// 		data: join,
+// 		success: res => {
+// 			console.log('消息发送成功');
+// 		}
+// 	});
+// });