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

206 lines
4.2 KiB

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