index.js 9.87 KB
import { getGoodsCategoryConfiguration } from "@/api/sys.base.js"
import { getAllGoodsList } from "@api/grounding"
import { getBaseSceneCodeBySceneType } from "@/common/sceneBaseSceneCode"
import { merchantSettleDistributeRuleLastRule } from "@api/shelfManage"
import { paymentChannelList } from "@api/merchant"
export default {
  namespaced: true,
  state: {
    estimatedFlow: "",
    estimatedUpgradeRate: "",
    operationModeAsync: "",
    shelfUpdateMerchant: {
      merchantId: "",
      merchantName: "",
      scene: ""
    },
    shelfUpdateParMer: {},
    shelfUpdateContractMer: {},
    operationMode: "",
    packageNoticeList: [], // 套餐数据
    // 商品数据
    goodsInfoData: {
      id: "",
      baseGoodsId: "",
      sapCode: "",
      supplierCode: "",
      supplierName: "",
      sceneName: "",
      sceneType: "",
      goodsName: "",
      baseGoodsName: "",
      goodsCode: "",
      brandMerchantName: "",
      specificationType: "1",
      thumbUrl: [],
      goodsTypeName: "",
      goodsImgList: [],
      goodsId: [],
      specificationDetailList: [],
      upgradeGoodsList: [],
      distributionType: "0",
      goodsSpecificationNameList: [],
      status: "",
      goodsSpecificationList: [],
      goodsPositionList: [],
      applicableModelNames: "",
      positionNames: "",
      shoppingMallFlag: "0",
      goodsSpecificationSingleList: [],
      specificationSingleDetailList: [],
      selectedSceneCode: "",
      goodsType: "",
      baseGoodsNameEmpty: false,
      scrmFlag: "N"
    },
    promiseGetGoodsCategoryConfiguration: null,
    goodsCategoryConfigurationList: null,
    isLoadingGoodsCategoryConfiguration: false, // 加载状态
    promiseGetGoods: null,
    goods: null,
    isLoadingGoods: false, // 加载状态
    merchantSettleDistributeRuleLastRule: null,
    firstLineSunshineOnlinePointsFlag: null,
    firstLineSunshine2OnlinePointsFlag: null,
    paymentChannelList: [],
    isLoadingPaymentChannelList: null,
    promiseGetPaymentChannelList: null
  },
  mutations: {
    /**
     * @description 数据改变
     * @param {Object} state state
     */
    setUpdateGoodsInfo(state, data) {
      state.goodsInfoData = Object.assign(state.goodsInfoData, data)
    },
    SET_DATA(state, { data, name }) {
      state[name] = data
    },
    SET_LOADING(state, { isLoading, name }) {
      state[name] = isLoading
    },
    SET_ERROR(state, error) {
      state.error = error
    },
    SET_PROMISE(state, { promise, name }) {
      state[name] = promise
    }
  },
  actions: {
    // ...原有fetchCategoryList
    async refreshCategoryList({ commit, dispatch }) {
      // 清空旧数据
      commit("SET_DATA", {
        data: null,
        name: "goodsCategoryConfigurationList"
      })
      dispatch("getGoodsCategoryConfiguration") // 重新请求
      // 清空旧数据
      commit("SET_DATA", {
        data: null,
        name: "goods"
      })
      commit("SET_DATA", {
        data: null,
        name: "operationModeAsync"
      })
      commit("SET_DATA", {
        data: null,
        name: "firstLineSunshineOnlinePointsFlag"
      })
      commit("SET_DATA", {
        data: null,
        name: "firstLineSunshine2OnlinePointsFlag"
      })
      commit("SET_DATA", {
        data: null,
        name: "merchantSettleDistributeRuleLastRule"
      })
      dispatch("getGoods") // 重新请求
    },
    async getGoodsCategoryConfiguration({ commit, state }) {
      if (state.goodsCategoryConfigurationList) {
        return state.goodsCategoryConfigurationList
      }
      // 正在请求,返回现有Promise
      if (state.promiseGetGoodsCategoryConfiguration) {
        return state.promiseGetGoodsCategoryConfiguration
      }
      try {
        commit("SET_LOADING", {
          isLoading: true,
          name: "isLoadingGoodsCategoryConfiguration"
        })
        const promise = getGoodsCategoryConfiguration({}) // 替换为你的API调用
        commit("SET_PROMISE", {
          promise,
          name: "promiseGetGoodsCategoryConfiguration"
        })

        const response = await promise
        commit("SET_DATA", {
          data: response.list,
          name: "goodsCategoryConfigurationList"
        })
        // 判断一下是商户多还是分账规则多, 去最大值做loading
        const {
          createBusinessApproveMerchantList,
          merchantSettleDistributeRule
        } = response

        let timeNum = Math.max(
          createBusinessApproveMerchantList?.length || 0,
          merchantSettleDistributeRule?.goodsTypeGroupRules || 0,
          15
        )
        console.log(timeNum, "timeNum")
        timeNum = timeNum >= 15 ? timeNum : 0
        console.log(timeNum, "timeNum")
        setTimeout(() => {
          commit("SET_LOADING", {
            isLoading: false,
            name: "isLoadingGoodsCategoryConfiguration"
          })
        }, timeNum * 1000)
        return response.list
      } finally {
        // commit("SET_LOADING", {
        //   isLoading: false,
        //   name: "isLoadingGoodsCategoryConfiguration"
        // })
        commit("SET_PROMISE", {
          promise: null,
          name: "promiseGetGoodsCategoryConfiguration"
        }) // 清除Promise
      }
    },
    async getGoods({ commit, state }) {
      if (state.goods) {
        return state.goods
      }
      // 正在请求,返回现有Promise
      if (state.promiseGetGoods) {
        return state.promiseGetGoods
      }
      try {
        commit("SET_LOADING", {
          isLoading: true,
          name: "isLoadingGoods"
        })
        const promise = getAllGoodsList({
          currentPage: 1,
          pageSize: 20,
          baseSceneCode: getBaseSceneCodeBySceneType(0)
        })
        commit("SET_PROMISE", {
          promise,
          name: "promiseGetGoods"
        })

        const response = await promise
        console.log(response, "getGoods")
        commit("SET_DATA", {
          data: response,
          name: "goods"
        })
        return response
      } finally {
        commit("SET_LOADING", {
          isLoading: false,
          name: "isLoadingGoods"
        })
        commit("SET_PROMISE", {
          promise: null,
          name: "promiseGetGoods"
        }) // 清除Promise
      }
    },
    async getPaymentChannelList({ commit, state }) {
      if (state.paymentChannelList && state.paymentChannelList.length) {
        return state.paymentChannelList
      }
      // 正在请求,返回现有Promise
      if (state.promiseGetPaymentChannelList) {
        return state.promiseGetPaymentChannelList
      }
      try {
        commit("SET_LOADING", {
          isLoading: true,
          name: "isLoadingPaymentChannelList"
        })
        const promise = paymentChannelList({}) // 替换为你的API调用
        commit("SET_PROMISE", {
          promise,
          name: "promiseGetPaymentChannelList"
        })

        const response = await promise
        console.log(response, "getGoods")
        commit("SET_DATA", {
          data: response,
          name: "paymentChannelList"
        })
        return response
      } finally {
        commit("SET_LOADING", {
          isLoading: false,
          name: "isLoadingPaymentChannelList"
        })
        commit("SET_PROMISE", {
          promise: null,
          name: "promiseGetPaymentChannelList"
        }) // 清除Promise
      }
    },
    async getMerchantSettleDistributeRuleLastRule({ commit, state }, id) {
      console.log(id, "getMerchantSettleDistributeRuleLastRule")
      if (state.merchantSettleDistributeRuleLastRule) {
        return state.merchantSettleDistributeRuleLastRule
      }
      // 正在请求,返回现有Promise
      if (state.promiseMerchantSettleDistributeRuleLastRuleInfo) {
        return state.promiseMerchantSettleDistributeRuleLastRuleInfo
      }
      try {
        commit("SET_LOADING", {
          isLoading: true,
          name: "isLoadingRule"
        })
        const promise = merchantSettleDistributeRuleLastRule({
          id
        }) // 替换为你的API调用
        commit("SET_PROMISE", {
          promise,
          name: "promiseMerchantSettleDistributeRuleLastRuleInfo"
        })

        const response = await promise
        console.log(response, "getMerchantSettleDistributeRuleLastRule")
        commit("SET_DATA", {
          data: response,
          name: "merchantSettleDistributeRuleLastRule"
        })
        return response
      } finally {
        commit("SET_LOADING", {
          isLoading: false,
          name: "isLoadingRule"
        })
        commit("SET_PROMISE", {
          promise: null,
          name: "promiseMerchantSettleDistributeRuleLastRuleInfo"
        }) // 清除Promise
      }
    }
  },
  getters: {
    getGoodsInfoData(state) {
      return state.goodsInfoData
    },
    merchantSettleDistributeRuleLastRuleInfo(state) {
      return state.merchantSettleDistributeRuleLastRule
    },
    packageNoticeList(state) {
      return state.packageNoticeList
    },
    operationMode(state) {
      return state.operationMode
    },
    shelfUpdateContractMer(state) {
      return state.shelfUpdateContractMer
    },
    shelfUpdateMerchant(state) {
      return state.shelfUpdateMerchant
    },
    shelfUpdateParMer(state) {
      return state.shelfUpdateParMer
    },
    estimatedFlow(state) {
      return state.estimatedFlow
    },
    estimatedUpgradeRate(state) {
      return state.estimatedUpgradeRate
    },
    operationModeAsync(state) {
      return state.operationModeAsync
    },
    firstLineSunshineOnlinePointsFlagAsync(state) {
      return state.firstLineSunshineOnlinePointsFlag
    },
    firstLineSunshine2OnlinePointsFlagAsync(state) {
      return state.firstLineSunshine2OnlinePointsFlag
    },
    isLoadingGoodsCategoryConfiguration(state) {
      return state.isLoadingGoodsCategoryConfiguration
    },
    shelfType(state) {
      // 多商户
      return (
        state.merchantSettleDistributeRuleLastRule?.basicInfo.shelfType == 2
      )
    }
  }
}