123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- import packetCode from './PacketCodeC.js'
- import store from './store/index.js'
- import * as config from './config'
- import helper from '@/common/helper.js';
- export default class Websocket {
- constructor({
- heartCheck,
- isReconnection
- }) {
- // 是否连接
- this._isLogin = false;
- // 当前网络状态
- this._netWork = true;
- // 是否人为退出
- this._isClosed = false;
- // 心跳检测频率
- this._timeout = 3000;
- this._timeoutObj = null;
- this._timeoutObj1 = null;
- // 当前重连次数
- this._connectNum = 0;
- // 心跳检测和断线重连开关,true为启用,false为关闭
- this._heartCheck = heartCheck;
- this._isReconnection = isReconnection;
- this._showLoginDialog = true
- // this._onSocketOpened();
- }
- // 心跳重置
- _reset() {
- clearTimeout(this._timeoutObj);
- clearTimeout(this._timeoutObj1);
- return this;
- }
- // 心跳开始
- _start(options) {
- let _this = this;
- this._timeoutObj = setInterval(() => {
- //发送心跳
- _this.sendHeartbeatData(options);
- _this.getInfo()
- }, this._timeout);
- this._timeoutObj1 = setInterval(() => {
- _this.getTips()
- }, 1000*60);
- }
- // 监听websocket连接关闭
- onSocketClosed(options) {
- uni.onSocketError(err => {
- console.log('当前websocket连接已关闭,错误信息为:' + JSON.stringify(err));
- // 停止心跳连接
- if (this._heartCheck) {
- this._reset();
- }
- // 关闭已登录开关
- this._isLogin = false;
- // 检测是否是用户自己退出小程序
- console.log('------------------开启重连--------------------------------')
- if (!this._isClosed) {
- // 进行重连
- if (this._isReconnection) {
- this._reConnect(options)
- }
- }
-
- })
- uni.onSocketClose(err => {
- })
- }
- // 检测网络变化
- onNetworkChange(options) {
- uni.onNetworkStatusChange(res => {
- console.log('当前网络状态:' + res.isConnected);
- if (!this._netWork) {
- this._isLogin = false;
- // 进行重连
- if (this._isReconnection) {
- this._reConnect(options)
- }
- }
- })
- }
- _onSocketOpened(options) {
- uni.onSocketOpen(res => {
- console.log('【websocket】已打开');
- // 打开已登录开关
- this._isLogin = true;
- // 发送心跳
- if (this._heartCheck) {
- this._reset()._start(options);
- }
- // 发送登录信息
- this.sendLoginData();
- // 打开网络开关
- this._netWork = true;
- })
- }
- // 接收服务器返回的消息
- onReceivedMsg(callBack) {
- uni.onSocketMessage(event => {
- if (typeof callBack == "function") {
- callBack(event)
- } else {
- console.log('参数的类型必须为函数')
- }
- })
- }
- // 建立websocket连接
- initWebSocket(options) {
- let _this = this;
- if (this._isLogin) {
- console.log("您已经登录了");
- } else {
- // 检查网络
- uni.getNetworkType({
- success(result) {
- if (result.networkType != 'none') {
- // 开始建立连接
- console.log('建立websocket连接' + options.url);
- uni.connectSocket({
- url: options.url,
- success(res) {
- if (typeof options.success == "function") {
- options.success(res)
- _this._onSocketOpened(options);
- } else {
- console.log('参数的类型必须为函数')
- }
- },
- fail(err) {
- if (typeof options.fail == "function") {
- options.fail(err)
- } else {
- console.log('参数的类型必须为函数')
- }
- }
- })
- } else {
- console.log('网络已断开');
- _this._netWork = false;
- // 网络断开后显示model
- // uni.showModal({
- // title: '网络错误',
- // content: '请重新打开网络',
- // success: function(res) {
- // if (res.confirm) {
- // console.log('用户点击确定')
- // }
- // }
- // })
- uni.showToast({
- title: "网络错误,请检查网络状况",
- icon: 'none'
- })
- }
- }
- })
- }
- }
- // 发送websocket消息
- sendWebSocketMsg(options) {
- this.sendBinary(1,options);
- }
- //发送心跳连接
- sendHeartbeatData(options) {
- var that = this
- let packet = {
- version: 1,
- command: 17,
- token: store.state.userData.token
- }
- this.sendBinary(99, {
- data: packet,
- success(res) {
- // console.log('【websocket】心跳连接成功');
- if(!that._showLoginDialog){
- that._showLoginDialog= true
- }
- },
- fail(err) {
- console.log('【websocket】心跳连接失败');
- console.log(err)
- console.log('this._showLoginDialog',that._showLoginDialog )
- uni.hideLoading()
- that._isLogin = false
- that._reConnect(options)
- }
- });
-
-
- }
- //发送第一次连接数据
- sendLoginData() {
- this.sendBinary(99, {
- data: {},
- success(res) {
- console.log('【websocket】第一次连接成功')
- },
- fail(err) {
- console.log('【websocket】第一次连接失败')
- console.log(err)
- }
- });
- // this.sendBinary(99, {});
- // socket.sendSocketMessage({
- // // 这里是第一次建立连接所发送的信息,应由前后端商量后决定
- // data: JSON.stringify({
- // "key": 'value'
- // })
- // })
- }
- // 重连方法,会根据时间频率越来越慢
- _reConnect(options) {
- let timer, _this = this;
- if (this._connectNum < 20) {
- timer = setTimeout(() => {
- this.initWebSocket(options)
- }, 500)
- this._connectNum += 1;
- } else if (this._connectNum < 50) {
- timer = setTimeout(() => {
- this.initWebSocket(options)
- }, 1000)
- this._connectNum += 1;
- } else {
- timer = setTimeout(() => {
- this.initWebSocket(options)
- }, 3000)
- this._connectNum += 1;
- }
- }
- // 关闭websocket连接
- closeWebSocket() {
- uni.closeSocket();
- this._isClosed = true;
- }
- //发送二进制
- sendBinary(commendType, options) {
- uni.sendSocketMessage({
- data: packetCode.encode(options.data),
- success(res) {
- if (typeof options.success == "function") {
- options.success(res)
- } else {
- console.log('参数的类型必须为函数')
- }
- },
- fail(err) {
- if (typeof options.fail == "function") {
- options.fail(err)
- } else {
- console.log('参数的类型必须为函数')
- }
- }
- });
- }
- getTips(){
- return new Promise(resolve => {
- let baseUrl = config.def().baseUrl
- let baseUrlNew = config.def().baseUrlNew
- var userInfo
- if (!userInfo || !userInfo.accessToken) {
- userInfo = uni.getStorageSync('userInfo')
- }
- var pcUserInfo=uni.getStorageSync('pcUserInfo')
- if(!pcUserInfo && userInfo){
- uni.request({
- url: baseUrlNew + '/commonUser/api/loginQuickly',
- data: {
- mobilePhone: userInfo.phone,
- veriCode: "123456",
- },
- method: 'POST',
- success: (res) => {
- if (res.statusCode === 200) {
- uni.setStorageSync('pcUserInfo', res.data)
- helper.getListByUserId()
- }
- else{
- uni.request({
- url: baseUrlNew + '/commonUser/api/loginQuickly',
- data: {
- mobilePhone: "13333333333",
- veriCode: "123456",
- },
- method: 'POST',
- success: (res) => {
- if (res.statusCode === 200) {
- uni.setStorageSync('pcUserInfo', res.data)
- helper.getListByUserId()
- }
- }
- })
- }
- }
- })
- }
- uni.request({
- url: baseUrlNew + '/notice/query/noticeNumber',
- data: {
-
- },
- method: 'GET',
- success: (res) => {
- if (res.data.data) {
- let name = 'myTip';
- let value = res.data.data.task;
- store.commit('$uStore', {
- name,
- value
- });
- if(value != 0&&value){
- uni.setTabBarBadge({
- index:4,
- text:value+""
- })
- }
- name = 'taskTip';
- value = res.data.data.task;
- store.commit('$uStore', {
- name,
- value
- });
- // name = 'contractTip';
- // value = res.data.data.contractTip;
- // store.commit('$uStore', {
- // name,
- // value
- // });
- }
- }
- })
-
- // let accessToken = userInfo ? userInfo.accessToken : ''
- // var data = {}
- // var _mt = 'getTips'
- // var _gp = 'integral'
- // uni.request({
- // url: baseUrl + '/m.api',
- // data: {
- // ...data,
- // _gp,
- // _mt
- // },
- // method: 'POST',
- // header: {
- // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
- // 'ACCESSTOKEN': accessToken
- // },
- // success: (res) => {
- // if (res.statusCode === 200) {
- // let name = 'myTip';
- // let value = res.data.data.myTips;
- // store.commit('$uStore', {
- // name,
- // value
- // });
- // name = 'taskTip';
- // value = res.data.data.task;
- // store.commit('$uStore', {
- // name,
- // value
- // });
- // name = 'contractTip';
- // value = res.data.data.contract;
- // store.commit('$uStore', {
- // name,
- // value
- // });
- // }
- // }
- // })
- })
- }
- getInfo(){
- return new Promise(resolve => {
- var hour = new Date().getHours();
- if((hour >= 9 && hour < 12) ||(hour >= 13 && hour < 15)){
- var infoList = [];
- uni.request({
- url: "https://hq.sinajs.cn/list=C0,C2109,C2111,C2201,C2203,C2205,C2207,A0,A2109,A2111,A2201,A2203,A2205,A2207",
- // url: "https://hq.sinajs.cn/list=C2109",
- header: {
- 'content-type': 'application/x-www-form-urlencoded'
- },
- success: function(result) {
- // resolve调用后,即可传递到调用方使用then或者async+await同步方式进行处理逻辑
- var tmp = result.data.split('"')
- for(var i = 1; i<tmp.length;i=i+2){
- var list = tmp[i].split(",")
- var data = {
- goodsName:list[0],
- newPrice:list[6],
- openPrice:list[2]
- }
- if(data.goodsName){
- infoList.push(data)
- }
- }
- let name = 'infoList';
- let value = infoList;
- store.commit('$uStore', {
- name,
- value
- });
- // console.log("infoList",infoList)
- },
- fail: function(e) {
- console.log('error in...')
- // reject调用后,即可传递到调用方使用catch或者async+await同步方式进行处理逻辑
- reject(e)
- },
- })
- }
- })
- }
- }
|