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

145 lines
2.8 KiB

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. api('getInfo', res => {
  81. if(res.code == 200){
  82. state.userInfo = res.result
  83. }
  84. })
  85. },
  86. // 退出登录
  87. logout(state){
  88. uni.showModal({
  89. title: '确认退出登录吗',
  90. success(r) {
  91. if(r.confirm){
  92. state.userInfo = {}
  93. state.role = false
  94. uni.removeStorageSync('token')
  95. uni.reLaunch({
  96. url: '/pages/index/index'
  97. })
  98. }
  99. }
  100. })
  101. },
  102. setTravelerList(state, data) {
  103. state.travelerList = data
  104. },
  105. setOrderInfo(state, data) {
  106. state.orderInfo = data
  107. },
  108. setCouponInfo(state, data) {
  109. state.couponInfo = data
  110. },
  111. setMemberInfo(state, data) {
  112. state.memberInfo = data
  113. },
  114. },
  115. actions: {
  116. async collect(state, id) {
  117. console.log('collect', id)
  118. try {
  119. // todo: fetch
  120. // await fetch('collect', { id })
  121. uni.showToast({
  122. icon: 'success',
  123. title: '已收藏',
  124. });
  125. return true
  126. } catch (err) {
  127. return false
  128. }
  129. },
  130. },
  131. })
  132. export default store