<!--
  Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.

  https://www.mall4j.com/

  未经允许，不可做商业用途！

  版权所有，侵权必究！
-->

<template>
  <view class="Mall4j page-login-success">
    <view class="success-img">
      <image
        src="@/package-user/static/images/allow.png"
        mode="heightFix"
      />
    </view>
    <view class="title">
      登录成功，账户已绑定此微信号
    </view>
    <view class="sub-title">
      后续可在 [我的] 进行账户管理
    </view>
    <button
      class="btn"
      @tap="onBack"
    >
      返回
    </button>
  </view>
</template>

<script setup>

/**
 * 生命周期函数--监听页面加载
 */
onLoad(() => {
  // 头部导航标题
  uni.setNavigationBarTitle({
    title: $t('userLogin.userLogin')
  })
})

/**
 * 生命周期函数--监听页面显示
 */
onShow(() => {
  // 头部导航标题
  uni.setNavigationBarTitle({
    title: $t('userLogin.userLogin')
  })
})

const onBack = async () => {
  try {
    const routeUrlAfterLogin = uni.getStorageSync('cloudRouteUrlAfterLogin')
    const pages = getCurrentPages()
    if (pages[pages.length - 1].route === 'pages/detail/detail' || pages[pages.length - 1].route === 'package-activities/pages/group-detail/group-detail') {
      // 如果当前页面是商品详情页或拼团详情页，则不跳转其它页面
      return
    }
    const prevPage = pages[pages.length - 2]
    if (!prevPage) {
      uni.switchTab({
        url: '/pages/index/index'
      })
      return
    }
    // 判断上一页面是否为tabbar页面 (首页和分类页无需登录接口)
    const isTabbar = prevPage.route === 'pages/user/user' || prevPage.route === 'pages/basket/basket'
    if (prevPage.route === 'package-activities/pages/live-room/live-room' || prevPage.route === 'package-activities/pages/wx-player/wx-player') {
      // 请求用户信息
      const userInfo = await util.getUserInfo()
      uni.setStorageSync('cloudUserDetails', userInfo)
      uni.setStorageSync('cloudLiveRoomReload', true)
    }
    if (isTabbar) {
      uni.switchTab({
        url: '/' + prevPage.route
      })
    } else {
      // 非tabbar页面
      let backDelata = 0
      pages.forEach((page, index) => {
        if (page.$page.fullPath === routeUrlAfterLogin) {
          backDelata = pages.length - index - 1
        }
      })
      if (backDelata) {
        uni.navigateBack({
          delta: backDelata
        })
      } else {
        uni.switchTab({
          url: '/pages/index/index'
        })
      }
    }
  } catch (e) {
    uni.switchTab({
      url: '/pages/my/my'
    })
  }
}

</script>
<style lang="scss" scoped>
@use "./login-success.scss";
</style>
