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

181 lines
3.7 KiB

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. //Vuex.Store 构造器选项
  6. const store = new Vuex.Store({
  7. state: {
  8. configList: {}, //配置列表
  9. userInfo: {}, //用户信息
  10. riceInfo: {}, //用户相关信息
  11. category: [], //分类信息
  12. payOrderProduct: [], //支付订单中的商品
  13. promotionUrl : '',//分享二维码
  14. },
  15. getters: {},
  16. mutations: {
  17. // 初始化配置
  18. initConfig(state) {
  19. api('getConfig', res => {
  20. const configList = {
  21. ...state.configList,
  22. }
  23. if (res.code == 200) {
  24. res.result.forEach(n => {
  25. configList[n.keyName] = n.keyContent;
  26. configList[n.keyName + '_keyValue'] = n.keyValue;
  27. });
  28. }
  29. // 临时改一下,后续改回接口获取
  30. configList['logo_name'] = '愈然工坊'
  31. state.configList = configList
  32. uni.$emit('initConfig', state.configList)
  33. })
  34. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  35. // config.forEach(k => {
  36. // api(k, res => {
  37. // if (res.code == 200) {
  38. // state.configList[k] = res.result
  39. // }
  40. // })
  41. // })
  42. },
  43. login(state, config) {
  44. uni.showLoading({
  45. title: '登录中...'
  46. })
  47. uni.login({
  48. success(res) {
  49. if (res.errMsg != "login:ok") {
  50. return
  51. }
  52. let data = {
  53. code: res.code,
  54. }
  55. if (uni.getStorageSync('shareId')) {
  56. data.shareId = uni.getStorageSync('shareId')
  57. }
  58. api('wxLogin', data, res => {
  59. uni.hideLoading()
  60. if (res.code != 200) {
  61. // 接口一直返回找不到openid,临时放行
  62. res.result = {
  63. userInfo: {},
  64. }
  65. // return
  66. }
  67. state.userInfo = res.result.userInfo
  68. uni.setStorageSync('token', res.result.token)
  69. if (!state.userInfo.nickName ||
  70. !state.userInfo.headImage ||
  71. !state.userInfo.phone
  72. ) {
  73. uni.navigateTo({
  74. url: '/pages_order/auth/wxUserInfo'
  75. })
  76. } else {
  77. uni.navigateBack(-1)
  78. }
  79. })
  80. }
  81. })
  82. },
  83. getUserInfo(state) {
  84. api('getInfo', res => {
  85. if (res.code == 200) {
  86. state.userInfo = res.result
  87. if (!state.userInfo.nickName ||
  88. !state.userInfo.headImage ||
  89. !state.userInfo.phone
  90. ) {
  91. uni.showModal({
  92. title: '申请获取您的信息!',
  93. cancelText: '稍后补全',
  94. confirmText: '现在补全',
  95. success(e) {
  96. if (e.confirm) {
  97. uni.navigateTo({
  98. url: '/pages_order/auth/wxUserInfo'
  99. })
  100. }
  101. }
  102. })
  103. }
  104. }
  105. })
  106. },
  107. getRiceInfo(state) {
  108. api('getRiceInfo', {
  109. token: uni.getStorageSync('token') || ''
  110. }, res => {
  111. if (res.code == 200) {
  112. state.riceInfo = res.result
  113. }
  114. })
  115. },
  116. // 退出登录
  117. logout(state, reLaunch = false) {
  118. // uni.showModal({
  119. // title: '确认退出登录吗',
  120. // success(r) {
  121. // if (r.confirm) {
  122. // state.userInfo = {}
  123. // uni.removeStorageSync('token')
  124. // uni.reLaunch({
  125. // url: '/pages/index/index'
  126. // })
  127. // }
  128. // }
  129. // })
  130. state.userInfo = {}
  131. uni.removeStorageSync('token')
  132. if(reLaunch){
  133. uni.reLaunch({
  134. url: '/pages/index/index'
  135. })
  136. }
  137. },
  138. getQrCode(state) {
  139. api('getInviteCode', res => {
  140. if (res.code == 200) {
  141. state.promotionUrl = Vue.prototype.$config.aliOss.url + res.result.url
  142. }
  143. uni.hideLoading()
  144. })
  145. },
  146. // 查询分类接口
  147. getCategoryList(state) {
  148. api('getCategoryPidList', res => {
  149. if (res.code == 200) {
  150. state.category = res.result
  151. }
  152. })
  153. },
  154. // 设置支付订单中的商品
  155. setPayOrderProduct(state, data) {
  156. state.payOrderProduct = data
  157. },
  158. setPromotionUrl(state, data){
  159. state.promotionUrl = data
  160. },
  161. },
  162. actions: {},
  163. })
  164. export default store