index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. isOpenLocation: false, // 用户是否开启授权开启定位
  35. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  36. // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
  37. vuex_allLookUp: [], // 数据字典
  38. vuex_paymentTypeList: [], // 支付方式
  39. vuex_userInfo: null,
  40. vuex_openid: ""
  41. },
  42. getters:{
  43. getOpeid(state){
  44. return state.vuex_openid
  45. }
  46. },
  47. mutations: {
  48. login(state, data) {
  49. console.log(data)
  50. state.hasLogin = true;
  51. uni.setStorageSync('token', data.token);
  52. uni.setStorageSync('userInfo', data.customer);
  53. state.vuex_userInfo = data.customer
  54. },
  55. logout(state) {
  56. state.hasLogin = false;
  57. uni.removeStorageSync('token')
  58. uni.removeStorageSync('userInfo')
  59. state.vuex_userInfo = null
  60. },
  61. openId(state,id) {
  62. state.vuex_openid = id
  63. uni.setStorageSync('openid', id);
  64. },
  65. $uStore(state, payload) {
  66. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  67. let nameArr = payload.name.split('.');
  68. let saveKey = '';
  69. let len = nameArr.length;
  70. if(nameArr.length >= 2) {
  71. let obj = state[nameArr[0]];
  72. for(let i = 1; i < len - 1; i ++) {
  73. obj = obj[nameArr[i]];
  74. }
  75. obj[nameArr[len - 1]] = payload.value;
  76. saveKey = nameArr[0];
  77. } else {
  78. // 单层级变量,在state就是一个普通变量的情况
  79. state[payload.name] = payload.value;
  80. saveKey = payload.name;
  81. }
  82. // 保存变量到本地,见顶部函数定义
  83. saveLifeData(saveKey, state[saveKey])
  84. },
  85. isOpenLocation(state,isOpenLocation){
  86. state.isOpenLocation = isOpenLocation
  87. },
  88. setHasLogin(state,isLogin){
  89. state.hasLogin = isLogin;
  90. state.vuex_userInfo = uni.getStorageSync('userInfo')
  91. }
  92. },
  93. actions: {
  94. wxLogin(context,provider){
  95. uni.login({
  96. provider: provider,
  97. success(result) {
  98. console.log(result)
  99. if (result.code) {
  100. code2Session({
  101. code: result.code
  102. }).then(res => {
  103. console.log(res)
  104. if (res.status === '200') {
  105. context.commit('openId',res.data)
  106. };
  107. });
  108. }
  109. }
  110. });
  111. },
  112. // 登出
  113. userLogout(context,data){
  114. logout().then(res => {
  115. console.log(res,'out')
  116. if(res.status == 200){
  117. context.commit('logout')
  118. uni.navigateTo({
  119. url: '/pages/login/login'
  120. });
  121. }
  122. })
  123. },
  124. // 检测是否登录
  125. checkLogin(context,callback){
  126. console.log(callback)
  127. checkLogin().then(res => {
  128. if(res.status == '200'){
  129. context.commit('setHasLogin',true)
  130. callback&&callback()
  131. }else{
  132. context.commit('logout')
  133. }
  134. })
  135. },
  136. // 检测是否开启定位
  137. checkIsOpenLocation(context,data) {
  138. uni.authorize({
  139. scope: 'scope.userLocation',
  140. success:()=> {
  141. // 判断手机设置是否开启对微信的定位服务
  142. const systemInfo = uni.getSystemInfoSync()
  143. if(systemInfo.locationAuthorized) {
  144. context.commit('isOpenLocation', true)
  145. } else {
  146. context.commit('isOpenLocation', false)
  147. }
  148. },
  149. fail() {
  150. uni.getSetting({
  151. success(res) {
  152. if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
  153. context.commit('isOpenLocation', false)
  154. } else {
  155. context.commit('isOpenLocation', true)
  156. }
  157. },
  158. fail() {
  159. context.commit('isOpenLocation', false)
  160. }
  161. })
  162. }
  163. })
  164. },
  165. }
  166. })
  167. export default store