App.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <script>
  2. var wsUrl="ws://127.0.0.1:1234";
  3. var pingpangTimes=null;
  4. var initTimes=null;
  5. var isSocketOpen=false;
  6. var socket=null;
  7. export default {
  8. data() {
  9. return {
  10. };
  11. },
  12. onLaunch: function() {
  13. // this.initSocket()
  14. // console.log('App Launch')
  15. // var that=this;
  16. // uni.$on('initSocket', () => {
  17. // that.initSocket()
  18. // })
  19. // uni.$on('sendMsg', (item) => {
  20. // that.sendMsg(item)
  21. // })
  22. // uni.$on('closeWebSocket', () => {
  23. // that.closeWebSocket()
  24. // })
  25. // this.getEWechatSdk();
  26. // var initTimes=setInterval(function(){
  27. // if(!isSocketOpen){
  28. // try{
  29. // uni.closeSocket();
  30. // }
  31. // catch(err){
  32. // }
  33. // clearInterval(pingpangTimes);
  34. // that.initSocket()
  35. // }
  36. // },5000);
  37. },
  38. onShow: function() {
  39. console.log('App Show')
  40. },
  41. onHide: function() {
  42. console.log('App Hide')
  43. },
  44. onUnload() {
  45. console.log(pingpangTimes)
  46. clearInterval(pingpangTimes)
  47. // clearInterval(initTimes)
  48. },
  49. methods:{
  50. getEWechatSdk() {
  51. let eWechatSdk = ''
  52. if (/(Android)/i.test(navigator.userAgent)) {
  53. eWechatSdk = 'jWeixin'
  54. } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  55. eWechatSdk = 'wx'
  56. } else {
  57. eWechatSdk = 'jWeixin'
  58. }
  59. uni.setStorageSync("wxSdk",eWechatSdk)
  60. },
  61. closeWebSocket(){
  62. if(socket!=null){
  63. uni.closeSocket();
  64. }
  65. clearInterval(pingpangTimes)
  66. },
  67. reConnect(){
  68. var that=this;
  69. try{
  70. uni.closeSocket();
  71. }
  72. catch(e){
  73. }
  74. setTimeout(function(){
  75. that.initSocket();
  76. },200);
  77. },
  78. initSocket(){
  79. var that=this;
  80. console.log("initSocket")
  81. //创建一个socket连接
  82. socket=uni.connectSocket({
  83. url:wsUrl,
  84. multiple:true,
  85. success: res=>{
  86. //先确保清除了之前的心跳定时器
  87. clearInterval(pingpangTimes)
  88. uni.onSocketMessage((res)=>{
  89. const redata = JSON.parse(res.data);
  90. console.log(redata);
  91. if(redata.cmd=='deleteId'){
  92. uni.$emit('deleteId');
  93. }
  94. else if(redata.cmd=='init'){
  95. uni.$emit('init',redata.data);
  96. }
  97. else if(redata.cmd=='reload'){
  98. uni.$emit('reload');
  99. }
  100. else if(redata.cmd=='refresh'){
  101. uni.$emit('refresh');
  102. }
  103. else if(redata.cmd=='sendMsg'){
  104. // //发送消息
  105. // uni.$emit('sendMsg',redata.data);
  106. }
  107. else if(redata.cmd=='sendStatus'){
  108. uni.$emit('sendStatus',redata.data);
  109. }
  110. })
  111. pingpangTimes=setInterval(()=>{
  112. var key=uni.getStorageSync("key");
  113. var data={cmd:"heartbeat",data:key};
  114. console.log(data)
  115. uni.sendSocketMessage({
  116. data:JSON.stringify(data),
  117. success:()=>{
  118. console.log('WebSocket发送心条数据!');
  119. },
  120. fail:(res)=>{
  121. console.log(data);
  122. isSocketOpen=false
  123. }
  124. });
  125. },2000)
  126. },
  127. error: res=>{
  128. uni.$emit('websocket',0);
  129. console.log(res)
  130. },
  131. })
  132. //监听socket打开
  133. uni.onSocketOpen(()=>{
  134. isSocketOpen=true
  135. console.log('WebSocket连接已打开!!');
  136. // uni.$emit('websocket',1);
  137. uni.showToast({
  138. title: "插件已打开",
  139. icon: 'none',
  140. });
  141. })
  142. //监听socket关闭
  143. uni.onSocketClose(()=>{
  144. isSocketOpen=false
  145. clearInterval(pingpangTimes)
  146. console.log('WebSocket连接已关闭!');
  147. // uni.$emit('websocket',0);
  148. uni.showToast({
  149. title: "插件离线",
  150. icon: 'none',
  151. });
  152. uni.$emit('init',0);
  153. //重连
  154. that.reConnect()
  155. })
  156. //监听socket错误
  157. uni.onSocketError(()=>{
  158. isSocketOpen=false
  159. clearInterval(pingpangTimes)
  160. console.log('WebSocket连接打开失败');
  161. // uni.$emit('websocket',0);
  162. uni.$emit('init',0);
  163. // uni.showToast({
  164. // title: "插件离线",
  165. // icon: 'none',
  166. // });
  167. that.reConnect()
  168. })
  169. },
  170. sendMsg(data){
  171. if(isSocketOpen){
  172. uni.sendSocketMessage({
  173. data: JSON.stringify(data),
  174. success:()=>{
  175. console.log("发送成功")
  176. },
  177. fail:()=>{
  178. console.log("发送失败")
  179. }
  180. });
  181. }
  182. },
  183. }
  184. }
  185. </script>
  186. <style lang="scss">
  187. /*每个页面公共css */
  188. @import "@/uni_modules/uview-ui/index.scss";
  189. @import '@/assets/css/theme.scss';
  190. @import '@/assets/css/common.scss';
  191. view{
  192. box-sizing: border-box;
  193. }
  194. .ellipsis{
  195. overflow: hidden;
  196. text-overflow: ellipsis;
  197. white-space: nowrap;
  198. }
  199. .ellipsis2{
  200. overflow:hidden;
  201. text-overflow:ellipsis;
  202. display:-webkit-box;
  203. -webkit-box-orient:vertical;
  204. -webkit-line-clamp:2;
  205. }
  206. .no-data-box{
  207. height:100%;
  208. width: 100%;
  209. display: flex;
  210. justify-content: center;
  211. align-items: center;
  212. flex-direction: column;
  213. image{
  214. width: 264upx;
  215. height: 212upx;
  216. }
  217. .empty-title{
  218. margin-top: 20rpx;
  219. font-size: 28rpx;
  220. color: gray;
  221. }
  222. }
  223. .w-calc-30 {
  224. padding: 0 30rpx;
  225. width: calc(100% - 60rpx);
  226. }
  227. .hb {
  228. height: 100%;
  229. box-sizing: border-box;
  230. }
  231. .hidden {
  232. overflow: hidden;
  233. }
  234. .base-color {
  235. color: $--base-color;
  236. }
  237. .base-color-2 {
  238. color: $--base-color2;
  239. }
  240. .base-color-3 {
  241. color: $--base-color3;
  242. }
  243. .base-color-9 {
  244. color: $--base-color-9;
  245. }
  246. .base-color-8 {
  247. color: $--base-color-f8;
  248. }
  249. .base-color-6 {
  250. color: $--base-color-6;
  251. }
  252. .base-color-gray {
  253. color: $--base-color-gray;
  254. }
  255. .base-color-red{
  256. color: $--base-color-red;
  257. }
  258. .base-color-dark {
  259. color: $--base-color-dark;
  260. }
  261. .base-color-dark2 {
  262. color: $--base-color-dark2;
  263. }
  264. .base-price {
  265. color: $--base-color-price;
  266. }
  267. .base-success {
  268. color: $--base-color-success;
  269. }
  270. .base-bg {
  271. background: $--base-bg;
  272. }
  273. .base-bg-2 {
  274. background: $--base-bg2;
  275. }
  276. .base-bg-red{
  277. background: $--base-bg-red;
  278. }
  279. .base-bg-f{
  280. background-color: $--base-bg-f;
  281. }
  282. .base-bg-f8{
  283. background-color: $--base-color-f8;
  284. }
  285. .base-bg-f5{
  286. background-color: $--base-color-f5;
  287. }
  288. .base-bg-9{
  289. background-color: $--base-color-9;
  290. }
  291. .base-bg-blue{
  292. background:$--base-bg-blue;
  293. }
  294. .base-bg-sure{
  295. background:$--base-sure-bg;
  296. }
  297. .base-bg-orange{
  298. background:$--base-bg-orange;
  299. }
  300. .base-bg-false{
  301. background:$--base-false-bg;
  302. }
  303. .bor-blue{
  304. border: 2rpx solid $--base-bor-blue;
  305. }
  306. .bor-red{
  307. border: 2rpx solid $--base-bor-red;
  308. }
  309. .colorf {
  310. color: #fff;
  311. }
  312. .bgf {
  313. background: #fff;
  314. }
  315. .fixed {
  316. position: fixed;
  317. }
  318. .absolute {
  319. position: absolute;
  320. }
  321. .relative {
  322. position: relative;
  323. }
  324. .w100 {
  325. width: 100%;
  326. }
  327. .h100 {
  328. height: 100%;
  329. }
  330. .card {
  331. background: #fff;
  332. border-radius: 15rpx;
  333. }
  334. .cover-height {
  335. height: 100%;
  336. display: flex;
  337. flex-direction: column;
  338. box-sizing: border-box;
  339. }
  340. .row {
  341. display: flex;
  342. flex-direction: row;
  343. }
  344. .column {
  345. display: flex;
  346. flex-direction: column;
  347. }
  348. .justify-start {
  349. display: flex;
  350. justify-content: flex-start;
  351. }
  352. .justify-center {
  353. display: flex;
  354. justify-content: center;
  355. }
  356. .justify-end {
  357. display: flex;
  358. justify-content: flex-end;
  359. }
  360. .justify-around {
  361. display: flex;
  362. justify-content: space-around;
  363. }
  364. .justify-evenly {
  365. display: flex;
  366. justify-content: space-evenly;
  367. }
  368. .justify-between {
  369. display: flex;
  370. justify-content: space-between;
  371. }
  372. .align-start {
  373. display: flex;
  374. align-items: flex-start;
  375. }
  376. .align-center {
  377. display: flex;
  378. align-items: center;
  379. }
  380. .align-end {
  381. display: flex;
  382. align-items: flex-end;
  383. }
  384. .center {
  385. display: flex;
  386. justify-content: center;
  387. align-items: center;
  388. }
  389. .centerV {
  390. display: flex;
  391. justify-content: center;
  392. align-items: center;
  393. flex-direction: column;
  394. }
  395. .wrap {
  396. flex-wrap: wrap;
  397. }
  398. .flex-1 {
  399. flex: 1;
  400. }
  401. .ellipsis {
  402. overflow: hidden;
  403. text-overflow: ellipsis;
  404. display: -webkit-box;
  405. -webkit-box-orient: vertical;
  406. box-sizing: border-box;
  407. width: 100%;
  408. -webkit-line-clamp: 1;
  409. }
  410. .lines-2 {
  411. -webkit-line-clamp: 2 !important;
  412. }
  413. .lines-3 {
  414. -webkit-line-clamp: 3 !important;
  415. }
  416. .bold {
  417. font-weight: bold;
  418. }
  419. .line-through {
  420. text-decoration: line-through;
  421. }
  422. .nowrap {
  423. white-space: nowrap;
  424. }
  425. .scrollx {
  426. overflow-x: scroll;
  427. }
  428. .scrolly {
  429. overflow-y: scroll;
  430. }
  431. .cvauto {
  432. content-visibility: auto;
  433. }
  434. </style>
  435. <style lang="less">
  436. /*每个页面公共css */
  437. @import './assets/iconfont/iconfont.css';
  438. @import './assets/css/common.less';
  439. </style>