建材商城系统20241014
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.

209 lines
4.3 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import utils from '../utils/utils.js'
  4. Vue.use(Vuex); //vue的插件机制
  5. import api from '@/api/api.js'
  6. //Vuex.Store 构造器选项
  7. const store = new Vuex.Store({
  8. state: {
  9. configList: {}, //配置列表
  10. userInfo: {}, //用户信息
  11. riceInfo: {}, //用户相关信息
  12. category: [], //分类信息
  13. payOrderProduct: [], //支付订单中的商品
  14. promotionUrl : '',//分享二维码
  15. cityList : [],
  16. selectCity : {},
  17. },
  18. getters: {
  19. },
  20. mutations: {
  21. // 初始化配置
  22. initConfig(state) {
  23. api('getConfig', res => {
  24. const configList = {
  25. ...state.configList,
  26. }
  27. if (res.code == 200) {
  28. res.result.forEach(n => {
  29. configList[n.keyName] = n.keyContent;
  30. configList[n.keyName + '_keyValue'] = n.keyValue;
  31. });
  32. }
  33. state.configList = configList
  34. uni.$emit('initConfig', state.configList)
  35. })
  36. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  37. // config.forEach(k => {
  38. // api(k, res => {
  39. // if (res.code == 200) {
  40. // state.configList[k] = res.result
  41. // }
  42. // })
  43. // })
  44. },
  45. login(state, config = {}) {
  46. uni.showLoading({
  47. title: '登录中...'
  48. })
  49. uni.login({
  50. success : res => {
  51. if (res.errMsg != "login:ok") {
  52. return
  53. }
  54. let data = {
  55. code: res.code,
  56. }
  57. if (uni.getStorageSync('shareId')) {
  58. data.shareId = uni.getStorageSync('shareId')
  59. }
  60. api('wxLogin', data, res => {
  61. uni.hideLoading()
  62. if (res.code != 200) {
  63. return
  64. }
  65. state.userInfo = res.result.userInfo
  66. uni.setStorageSync('token', res.result.token)
  67. if(config.path){
  68. let path = config.path
  69. delete config.path
  70. delete config.shareId
  71. let para = utils.objectToUrlParams(config)
  72. uni.reLaunch({
  73. url: `${path}?${para}`,
  74. })
  75. return
  76. }
  77. if (!state.userInfo.nickName ||
  78. !state.userInfo.headImage ||
  79. !state.userInfo.phone
  80. ) {
  81. uni.navigateTo({
  82. url: '/pages_order/auth/wxUserInfo'
  83. })
  84. } else {
  85. utils.navigateBack(-1)
  86. }
  87. })
  88. }
  89. })
  90. },
  91. getUserInfo(state) {
  92. api('getInfo', res => {
  93. if (res.code == 200) {
  94. state.userInfo = res.result
  95. if (!state.userInfo.nickName ||
  96. !state.userInfo.headImage
  97. // !state.userInfo.phone
  98. ) {
  99. uni.showModal({
  100. title: '申请获取您的信息!',
  101. cancelText: '稍后补全',
  102. confirmText: '现在补全',
  103. success(e) {
  104. if (e.confirm) {
  105. uni.navigateTo({
  106. url: '/pages_order/auth/wxUserInfo'
  107. })
  108. }
  109. }
  110. })
  111. }
  112. }
  113. })
  114. },
  115. getRiceInfo(state) {
  116. api('getRiceInfo', {
  117. token: uni.getStorageSync('token') || ''
  118. }, res => {
  119. if (res.code == 200) {
  120. state.riceInfo = res.result
  121. }
  122. })
  123. },
  124. // 退出登录
  125. logout(state, reLaunch = false) {
  126. uni.showModal({
  127. title: '确认退出登录吗',
  128. success(r) {
  129. if (r.confirm) {
  130. state.userInfo = {}
  131. uni.removeStorageSync('token')
  132. uni.reLaunch({
  133. url: '/pages/index/index'
  134. })
  135. }
  136. }
  137. })
  138. // state.userInfo = {}
  139. // uni.removeStorageSync('token')
  140. // if(reLaunch){
  141. // uni.reLaunch({
  142. // url: '/pages/index/index'
  143. // })
  144. // }
  145. },
  146. getQrCode(state) {
  147. let that = this;
  148. if(!uni.getStorageSync('token')){
  149. return
  150. }
  151. uni.getImageInfo({
  152. src: `${Vue.prototype.$config.baseUrl}/info/createQrCode?token=${uni.getStorageSync('token')}`,
  153. success : res => {
  154. uni.hideLoading()
  155. that.commit('setPromotionUrl', res.path)
  156. },
  157. fail : err => {
  158. }
  159. })
  160. },
  161. // 查询分类接口
  162. getCategoryList(state) {
  163. api('getCategoryPidList', res => {
  164. if (res.code == 200) {
  165. state.category = res.result
  166. }
  167. })
  168. },
  169. // 设置支付订单中的商品
  170. setPayOrderProduct(state, data) {
  171. state.payOrderProduct = data
  172. },
  173. setPromotionUrl(state, data){
  174. state.promotionUrl = data
  175. },
  176. setCity(state, data){
  177. state.selectCity = data
  178. },
  179. getCityList(state){
  180. api('getCity', res => {
  181. if (res.code == 200) {
  182. state.cityList = res.result
  183. state.selectCity = res.result[0]
  184. uni.$emit('initCity', res.result[0])
  185. }
  186. })
  187. },
  188. },
  189. actions: {},
  190. })
  191. export default store