混凝土运输管理微信小程序、替班
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
2.9 KiB

3 weeks ago
1 week ago
3 weeks ago
1 week ago
3 weeks ago
1 week ago
3 weeks ago
1 week ago
3 weeks ago
1 week ago
3 weeks ago
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex); //vue的插件机制
  4. import api from '@/api/api.js'
  5. //Vuex.Store 构造器选项
  6. const store = new Vuex.Store({
  7. state: {
  8. configList: {}, //配置列表
  9. userInfo : {}, //用户信息
  10. role : '0', // 当前角色:0-用户/泵司,1-企业,2-员工,3-区域管理员
  11. roleOptions: [
  12. { value: '0', label: '用户/泵司' },
  13. { value: '1', label: '企业用户' },
  14. { value: '2', label: '员工' },
  15. { value: '3', label: '区域管理员' }
  16. ],
  17. },
  18. getters: {
  19. // 获取当前角色文本
  20. currentRoleText: (state) => {
  21. const role = state.roleOptions.find(r => r.value === state.role);
  22. return role ? role.label : '用户/泵司';
  23. },
  24. // 获取角色选项
  25. getRoleOptions: (state) => {
  26. return state.roleOptions;
  27. }
  28. },
  29. mutations: {
  30. // 初始化配置
  31. initConfig(state){
  32. api('getConfig', res => {
  33. const configList = {
  34. ...state.configList,
  35. }
  36. if (res.code == 200) {
  37. res.result.forEach(n => {
  38. configList[n.keyName] = n.keyContent;
  39. configList[n.keyName + '_keyValue'] = n.keyValue;
  40. });
  41. }
  42. state.configList = configList
  43. uni.$emit('initConfig', state.configList)
  44. })
  45. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  46. // config.forEach(k => {
  47. // api(k, res => {
  48. // if (res.code == 200) {
  49. // state.configList[k] = res.result
  50. // }
  51. // })
  52. // })
  53. },
  54. // 微信登录
  55. login(state){
  56. uni.showLoading({
  57. title: '登录中...'
  58. })
  59. uni.login({
  60. success(res) {
  61. if(res.errMsg != "login:ok"){
  62. return
  63. }
  64. api('wxLogin', {
  65. code : res.code
  66. }, res => {
  67. uni.hideLoading()
  68. if(res.code != 200){
  69. return
  70. }
  71. state.userInfo = res.result.userInfo
  72. uni.setStorageSync('token', res.result.token)
  73. if(!state.userInfo.nickName || !state.userInfo.headImage){
  74. uni.navigateTo({
  75. url: '/pages_order/auth/wxUserInfo'
  76. })
  77. }else{
  78. uni.navigateBack(-1)
  79. }
  80. })
  81. }
  82. })
  83. },
  84. // 获取用户个人信息
  85. getUserInfo(state){
  86. api('getInfo', res => {
  87. if(res.code == 200){
  88. state.userInfo = res.result
  89. }
  90. })
  91. },
  92. // 切换角色
  93. switchRole(state, roleValue) {
  94. state.role = roleValue;
  95. uni.setStorageSync('role', roleValue);
  96. // 触发全局事件通知角色变更
  97. uni.$emit('roleChanged', roleValue);
  98. },
  99. // 初始化角色
  100. initRole(state) {
  101. const savedRole = uni.getStorageSync('role');
  102. if (savedRole) {
  103. state.role = savedRole;
  104. }
  105. },
  106. // 退出登录
  107. logout(state){
  108. uni.showModal({
  109. title: '确认退出登录吗',
  110. success(r) {
  111. if(r.confirm){
  112. state.userInfo = {}
  113. state.role = '0'
  114. uni.removeStorageSync('token')
  115. uni.removeStorageSync('role')
  116. uni.reLaunch({
  117. url: '/pages/index/index'
  118. })
  119. }
  120. }
  121. })
  122. },
  123. },
  124. actions: {},
  125. })
  126. export default store