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

188 lines
3.8 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 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. 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. userCenterData: {},
  13. travelerList: null,
  14. orderInfo: null,
  15. couponInfo: null,
  16. memberInfo: null,
  17. liveInfo: null,
  18. },
  19. getters: {
  20. // 角色 true为水洗店 false为酒店 : 身份判断如果不需要,可以删除
  21. userShop(state){
  22. return state.shop
  23. }
  24. },
  25. mutations: {
  26. // 初始化配置
  27. async initConfig(state) {
  28. const records = (await fetch('getConfig'))?.records
  29. const configList = {
  30. ...state.configList,
  31. }
  32. records.forEach(n => {
  33. configList[n.paramCode] = n.paramImage || n.paramText || n.paramTextarea;
  34. });
  35. const periodList = (await fetch('queryPeriodList')).records
  36. configList['periodList'] = periodList
  37. const commentOptionList = (await fetch('queryCommentOptionList')).records
  38. configList['commentOptionList'] = commentOptionList
  39. const experienceQuestionList = (await fetch('queryExperienceQuestionList')).records
  40. configList['experienceQuestionList'] = experienceQuestionList
  41. state.configList = configList
  42. uni.$emit('initConfig', state.configList)
  43. },
  44. // 微信登录
  45. login(state){
  46. uni.showLoading({
  47. title: '登录中...'
  48. })
  49. uni.login({
  50. success(res) {
  51. if(res.errMsg != "login:ok"){
  52. return
  53. }
  54. let data = {
  55. code: res.code,
  56. }
  57. if (uni.getStorageSync('shareId')) {
  58. data.vid = uni.getStorageSync('shareId')
  59. }
  60. api('wxLogin', data, res => {
  61. uni.hideLoading()
  62. if(res.code != 200){
  63. return
  64. }
  65. state.userInfo = res.result.userInfo
  66. uni.setStorageSync('token', res.result.token)
  67. if(!state.userInfo.nickName || !state.userInfo.headImage){
  68. uni.navigateTo({
  69. url: '/pages_order/auth/wxUserInfo'
  70. })
  71. }else{
  72. uni.navigateBack(-1)
  73. }
  74. })
  75. }
  76. })
  77. },
  78. // 获取用户个人信息
  79. getUserInfo(state){
  80. api('getInfo', res => {
  81. if(res.code == 200){
  82. const result = res.result
  83. switch (result.role) {
  84. case '0':
  85. result.roleDesc = '家长'
  86. break
  87. case '1':
  88. result.roleDesc = '学生'
  89. break
  90. default:
  91. break
  92. }
  93. state.userInfo = result
  94. if (!state.userInfo.role) {
  95. uni.navigateTo({
  96. url: '/pages_order/auth/roleChoose'
  97. })
  98. }
  99. }
  100. })
  101. },
  102. // 获取个人基础数据
  103. getUserCenterData(state){
  104. api('queryUserCenterData', res => {
  105. if(res.code == 200){
  106. state.userCenterData = res.result
  107. }
  108. })
  109. },
  110. // 退出登录
  111. logout(state){
  112. uni.showModal({
  113. title: '确认退出登录吗',
  114. success(r) {
  115. if(r.confirm){
  116. state.userInfo = {}
  117. state.role = false
  118. uni.removeStorageSync('token')
  119. uni.reLaunch({
  120. url: '/pages/index/index'
  121. })
  122. }
  123. }
  124. })
  125. },
  126. setTravelerList(state, data) {
  127. state.travelerList = data
  128. },
  129. setOrderInfo(state, data) {
  130. state.orderInfo = data
  131. },
  132. setCouponInfo(state, data) {
  133. state.couponInfo = data
  134. },
  135. setMemberInfo(state, data) {
  136. state.memberInfo = data
  137. },
  138. setLiveInfo(state, data) {
  139. console.log('liveInfo', data)
  140. state.liveInfo = data
  141. },
  142. },
  143. actions: {
  144. async collect(state, activityId) {
  145. console.log('collect', activityId)
  146. try {
  147. const res = await fetch('collectionActivity', { activityId }, false)
  148. uni.showToast({
  149. icon: 'success',
  150. title: res.message,
  151. });
  152. return true
  153. } catch (err) {
  154. console.log('collect err', err)
  155. uni.showToast({
  156. icon: 'error',
  157. title: err.message,
  158. });
  159. return false
  160. }
  161. },
  162. },
  163. })
  164. export default store