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

126 lines
2.4 KiB

2 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. },
  14. getters: {
  15. },
  16. mutations: {
  17. // 初始化配置
  18. initConfig(state){
  19. api('getConfig', res => {
  20. if(res.code == 200){
  21. res.result.forEach(n => {
  22. state.configList[n.keyName] = n.keyContent
  23. })
  24. }
  25. })
  26. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  27. // config.forEach(k => {
  28. // api(k, res => {
  29. // if (res.code == 200) {
  30. // state.configList[k] = res.result
  31. // }
  32. // })
  33. // })
  34. },
  35. login(state){
  36. uni.showLoading({
  37. title: '登录中...'
  38. })
  39. uni.login({
  40. success(res) {
  41. if(res.errMsg != "login:ok"){
  42. return
  43. }
  44. let data = {
  45. code : res.code
  46. }
  47. if(uni.getStorageSync('shareId')){
  48. data.shareId = uni.getStorageSync('shareId')
  49. }
  50. api('wxLogin', data, res => {
  51. uni.hideLoading()
  52. if(res.code != 200){
  53. return
  54. }
  55. state.userInfo = res.result.userInfo
  56. uni.setStorageSync('token', res.result.token)
  57. if(!state.userInfo.nickName || !state.userInfo.headImage){
  58. uni.navigateTo({
  59. url: '/pages_order/auth/wxUserInfo'
  60. })
  61. }else{
  62. uni.navigateBack(-1)
  63. }
  64. })
  65. }
  66. })
  67. },
  68. getUserInfo(state){
  69. api('getInfo', res => {
  70. if(res.code == 200){
  71. state.userInfo = res.result
  72. }
  73. })
  74. },
  75. getRiceInfo(state){
  76. api('getRiceInfo', {
  77. token : uni.getStorageSync('token') || ''
  78. },res => {
  79. if(res.code == 200){
  80. state.riceInfo = res.result
  81. }
  82. })
  83. },
  84. // 退出登录
  85. logout(state){
  86. uni.showModal({
  87. title: '确认退出登录吗',
  88. success(r) {
  89. if(r.confirm){
  90. state.userInfo = {}
  91. state.role = false
  92. uni.removeStorageSync('token')
  93. uni.reLaunch({
  94. url: '/pages/index/index'
  95. })
  96. }
  97. }
  98. })
  99. },
  100. // 查询分类接口
  101. getCategoryList(state){
  102. api('getCategoryList', res => {
  103. if(res.code == 200){
  104. state.category = res.result
  105. }
  106. })
  107. },
  108. // 设置支付订单中的商品
  109. setPayOrderProduct(state, data){
  110. state.payOrderProduct = data
  111. },
  112. },
  113. actions: {},
  114. })
  115. export default store