知识付费微信小程序
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.

93 lines
1.8 KiB

9 months ago
9 months ago
9 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. let config = ['getPrivacyPolicy', 'getUserAgreement']
  20. config.forEach(k => {
  21. api(k, res => {
  22. if (res.code == 200) {
  23. state.configList[k] = res.result
  24. }
  25. })
  26. })
  27. let apiConfig = [ 'getPrivacyPolicy' , 'getUserAgreement' ] //需要访问不同接口才能得到的配置数据
  28. let key = ['privacyAgreement','userAgreement']
  29. apiConfig.forEach((item,index) => {
  30. api(item,res => {
  31. state.configList[key[index]] = res.result
  32. })
  33. })
  34. },
  35. login(state) {
  36. uni.showLoading({
  37. title: '登录中...'
  38. })
  39. uni.login({
  40. success(res) {
  41. if (res.errMsg != "login:ok") {
  42. return
  43. }
  44. api('loginLogin', {
  45. code: res.code
  46. }, res => {
  47. uni.hideLoading()
  48. if (res.code != 200) {
  49. return
  50. }
  51. state.userInfo = res.result.userInfo
  52. uni.setStorageSync('token', res.result.token)
  53. if (!state.userInfo.nickName || !state.userInfo.headImage) {
  54. uni.navigateTo({
  55. url: '/pages/login/wxUserInfo'
  56. })
  57. }else{
  58. uni.switchTab({
  59. url: '/pages/contentDetail/contentDetail'
  60. })
  61. }
  62. })
  63. },
  64. fail(err) {
  65. console.error(err)
  66. }
  67. })
  68. },
  69. getUserInfo(state) {
  70. api('getInfo', res => {
  71. if(res.code == 200){
  72. state.userInfo = res.result
  73. if (!state.userInfo.nickName || !state.userInfo.headImage) {
  74. uni.navigateTo({
  75. url: '/pages/login/wxUserInfo'
  76. })
  77. }
  78. }
  79. })
  80. },
  81. },
  82. actions: {},
  83. })
  84. export default store