App.vue 7.6 KB

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