index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_payType','vuex_payStatus','vuex_orderInfo','vuex_workStatus'];
  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_payType: lifeData.vuex_payType ? lifeData.vuex_payType : [], // 支付方式数据字典
  34. vuex_payStatus: lifeData.vuex_payStatus ? lifeData.vuex_payStatus : [], // 支付状态数据字典
  35. // 如果无需保存到本地永久存储,无需lifeData.xx方式
  36. vuex_socket: null,
  37. vuex_wsMessageData: '',
  38. vuex_heartInterId: null,
  39. // 设备是否正在运行中
  40. vuex_workStatus: 'stop',
  41. // 订单信息
  42. vuex_orderInfo: {
  43. storeId: '', // 网点id
  44. bizId: '', // 设备编号
  45. itemCode: '', // 服务类型
  46. duration: 0, // 不同服务的机器运行时间
  47. orderNo: ''// 订单编号
  48. }
  49. },
  50. mutations: {
  51. $uStore(state, payload) {
  52. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  53. let nameArr = payload.name.split('.');
  54. let saveKey = '';
  55. let len = nameArr.length;
  56. if(nameArr.length >= 2) {
  57. let obj = state[nameArr[0]];
  58. for(let i = 1; i < len - 1; i ++) {
  59. obj = obj[nameArr[i]];
  60. }
  61. obj[nameArr[len - 1]] = payload.value;
  62. saveKey = nameArr[0];
  63. } else {
  64. // 单层级变量,在state就是一个普通变量的情况
  65. state[payload.name] = payload.value;
  66. saveKey = payload.name;
  67. }
  68. // 保存变量到本地,见顶部函数定义
  69. saveLifeData(saveKey, state[saveKey])
  70. },
  71. // 创建通知消息websocket
  72. $webSocket(state, params) {
  73. let _this = this
  74. let token = getApp().globalData.token
  75. let url = getApp().globalData.baseUrl
  76. let wsBaseUrl = url.indexOf("https:")>=0 ? url.replace("https:","wss:") : url.replace("http:","ws:")
  77. let wsUrl = wsBaseUrl + 'websocket/weixin/' + state.vuex_orderInfo.bizId
  78. // 开始连接
  79. state.vuex_socket = uni.connectSocket({
  80. header: {'X-AUTH-TOKEN':token},
  81. url: wsUrl, // ws 请求地址
  82. complete: ()=> {
  83. console.log('连接complete回调!')
  84. }
  85. });
  86. // 打开连接
  87. state.vuex_socket.onOpen(function(e){
  88. console.log('连接成功!')
  89. })
  90. // 连接关闭
  91. state.vuex_socket.onClose(function(e){
  92. console.log('ws关闭!')
  93. })
  94. // 连接错误
  95. state.vuex_socket.onError(function(e){
  96. console.log('ws错误!', JSON.stringify(e))
  97. })
  98. // 接受消息
  99. state.vuex_socket.onMessage(function(e){
  100. if (e.data != 'pong'){
  101. let ret = JSON.parse(e.data)
  102. console.log('ws接收消息!', ret)
  103. uni.$emit('wsMessage',ret)
  104. }
  105. })
  106. // 发送心跳包
  107. state.vuex_heartInterId = setInterval(function () {
  108. console.log(state.vuex_socket.readyState)
  109. if(state.vuex_socket.readyState != 1){
  110. state.vuex_socket = uni.connectSocket({
  111. header: {'X-AUTH-TOKEN':token},
  112. url: wsUrl, // ws 请求地址
  113. complete: ()=> {
  114. console.log('连接complete回调!')
  115. }
  116. });
  117. }else{
  118. _this.commit("$sendWebsocket","ping")
  119. }
  120. }, 10000)
  121. },
  122. // 关闭websocket
  123. $closeWebsocket(state){
  124. if(state.vuex_socket){
  125. state.vuex_socket.close({
  126. complete: function(e){
  127. console.log('关闭websocket完成', e)
  128. // 停止心跳
  129. clearInterval(state.vuex_heartInterId)
  130. }
  131. })
  132. }
  133. },
  134. // 发送消息
  135. $sendWebsocket(state, msg) {
  136. state.vuex_socket.send({
  137. data: msg,
  138. complete: function(e){
  139. console.log('ws发送完成', e)
  140. }
  141. })
  142. },
  143. }
  144. })
  145. export default store