index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. let lifeData = {};
  5. try{
  6. // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
  7. lifeData = uni.getStorageSync('lifeData');
  8. }catch(e){
  9. }
  10. // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
  11. let saveStateKeys = ['vuex_user', 'vuex_token', 'vuex_userData','vuex_hasLogin','vuex_openid'];
  12. // 保存变量到本地存储中
  13. const saveLifeData = function(key, value){
  14. // 判断变量名是否在需要存储的数组中
  15. if(saveStateKeys.indexOf(key) != -1) {
  16. // 获取本地存储的lifeData对象,将变量添加到对象中
  17. let tmp = uni.getStorageSync('lifeData');
  18. // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
  19. tmp = tmp ? tmp : {};
  20. tmp[key] = value;
  21. // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
  22. uni.setStorageSync('lifeData', tmp);
  23. }
  24. }
  25. const store = new Vuex.Store({
  26. // 下面这些值仅为示例,使用过程中请删除
  27. state: {
  28. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  29. // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
  30. vuex_user: lifeData.vuex_user ? lifeData.vuex_user : {account:'',password:''},
  31. vuex_token: lifeData.vuex_token ? lifeData.vuex_token : '',
  32. vuex_userData: lifeData.vuex_userData ? lifeData.vuex_userData : '',
  33. vuex_hasLogin: lifeData.vuex_hasLogin ? lifeData.vuex_hasLogin : false,
  34. vuex_openid: lifeData.vuex_openid ? lifeData.vuex_openid : '',
  35. // 如果无需保存到本地永久存储,无需lifeData.xx方式
  36. },
  37. mutations: {
  38. $uStore(state, payload) {
  39. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  40. let nameArr = payload.name.split('.');
  41. let saveKey = '';
  42. let len = nameArr.length;
  43. if(nameArr.length >= 2) {
  44. let obj = state[nameArr[0]];
  45. for(let i = 1; i < len - 1; i ++) {
  46. obj = obj[nameArr[i]];
  47. }
  48. obj[nameArr[len - 1]] = payload.value;
  49. saveKey = nameArr[0];
  50. } else {
  51. // 单层级变量,在state就是一个普通变量的情况
  52. state[payload.name] = payload.value;
  53. saveKey = payload.name;
  54. }
  55. // 保存变量到本地,见顶部函数定义
  56. saveLifeData(saveKey, state[saveKey])
  57. },
  58. // 创建通知消息websocket
  59. $webSocket(state, params) {
  60. let _this = this
  61. let userInfo = state.vuex_userData
  62. let url = getApp().globalData.baseUrl
  63. let wsBaseUrl = url.indexOf("https:")>=0 ? url.replace("https:","wss:") : url.replace("http:","ws:")
  64. let wsUrl = wsBaseUrl + 'websocket/' + 's' + userInfo.parentOrgId + 'o' + userInfo.orgId + 'u' + userInfo.userid + 'eapp'
  65. // 开始连接
  66. state.vuex_socket = uni.connectSocket({
  67. url: wsUrl, // ws 请求地址
  68. complete: ()=> {
  69. console.log('连接complete回调!')
  70. // 刷新未读消息数量
  71. uni.$emit("updateUnreadcount",{msg:''})
  72. }
  73. });
  74. // 打开连接
  75. state.vuex_socket.onOpen(function(e){
  76. console.log('连接成功!')
  77. })
  78. // 连接关闭
  79. state.vuex_socket.onClose(function(e){
  80. console.log('ws关闭!')
  81. })
  82. // 连接错误
  83. state.vuex_socket.onError(function(e){
  84. console.log('ws错误!', JSON.stringify(e))
  85. })
  86. // 接受消息
  87. state.vuex_socket.onMessage(function(e){
  88. if (e.data != 'pong'){
  89. let ret = JSON.parse(e.data)
  90. console.log('ws接收!', ret)
  91. state.vuex_wsMessageData = ret
  92. // 入厂车辆拍照识别
  93. if (ret.type == 'enter_store_vehicle'){}
  94. // 新未读消息
  95. if (ret.type == 'no_read_count'){
  96. state.vuex_messagUnReadCount = ret.data.no_read_count
  97. }
  98. }
  99. })
  100. // 发送心跳包
  101. state.vuex_heartInterId = setInterval(function () {
  102. console.log(state.vuex_socket.readyState)
  103. if(state.vuex_socket.readyState != 1){
  104. state.vuex_socket = uni.connectSocket({
  105. url: wsUrl, // ws 请求地址
  106. complete: ()=> {
  107. console.log('连接complete回调!')
  108. // 刷新未读消息数量
  109. uni.$emit("updateUnreadcount",{msg:''})
  110. }
  111. });
  112. }else{
  113. _this.commit("$sendWebsocket","ping")
  114. }
  115. }, 10000)
  116. },
  117. // 关闭websocket
  118. $closeWebsocket(state){
  119. if(state.vuex_socket){
  120. state.vuex_socket.close({
  121. complete: function(e){
  122. console.log('关闭websocket完成', e)
  123. // 停止心跳
  124. clearInterval(state.vuex_heartInterId)
  125. }
  126. })
  127. }
  128. },
  129. // 发送消息
  130. $sendWebsocket(state, msg) {
  131. state.vuex_socket.send({
  132. data: msg,
  133. complete: function(e){
  134. console.log('ws发送完成', e)
  135. }
  136. })
  137. },
  138. },
  139. actions: {
  140. // 检测是否登录
  141. checkLogin(context,data){
  142. checkLogin().then(res => {
  143. if(res.status == '200'){
  144. context.commit('$uStore',{name:'vuex_hasLogin',value:true})
  145. context.commit('$uStore',{name:'vuex_token',value: res.data.token})
  146. }else{
  147. context.commit('$uStore',{name:'vuex_hasLogin',value:false})
  148. context.commit('$uStore',{name:'vuex_token',value: ''})
  149. }
  150. })
  151. }
  152. }
  153. })
  154. export default store