推拿小程序前端代码仓库
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.

166 lines
3.4 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 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. const ROLE_AND_DESC_FIELDS_MAPPING = {
  6. 0: '',
  7. 1: 'member-personal', //个人会员
  8. 2: 'member-business', //企业会员
  9. }
  10. //Vuex.Store 构造器选项
  11. const store = new Vuex.Store({
  12. state: {
  13. configList: {}, //配置列表
  14. userInfo: {}, //用户信息
  15. riceInfo: {}, //用户相关信息
  16. payOrderProduct: [], //支付订单中的商品
  17. },
  18. getters: {
  19. role(state) {
  20. return ROLE_AND_DESC_FIELDS_MAPPING[state.userInfo?.role]
  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.records.forEach(n => {
  32. configList[n.paramCode] = n.paramValueImage || n.paramValueText || n.paramValueArea;
  33. // configList[n.paramCode + '_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. login(state, config) {
  49. uni.showLoading({
  50. title: '登录中...'
  51. })
  52. uni.login({
  53. success(res) {
  54. if (res.errMsg != "login:ok") {
  55. return
  56. }
  57. let data = {
  58. code: res.code,
  59. }
  60. if (uni.getStorageSync('shareId')) {
  61. data.shareId = uni.getStorageSync('shareId')
  62. }
  63. api('wxLogin', data, res => {
  64. uni.hideLoading()
  65. if (res.code != 200) {
  66. return
  67. }
  68. Vue.set(state, 'userInfo', res.result.userInfo)
  69. uni.setStorageSync('token', res.result.token)
  70. if (!state.userInfo.nickName ||
  71. !state.userInfo.headImage ||
  72. !state.userInfo.phone
  73. ) {
  74. uni.navigateTo({
  75. url: '/pages_order/auth/wxUserInfo'
  76. })
  77. } else {
  78. uni.navigateBack(-1)
  79. }
  80. })
  81. }
  82. })
  83. },
  84. getUserInfo(state) {
  85. api('getInfo', res => {
  86. if (res.code == 200) {
  87. Vue.set(state, 'userInfo', res.result)
  88. if (!state.userInfo.nickName ||
  89. !state.userInfo.headImage ||
  90. !state.userInfo.phone
  91. ) {
  92. uni.showModal({
  93. title: '申请获取您的信息!',
  94. cancelText: '稍后补全',
  95. confirmText: '现在补全',
  96. success(e) {
  97. if (e.confirm) {
  98. uni.navigateTo({
  99. url: '/pages_order/auth/wxUserInfo'
  100. })
  101. }
  102. }
  103. })
  104. }
  105. }
  106. })
  107. },
  108. getRiceInfo(state) {
  109. // todo
  110. return
  111. api('getRiceInfo', {
  112. token: uni.getStorageSync('token') || ''
  113. }, res => {
  114. if (res.code == 200) {
  115. state.riceInfo = res.result
  116. }
  117. })
  118. },
  119. // 退出登录
  120. logout(state, reLaunch = false) {
  121. // uni.showModal({
  122. // title: '确认退出登录吗',
  123. // success(r) {
  124. // if (r.confirm) {
  125. // state.userInfo = {}
  126. // uni.removeStorageSync('token')
  127. // uni.reLaunch({
  128. // url: '/pages/index/index'
  129. // })
  130. // }
  131. // }
  132. // })
  133. state.userInfo = {}
  134. uni.removeStorageSync('token')
  135. if(reLaunch){
  136. uni.reLaunch({
  137. url: '/pages/index/index'
  138. })
  139. }
  140. },
  141. // 设置支付订单中的商品
  142. setPayOrderProduct(state, data) {
  143. state.payOrderProduct = data
  144. },
  145. },
  146. actions: {},
  147. })
  148. export default store