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

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