敢为人鲜小程序前端代码仓库
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.

218 lines
4.7 KiB

5 months ago
5 months ago
5 months ago
1 month ago
5 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
3 months ago
5 months ago
5 months ago
5 months ago
3 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
2 months ago
2 months ago
2 months ago
3 months ago
5 months ago
1 month ago
5 months ago
1 month ago
5 months ago
5 months ago
5 months ago
4 months ago
1 month ago
1 month ago
5 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. levelInfo: {}, //团员等级信息
  12. cartData: [], //购物车数据
  13. riceInfo: {}, //用户相关信息
  14. category: [], //分类信息
  15. payOrderProduct: [], //支付订单中的商品
  16. promotionUrl : '',//分享二维码
  17. couponData: {} //选中的优惠券数据
  18. },
  19. getters: {},
  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.records.forEach(n => {
  29. state.configList[n.paramCode] = n.paramValueText ||
  30. n.paramValue ||
  31. n.paramValueImage
  32. });
  33. console.log('configList', state.configList);
  34. }
  35. // state.configList = configList
  36. uni.$emit('initConfig', state.configList)
  37. })
  38. // let config = ['getPrivacyPolicy', 'getUserAgreement']
  39. // config.forEach(k => {
  40. // api(k, res => {
  41. // if (res.code == 200) {
  42. // state.configList[k] = res.result
  43. // }
  44. // })
  45. // })
  46. },
  47. login(state, config = {}) {
  48. uni.showLoading({
  49. title: '登录中...'
  50. })
  51. uni.login({
  52. success : res => {
  53. if (res.errMsg != "login:ok") {
  54. return
  55. }
  56. let data = {
  57. code: res.code,
  58. }
  59. // 如果通过分享者链接进入小程序时,会将分享者ID存储在本地
  60. if (uni.getStorageSync('shareId')) {
  61. data.shareId = uni.getStorageSync('shareId')
  62. }
  63. api('wxLogin', data, res => {
  64. uni.hideLoading()
  65. if (res.code != 200) {
  66. return
  67. }
  68. state.userInfo = res.result.userInfo
  69. uni.setStorageSync('token', res.result.token)
  70. if(config.path){
  71. let path = config.path
  72. delete config.path
  73. delete config.shareId
  74. let para = utils.objectToUrlParams(config)
  75. uni.reLaunch({
  76. url: `${path}?${para}`,
  77. })
  78. return
  79. }
  80. if (!state.userInfo.nickName ||
  81. !state.userInfo.headImage ||
  82. !state.userInfo.phone
  83. ) {
  84. uni.navigateTo({
  85. url: '/pages_order/auth/wxUserInfo'
  86. })
  87. } else {
  88. // 直接登录成功
  89. store.commit('getUserInfo')
  90. utils.navigateBack(-1)
  91. }
  92. })
  93. }
  94. })
  95. },
  96. getUserInfo(state) {
  97. api('getUserCenterData', res => {
  98. if (res.code == 200) {
  99. state.userInfo = res.result.member
  100. state.levelInfo = res.result.level
  101. // console.log(state.levelInfo);
  102. if (!state.userInfo.nickName ||
  103. !state.userInfo.headImage ||
  104. !state.userInfo.phone
  105. ) {
  106. uni.showModal({
  107. title: '申请获取您的信息!',
  108. cancelText: '稍后补全',
  109. confirmText: '现在补全',
  110. success(e) {
  111. if (e.confirm) {
  112. uni.navigateTo({
  113. url: '/pages_order/auth/wxUserInfo'
  114. })
  115. }
  116. }
  117. })
  118. }
  119. }
  120. })
  121. },
  122. getRiceInfo(state) {
  123. api('getRiceInfo', {
  124. token: uni.getStorageSync('token') || ''
  125. }, res => {
  126. if (res.code == 200) {
  127. state.riceInfo = res.result
  128. }
  129. })
  130. },
  131. // 退出登录
  132. logout(state, reLaunch = false) {
  133. // uni.showModal({
  134. // title: '确认退出登录吗',
  135. // success(r) {
  136. // if (r.confirm) {
  137. // state.userInfo = {}
  138. // uni.removeStorageSync('token')
  139. // uni.reLaunch({
  140. // url: '/pages/index/index'
  141. // })
  142. // }
  143. // }
  144. // })
  145. state.userInfo = {}
  146. state.levelInfo = {}
  147. uni.removeStorageSync('token')
  148. if(reLaunch){
  149. uni.reLaunch({
  150. url: '/pages/index/category'
  151. })
  152. }
  153. },
  154. getQrCode(state) {
  155. let that = this;
  156. // 注释掉登录检查
  157. if(!uni.getStorageSync('token')){
  158. return
  159. }
  160. uni.getImageInfo({
  161. src: `${Vue.prototype.$config.baseUrl}/info_common/getInviteCode?token=${uni.getStorageSync('token')}`,
  162. success : res => {
  163. that.commit('setPromotionUrl', res.path)
  164. },
  165. fail : err => {
  166. }
  167. })
  168. },
  169. // 查询分类接口
  170. getCategoryList(state, fn) {
  171. api('queryCategoryList', {
  172. pageNo: 1,
  173. pageSize: 9999,
  174. }, res => {
  175. if (res.code == 200) {
  176. state.category = res.result.records
  177. fn && fn(state.category)
  178. }
  179. })
  180. },
  181. // 设置支付订单中的商品
  182. setPayOrderProduct(state, data) {
  183. state.payOrderProduct = data
  184. },
  185. setPromotionUrl(state, data){
  186. state.promotionUrl = data
  187. },
  188. // 设置购物车数据
  189. setCartData(state, data){
  190. console.log('data', data)
  191. state.cartData = data
  192. },
  193. },
  194. actions: {},
  195. })
  196. export default store