1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120 |
- <template>
- <!--
-
- 设定:
- suo:锁;
- flower:花,共7种,图片分别为flower1.png flower2.png flower7.png;
- zacao:杂草;
- tian:田地,如果是待解锁的田地,图片用tian_suo.png,如果是已解锁就用tian.png;
- souhuo:收获标识;
- select_zz:7种花的view,默认应该隐藏,点击播种时显示,播种结束后隐藏
- 4个按钮:
- 解锁荒地:点击时,将第一块待解锁的田地转换为已解锁地
- 除草:点击时,让杂草隐藏
- 播种:点击时,需要先将7种花显示出来,选择其中一种,然后再点击除去杂草的地,然后该块地上显示种子,然后10分钟倒计时,然后显示对应的花,以及收获标识
- 收获:点击后,有收获标识的土地上的收获标识图片隐藏,花隐藏,出现杂草
- 操作流程:
- 默认显示待解锁的田地(其他几个图标都隐藏),点击"解锁荒地",点击"除草",点击"播种",到时间后点击"收获" -->
- <view class="page" v-if="show">
- <view class="task-board">
- <view class="task_day" @click="handleTaskDayClick" :class="{'task-day-active': taskDayActive}">
- <image src="../../static/island/huatian/btn_task_day.png" class="task"></image>
- </view>
- <view class="board-title">
- </view>
- <view class="bodyContent">
- <block v-for="(field, index) in fields" :key="index">
- <view class="blank" v-if="index == 0 || index == 7 || index == 10"></view>
- <view class="item" :class="{
- 'locked': !field.isUnlocked,
- 'weedy': field.hasWeed,
- 'weed-blink': weedMode && field.hasWeed,
- 'planted': field.flower,
- 'growing': field.isGrowing,
- 'ready': field.canHarvest,
- 'harvest-blink': harvestMode && field.canHarvest
- }" @click="handleFieldClick(index)">
- <image v-if="!field.isUnlocked" src="../../static/island/huatian/suo.png" class="suo"></image>
- <image v-if="field.flower && field.growthCompleted"
- :src="`../../static/island/huatian/flower${field.flowerType}.png`" class="flower"></image>
- <image v-if="field.seedStage && field.isGrowing" src="../../static/island/huatian/zongzi.png"
- class="flower"></image>
- <image v-if="field.hasWeed && field.isUnlocked" src="../../static/island/huatian/zacao.png"
- class="zacao"></image>
- <image
- :src="field.isUnlocked ? '../../static/island/huatian/tian.png' : '../../static/island/huatian/tian_suo.png'"
- class="tian"></image>
- <image v-if="field.canHarvest" src="../../static/island/huatian/souhuo.png" class="souhuo">
- </image>
- <view class="timer" v-if="field.isGrowing">
- <image src="../../static/island/huatian/timer.png" class="icon"></image>
- <text>{{formatTime(field.remainingTime)}}</text>
- </view>
- </view>
- <view class="blank" v-if="index == 2 || index == 9 || index == 12"></view>
- </block>
- </view>
- <view class="close" @click="onClose">
- <image src="../../static/island/UI/btn_close.png" class="close"></image>
- </view>
- <view class="huatian-tips" @click="showHuaTianTips">
- <image src="../../static/island/huatian/tips_icon.png" class="tips-icon"></image>
- </view>
- <view class="btn_list">
- <view class="item item1" @click="showUnlockConfirm">
- 解锁荒地
- </view>
- <view class="item item2" @click="removeWeed">
- 除草
- </view>
- <view class="item item3" @click="showFlowerSelect">
- 播种
- </view>
- <view class="item item4" @click="enterHarvestMode">
- 收获
- </view>
- </view>
- <view class="select_zz" v-if="showFlowerSelection">
- <view class="item" v-for="(flower, idx) in flowers" :key="idx" @click="selectFlower(idx)"
- v-if="flower.count > 0">
- <view class="img">
- <image :src="`../../static/island/huatian/flower${idx+1}.png`"></image>
- </view>
- <view class="foot">
- {{flower.count}}
- </view>
- </view>
- <view class="jiantou">
- <image class="jiantou" src="../../static/island/huatian/jiantou.png"></image>
- </view>
- </view>
- </view>
- <!-- 解锁确认对话框 -->
- <view class="confirm-dialog" v-if="showUnlockDialog">
- <view class="dialog-content">
- <view class="dialog-title">解锁荒地</view>
- <view class="dialog-text">是否支付{{nextLandPrice}}铃钱解锁荒地?</view>
- <view class="dialog-buttons">
- <view class="btn-cancel" @click="cancelUnlock">取消</view>
- <view class="btn-confirm" @click="confirmUnlock">确认</view>
- </view>
- </view>
- </view>
- <view class="guild" v-if="showGuide">
- <view class="content">
- "欢迎来到花田!在这里,你将成为农场的主人,体验种植和经营农场的乐趣。首先,让我们来开垦一块土地吧。只需支付铃钱即可解锁!"
- </view>
- <view class="title">
- 罗奇
- </view>
- <view class="npc">
- <image src="../../static/island/npc.png"></image>
- </view>
- </view>
- <view class="huatian-tips-dialog" v-if="showTipsDialog">
- <view class="tips-content">
- <view class="tips-title">花田心得</view>
- <view class="tips-close" @click="closeTipsDialog">
- <image src="../../static/island/UI/btn_close.png"></image>
- </view>
- <swiper class="tips-swiper" :current="currentPage" @change="handlePageChange">
- <swiper-item>
- <view class="tips-items">
- <view class="tips-item">
- <image src="../../static/island/huatian/tips1.png" class="tips-img"></image>
- <view class="tips-text">点击【解锁荒地】,中高亮闪烁的荒地,消耗铃钱解锁新的种植地块。</view>
- </view>
- <view class="tips-item">
- <image src="../../static/island/huatian/tips2.png" class="tips-img"></image>
- <view class="tips-text">点击【除草】,选中高亮闪烁的地块进行消除杂草,放能进行种植。</view>
- </view>
- </view>
- </swiper-item>
- <swiper-item>
- <view class="tips-items">
- <view class="tips-item">
- <image src="../../static/island/huatian/tips3.png" class="tips-img"></image>
- <view class="tips-text">点击【播种】,选心仪的花种播撒至高亮区域,查看成熟期或消耗 M 币加速生长。</view>
- </view>
- <view class="tips-item">
- <image src="../../static/island/huatian/tips4.png" class="tips-img"></image>
- <view class="tips-text">当花朵绽放成熟期已至,点击【收获】选中,进行采摘吧。</view>
- </view>
- </view>
- </swiper-item>
- <swiper-item>
- <view class="tips-items">
- <view class="tips-item">
- <image src="../../static/island/huatian/tips5.png" class="tips-img"></image>
- <view class="tips-text">收获的花朵将放在你的背包中,你可以对花朵进行出售,来赚取铃钱哟。</view>
- </view>
- <view class="tips-item">
- <image src="../../static/island/huatian/tips6.png" class="tips-img"></image>
- <view class="tips-text">花朵种子可以在花田旁边的【花店】中购买哟。</view>
- </view>
- </view>
- </swiper-item>
- </swiper>
- <view class="tips-dots">
- <view class="dot" v-for="index in 3" :key="index" :class="{'active': currentPage === index-1}"></view>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: 'TaskDialog',
- components: {
- },
- props: {
- visible: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- show: false,
- dataLoaded: false,
- // 用户ID
- ssoId: 0,
- // 是否第一次打开花田
- isFirstOpenHuaTian: true,
- // 田地数据
- fields: Array(20).fill().map(() => ({
- id: 0, // 后端数据库ID
- tid: 0, // 对应的土地表ID
- isUnlocked: false,
- hasWeed: false,
- flower: false,
- flowerType: null,
- isGrowing: false,
- growthCompleted: false,
- canHarvest: false,
- remainingTime: 0,
- timer: null,
- seedStage: false,
- plantTime: '',
- state: 0 // 与后端对应的状态
- })),
- // 花的数据
- flowers: [],
- // 种子数据
- seeds: [],
- // 用户铃钱
- money: 0,
- // 下一块要解锁的土地价格
- nextLandPrice: 100,
- // 选择状态
- selectedFlowerIndex: -1,
- showFlowerSelection: false,
- showGuide: false,
- plantMode: false,
- showUnlockDialog: false, // 解锁确认对话框显示状态
- // 当前选择的土地ID
- selectedLandId: 0,
- // 成长检查定时器
- growthCheckTimer: null,
- weedMode: false, // 添加除草模式状态
- weedyFields: [], // 添加可除草的地块列表
- harvestMode: false, // 添加收获模式状态
- harvestableFields: [], // 添加可收获的地块列表
- taskDayActive: false,
- // 主岛引用
- mainLand: null,
- showTipsDialog: false, // 添加花田心得弹框显示状态
- currentPage: 0, // 当前页码
- }
- },
- watch: {
- visible: {
- immediate: true,
- handler(val) {
- if (val) {
- this.fetchData();
- } else {
- this.show = false;
- this.dataLoaded = false;
- }
- }
- }
- },
- onLoad() {
- // 初始化获取数据
- this.fetchData();
- },
- onShow() {
- // 重新获取数据
- this.fetchData();
- },
- beforeDestroy() {
- // 清除所有定时器
- this.fields.forEach(field => {
- if (field.timer) {
- clearInterval(field.timer);
- }
- });
- // 清除生长检查定时器
- if (this.growthCheckTimer) {
- clearInterval(this.growthCheckTimer);
- }
- },
- methods: {
- // 关闭界面
- onClose() {
- this.show = false;
- this.$emit('update:visible', false);
- this.$emit('close');
- },
- // 初始化获取数据
- async fetchData() {
- console.log("uuid", getApp().globalData.uuid)
- try {
- // 检查是否是第一次打开花田
- const isFirstOpen = uni.getStorageSync('isFirstOpenHuaTian');
- if (isFirstOpen === '') {
- // 第一次打开,设置标志并显示提示对话框
- uni.setStorageSync('isFirstOpenHuaTian', 'false');
- this.isFirstOpenHuaTian = true;
- } else {
- this.isFirstOpenHuaTian = false;
- }
-
- uni.request({
- url: this.$apiHost + '/Game/huatian/index',
- method: 'POST',
- data: {
- uuid: getApp().globalData.uuid,
- },
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'sign': getApp().globalData.headerSign,
- },
- success: (res) => {
- if (res.data && res.data.code === 0) {
- // 更新用户铃钱
- this.money = res.data.money;
- // 更新种子数据
- this.seeds = res.data.seeds || [];
- // 更新背包数据
- const bagItems = res.data.bag_list || [];
- // 更新花的数据(背包中type=1的是种子)
- this.flowers = [];
- for (let i = 1; i <= 7; i++) {
- // 找到对应的种子数量
- const seedBag = bagItems.find(item => item.type === 1 && item.tid === i);
- this.flowers.push({
- id: i,
- name: this.seeds[i - 1]?.name || `花${i}`,
- count: seedBag ? seedBag.num : 0
- });
- }
- // 更新土地数据
- const tudiList = res.data.tudi_list || [];
- // 重置田地数据
- this.fields = Array(20).fill().map((_, index) => ({
- id: 0,
- tid: index + 1,
- isUnlocked: false,
- hasWeed: false,
- flower: false,
- flowerType: null,
- isGrowing: false,
- growthCompleted: false,
- canHarvest: false,
- remainingTime: 0,
- timer: null,
- seedStage: false,
- plantTime: '',
- state: 0
- }));
- // 更新已解锁的田地状态
- tudiList.forEach(land => {
- const fieldIndex = land.tid - 1;
- if (fieldIndex >= 0 && fieldIndex < this.fields.length) {
- const field = this.fields[fieldIndex];
- field.id = land.id;
- field.tid = land.tid;
- field.isUnlocked = true;
- field.state = land.state;
- field.plantTime = land.plant_time;
- // 根据状态设置不同的显示
- if (land.state === -1) {
- // 杂草地
- field.hasWeed = true;
- } else if (land.state === 0) {
- // 待播种的空地
- field.hasWeed = false;
- } else if (land.state >= 1 && land.state <= 7) {
- // 种植了花
- field.hasWeed = false;
- field.flowerType = land.zzid;
- // 计算是否已经成熟
- if (land.plant_time) {
- const plantTime = new Date(land.plant_time);
- const now = new Date();
- const growthHours = (now - plantTime) / (1000 * 60 * 60);
- // 获取对应种子的生长周期
- const seed = this.seeds.find(s => s.id === land.state);
- const limitHours = seed ? seed.limit_time : 0;
- if (growthHours >= limitHours) {
- // 已成熟
- field.flower = true;
- field.growthCompleted = true;
- field.canHarvest = true;
- } else {
- // 未成熟,显示生长中
- field.isGrowing = true;
- field.seedStage = true;
- field.remainingTime = Math.ceil((limitHours - growthHours) * 60 * 60);
- this.startGrowthTimer(fieldIndex);
- }
- }
- } else if (land.state === 99) {
- // 成熟待收割
- field.hasWeed = false;
- field.flower = true;
- field.flowerType = land.flowerType || land.zzid;
- field.growthCompleted = true;
- field.canHarvest = true;
- }
- }
- });
- // 找到下一块可解锁土地的价格
- for (let i = 0; i < this.fields.length; i++) {
- if (!this.fields[i].isUnlocked) {
- console.log("this.landpric", i + 1)
- // 从服务端获取土地价格
- uni.request({
- url: this.$apiHost + '/Game/huatian/get_land_price',
- method: 'POST',
- data: {
- uuid: getApp().globalData.uuid,
- land_id: i + 1
- },
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'sign': getApp().globalData.headerSign,
- uuid: getApp().globalData.uuid,
- },
- success: (res) => {
- console.log('res.data', res.data);
- if (res.data && res.data.code === 0) {
- this.nextLandPrice = res.data.data.price;
- } else {
- // 如果获取失败,使用默认值
- this.nextLandPrice = 100;
- }
- // 数据加载完成,显示界面
- this.dataLoaded = true;
- this.show = true;
-
- // 如果是第一次打开,显示提示对话框
- if (this.isFirstOpenHuaTian) {
- this.showTipsDialog = true;
- }
- },
- fail: (err) => {
- console.error('获取土地价格异常', err);
- // 如果获取失败,使用默认值
- this.nextLandPrice = 100;
- // 数据加载完成,显示界面
- this.dataLoaded = true;
- this.show = true;
- }
- });
- break;
- }
- }
- // 启动定时检查植物生长状态
- this.startGrowthCheck();
- } else {
- uni.showToast({
- title: res.data?.msg || '获取数据失败',
- icon: 'none'
- });
- this.dataLoaded = true;
- this.show = true;
- }
- },
- fail: (err) => {
- console.error('获取数据异常', err);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- this.dataLoaded = true;
- this.show = true;
- }
- });
- } catch (error) {
- console.error('获取数据异常', error);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- this.dataLoaded = true;
- this.show = true;
- }
- },
- // 显示解锁确认对话框
- showUnlockConfirm() {
- this.harvestMode = false;
- this.weedMode = false;
- this.showUnlockDialog = true;
- },
- // 取消解锁
- cancelUnlock() {
- this.showUnlockDialog = false;
- },
- // 确认解锁
- async confirmUnlock() {
- try {
- uni.request({
- url: this.$apiHost + '/Game/huatian/unlock_land',
- method: 'POST',
- data: {
- uuid: getApp().globalData.uuid,
- },
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'sign': getApp().globalData.headerSign,
- uuid: getApp().globalData.uuid,
- },
- success: (res) => {
- if (res.data && res.data.code === 0) {
- uni.showToast({
- title: '解锁成功',
- icon: 'success'
- });
- // 更新用户铃钱
- this.money = res.data.data.money;
- // 重新获取数据
- this.fetchData();
- } else {
- uni.showToast({
- title: res.data?.msg || '解锁失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- console.error('解锁土地异常', err);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- }
- });
- } catch (error) {
- console.error('解锁土地异常', error);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- }
- this.showUnlockDialog = false;
- },
- // 除草
- async removeWeed() {
- this.harvestMode = false;
- if (this.weedMode) {
- // 如果已经在除草模式,则退出
- this.weedMode = false;
- this.weedyFields = [];
- return;
- }
- // 找到所有有杂草的已解锁土地
- this.weedyFields = this.fields.filter(field => field.isUnlocked && field.hasWeed);
- if (this.weedyFields.length === 0) {
- uni.showToast({
- title: '没有需要除草的土地',
- icon: 'none'
- });
- return;
- }
- // 进入除草模式
- this.weedMode = true;
- uni.showToast({
- title: '请点击需要除草的土地',
- icon: 'none'
- });
- },
- // 进入收获模式
- enterHarvestMode() {
- this.weedMode = false;
- if (this.harvestMode) {
- // 如果已经在收获模式,则退出
- this.harvestMode = false;
- this.harvestableFields = [];
- return;
- }
- // 找到所有可收获的土地
- this.harvestableFields = this.fields.filter(field => field.canHarvest);
- if (this.harvestableFields.length === 0) {
- uni.showToast({
- title: '没有可收获的花',
- icon: 'none'
- });
- return;
- }
- // 进入收获模式
- this.harvestMode = true;
- uni.showToast({
- title: '请点击需要收获的土地',
- icon: 'none'
- });
- },
- // 显示花的选择
- showFlowerSelect() {
- this.harvestMode = false;
- this.weedMode = false;
- if (this.showFlowerSelection) {
- this.showFlowerSelection = false;
- return;
- }
- // 检查是否有可播种的土地
- const canPlantFields = this.fields.filter(field => field.isUnlocked && !field.hasWeed && !field.flower && !
- field.isGrowing);
- if (canPlantFields.length === 0) {
- uni.showToast({
- title: '没有可播种的土地',
- icon: 'none'
- });
- return;
- }
- this.showFlowerSelection = true;
- this.plantMode = false;
- this.selectedFlowerIndex = -1;
- },
- // 选择花
- selectFlower(idx) {
- this.selectedFlowerIndex = idx;
- this.showFlowerSelection = false;
- this.plantMode = true;
- },
- // 点击田地
- handleFieldClick(index) {
- if (this.weedMode) {
- // 在除草模式下,只处理有草的地块
- if (this.fields[index].hasWeed) {
- this.removeWeedFromField(index);
- }
- return;
- }
- if (this.harvestMode) {
- // 在收获模式下,只处理可收获的地块
- if (this.fields[index].canHarvest) {
- this.harvestField(this.fields[index].id);
- }
- return;
- }
- if (this.plantMode) {
- if (this.fields[index].canHarvest) {
- }
- this.handleFieldClickBZ(index);
- return;
- }
- // 原有的种植逻辑
- if (!this.fields[index].isUnlocked) {
- uni.showToast({
- title: '该地块未解锁',
- icon: 'none'
- });
- return;
- }
- if (this.fields[index].flower) {
- if (this.fields[index].canHarvest) {
- this.harvestField(this.fields[index].id);
- } else {
- uni.showToast({
- title: '花朵正在生长中',
- icon: 'none'
- });
- }
- return;
- }
- // 打开种子选择弹窗
- this.currentFieldIndex = index;
- this.showSeedSelector = true;
- },
- // 点击田地
- async handleFieldClickBZ(index) {
- const field = this.fields[index];
- // 已解锁土地且处于播种模式
- if (this.plantMode && field.isUnlocked && !field.hasWeed && !field.flower && !field.isGrowing) {
- // 设置当前选中的土地
- this.selectedLandId = field.id;
- // 播种
- try {
- uni.request({
- url: this.$apiHost + '/Game/huatian/plant_seed',
- method: 'POST',
- data: {
- uuid: getApp().globalData.uuid,
- land_id: field.id,
- seed_id: this.selectedFlowerIndex + 1
- },
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'sign': getApp().globalData.headerSign,
- uuid: getApp().globalData.uuid,
- },
- success: (res) => {
- if (res.data && res.data.code === 0) {
- // 更新土地状态
- field.isGrowing = true;
- field.seedStage = true;
- field.flowerType = this.selectedFlowerIndex + 1;
- field.state = this.selectedFlowerIndex + 1;
- field.plantTime = res.data.data.plantTime;
- // 计算剩余生长时间
- const limitTimeHours = res.data.data.limitTime;
- field.remainingTime = limitTimeHours * 60 * 60;
- // 开始计时
- this.startGrowthTimer(index);
- // 更新种子数量
- const flowerData = this.flowers[this.selectedFlowerIndex];
- if (flowerData && flowerData.count > 0) {
- flowerData.count--;
- }
- // 退出播种模式
- this.plantMode = false;
- uni.showToast({
- title: '播种成功',
- icon: 'success'
- });
- } else {
- uni.showToast({
- title: res.data?.msg || '播种失败',
- icon: 'none'
- });
- this.plantMode = false;
- }
- },
- fail: (err) => {
- console.error('播种异常', err);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- this.plantMode = false;
- }
- });
- } catch (error) {
- console.error('播种异常', error);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- this.plantMode = false;
- }
- } else if (field.canHarvest) {
- // 收获花朵
- this.harvestField(field.id);
- }
- },
- // 从特定地块移除杂草
- removeWeedFromField(index) {
- // 调用除草API
- uni.request({
- url: this.$apiHost + '/Game/huatian/remove_weed',
- method: 'POST',
- data: {
- uuid: getApp().globalData.uuid,
- land_id: this.fields[index].id
- },
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'sign': getApp().globalData.headerSign,
- uuid: getApp().globalData.uuid,
- },
- success: (res) => {
- if (res.data && res.data.code === 0) {
- // 更新地块状态
- this.fields[index].hasWeed = false;
- this.fields[index].state = 0;
- // 从可除草地块列表中移除
- this.weedyFields = this.weedyFields.filter(f => f.id !== this.fields[index].id);
- uni.showToast({
- title: '除草成功',
- icon: 'success'
- });
- // 如果所有草都已清除,退出除草模式
- if (this.weedyFields.length === 0) {
- this.weedMode = false;
- uni.showToast({
- title: '所有杂草已清除',
- icon: 'success'
- });
- }
- } else {
- uni.showToast({
- title: res.data?.msg || '除草失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- console.error('除草异常', err);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- }
- });
- },
- // 开始生长计时器
- startGrowthTimer(fieldIndex) {
- const field = this.fields[fieldIndex];
- // 清除可能存在的旧定时器
- if (field.timer) {
- clearInterval(field.timer);
- }
- field.timer = setInterval(() => {
- field.remainingTime--;
- if (field.remainingTime <= 0) {
- // 生长完成
- clearInterval(field.timer);
- field.isGrowing = false;
- field.seedStage = false;
- field.growthCompleted = true;
- field.flower = true;
- field.canHarvest = true;
- }
- }, 1000);
- },
- // 收获特定地块
- async harvestField(landId) {
- console.log("land", landId);
- try {
- uni.request({
- url: this.$apiHost + '/Game/huatian/harvest',
- method: 'POST',
- data: {
- uuid: getApp().globalData.uuid,
- land_id: landId
- },
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'sign': getApp().globalData.headerSign,
- uuid: getApp().globalData.uuid,
- },
- success: (res) => {
- if (res.data && res.data.code === 0) {
- // 找到对应的田地
- const fieldIndex = this.fields.findIndex(f => f.id === landId);
- if (fieldIndex >= 0) {
- const field = this.fields[fieldIndex];
- // 重置状态
- field.flower = false;
- field.flowerType = null;
- field.growthCompleted = false;
- field.canHarvest = false;
- field.hasWeed = true;
- field.state = -1;
- // 如果有定时器,清除它
- if (field.timer) {
- clearInterval(field.timer);
- field.timer = null;
- }
- }
- // 重新获取数据以更新背包
- this.fetchData();
- uni.showToast({
- title: `收获了${res.data.data.harvestNum}朵花`,
- icon: 'success'
- });
- // 从可收获地块列表中移除
- this.harvestableFields = this.harvestableFields.filter(f => f.id !==
- landId);
- // 如果所有花都已收获,退出收获模式
- if (this.harvestableFields.length === 0) {
- this.harvestMode = false;
- uni.showToast({
- title: '所有花已收获',
- icon: 'success'
- });
- }
- } else {
- uni.showToast({
- title: res.data?.msg || '收获失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- console.error('收获异常', err);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- }
- });
- } catch (error) {
- console.error('收获异常', error);
- uni.showToast({
- title: '网络异常,请重试',
- icon: 'none'
- });
- }
- },
- // 收获所有可收获的花
- async harvest() {
- const harvestFields = this.fields.filter(field => field.canHarvest);
- if (harvestFields.length === 0) {
- uni.showToast({
- title: '没有可收获的花',
- icon: 'none'
- });
- return;
- }
- try {
- for (const field of harvestFields) {
- await this.harvestField(field.id);
- }
- } catch (error) {
- console.error('批量收获异常', error);
- }
- },
- // 格式化时间显示
- formatTime(seconds) {
- const hours = Math.floor(seconds / 3600);
- const minutes = Math.floor((seconds % 3600) / 60);
- const secs = seconds % 60;
-
- // 获取当前时间,用于控制冒号闪烁
- const now = new Date();
- const blinkState = now.getSeconds() % 2 === 0;
-
- // 格式化为 "HH:MM" 格式
- return `${hours.toString().padStart(2, '0')}${blinkState ? ':' : ' '}${minutes.toString().padStart(2, '0')}`;
- },
- // 开始定时检查植物生长状态
- startGrowthCheck() {
- // 清除可能存在的旧定时器
- if (this.growthCheckTimer) {
- clearInterval(this.growthCheckTimer);
- }
- // 每分钟检查一次植物生长状态
- this.growthCheckTimer = setInterval(async () => {
- try {
- uni.request({
- url: this.$apiHost + '/Game/huatian/check_growth',
- method: 'POST',
- data: {
- uuid: getApp().globalData.uuid,
- },
- header: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'sign': getApp().globalData.headerSign,
- uuid: getApp().globalData.uuid,
- },
- success: (res) => {
- if (res.data && res.data.code === 0) {
- const growthData = res.data.data || [];
- // 更新每块土地的状态
- growthData.forEach(item => {
- if (item.isReady) {
- // 找到对应的田地
- const field = this.fields.find(f => f.id ===
- item.landId);
- if (field) {
- // 更新为可收获状态
- field.isGrowing = false;
- field.seedStage = false;
- field.growthCompleted = true;
- field.flower = true;
- field.canHarvest = true;
- field.state = 99;
- // 清除定时器
- if (field.timer) {
- clearInterval(field.timer);
- field.timer = null;
- }
- }
- }
- });
- }
- },
- fail: (err) => {
- console.error('检查生长状态异常', err);
- }
- });
- } catch (error) {
- console.error('检查生长状态异常', error);
- }
- }, 60000); // 60秒检查一次
- },
- handleTaskDayClick() {
- this.taskDayActive = true;
- setTimeout(() => {
- this.taskDayActive = false;
- }, 200);
-
- // 如果mainLand存在,调用其onTaskClick方法
- if (this.mainLand) {
- // 关闭当前花田界面
- // this.onClose();
- // 打开任务对话框
- this.mainLand.onTaskClick();
- }
- },
- // 设置mainLand引用
- setMainLand(mainLand) {
- this.mainLand = mainLand;
- },
- // 显示花田心得弹框
- showHuaTianTips() {
- this.showTipsDialog = true;
- },
-
- // 关闭花田心得弹框
- closeTipsDialog() {
- this.showTipsDialog = false;
- },
- handlePageChange(e) {
- this.currentPage = e.detail.current;
- },
- },
- }
- </script>
- <style lang="scss">
- @import './HuaTian.scss';
- @keyframes weedBlink {
- 0% {
- opacity: 1;
- transform: scale(1);
- }
- 50% {
- opacity: 0.5;
- transform: scale(1.1);
- }
- 100% {
- opacity: 1;
- transform: scale(1);
- }
- }
- .weed-blink {
- animation: weedBlink 1s infinite;
- z-index: 10;
- }
- @keyframes harvestBlink {
- 0% {
- opacity: 1;
- transform: scale(1);
- }
- 50% {
- opacity: 0.5;
- transform: scale(1.1);
- }
- 100% {
- opacity: 1;
- transform: scale(1);
- }
- }
- .harvest-blink {
- animation: harvestBlink 1s infinite;
- z-index: 10;
- }
- .task-day-active {
- transform: scale(0.95);
- opacity: 0.8;
- transition: all 0.2s ease;
- }
- </style>
|