zhengxin 5 лет назад
Родитель
Сommit
bedde67855
3 измененных файлов с 174 добавлено и 15 удалено
  1. 23 15
      ghs/src/api/common/index.js
  2. 37 0
      ghs/src/mixins/priceSocket.js
  3. 114 0
      ghs/src/utils/socket.js

+ 23 - 15
ghs/src/api/common/index.js

@@ -1,40 +1,48 @@
-import https from '@/plugins/luch-request_0.0.7/request'
+import https from "@/plugins/luch-request_0.0.7/request";
 
-export const getOptions = param => https.get('/apihkds/api/option/getOptions', param)
+export const getOptions = param =>
+	https.get("/apihkds/api/option/getOptions", param);
 
 // 图片上传
-export const uploadPic = data => https.post('/upload/save-img', data)
+export const uploadPic = data => https.post("/upload/save-img", data);
 
 // M 端商家信息
-export const getMerchantInfo = param => https.get('/merchant/get-info', param)
+export const getMerchantInfo = param => https.get("/merchant/get-info", param);
 
 // M 端用户信息
-export const getShopUserApi = param => https.get('/user/my-profile', param)
+export const getShopUserApi = param => https.get("/user/my-profile", param);
 
 // 商户信息
-export const getUserApi = param => https.get('/merchant/detail', param)
+export const getUserApi = param => https.get("/merchant/detail", param);
 
 // 员工信息
-export const getStaffApi = param => https.get('/admin/login-detail', param)
+export const getStaffApi = param => https.get("/admin/login-detail", param);
 
 // 常用链接 b
-export const commonUrl = param => https.get('/merchant/common-url', param)
+export const commonUrl = param => https.get("/merchant/common-url", param);
 
 // 常用链接 b
-export const getFestList = param => https.get('/fest/list', param)
+export const getFestList = param => https.get("/fest/list", param);
 
 // 商家的门店 m
-export const getShop = param => https.get('/shop/detail', param)
+export const getShop = param => https.get("/shop/detail", param);
 
 // 获取验证码 m
-export const sendSms = param => https.get('/inform-user/send-sms', param)
+export const sendSms = param => https.get("/inform-user/send-sms", param);
 
 // 地图查询 m b s w
-export const suggestion = param => https.get('/map/suggestion', param)
+export const suggestion = param => https.get("/map/suggestion", param);
 
 // 微信分享 m b
-export const wxShare = data => https.post('/wx-share/get-params', data)
+export const wxShare = data => https.post("/wx-share/get-params", data);
 // 获取所有门店
-export const shopAll = data => https.get('/shop/all', data)
+export const shopAll = data => https.get("/shop/all", data);
 // 切换门店
-export const toggleShop = data => https.post('/shop/toggle-shop', data)
+export const toggleShop = data => https.post("/shop/toggle-shop", data);
+
+//socket链接初始化
+export const initSocket = async clientId => {
+	const st = uni.getStorageSync("token");
+
+	return https.post(`/ws/bind?token=${st}`, { clientId: clientId });
+};

+ 37 - 0
ghs/src/mixins/priceSocket.js

@@ -0,0 +1,37 @@
+import SocketUtil from "@/utils/socket";
+import { initSocket } from "@/api/common/index";
+import priceAudio from "@/mixins/priceAudio";
+
+export default {
+	mixins: [priceAudio],
+	data() {
+		return {
+			priceSocket: null
+		};
+	},
+	onLaunch() {
+		this.priceSocket = new SocketUtil({
+			onOpen: () => {},
+			onMessage: data => {
+				let type = data.type || "";
+				switch (type) {
+					case "init":
+						let clientId = data.client_id;
+
+						initSocket(clientId);
+						break;
+					case "money":
+						//到账消息    data:  {type: "money", msg: "微信到账1.2", money: 1.2}
+						console.log(data);
+
+						this.playPriceAudio(data.money);
+						break;
+					default:
+						console.log("default");
+				}
+			},
+			onClose: () => {},
+			onError: () => {}
+		});
+	}
+};

+ 114 - 0
ghs/src/utils/socket.js

@@ -0,0 +1,114 @@
+const SocketConfig = {
+	retry: 20,
+	timeout: 10000,
+	timeHandler: null,
+
+	reset: function() {
+		clearTimeout(this.timeHandler);
+		this.timeHandler = null;
+		return this;
+	},
+	start: function(send) {
+		this.timeHandler = setTimeout(() => {
+			send && send();
+			this.reset().start(send);
+		}, this.timeout);
+	}
+};
+class SocketUtil {
+	constructor({ url, onOpen, onMessage, onClose, onError }) {
+		this.url = url || "api.ghs.huaml.com:8282";
+		this.socket = this.connectSocket(this.url);
+		this.onOpenCall = onOpen;
+		this.onMessageCall = onMessage;
+		this.onCloseCall = onClose;
+		this.onErrorCall = onError;
+		this.retryTime = 0;
+		this.reconnectHandler = null;
+		this.lockReconnect = false;
+
+		this.socket.onOpen(this.openSocket);
+		// 监听消息
+
+		this.socket.onMessage(this.onMessageSocket);
+
+		// 监听Socket的关闭
+		this.socket.onClose(this.closeSocket);
+
+		this.socket.onError(this.errorSocket);
+
+		// uni.onSocketOpen(function(res) {
+		// 	console.log("WebSocket连接已打开!");
+		// });
+		// uni.onSocketMessage(function(res) {
+		// 	console.log("收到服务器内容:" + res.data);
+		// });
+		// uni.onSocketOpen(function() {
+		// 	uni.closeSocket();
+		// });
+	}
+	connectSocket(url) {
+		let path = `ws://${url}`;
+		// #ifdef  MP-WEIXIN
+		path = `wss://${url}`;
+		// #endif
+
+		const task = uni.connectSocket({
+			url: path,
+			complete: () => {}
+		});
+		return task;
+	}
+	openSocket = event => {
+		console.log("contact ok", event);
+
+		SocketConfig.reset().start(() => {
+			this.send("ping");
+		});
+
+		this.onOpenCall && this.onOpenCall(event);
+	};
+
+	onMessageSocket = data => {
+		console.log("Client received a message", data);
+		SocketConfig.reset().start(() => {
+			this.send("ping");
+		});
+
+		try {
+			let info = JSON.parse(data.data);
+			this.onMessageCall && this.onMessageCall(info);
+		} catch (error) {}
+	};
+	errorSocket(err) {
+		console.log("contact error", err);
+		this.onErrorCall && this.onErrorCall(err);
+	}
+
+	closeSocket() {
+		console.log("Client notified socket has closed");
+		this.onCloseCall && this.onCloseCall();
+	}
+	send(data) {
+		this.socket.send({
+			data: JSON.stringify(data)
+		});
+	}
+	colse() {
+		this.socket.close();
+	}
+	reconnect() {
+		if (this.lockReconnect) return;
+		this.lockReconnect = true;
+		clearTimeout(this.reconnectHandler);
+		if (this.retryTime < SocketConfig.retry) {
+			this.reconnectHandler = setTimeout(() => {
+				this.socket = this.connectSocket(this.url);
+				this.lockReconnect = false;
+			}, 5000);
+			this.retryTime++;
+		}
+	}
+}
+
+export default SocketUtil;