App.vue 7.6 KB

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