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

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