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

200 lines
4.2 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
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_TYPE_AND_DESC_FIELDS_MAPPING = {
  6. default: 'member-personal',
  7. 0: 'member-personal', //个人会员
  8. 1: 'member-business', //企业会员
  9. }
  10. //Vuex.Store 构造器选项
  11. const store = new Vuex.Store({
  12. state: {
  13. configList: {}, //配置列表
  14. userInfo: {}, //用户信息
  15. userCenterData: {},
  16. vipInfo: {},
  17. riceInfo: {}, //用户相关信息
  18. payOrderProduct: [], //支付订单中的商品
  19. },
  20. getters: {
  21. role(state) {
  22. const { validTime, massageVipCombo } = state.vipInfo || {}
  23. if (!validTime || new Date().getTime() > new Date(validTime).getTime()) {
  24. return ''
  25. }
  26. const { type } = massageVipCombo || {}
  27. return ROLE_TYPE_AND_DESC_FIELDS_MAPPING[type] || ROLE_TYPE_AND_DESC_FIELDS_MAPPING.default
  28. },
  29. },
  30. mutations: {
  31. // 初始化配置
  32. initConfig(state) {
  33. api('getConfig', res => {
  34. const configList = {
  35. ...state.configList,
  36. }
  37. if (res.code == 200) {
  38. res.result.records.forEach(n => {
  39. configList[n.paramCode] = n.paramValueImage || n.paramValueText || n.paramValueArea;
  40. // configList[n.paramCode + '_keyValue'] = n.keyValue;
  41. });
  42. }
  43. state.configList = configList
  44. uni.$emit('initConfig', state.configList)
  45. })
  46. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  47. // config.forEach(k => {
  48. // api(k, res => {
  49. // if (res.code == 200) {
  50. // state.configList[k] = res.result
  51. // }
  52. // })
  53. // })
  54. },
  55. login(state, config) {
  56. // #ifdef MP-WEIXIN
  57. this.commit('wxLogin', config)
  58. // #endif
  59. // #ifdef H5
  60. this.commit('h5Login', config)
  61. // #endif
  62. },
  63. h5Login(state, config){
  64. Vue.set(state, 'userInfo', config.userInfo)
  65. uni.setStorageSync('token', config.token)
  66. uni.reLaunch({
  67. url: '/pages/index/index'
  68. })
  69. },
  70. wxLogin(state, config){
  71. uni.showLoading({
  72. title: '登录中...'
  73. })
  74. uni.login({
  75. success(res) {
  76. if (res.errMsg != "login:ok") {
  77. return
  78. }
  79. let data = {
  80. code: res.code,
  81. }
  82. if (uni.getStorageSync('shareId')) {
  83. data.shareId = uni.getStorageSync('shareId')
  84. }
  85. api('wxLogin', data, res => {
  86. uni.hideLoading()
  87. if (res.code != 200) {
  88. return
  89. }
  90. Vue.set(state, 'userInfo', res.result.userInfo)
  91. uni.setStorageSync('token', res.result.token)
  92. if (!state.userInfo.nickName ||
  93. !state.userInfo.headImage ||
  94. !state.userInfo.phone
  95. ) {
  96. uni.navigateTo({
  97. url: '/pages_order/auth/wxUserInfo'
  98. })
  99. } else {
  100. uni.navigateBack(-1)
  101. }
  102. })
  103. }
  104. })
  105. },
  106. getUserInfo(state) {
  107. api('getInfo', res => {
  108. if (res.code == 200) {
  109. Vue.set(state, 'userInfo', res.result)
  110. if (!state.userInfo.nickName ||
  111. !state.userInfo.headImage ||
  112. !state.userInfo.phone
  113. ) {
  114. uni.showModal({
  115. title: '申请获取您的信息!',
  116. cancelText: '稍后补全',
  117. confirmText: '现在补全',
  118. success(e) {
  119. if (e.confirm) {
  120. uni.navigateTo({
  121. url: '/pages_order/auth/wxUserInfo'
  122. })
  123. }
  124. }
  125. })
  126. }
  127. }
  128. })
  129. },
  130. getUserCenterInfo(state) {
  131. api('getUserCenterData', res => {
  132. if (res.code == 200) {
  133. Vue.set(state, 'vipInfo', res.result?.vipInfo || {})
  134. Vue.set(state.userCenterData, 'score', res.result?.score || 0)
  135. }
  136. })
  137. },
  138. getRiceInfo(state) {
  139. // todo
  140. return
  141. api('getRiceInfo', {
  142. token: uni.getStorageSync('token') || ''
  143. }, res => {
  144. if (res.code == 200) {
  145. state.riceInfo = res.result
  146. }
  147. })
  148. },
  149. // 退出登录
  150. logout(state, reLaunch = false) {
  151. // uni.showModal({
  152. // title: '确认退出登录吗',
  153. // success(r) {
  154. // if (r.confirm) {
  155. // state.userInfo = {}
  156. // uni.removeStorageSync('token')
  157. // uni.reLaunch({
  158. // url: '/pages/index/index'
  159. // })
  160. // }
  161. // }
  162. // })
  163. state.userInfo = {}
  164. uni.removeStorageSync('token')
  165. if(reLaunch){
  166. uni.reLaunch({
  167. url: '/pages/index/index'
  168. })
  169. }
  170. },
  171. // 设置支付订单中的商品
  172. setPayOrderProduct(state, data) {
  173. state.payOrderProduct = data
  174. },
  175. },
  176. actions: {},
  177. })
  178. export default store