App.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <script>
  2. export default {
  3. globalData: {
  4. token: "",
  5. // 文本消息
  6. socketTask: null,
  7. urlData: "ws://192.168.1.17:12345/api/api_gateway?method=control_center.real_time.im_message",
  8. is_open_socket: false, // 确保websocket是打开状态
  9. list: [],
  10. userId: null, // 用户ID
  11. // 即构
  12. video: null, //房间密码和token
  13. app: []
  14. },
  15. onLoad() {},
  16. onLaunch: function() {
  17. // App启动
  18. var that = this
  19. // uni.hideTabBar({})
  20. uni.getStorage({
  21. key: 'session_key',
  22. success: function(res) {
  23. // console.log(res)
  24. that.globalData.token = res.data
  25. // console.log('登录成功了')
  26. that.msgInit(); // 文本初始化
  27. },
  28. fail: function(err) {
  29. // console.log(err)
  30. // console.log('没有登录成功')
  31. }
  32. })
  33. },
  34. onShow: function() {
  35. // App展示在前台
  36. // console.log('App Show')、
  37. uni.getStorage({
  38. key: 'session_key',
  39. success: (res) => {
  40. console.log(res)
  41. if (res.data != "") {
  42. this.getlistinfo()
  43. } else {
  44. uni.navigateTo({
  45. url: "../login/login"
  46. })
  47. }
  48. },
  49. fail:()=> {
  50. uni.navigateTo({
  51. url: "../login/login"
  52. })
  53. }
  54. })
  55. },
  56. onHide: function() {
  57. // App不再再展示在前台
  58. // console.log('App Hide')
  59. },
  60. methods: {
  61. // 实时通信 - 初始化
  62. async getlistinfo() {
  63. const res = await this.$myRequest({
  64. url: '/api/api_gateway?method=sysmenage.usermanager.user_info',
  65. })
  66. // console.log(res)
  67. this.app = res.children.filter((item) => {
  68. return item.purview_name == "APP"
  69. })
  70. console.log(this.app)
  71. if (this.app.length != 0) {
  72. if (this.app[0].children[0].purview_name == "我的任务") {
  73. uni.switchTab({
  74. url: "../index/index"
  75. })
  76. } else if (this.app[0].children[0].purview_name == "监督") {
  77. uni.switchTab({
  78. url: "../supervise/index"
  79. })
  80. uni.setTabBarItem({
  81. index: 0,
  82. visible: false
  83. });
  84. } else {
  85. uni.switchTab({
  86. url: "../response/index"
  87. })
  88. uni.setTabBarItem({
  89. index: 0,
  90. visible: false
  91. });
  92. uni.setTabBarItem({
  93. index: 1,
  94. visible: false
  95. });
  96. }
  97. } else {
  98. uni.setTabBarItem({
  99. index: 0,
  100. visible: false
  101. });
  102. uni.setTabBarItem({
  103. index: 1,
  104. visible: false
  105. });
  106. uni.switchTab({
  107. url: "../response/index"
  108. })
  109. }
  110. // console.log(this.app)
  111. },
  112. msgInit() {
  113. var that = this
  114. // 连接webscoket
  115. // console.log(this.globalData.urlData)
  116. that.globalData.socketTask = uni.connectSocket({
  117. url: this.globalData.urlData + "&token=" + that.globalData.token,
  118. success(data) {
  119. // console.log("websocket连接成功");
  120. }
  121. });
  122. // 发送消息 - 获取对话列表
  123. var obj = {
  124. 'action': 'list', // 动作标识,必填
  125. 'recv_user_id': '', // 接收人用户id, 非必填
  126. 'data': {}
  127. }
  128. that.globalData.socketTask.onOpen((res) => {
  129. that.globalData.socketTask.send({
  130. data: JSON.stringify(obj),
  131. async success(res) {
  132. that.globalData.is_open_socket = true
  133. console.log("消息发送成功");
  134. },
  135. });
  136. })
  137. // // 接收服务器返回的消息
  138. // // 注:只有连接正常打开中 ,才能正常收到消息
  139. that.globalData.socketTask.onMessage((res) => {
  140. console.log("收到服务器内容:");
  141. var val = JSON.parse(res.data)
  142. if (val.action == 'list') {
  143. var obj1 = {
  144. 'action': 'list', // 动作标识,必填
  145. 'recv_user_id': that.globalData.userId, // 接收人用户id, 非必填
  146. 'data': {
  147. 'msg_status': false, // 消息未读
  148. 'msg_info': '', // 发送消息
  149. }
  150. }
  151. that.globalData.socketTask.send({
  152. data: JSON.stringify(obj1),
  153. async success(res) {
  154. // console.log("消息发送成功");
  155. },
  156. });
  157. } else if (val.action == 'none') {
  158. // console.log('进入了')
  159. that.globalData.list = val.data
  160. console.log(that.globalData.list)
  161. this.$store.state.list = val.data
  162. // console.log(this.$store.state.list)
  163. } else if (val.action == 'recv_video') {
  164. // console.log(val.data)
  165. that.globalData.video = val.data
  166. this.$store.state.video = val.data
  167. console.log(this.$store.state.video)
  168. }
  169. });
  170. // console.log(that.socketTask)
  171. },
  172. },
  173. }
  174. </script>
  175. <!-- // App.vue文件 中首行的位置引入,注意给style标签加入lang="scss"属性 -->
  176. <style lang="scss">
  177. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  178. @import "uview-ui/index.scss";
  179. </style>