艺易修小程序24.08.21
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.

86 lines
2.1 KiB

10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months 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. },
  11. getters: {
  12. getConfig(state) {
  13. return state.configList
  14. }
  15. },
  16. mutations: {
  17. // 初始化配置
  18. initConfig(state) {
  19. //初始化用户信息(防止用户每次点击重新进入小程序用户信息都没有了)
  20. if (uni.getStorageSync('token') && uni.getStorageSync('userInfo')) {
  21. state.userInfo = JSON.parse(uni.getStorageSync('userInfo'))
  22. }
  23. let apiConfig = [ 'getPrivacyPolicy' , 'getUserAgreement' ] //需要访问不同接口才能得到的配置数据
  24. let key = ['privacyAgreement','userAgreement']
  25. apiConfig.forEach((item,index) => {
  26. api(item,res => {
  27. state.configList[key[index]] = res.result
  28. })
  29. })
  30. },
  31. login(state , form) {
  32. if (!form) { //微信登录
  33. uni.login({
  34. success(res) {
  35. if (res.errMsg != "login:ok") {
  36. return
  37. }
  38. api('studentLogin', {
  39. code: res.code
  40. }, res => {
  41. if (res.code != 200) {
  42. return
  43. }
  44. uni.setStorageSync('token', res.result.token);
  45. uni.setStorageSync('userInfo', JSON.stringify(res.result.userInfo));
  46. state.userInfo = res.result.userInfo
  47. if (!state.userInfo.nickName || !state.userInfo.headImage) {
  48. uni.navigateTo({
  49. url: '/pages/login/wxUserInfo'
  50. })
  51. } else {
  52. uni.switchTab({
  53. url: '/pages/repair/repair'
  54. })
  55. }
  56. })
  57. },
  58. fail(err) {
  59. console.error(err)
  60. }
  61. })
  62. } else { //手机登录
  63. api('repairLogin' , form , res => {
  64. if (res.code == 200) {
  65. uni.setStorageSync('token', res.result.token);
  66. uni.setStorageSync('userInfo', JSON.stringify(res.result.userInfo));
  67. state.userInfo = res.result.userInfo
  68. uni.switchTab({
  69. url: '/pages/repair/repair'
  70. })
  71. }
  72. })
  73. }
  74. },
  75. setUserInfo(state, userInfo) {
  76. state.userInfo = userInfo
  77. },
  78. },
  79. actions: {},
  80. })
  81. export default store