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

122 lines
2.6 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 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. userInfo:state => state.userInfo,
  17. isLogin:state=>state.token?true:false
  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,commit){
  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.switchTab({
  67. url:'/pages/index/index'
  68. })
  69. }
  70. })
  71. }
  72. })
  73. },
  74. getUserInfo(state){
  75. api('getInfo', res => {
  76. if(res.code == 200){
  77. uni.setStorageSync('userInfo',JSON.stringify(res.result))
  78. state.userInfo = res.result
  79. }
  80. })
  81. },
  82. getRiceInfo(state){
  83. api('getRiceInfo', {
  84. token : uni.getStorageSync('token') || ''
  85. },res => {
  86. if(res.code == 200){
  87. state.riceInfo = res.result
  88. }
  89. })
  90. },
  91. // 退出登录
  92. logout(state){
  93. state.userInfo = {}
  94. state.role = false
  95. state.token = ""
  96. uni.removeStorageSync('token')
  97. },
  98. // 查询分类接口
  99. getCategoryList(state){
  100. api('getCategoryList', res => {
  101. if(res.code == 200){
  102. state.category = res.result
  103. }
  104. })
  105. },
  106. // 设置支付订单中的商品
  107. setPayOrderProduct(state, data){
  108. state.payOrderProduct = data
  109. },
  110. },
  111. actions: {},
  112. })
  113. export default store