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

149 lines
3.0 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. state.configList[n.keyName + '_keyValue'] = n.keyValue
  24. })
  25. }
  26. })
  27. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  28. // config.forEach(k => {
  29. // api(k, res => {
  30. // if (res.code == 200) {
  31. // state.configList[k] = res.result
  32. // }
  33. // })
  34. // })
  35. },
  36. login(state, phoneCode){
  37. uni.showLoading({
  38. title: '登录中...'
  39. })
  40. uni.login({
  41. success(res) {
  42. if(res.errMsg != "login:ok"){
  43. return
  44. }
  45. let data = {
  46. code : res.code,
  47. phoneCode,
  48. }
  49. if(uni.getStorageSync('shareId')){
  50. data.shareId = uni.getStorageSync('shareId')
  51. }
  52. api('wxLogin', data, res => {
  53. uni.hideLoading()
  54. if(res.code != 200){
  55. return
  56. }
  57. state.userInfo = res.result.userInfo
  58. uni.setStorageSync('token', res.result.token)
  59. if(!state.userInfo.nickName
  60. || !state.userInfo.headImage
  61. || !state.userInfo.phone
  62. ){
  63. uni.navigateTo({
  64. url: '/pages_order/auth/wxUserInfo'
  65. })
  66. }else{
  67. uni.navigateBack(-1)
  68. }
  69. })
  70. }
  71. })
  72. },
  73. getUserInfo(state){
  74. api('getInfo', res => {
  75. if(res.code == 200){
  76. state.userInfo = res.result
  77. if(!state.userInfo.nickName
  78. || !state.userInfo.headImage
  79. || !state.userInfo.phone
  80. ){
  81. uni.showModal({
  82. title: '申请获取您的信息!',
  83. cancelText: '稍后补全',
  84. confirmText: '现在补全',
  85. success(e) {
  86. if(e.confirm){
  87. uni.navigateTo({
  88. url: '/pages_order/auth/wxUserInfo'
  89. })
  90. }
  91. }
  92. })
  93. }
  94. }
  95. })
  96. },
  97. getRiceInfo(state){
  98. api('getRiceInfo', {
  99. token : uni.getStorageSync('token') || ''
  100. },res => {
  101. if(res.code == 200){
  102. state.riceInfo = res.result
  103. }
  104. })
  105. },
  106. // 退出登录
  107. logout(state){
  108. uni.showModal({
  109. title: '确认退出登录吗',
  110. success(r) {
  111. if(r.confirm){
  112. state.userInfo = {}
  113. state.role = false
  114. uni.removeStorageSync('token')
  115. uni.reLaunch({
  116. url: '/pages/index/index'
  117. })
  118. }
  119. }
  120. })
  121. },
  122. // 查询分类接口
  123. getCategoryList(state){
  124. api('getCategoryList', res => {
  125. if(res.code == 200){
  126. state.category = res.result
  127. }
  128. })
  129. },
  130. // 设置支付订单中的商品
  131. setPayOrderProduct(state, data){
  132. state.payOrderProduct = data
  133. },
  134. },
  135. actions: {},
  136. })
  137. export default store