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

203 lines
4.3 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 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.setStorageSync('userInfo', config.userInfo)
  67. uni.reLaunch({
  68. url: '/pages/index/index'
  69. })
  70. },
  71. wxLogin(state, config){
  72. uni.showLoading({
  73. title: '登录中...'
  74. })
  75. uni.login({
  76. success(res) {
  77. if (res.errMsg != "login:ok") {
  78. return
  79. }
  80. let data = {
  81. code: res.code,
  82. }
  83. if (uni.getStorageSync('shareId')) {
  84. data.shareId = uni.getStorageSync('shareId')
  85. }
  86. api('wxLogin', data, res => {
  87. uni.hideLoading()
  88. if (res.code != 200) {
  89. return
  90. }
  91. Vue.set(state, 'userInfo', res.result.userInfo)
  92. uni.setStorageSync('token', res.result.token)
  93. if (!state.userInfo.nickName ||
  94. !state.userInfo.headImage ||
  95. !state.userInfo.phone
  96. ) {
  97. uni.navigateTo({
  98. url: '/pages_order/auth/wxUserInfo'
  99. })
  100. } else {
  101. uni.navigateBack(-1)
  102. }
  103. })
  104. }
  105. })
  106. },
  107. getUserInfo(state) {
  108. api('getInfo', res => {
  109. if (res.code == 200) {
  110. Vue.set(state, 'userInfo', res.result)
  111. uni.setStorageSync('userInfo', res.result)
  112. if (!state.userInfo.nickName ||
  113. !state.userInfo.headImage
  114. ||
  115. !state.userInfo.phone
  116. ) {
  117. uni.showModal({
  118. title: '申请获取您的信息!',
  119. cancelText: '稍后补全',
  120. confirmText: '现在补全',
  121. success(e) {
  122. if (e.confirm) {
  123. uni.navigateTo({
  124. url: '/pages_order/auth/wxUserInfo'
  125. })
  126. }
  127. }
  128. })
  129. }
  130. }
  131. })
  132. },
  133. getUserCenterInfo(state) {
  134. api('getUserCenterData', res => {
  135. if (res.code == 200) {
  136. Vue.set(state, 'vipInfo', res.result?.vipInfo || {})
  137. Vue.set(state.userCenterData, 'score', res.result?.score || 0)
  138. }
  139. })
  140. },
  141. getRiceInfo(state) {
  142. // todo
  143. return
  144. api('getRiceInfo', {
  145. token: uni.getStorageSync('token') || ''
  146. }, res => {
  147. if (res.code == 200) {
  148. state.riceInfo = res.result
  149. }
  150. })
  151. },
  152. // 退出登录
  153. logout(state, reLaunch = false) {
  154. // uni.showModal({
  155. // title: '确认退出登录吗',
  156. // success(r) {
  157. // if (r.confirm) {
  158. // state.userInfo = {}
  159. // uni.removeStorageSync('token')
  160. // uni.reLaunch({
  161. // url: '/pages/index/index'
  162. // })
  163. // }
  164. // }
  165. // })
  166. state.userInfo = {}
  167. uni.removeStorageSync('token')
  168. if(reLaunch){
  169. uni.reLaunch({
  170. url: '/pages/index/index'
  171. })
  172. }
  173. },
  174. // 设置支付订单中的商品
  175. setPayOrderProduct(state, data) {
  176. state.payOrderProduct = data
  177. },
  178. },
  179. actions: {},
  180. })
  181. export default store