<template>
	<view class="transferMoney">
		<button @click="goWxPage">测试</button>
		<button @click="getOpenId">获取code</button>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	import {
			requestMerchantTransfer
		} from "@/uni_modules/uni-requestMerchantTransfer";
	// #endif

	import request from "@/utils/request";
	export default {
		name: "transferMoney",
		props: {
			lotteryId: {
				type: Number,
				default: 0,
			},
		},
		data() {
			return {};
		},

		methods: {
			getOpenId() {
				console.log("调用登录接口获取openid");
				uni.login({
					provider: "weixin",
					onlyAuthorize: true,
					success: (loginRes) => {
						  console.log("loginRes:", loginRes);
						const code = loginRes.code;
						if (code) {
							
							request
								.post("/user/user/getOpenidApp", {
									code
								})
								.then((res) => {
									if(res.code==200){
										console.log("获取openid成功：", res);
										
										if (res.data.openid) {
											uni.setStorageSync("app_openid", res.data.openid);
											this.goWxPage();
										}
									}
								})
								.catch((err) => {
									console.error("获取openid失败：", err);
								});
						}
					},
					fail: function(err) {
						console.error("登录接口调用失败：", err);
					},
				});
			},
			goWxPage() {
				const openid = uni.getStorageSync("app_openid");
				if (!openid) {
					this.getOpenId();
					return;
				}
				const id = this.lotteryId;
				console.log(
					"开始发起企业付款，openid:",
					openid,
					" id:",
					id
				);
				request
					.post("/user/lottery/claimRedPacket", {
						id,
						openid: openid
					})
					.then((wxPayRes) => {
						console.log("wxpay res:", wxPayRes);
						if (wxPayRes.code != 200) {
							uni.showToast({
								title: "领取失败，请稍后重试",
								icon: "none",
							});
							return;
						}
						requestMerchantTransfer({
							mchId: wxPayRes.data.mch_id,
							appId: wxPayRes.data.appid,
							package: wxPayRes.data.package_info,
							success: (res) => {
								console.log("requestMerchantTransfer ", res);
							},
							fail: (res) => {
								console.log(res.errMsg, '失败');
							}
						});
					})
					.catch((err) => {
						console.error("wxpay err:", err);
					});
			},
			checkSystemType() {
				try {
					const systemInfo = uni.getSystemInfoSync();
					const {
						platform,
						system
					} = systemInfo;
					let systemType = "";

					// 优先通过 system 字段判断鸿蒙（兼容模式下 platform 仍为 android，但 system 含 HarmonyOS）
					if (system.includes("HarmonyOS")) {
						systemType = "harmony"; // 鸿蒙系统（兼容/原生）
						// 进一步区分鸿蒙原生 vs 安卓兼容模式
						if (platform === "harmony") {
							systemType = "harmony_original"; // 纯鸿蒙原生模式
						} else if (platform === "android") {
							systemType = "harmony_android"; // 鸿蒙安卓兼容模式
						}
					} else if (platform === "android") {
						systemType = "android"; // 纯安卓系统（非鸿蒙）
					} else if (platform === "ios") {
						systemType = "ios"; // iOS 系统
					} else {
						systemType = "other"; // 小程序/PC/其他系统
					}

					//   const { isHarmony, systemType } = this.checkSystemType();
					//   if (isHarmony) {
					//     console.log("当前是鸿蒙系统");
					//     if (systemType === "harmony_original") {
					//       console.log("纯鸿蒙原生模式");
					//     } else if (systemType === "harmony_android") {
					//       console.log("鸿蒙安卓兼容模式");
					//     }
					//   } else if (systemType === "android") {
					//     console.log("当前是纯安卓系统");
					//   } else if (systemType === "ios") {
					//     console.log("当前是iOS系统");
					//   }

					return {
						systemType, // 系统类型：harmony/harmony_original/harmony_android/android/ios/other
						systemInfo, // 完整系统信息（含版本、设备型号等）
						isHarmony: system.includes("HarmonyOS"), // 是否为鸿蒙系统（布尔值）
					};
				} catch (err) {
					console.error("检测系统类型失败", err);
					return {
						systemType: "unknown",
						isHarmony: false
					};
				}
			},
		},
	};
</script>

<style scoped lang="less">
	.transferMoney {}
</style>