鸿宇研学生前端代码
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.

167 lines
3.4 KiB

3 weeks ago
3 weeks ago
3 weeks ago
3 weeks 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. import fetch from '@/api/fetch.js'
  6. //Vuex.Store 构造器选项
  7. const store = new Vuex.Store({
  8. state: {
  9. configList: {}, //配置列表
  10. shop : false,//身份判断如果不需要,可以删除
  11. userInfo : {}, //用户信息
  12. travelerList: null,
  13. orderInfo: null,
  14. couponInfo: null,
  15. memberInfo: null,
  16. liveInfo: null,
  17. },
  18. getters: {
  19. // 角色 true为水洗店 false为酒店 : 身份判断如果不需要,可以删除
  20. userShop(state){
  21. return state.shop
  22. }
  23. },
  24. mutations: {
  25. // 初始化配置
  26. async initConfig(state) {
  27. const records = (await fetch('getConfig'))?.records
  28. const configList = {
  29. ...state.configList,
  30. }
  31. records.forEach(n => {
  32. configList[n.paramCode] = n.paramImage || n.paramText || n.paramTextarea;
  33. });
  34. state.configList = configList
  35. // todo: fetch
  36. // fetch('queryConfigByParamCode', { paramCode: 'studytour_period' })
  37. uni.$emit('initConfig', state.configList)
  38. return
  39. api('getConfig', res => {
  40. const configList = {
  41. ...state.configList,
  42. }
  43. if (res.code == 200) {
  44. res.result.forEach(n => {
  45. configList[n.keyName] = n.keyContent;
  46. configList[n.keyName + '_keyValue'] = n.keyValue;
  47. });
  48. }
  49. state.configList = configList
  50. uni.$emit('initConfig', state.configList)
  51. })
  52. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  53. // config.forEach(k => {
  54. // api(k, res => {
  55. // if (res.code == 200) {
  56. // state.configList[k] = res.result
  57. // }
  58. // })
  59. // })
  60. },
  61. // 微信登录
  62. login(state){
  63. uni.showLoading({
  64. title: '登录中...'
  65. })
  66. uni.login({
  67. success(res) {
  68. if(res.errMsg != "login:ok"){
  69. return
  70. }
  71. api('wxLogin', {
  72. code : res.code
  73. }, res => {
  74. uni.hideLoading()
  75. if(res.code != 200){
  76. return
  77. }
  78. state.userInfo = res.result.userInfo
  79. uni.setStorageSync('token', res.result.token)
  80. if(!state.userInfo.nickName || !state.userInfo.headImage){
  81. uni.navigateTo({
  82. url: '/pages_order/auth/wxUserInfo'
  83. })
  84. }else{
  85. uni.navigateBack(-1)
  86. }
  87. })
  88. }
  89. })
  90. },
  91. // 获取用户个人信息
  92. getUserInfo(state){
  93. api('getInfo', res => {
  94. if(res.code == 200){
  95. state.userInfo = res.result
  96. }
  97. })
  98. },
  99. // 退出登录
  100. logout(state){
  101. uni.showModal({
  102. title: '确认退出登录吗',
  103. success(r) {
  104. if(r.confirm){
  105. state.userInfo = {}
  106. state.role = false
  107. uni.removeStorageSync('token')
  108. uni.reLaunch({
  109. url: '/pages/index/index'
  110. })
  111. }
  112. }
  113. })
  114. },
  115. setTravelerList(state, data) {
  116. state.travelerList = data
  117. },
  118. setOrderInfo(state, data) {
  119. state.orderInfo = data
  120. },
  121. setCouponInfo(state, data) {
  122. state.couponInfo = data
  123. },
  124. setMemberInfo(state, data) {
  125. state.memberInfo = data
  126. },
  127. setLiveInfo(state, data) {
  128. console.log('liveInfo', data)
  129. state.liveInfo = data
  130. },
  131. },
  132. actions: {
  133. async collect(state, activityId) {
  134. console.log('collect', activityId)
  135. try {
  136. await fetch('collectionActivity', { activityId })
  137. uni.showToast({
  138. icon: 'success',
  139. title: '已收藏',
  140. });
  141. return true
  142. } catch (err) {
  143. console.log('collect err', err)
  144. return false
  145. }
  146. },
  147. },
  148. })
  149. export default store