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

201 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
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. // ||
  113. // !state.userInfo.phone
  114. ) {
  115. uni.showModal({
  116. title: '申请获取您的信息!',
  117. cancelText: '稍后补全',
  118. confirmText: '现在补全',
  119. success(e) {
  120. if (e.confirm) {
  121. uni.navigateTo({
  122. url: '/pages_order/auth/wxUserInfo'
  123. })
  124. }
  125. }
  126. })
  127. }
  128. }
  129. })
  130. },
  131. getUserCenterInfo(state) {
  132. api('getUserCenterData', res => {
  133. if (res.code == 200) {
  134. Vue.set(state, 'vipInfo', res.result?.vipInfo || {})
  135. Vue.set(state.userCenterData, 'score', res.result?.score || 0)
  136. }
  137. })
  138. },
  139. getRiceInfo(state) {
  140. // todo
  141. return
  142. api('getRiceInfo', {
  143. token: uni.getStorageSync('token') || ''
  144. }, res => {
  145. if (res.code == 200) {
  146. state.riceInfo = res.result
  147. }
  148. })
  149. },
  150. // 退出登录
  151. logout(state, reLaunch = false) {
  152. // uni.showModal({
  153. // title: '确认退出登录吗',
  154. // success(r) {
  155. // if (r.confirm) {
  156. // state.userInfo = {}
  157. // uni.removeStorageSync('token')
  158. // uni.reLaunch({
  159. // url: '/pages/index/index'
  160. // })
  161. // }
  162. // }
  163. // })
  164. state.userInfo = {}
  165. uni.removeStorageSync('token')
  166. if(reLaunch){
  167. uni.reLaunch({
  168. url: '/pages/index/index'
  169. })
  170. }
  171. },
  172. // 设置支付订单中的商品
  173. setPayOrderProduct(state, data) {
  174. state.payOrderProduct = data
  175. },
  176. },
  177. actions: {},
  178. })
  179. export default store