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

131 lines
2.7 KiB

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