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

123 lines
2.7 KiB

10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
9 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
9 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
10 months ago
9 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. areaList : [],//地区列表信息
  13. selectArea : {},//当前选择的地区
  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. console.log("initConfig===============", res);
  29. })
  30. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  31. // config.forEach(k => {
  32. // api(k, res => {
  33. // if (res.code == 200) {
  34. // state.configList[k] = res.result
  35. // }
  36. // })
  37. // })
  38. },
  39. login(state,commit){
  40. uni.showLoading({
  41. title: '登录中...'
  42. })
  43. uni.login({
  44. success(res) {
  45. if(res.errMsg != "login:ok"){
  46. return
  47. }
  48. let data = {
  49. code : res.code
  50. }
  51. if(uni.getStorageSync('shareId')){
  52. data.shareId = uni.getStorageSync('shareId')
  53. }
  54. api('wxLogin', data, res => {
  55. uni.hideLoading()
  56. if(res.code != 200){
  57. return
  58. }
  59. state.userInfo = res.result.userInfo
  60. state.token = res.result.token
  61. uni.setStorageSync('token', res.result.token)
  62. if(!state.userInfo.nickName || !state.userInfo.headImage){
  63. uni.navigateTo({
  64. url: '/pages_login/wxUserInfo'
  65. })
  66. }else{
  67. uni.switchTab({
  68. url:'/pages/index/index'
  69. })
  70. }
  71. })
  72. }
  73. })
  74. },
  75. getUserInfo(state){
  76. api('getInfo', res => {
  77. if(res.code == 200){
  78. uni.setStorageSync('userInfo',JSON.stringify(res.result))
  79. state.userInfo = res.result
  80. }
  81. })
  82. },
  83. // getRiceInfo(state){
  84. // api('getRiceInfo', {
  85. // token : uni.getStorageSync('token') || ''
  86. // },res => {
  87. // if(res.code == 200){
  88. // state.riceInfo = res.result
  89. // }
  90. // })
  91. // },
  92. // 退出登录
  93. logout(state){
  94. state.userInfo = {}
  95. state.role = false
  96. state.token = ""
  97. uni.removeStorageSync('token')
  98. },
  99. // 查询地区
  100. getArea(state, fn){
  101. api('getArea', res => {
  102. if(res.code == 200){
  103. state.areaList = res.result
  104. fn && fn(res.result)
  105. }
  106. })
  107. },
  108. setArea(state, index){
  109. state.selectArea = state.areaList[index]
  110. },
  111. },
  112. actions: {},
  113. })
  114. export default store