index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. import {code2Session, logout, checkLogin} from '@/api/login.js'
  5. import {getLookUpDatas} from '@/api/data.js'
  6. let lifeData = {};
  7. try{
  8. // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
  9. lifeData = uni.getStorageSync('lifeData');
  10. }catch(e){
  11. }
  12. // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
  13. let saveStateKeys = ['vuex_carBrandList'];
  14. // 保存变量到本地存储中
  15. const saveLifeData = function(key, value){
  16. // 判断变量名是否在需要存储的数组中
  17. if(saveStateKeys.indexOf(key) != -1) {
  18. // 获取本地存储的lifeData对象,将变量添加到对象中
  19. let tmp = uni.getStorageSync('lifeData');
  20. // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
  21. tmp = tmp ? tmp : {};
  22. tmp[key] = value;
  23. // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
  24. uni.setStorageSync('lifeData', tmp);
  25. }
  26. }
  27. const store = new Vuex.Store({
  28. state: {
  29. /**
  30. * 是否需要强制登录
  31. */
  32. forcedLogin: false,
  33. hasLogin: false,
  34. openid: "",
  35. isOpenLocation: false, // 用户是否开启授权开启定位
  36. orderStatusList: [], // 服务订单状态数据字典数据
  37. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  38. // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
  39. vuex_carBrandList: lifeData.vuex_carBrandList ? lifeData.vuex_carBrandList : [] ,// 车型品牌数据
  40. vuex_carType: {}, // 选择的车辆品牌
  41. // 检测单详情数据
  42. vuex_checkOrderData:null,
  43. deviceNo: ""
  44. },
  45. getters:{
  46. getOpeid(state){
  47. return state.openid
  48. }
  49. },
  50. mutations: {
  51. login(state, userInfo) {
  52. console.log(userInfo,'--token')
  53. state.hasLogin = true;
  54. uni.setStorageSync('token', userInfo.token);
  55. uni.setStorageSync('userInfo', userInfo.user);
  56. },
  57. logout(state) {
  58. state.hasLogin = false;
  59. uni.removeStorageSync('token')
  60. uni.removeStorageSync('userInfo')
  61. },
  62. openId(state,id) {
  63. state.openid = id
  64. },
  65. checkOrder(state,data) {
  66. state.vuex_checkOrderData = data
  67. },
  68. $uStore(state, payload) {
  69. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  70. let nameArr = payload.name.split('.');
  71. let saveKey = '';
  72. let len = nameArr.length;
  73. if(nameArr.length >= 2) {
  74. let obj = state[nameArr[0]];
  75. for(let i = 1; i < len - 1; i ++) {
  76. obj = obj[nameArr[i]];
  77. }
  78. obj[nameArr[len - 1]] = payload.value;
  79. saveKey = nameArr[0];
  80. } else {
  81. // 单层级变量,在state就是一个普通变量的情况
  82. state[payload.name] = payload.value;
  83. saveKey = payload.name;
  84. }
  85. // 保存变量到本地,见顶部函数定义
  86. saveLifeData(saveKey, state[saveKey])
  87. },
  88. isOpenLocation(state,isOpenLocation){
  89. state.isOpenLocation = isOpenLocation
  90. },
  91. orderStatusList(state,orderStatusList){
  92. state.orderStatusList = orderStatusList
  93. },
  94. },
  95. actions: {
  96. wxLogin(context,provider){
  97. uni.login({
  98. provider: provider,
  99. success(result) {
  100. console.log(result)
  101. if (result.code) {
  102. code2Session({
  103. code: result.code
  104. }).then(res => {
  105. if (res.status === '200') {
  106. uni.setStorageSync('openid', res.data);
  107. context.commit('openId',res.data)
  108. };
  109. });
  110. }
  111. }
  112. });
  113. },
  114. // 登出
  115. userLogout(context,data){
  116. logout().then(res => {
  117. console.log(res,'out')
  118. if(res.status == 200){
  119. context.commit('logout')
  120. }
  121. })
  122. },
  123. // 版本检测
  124. checekAppUpdate(){
  125. const updateManager = uni.getUpdateManager();
  126. updateManager.onCheckForUpdate(function (res) {
  127. // 请求完新版本信息的回调
  128. console.log(res.hasUpdate);
  129. });
  130. updateManager.onUpdateReady(function (res) {
  131. uni.showModal({
  132. title: '更新提示',
  133. content: '新版本已经准备好,是否重启应用?',
  134. success(res) {
  135. if (res.confirm) {
  136. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  137. updateManager.applyUpdate();
  138. }
  139. }
  140. });
  141. });
  142. updateManager.onUpdateFailed(function (res) {
  143. // 新的版本下载失败
  144. });
  145. },
  146. // 检测是否登录
  147. checkLogin(context,data){
  148. checkLogin().then(res => {
  149. if(res.status == '200'){
  150. context.commit('login',res.data)
  151. }else{
  152. context.commit('logout')
  153. }
  154. })
  155. },
  156. // 检测是否开启定位
  157. checkIsOpenLocation(context,data) {
  158. uni.authorize({
  159. scope: 'scope.userLocation',
  160. success:()=> {
  161. // 判断手机设置是否开启对微信的定位服务
  162. const systemInfo = uni.getSystemInfoSync()
  163. if(systemInfo.locationAuthorized) {
  164. console.log('授权定位开启----------')
  165. context.commit('isOpenLocation', true)
  166. } else {
  167. console.log('授权定位未开启----------')
  168. context.commit('isOpenLocation', false)
  169. }
  170. },
  171. fail() {
  172. uni.getSetting({
  173. success(res) {
  174. console.log(res.authSetting,'gggggggggg')
  175. if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
  176. context.commit('isOpenLocation', false)
  177. } else {
  178. context.commit('isOpenLocation', true)
  179. }
  180. },
  181. fail() {
  182. context.commit('isOpenLocation', false)
  183. }
  184. })
  185. }
  186. })
  187. },
  188. // 获取订单状态数据字典
  189. getOrderStatusList(context,data) {
  190. getLookUpDatas({type:'ORDER_ON_CALL_STATUS'}).then(res =>{
  191. context.commit('orderStatusList', res.data || [])
  192. })
  193. },
  194. // 获取检测单详情数据
  195. getCheckOrderData(context,data){
  196. console.log(data,'==========data============index.js')
  197. context.commit('checkOrder', data)
  198. }
  199. }
  200. })
  201. export default store