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

141 lines
2.7 KiB

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