wangxy 4 mesi fa
parent
commit
6e905fb64c

+ 36 - 0
pages/game/components/constantConfig.js

@@ -0,0 +1,36 @@
+// 食物种类列表
+export const foodSpeciesList = [{
+		id: 25,
+		imgUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
+		credit: 100
+	},
+	{
+		id: 50,
+		imgUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
+		credit: 200
+	},
+	{
+		id: 100,
+		imgUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
+		credit: 300
+	}
+]
+
+// 大鹅成长阶段
+export const gooseGrowthTypeList = [{
+		id: 0,
+		imgUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
+	},
+	{
+		id: 1,
+		imgUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
+	},
+	{
+		id: 2,
+		imgUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
+	},
+	{
+		id: 3,
+		imgUrl: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
+	}
+]

+ 21 - 0
pages/game/components/food.vue

@@ -0,0 +1,21 @@
+<template>
+	<view>
+		<image v-if="imgUrl" :src="imgUrl"></image>
+	</view>
+</template>
+
+<script setup>
+	import {foodSpeciesList} from './constantConfig';
+	import { ref,computed } from "vue";
+	const props = defineProps({
+		shiwuId: [String,Number]
+	})
+	
+	const imgUrl = computed(() => {
+		const active = foodSpeciesList.find(item => item.id == props.shiwuId)
+		return active ? active.imgUrl: ''
+	})
+</script>
+
+<style>
+</style>

+ 41 - 0
pages/game/components/foodSelect.vue

@@ -0,0 +1,41 @@
+<template>
+	<!-- 食物图标按钮 -->
+	<view @click="handleOpen">选择</view>
+	<uni-popup ref="popupRef" type="bottom" border-radius="10px 10px 0 0" background-color="#fff">
+		<view style="height: 300px;display: flex;justify-content: space-between;">
+			<view v-for="item in foodSpeciesList" :key="item.id" @click="handleSelectFood(item)">
+				<img style="width: 50px;height: auto;" :src="item.imgUrl">
+				<text>{{item.credit}}</text>
+			</view>
+		</view>
+	</uni-popup>
+</template>
+
+<script setup>
+	import {foodSpeciesList} from './constantConfig';
+	import {ref} from "vue";
+	const emits = defineEmits(['weishi'])
+	const props = defineProps({
+		shiwuId: {
+			type: [String,Number]
+		}
+	})
+	
+	const popupRef = ref(null);
+	function handleOpen() {
+		popupRef.value.open();
+	}
+	function handleClose() {
+		popupRef.value.close();
+	}
+	function handleSelectFood(shiwu) {
+		emits('weishi',shiwu);
+	}
+	
+	defineExpose({
+		handleClose
+	})
+</script>
+
+<style>
+</style>

+ 30 - 0
pages/game/components/goose.vue

@@ -0,0 +1,30 @@
+<template>
+	<view>
+		<up-line-progress :percentage="progress" activeColor="#ff0000"></up-line-progress>
+		<image :src="imgUrl"></image>
+	</view>
+</template>
+
+<script setup>
+	import {gooseGrowthTypeList} from "./constantConfig.js"
+	import { ref,computed } from "vue";
+	import upLineProgress from "uview-plus/components/u-line-progress/u-line-progress.vue";  
+	
+	const props = defineProps({
+		grouthType: {
+			type: [String,Number]
+		},
+		progress: {
+			type: [String,Number],
+			default: 0
+		}
+	})
+	
+	const imgUrl = computed(() => {
+		const active = gooseGrowthTypeList.find(item => item.grouthType == props.grouthType)
+		return active ? active.imgUrl: ''
+	})
+</script>
+
+<style>
+</style>

+ 84 - 0
pages/game/useGame.js

@@ -0,0 +1,84 @@
+import {
+	reactive,
+	ref,
+	getCurrentInstance,
+	onMounted,
+	toRefs
+} from "vue";
+import {
+	onLoad
+} from '@dcloudio/uni-app';
+import {toast,getUserIdentity,catchError} from "@/utils/common";
+import {getYouxiInfo, getYouxiWeishi} from "@/api/game.js";
+import cacheManager from "@/utils/cacheManager.js"
+
+
+export function useGame() {
+	const UserCode = getUserIdentity();
+	const data = reactive({
+			credit: 0, // 当前积分
+			growth: 0, // 成长值
+			growthType:0, // 成长类型
+			progress:0, // 当前进度
+			shiwuId:null // 上一次食物
+	})
+	async function getGameInfo() {
+		const [err, info] = await catchError(getYouxiInfo());
+		if (err) {
+			toast("请求异常,请稍后尝试");
+			return;
+		}
+		const {
+			credit,growth,growthType,progress,shiwuId
+		} = info;
+		
+		data.credit = credit;
+		data.growth = growth;
+		data.growthType = growthType;
+		data.progress = progress;
+		data.shiwuId = shiwuId;
+	}
+	
+	async function handleWeishi(cShiwuId, doFinish) {
+		const [err, info] = await catchError(getYouxiWeishi({shiwuId:cShiwuId}));
+		if (err) {
+			toast("请求异常,请稍后尝试");
+			return;
+		}
+		const {
+			credit,growth,growthType,progress,shiwuId,changeFlag
+		} = info;
+		
+		data.credit = credit;
+		data.growthType = growthType;
+		data.progress = progress;
+		data.shiwuId = shiwuId;
+		
+		if (changeFlag) {
+			// 更新成长状态
+			updateCachegrowthType();
+		}
+		
+		doFinish && doFinish();
+	}
+	
+	// 更新缓存 大鹅成长值
+	function updateCachegrowthType(growthType) {
+		cacheManager.updateObject('auth', { growthType })
+	}
+	
+
+	onLoad(async (options) => {
+		if (UserCode !== "Visitor") {
+			getGameInfo();
+		} else {}
+	})
+	
+	return {
+		...toRefs(data),
+		UserCode,
+		handleWeishi,
+	}
+	
+}
+