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

199 lines
4.1 KiB

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