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

87 lines
2.2 KiB

6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
6 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. //用户未设置用户名和头像,跳转上传头像或设置昵称页面
  48. if (!state.userInfo.nickName || !state.userInfo.headImage) {
  49. uni.navigateTo({
  50. url: '/pages/login/wxUserInfo'
  51. })
  52. } else { //跳转报修页面
  53. uni.switchTab({
  54. url: '/pages/repair/repair'
  55. })
  56. }
  57. })
  58. },
  59. fail(err) {
  60. console.error(err)
  61. }
  62. })
  63. } else { //手机登录
  64. api('repairLogin' , form , res => {
  65. if (res.code == 200) {
  66. uni.setStorageSync('token', res.result.token);
  67. uni.setStorageSync('userInfo', JSON.stringify(res.result.userInfo));
  68. state.userInfo = res.result.userInfo
  69. uni.switchTab({
  70. url: '/pages/repair/repair'
  71. })
  72. }
  73. })
  74. }
  75. },
  76. setUserInfo(state, userInfo) {
  77. state.userInfo = userInfo
  78. },
  79. },
  80. actions: {},
  81. })
  82. export default store