Scene.uvue 8.61 KB
<template>
    <view class="fade-in-up">
        <SingleColumnSkeleton v-if="loading" :count="3" />
        <view v-else class="list-container">
            <template v-if="sceneList.length > 0">
                <view v-for="(item, index) in sceneList"
                      :key="index"
                      class="items"
                      :class="[item.actived ? 'active' : '', pressedIndex === index ? 'pressed' : '']"
                      @touchstart="handleTouchStart(index)"
                      @touchend="handleTouchEnd"
                      @click="selectScene(item, index)">
                    <view class="left">
                        <view class="scene-icon" :class="[`scene-type-${item.sceneType}`]">
                            <text class="scene-icon-text">{{ getSceneIconText(item.sceneType) }}</text>
                        </view>
                    </view>
                    <view class="right">
                        <text class="name">{{ item.sceneName }}</text>
                        <text class="shop_no">{{ item.sceneDesc }}</text>
                    </view>
                </view>
            </template>
            <no-data v-else />
        </view>
    </view>
</template>

<script setup lang="uts">
import { defineExpose, ref } from "vue";

import { apiGetMerchantBrand, apiSelectMerchantScene } from "@/api/user-api.uts";
import NoData from "./NoData.uvue";
import SingleColumnSkeleton from "./Skeleton/SingleColumnSkeleton.uvue";
import { storage } from "@/utils/storage.uts";

type SceneItem = {
  actived: boolean
  sceneDesc: string
  sceneName: string
  sceneType: number
}

const sceneList = ref<SceneItem[]>([
	{ sceneName: "轻改", sceneDesc: "太阳膜升级转化", sceneType: 1, actived: false },
	{ sceneName: "会员权益/升级", sceneDesc: "权益、延保", sceneType: 0, actived: false },
	{ sceneName: "二手车", sceneDesc: "二手车业务", sceneType: 7, actived: false }
]);

const loading = ref<boolean>(true);
const pressedIndex = ref<number>(-1);

let userInfo: null | UTSJSONObject = null;
let jumpTimer: number = 0;

function getSceneIconText(sceneType: number): string {
	switch (sceneType) {
		case 0:
			return "权益";
		case 1:
			return "轻改";
		case 7:
			return "二手";
		default:
			return "场景";
	}
}

function handleTouchStart(index: number): void {
	pressedIndex.value = index;
}

function handleTouchEnd(): void {
	pressedIndex.value = -1;
}

function jump(info: UTSJSONObject, data: any | null): void {
	const now = Date.now();
	if (now - jumpTimer < 3000) {
		return;
	}
	jumpTimer = now;
	console.log("跳转进入首页!");
  
	if (data !== null) {
		const loginRole = (data as UTSJSONObject).loginRoleOnSelectMerchant;
		if (loginRole !== null) {
			info.loginRoleOnSelectMerchant = loginRole;
		}
	}
  
	const shopMerchantId = info.shopMerchantId as null | string;
	if (shopMerchantId === null || shopMerchantId === "") {
		uni.showToast({ title: "参数异常", icon: "none" });
		return;
	}
  
	console.log(`调用 getMerchantBrand, merchantId: ${  shopMerchantId}`);
	apiGetMerchantBrand({ merchantId: shopMerchantId }).then((res: any): void => {
		console.log("getMerchantBrand 成功");
    
		const code = (res as UTSJSONObject).code as null | string;
		if (code === "0") {
			const dataObj = (res as UTSJSONObject).data as null | UTSJSONObject;
			const channelType = dataObj != null ? (dataObj.channelType as null | number) : null;
			info.channelType = channelType ?? 1;
		} else {
			info.channelType = 1;
		}
		info.staffUuid = info.uuid; 
		storage.set("userInfo", info, 0);
    
		storage.remove("boutique_YQorderSetting");
    
		const url = "/pages/index/index";
		console.log(`准备跳转: ${  url}`);
		uni.switchTab({
			url,
			fail: (_err: any) => {
				uni.reLaunch({ url });
			}
		});
	}).catch((error) => {
		console.error(`getMerchantBrand 失败: ${  JSON.stringify(error)}`);
		info.channelType = 1;
		storage.set("userInfo", info, 0);
    
		const url = "/pages/index/index";
		console.log(`准备跳转(catch): ${  url}`);
		uni.switchTab({
			url,
			fail: (_err: any) => {
				uni.reLaunch({ url });
			}
		});
	});
}

function init(info: any): void {
	// 将 any 转换为 UTSJSONObject
	userInfo = info as UTSJSONObject;
	loading.value = false;
	console.log("进入选择场景页!");
	const openSceneArr = userInfo.openSceneArr;
	console.log(`init - openSceneArr type: ${  typeof openSceneArr}`);
	if (openSceneArr != null) {
		console.log(`init - openSceneArr length: ${  (openSceneArr as any[]).length}`);
	} else {
		console.log("init - openSceneArr is null");
	}
}

function selectScene(item: SceneItem, index: number): void {
	const sceneType = item.sceneType;
	console.log(`选择场景${  sceneType  }!`);
  
	if (userInfo == null) {
		uni.showToast({ title: "参数异常", icon: "none" });
		return;
	}
  
	const openSceneArr = userInfo.openSceneArr as any[] | null;
	const isSuper = userInfo.isSuper as boolean | null;
  
	// 手动检查是否包含 sceneType(indexOf 在 App 端有 bug)
	let sceneAllowed = isSuper == true;
	if (!sceneAllowed && openSceneArr != null) {
		let i = 0;
		while (i < openSceneArr.length) {
			if (openSceneArr[i] == sceneType) {
				sceneAllowed = true;
				break;
			}
			i++;
		}
	}
  
	if (!sceneAllowed) {
		uni.showToast({ title: "暂未开通此场景", icon: "none" });
		return;
	}
  
	const list = sceneList.value;
	let i = 0;
	while (i < list.length) {
		list[i].actived = false;
		i++;
	}
	list[index].actived = true;
	sceneList.value = list;
  
	userInfo.sceneType = sceneType;
  
	if (isSuper == true) {
		jump(userInfo, null);
	} else {
		const shopMerchantId = userInfo.shopMerchantId as null | string;
		const shopMerchantName = userInfo.shopMerchantName as null | string;
    
		apiSelectMerchantScene({
			merchantId: shopMerchantId ?? "",
			merchantName: shopMerchantName ?? "",
			sceneType
		}).then((data: any): void => {
			jump(userInfo, data);
		}).catch((error) => {
			console.error("selectMerchantScene 失败:", error);
			uni.showToast({ title: "网络异常,请稍后重试", icon: "none" });
		});
	}
}

defineExpose({
	init
});
</script>

<style scoped lang="scss">
.list-container {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.items {
  min-height: 92px;
  background-color: var(--bg-card);
  border-radius: 16px;
  padding: 18px;
  display: flex;
  flex-direction: row;
  align-items: center;
  box-sizing: border-box;
  border: 1px solid rgba(226, 232, 240, 0.6);
  box-shadow: 
    0 2px 8px rgba(15, 23, 42, 0.04),
    0 8px 24px rgba(15, 23, 42, 0.06);
  transition: all 0.1s ease-out;
  width: 100%;
  margin-bottom: 16px;
  position: relative;
  overflow: hidden;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  &:active,
  &.pressed {
    transform: scale(0.96);
    box-shadow: 
      0 1px 2px rgba(15, 23, 42, 0.08),
      0 2px 6px rgba(15, 23, 42, 0.1);
    border-color: rgba(22, 119, 255, 0.4);
    background-color: var(--bg-pressed);
  }
  
  .left {
    margin-right: 16px;
    flex-shrink: 0;
    
    .scene-icon {
      width: 52px;
      height: 52px;
      border-radius: 26px;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
      transition: all 0.3s ease;
      
      .scene-icon-text {
        font-size: 13px;
        font-weight: 700;
        text-align: center;
        letter-spacing: 0.5px;
      }
    }
    
    // 不同场景的颜色
    .scene-type-0 {
      background-color: rgba(255, 157, 10, 0.1);
      .scene-icon-text { color: #ff9d0a; }
    }
    
    .scene-type-1 {
      background-color: rgba(242, 71, 36, 0.1);
      .scene-icon-text { color: #f24724; }
    }
    
    .scene-type-7 {
      background-color: rgba(52, 123, 255, 0.1);
      .scene-icon-text { color: #347bff; }
    }
  }
  
  .right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    
    .name {
      font-weight: 600;
      font-size: 16px;
      color: #1e293b;
      margin-bottom: 6px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      letter-spacing: 0.3px;
      line-height: 1.4;
    }
    
    .shop_no {
      font-size: 13px;
      color: #94a3b8;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      letter-spacing: 0.2px;
    }
  }
}

.active {
  background-color: var(--bg-active);
  border-color: rgba(22, 119, 255, 0.3);
  box-shadow: 
    0 4px 16px rgba(22, 119, 255, 0.12),
    0 12px 32px rgba(22, 119, 255, 0.16);
  
  .right .name {
    color: #1677FF;
    font-weight: 700;
  }
  
  .left .scene-icon {
    box-shadow: 0 4px 12px rgba(22, 119, 255, 0.2);
    transform: scale(1.05);
  }
}
</style>