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

147 lines
2.9 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. },
  17. getters: {
  18. // 角色 true为水洗店 false为酒店 : 身份判断如果不需要,可以删除
  19. userShop(state){
  20. return state.shop
  21. }
  22. },
  23. mutations: {
  24. // 初始化配置
  25. initConfig(state){
  26. api('getConfig', res => {
  27. const configList = {
  28. ...state.configList,
  29. }
  30. if (res.code == 200) {
  31. res.result.forEach(n => {
  32. configList[n.keyName] = n.keyContent;
  33. configList[n.keyName + '_keyValue'] = n.keyValue;
  34. });
  35. }
  36. state.configList = configList
  37. uni.$emit('initConfig', state.configList)
  38. })
  39. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  40. // config.forEach(k => {
  41. // api(k, res => {
  42. // if (res.code == 200) {
  43. // state.configList[k] = res.result
  44. // }
  45. // })
  46. // })
  47. },
  48. // 微信登录
  49. login(state){
  50. uni.showLoading({
  51. title: '登录中...'
  52. })
  53. uni.login({
  54. success(res) {
  55. if(res.errMsg != "login:ok"){
  56. return
  57. }
  58. api('wxLogin', {
  59. code : res.code
  60. }, 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. // todo delete
  81. return
  82. api('getInfo', res => {
  83. if(res.code == 200){
  84. state.userInfo = res.result
  85. }
  86. })
  87. },
  88. // 退出登录
  89. logout(state){
  90. uni.showModal({
  91. title: '确认退出登录吗',
  92. success(r) {
  93. if(r.confirm){
  94. state.userInfo = {}
  95. state.role = false
  96. uni.removeStorageSync('token')
  97. uni.reLaunch({
  98. url: '/pages/index/index'
  99. })
  100. }
  101. }
  102. })
  103. },
  104. setTravelerList(state, data) {
  105. state.travelerList = data
  106. },
  107. setOrderInfo(state, data) {
  108. state.orderInfo = data
  109. },
  110. setCouponInfo(state, data) {
  111. state.couponInfo = data
  112. },
  113. setMemberInfo(state, data) {
  114. state.memberInfo = data
  115. },
  116. },
  117. actions: {
  118. async collect(state, activityId) {
  119. console.log('collect', activityId)
  120. try {
  121. await fetch('collectionActivity', { activityId })
  122. uni.showToast({
  123. icon: 'success',
  124. title: '已收藏',
  125. });
  126. return true
  127. } catch (err) {
  128. console.log('collect err', err)
  129. return false
  130. }
  131. },
  132. },
  133. })
  134. export default store