import Taro, {
useDidShow,
useShareAppMessage,
getCurrentPages,
getCurrentInstance
} from '@tarojs/taro'
import { useState, useEffect, useCallback, useRef } from 'react'
import { updateUserInfo, fetchUserFavs, updateCheckChief } from '@/store/slices/user'
import { View, ScrollView, Text, Image, Button } from '@tarojs/components'
import { SG_APP_CONFIG, MERCHANT_TOKEN, SG_TOKEN } from '@/consts'
import { useSelector, useDispatch } from 'react-redux'
import { useImmer } from 'use-immer'
import {
SpLogin,
SpImage,
SpPrice,
CouponModal,
SpModal,
SpPrivacyModal,
SpTabbar,
SpPage,
SpButton
} from '@/components'
import api from '@/api'
import {
navigateTo,
getThemeStyle,
styleNames,
classNames,
showToast,
showModal,
isWeixin,
normalizeQuerys,
log,
VERSION_PLATFORM,
VERSION_STANDARD
} from '@/utils'
import { useLogin } from '@/hooks'
import CompVipCard from './comps/comp-vipcard'
import CompBanner from './comps/comp-banner'
import CompPanel from './comps/comp-panel'
import CompMenu from './comps/comp-menu'
import CompHelpCenter from './comps/comp-helpcenter'
import './index.scss'
import S from '@/spx'
import { AtButton } from 'taro-ui'
const initialConfigState = {
banner: {
isShow: false,
loginBanner: '',
noLoginBanner: '',
pageUrl: '',
urlOpen: false,
appId: null
},
menu: {
pointMenu: false, // 积分菜单
activity: false, // 活动预约
offline_order: false, // 线下订单
boost_activity: false, // 助力活动
boost_order: false, // 助力订单
complaint: false, // 投诉记录
community_order: false, // 社区团购
ext_info: false,
group: false, // 我的拼团
member_code: false, // 会员二维码
recharge: false, // 储值
ziti_order: false, // 自提
share_enable: false, // 分享
memberinfo_enable: false, // 个人信息
tenants: true, //商家入驻
purchase: true, // 员工内购
dianwu: false, // 店务,
community: false // 社区
},
infoAppId: '',
infoPage: '',
infoUrlIsOpen: true,
pointAppId: '',
pointPage: '',
pointUrlIsOpen: true,
memberConfig: {
// defaultImg: null,
vipImg: null
},
}
const initialState = {
favCount: 0,
point: 0,
couponCount: 0,
username: '',
avatar: '',
mobile: '',
waitPayNum: 0,
waitSendNum: 0,
waitRecevieNum: 0,
waitEvaluateNum: 0,
afterSalesNum: 0,
finishNum: 0,
zitiNum: 0,
deposit: 0,
isOpen: false
}
function MemberIndex (props) {
// console.log('===>getCurrentPages==>', getCurrentPages(), getCurrentInstance())
const $instance = getCurrentInstance()
const { isLogin, isNewUser, login, getUserInfoAuth } = useLogin({
autoLogin: true,
// policyUpdateHook: (isUpdate) => {
// // isUpdate && setPolicyModal(true)
// if (isUpdate) {
// RefLogin.current._setPolicyModal()
// }
// }
})
const [config, setConfig] = useImmer(initialConfigState)
const [state, setState] = useImmer(initialState)
const [policyModal, setPolicyModal] = useState(false)
const { userInfo = {}, vipInfo = {} } = useSelector((state) => state.user)
const { pointName } = useSelector((state) => state.sys)
// log.debug(`store userInfo: ${JSON.stringify(userInfo)}`)
const dispatch = useDispatch()
Taro.setNavigationBarColor({
frontColor: '#000000',
backgroundColor: '#ffffff',
})
useEffect(() => {
if (isLogin) {
getMemberCenterData()
setMemberBackground()
const { redirect } = $instance.router.params
if (redirect) {
Taro.redirectTo({ url: decodeURIComponent(redirect) })
}
}
}, [isLogin])
useEffect(() => {
getMemberCenterConfig()
// 白名单
getSettings()
}, [])
useDidShow(() => {
if (isLogin) {
getMemberCenterData()
} else {
// setState((draft) => {
// draft.isOpen = true
// })
}
})
// useDidShow(() => {
// if (isLogin) {
// getMemberCenterData()
// setMemberBackground()
// }
// // if (S.get(MERCHANT_TOKEN, true)) {
// // S.delete(MERCHANT_TOKEN, true)
// // }
// // if (S.get(SG_TOKEN)) {
// // setHeaderBlock()
// // }
// })
async function getSettings () {
const { whitelist_status = false } = await api.shop.homeSetting()
// 白名单配置
Taro.setStorageSync(SG_APP_CONFIG, {
whitelist_status
})
}
// 分享
useShareAppMessage(async (res) => {
const { share_title, share_pic_wechatapp } = await api.member.getMemberShareConfig()
const { logo } = await api.distribution.getDistributorInfo({
distributor_id: 0
})
return {
title: share_title,
imageUrl: share_pic_wechatapp || logo,
path: '/pages/index'
}
})
const setHeaderBlock = async () => {
const resAssets = await api.member.memberAssets()
const { discount_total_count, fav_total_count, point_total_count } = resAssets
setState((draft) => {
draft.favCount = fav_total_count
draft.point = point_total_count
draft.couponCount = discount_total_count
})
}
const getMemberCenterConfig = async () => {
const [bannerRes, menuRes, redirectRes, pointShopRes] = await Promise.all([
// 会员中心banner
await api.shop.getPageParamsConfig({
page_name: 'member_center_setting'
}),
// 菜单自定义
await api.shop.getPageParamsConfig({
page_name: 'member_center_menu_setting'
}),
// 积分跳转配置
await api.shop.getPageParamsConfig({
page_name: 'member_center_redirect_setting'
}),
// 积分商城
await api.pointitem.getPointitemSetting()
])
let banner,
menu,
redirectInfo = {}
if (bannerRes.list.length > 0) {
const { app_id, is_show, login_banner, no_login_banner, page, url_is_open } =
bannerRes.list[0].params.data
banner = {
isShow: is_show,
loginBanner: login_banner,
noLoginBanner: no_login_banner,
pageUrl: page,
urlOpen: url_is_open,
appId: app_id
}
}
if (menuRes.list.length > 0) {
menu = { ...menuRes.list[0].params.data, purchase: true }
}
if (S.getAuthToken() && (VERSION_PLATFORM || VERSION_STANDARD)) {
const { result, status } = await api.member.is_admin()
console.log('env:result', result)
console.log('env:status', status)
S.set('DIANWU_CONFIG', result, status)
menu = {
...menu,
dianwu: status
}
}
if (redirectRes.list.length > 0) {
const {
info_app_id,
info_page,
info_url_is_open,
point_app_id,
point_page,
point_url_is_open
} = redirectRes.list[0].params.data
redirectInfo = {
infoAppId: info_app_id,
infoPage: info_page,
infoUrlIsOpen: info_url_is_open,
pointAppId: point_app_id,
pointPage: point_page,
pointUrlIsOpen: point_url_is_open
}
}
setConfig((draft) => {
draft.banner = banner
draft.menu = {
...menu,
pointMenu: pointShopRes.entrance.mobile_openstatus
}
draft.infoAppId = redirectInfo.info_app_id
draft.infoPage = redirectInfo.info_page
draft.infoUrlIsOpen = redirectInfo.info_url_is_open
draft.pointAppId = redirectInfo.point_app_id
draft.pointPage = redirectInfo.point_page
draft.pointUrlIsOpen = redirectInfo.point_url_is_open
})
}
const setMemberBackground = async () => {
let memberRes = await api.member.memberInfo()
setConfig((draft) => {
draft.memberConfig = {
// defaultImg: memberRes?.cardInfo?.background_pic_url,
vipImg: memberRes?.vipgrade?.background_pic_url,
backgroundImg: memberRes?.memberInfo?.gradeInfo?.background_pic_url
}
})
setState((draft) => {
draft.deposit = memberRes.deposit / 100
})
dispatch(updateUserInfo(memberRes))
}
const getMemberCenterData = async () => {
// const resSales = await api.member.getSalesperson()
const resTrade = await api.trade.getCount()
// const resVip = await api.vip.getList()
// 大转盘
// const resTurntable = await api.wheel.getTurntableconfig()
const resAssets = await api.member.memberAssets()
const { discount_total_count, fav_total_count, point_total_count } = resAssets
// aftersales: 0 //待处理的售后总数
// aftersales_pending: 0 //待处理的售后
// aftersales_processing: 0 //处理中售后
// normal_not_rate: 3 //待评价
// normal_notpay_notdelivery: 0 // 未付款未发货
// normal_payed_daifahuo: 4 // 待发货
// normal_payed_daishouhuo: 0 // 待收货
// normal_payed_daiziti: 0 //待自提订单
// normal_payed_notdelivery: 4 // 已付款未发货 or 已付款已发货
// normal_finish:1 //已完成
const {
aftersales, // 待处理售后
normal_notpay_notdelivery, // 未付款未发货
normal_payed_daifahuo, // 待发货
normal_payed_daishouhuo, // 待收货
normal_payed_daiziti, // 待自提订单
normal_not_rate, // 待评论
normal_finish
} = resTrade
setState((draft) => {
draft.favCount = fav_total_count
draft.point = point_total_count
draft.couponCount = discount_total_count
draft.waitPayNum = normal_notpay_notdelivery
draft.waitSendNum = normal_payed_daifahuo
draft.waitRecevieNum = normal_payed_daishouhuo
draft.afterSalesNum = aftersales
draft.zitiNum = normal_payed_daiziti
draft.waitEvaluateNum = normal_not_rate
draft.finishNum = normal_finish
})
}
const handleClickLink = async (link) => {
// await getUserInfoAuth()
Taro.navigateTo({ url: link })
}
const handleClickPoint = () => {
const { pointAppId, pointPage, pointUrlIsOpen } = config
if (pointUrlIsOpen) {
Taro.navigateToMiniProgram({
appId: pointAppId,
path: pointPage
})
}
}
const handleClickService = async (item) => {
const { link, key } = item
// await getUserInfoAuth(key !== 'tenants')
// 分销推广
if (key == 'popularize') {
// 已经是分销员
if (userInfo.isPromoter) {
Taro.navigateTo({ url: link })
} else {
const { confirm } = await Taro.showModal({
title: '邀请推广',
content: '确定申请成为推广员?',
showCancel: true,
cancel: '取消',
confirmText: '确认',
confirmColor: '#0b4137'
})
if (!confirm) return
const { status } = await api.distribution.become()
if (status) {
Taro.showModal({
title: '恭喜',
content: '已成为推广员',
showCancel: false,
confirmText: '好'
})
}
}
return
}
if (key == 'useinfo') {
const { infoAppId, infoPage, infoUrlIsOpen } = config
if (infoUrlIsOpen) {
Taro.navigateToMiniProgram({
appId: infoAppId,
path: infoPage
})
}
}
if (key == 'community') {
const res = await api.community.checkChief()
dispatch(updateCheckChief(res))
if (res.status) {
Taro.navigateTo({ url: link })
} else {
Taro.navigateTo({ url: `/subpages/community/order` })
}
}
if (link) {
Taro.navigateTo({ url: link })
}
}
const VipGradeDom = () => {
if (isLogin) {
return (
{(userInfo && (userInfo.username || userInfo.mobile)) || '获取昵称'}
{/* {
Taro.navigateTo({ url: '/subpages/member/member-level' })
}}
>
{
{
true: vipInfo.grade_name || '会员',
false: userInfo?.gradeInfo?.grade_name || ''
}[vipInfo.isVip]
}
*/}
)
} else {
return (
立即登录
完善个人资料
{/* */}
)
}
}
if (!config) {
return null
}
// console.log(`member page:`, state, config);
const { memberConfig } = config
// console.log('====config===', config.menu)
return (
} isTop>
setState((draft) => { draft.isOpen = true })}
>
{VipGradeDom()}
{/*
{config.menu.member_code && (
)}
*/}
{false &&
优惠券(张)
{state.couponCount}
{`${pointName}`}
{state.point}
{process.env.NODE_ENV === 'development' && (
储值(¥)
)}
收藏(个)
{state.favCount}
}
{vipInfo.isOpen &&
{/* 会员卡等级 */}
{vipInfo.isOpen && (
)}
}
{isLogin &&
{
Taro.navigateTo({ url: '/subpages/member/member-level' })
}}>{
{
true: vipInfo.grade_name || '会员',
false: userInfo?.gradeInfo?.grade_name || ''
}[vipInfo.isVip]
}
}
{config.banner?.isShow && (
)}
全部订单
}
>
{config.menu.ziti_order && (
自提订单
您有{state.zitiNum}
个等待自提的订单
)}
{state.waitPayNum > 0 && (
{state.waitPayNum}
)}
待付款
{state.waitSendNum > 0 && (
{state.waitSendNum}
)}
待发货
{state.waitRecevieNum > 0 && (
{state.waitRecevieNum}
)}
待收货
{state.finishNum > 0 && (
{state.finishNum}
)}
已完成
{/* 先隐藏评价功能 02-24 */}
{
{state.waitEvaluateNum > 0 && (
{state.waitEvaluateNum}
)}
评价
}
{/*
*/}
{/*
*/}
{/*
*/}
{/* 隐私政策 */}
{/* {
setPolicyModal(false)
}}
onConfirm={() => {
login()
RefLogin._setPolicyModal()
setPolicyModal(false)
}}
/> */}
会员服务
{/* /subpages/pointshop/list */}
积分兑换
个人收藏
个人信息
地址管理
发票管理
)
}
export default MemberIndex