HuaTian.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. <template>
  2. <!--
  3. 设定:
  4. suo:锁;
  5. flower:花,共7种,图片分别为flower1.png flower2.png flower7.png;
  6. zacao:杂草;
  7. tian:田地,如果是待解锁的田地,图片用tian_suo.png,如果是已解锁就用tian.png;
  8. souhuo:收获标识;
  9. select_zz:7种花的view,默认应该隐藏,点击播种时显示,播种结束后隐藏
  10. 4个按钮:
  11. 解锁荒地:点击时,将第一块待解锁的田地转换为已解锁地
  12. 除草:点击时,让杂草隐藏
  13. 播种:点击时,需要先将7种花显示出来,选择其中一种,然后再点击除去杂草的地,然后该块地上显示种子,然后10分钟倒计时,然后显示对应的花,以及收获标识
  14. 收获:点击后,有收获标识的土地上的收获标识图片隐藏,花隐藏,出现杂草
  15. 操作流程:
  16. 默认显示待解锁的田地(其他几个图标都隐藏),点击"解锁荒地",点击"除草",点击"播种",到时间后点击"收获" -->
  17. <view class="page">
  18. <view class="task-board">
  19. <view class="task_day">
  20. <image src="../../static/island/huatian/btn_task_day.png" class="task"></image>
  21. </view>
  22. <view class="board-title">
  23. </view>
  24. <view class="bodyContent">
  25. <block v-for="(field, index) in fields" :key="index">
  26. <view class="blank" v-if="index == 0 || index == 7 || index == 10"></view>
  27. <view class="item" :class="{
  28. 'locked': !field.isUnlocked,
  29. 'weedy': field.hasWeed,
  30. 'weed-blink': weedMode && field.hasWeed,
  31. 'planted': field.flower,
  32. 'growing': field.isGrowing,
  33. 'ready': field.canHarvest,
  34. 'harvest-blink': harvestMode && field.canHarvest
  35. }" @click="handleFieldClick(index)">
  36. <image v-if="!field.isUnlocked" src="../../static/island/huatian/suo.png" class="suo"></image>
  37. <image v-if="field.flower && field.growthCompleted"
  38. :src="`../../static/island/huatian/flower${field.flowerType}.png`" class="flower"></image>
  39. <image v-if="field.seedStage && field.isGrowing" src="../../static/island/huatian/zongzi.png"
  40. class="flower"></image>
  41. <image v-if="field.hasWeed && field.isUnlocked" src="../../static/island/huatian/zacao.png"
  42. class="zacao"></image>
  43. <image
  44. :src="field.isUnlocked ? '../../static/island/huatian/tian.png' : '../../static/island/huatian/tian_suo.png'"
  45. class="tian"></image>
  46. <image v-if="field.canHarvest" src="../../static/island/huatian/souhuo.png" class="souhuo">
  47. </image>
  48. <view class="timer" v-if="field.isGrowing">
  49. <image src="../../static/island/huatian/timer.png" class="icon"></image>
  50. <text>{{formatTime(field.remainingTime)}}</text>
  51. </view>
  52. </view>
  53. <view class="blank" v-if="index == 2 || index == 9 || index == 12"></view>
  54. </block>
  55. </view>
  56. <view class="close" @click="onClose">
  57. <image src="../../static/island/UI/btn_close.png" class="close"></image>
  58. </view>
  59. <view class="btn_list">
  60. <view class="item item1" @click="showUnlockConfirm">
  61. 解锁荒地
  62. </view>
  63. <view class="item item2" @click="removeWeed">
  64. 除草
  65. </view>
  66. <view class="item item3" @click="showFlowerSelect">
  67. 播种
  68. </view>
  69. <view class="item item4" @click="enterHarvestMode">
  70. 收获
  71. </view>
  72. </view>
  73. <view class="select_zz" v-if="showFlowerSelection">
  74. <view class="item" v-for="(flower, idx) in flowers" :key="idx" @click="selectFlower(idx)"
  75. v-if="flower.count > 0">
  76. <view class="img">
  77. <image :src="`../../static/island/huatian/flower${idx+1}.png`"></image>
  78. </view>
  79. <view class="foot">
  80. {{flower.count}}
  81. </view>
  82. </view>
  83. <view class="jiantou">
  84. <image class="jiantou" src="../../static/island/huatian/jiantou.png"></image>
  85. </view>
  86. </view>
  87. </view>
  88. <!-- 解锁确认对话框 -->
  89. <view class="confirm-dialog" v-if="showUnlockDialog">
  90. <view class="dialog-content">
  91. <view class="dialog-title">解锁荒地</view>
  92. <view class="dialog-text">是否支付{{nextLandPrice}}铃钱解锁荒地?</view>
  93. <view class="dialog-buttons">
  94. <view class="btn-cancel" @click="cancelUnlock">取消</view>
  95. <view class="btn-confirm" @click="confirmUnlock">确认</view>
  96. </view>
  97. </view>
  98. </view>
  99. <view class="guild" v-if="showGuide">
  100. <view class="content">
  101. "欢迎来到花田!在这里,你将成为农场的主人,体验种植和经营农场的乐趣。首先,让我们来开垦一块土地吧。只需支付铃钱即可解锁!"
  102. </view>
  103. <view class="title">
  104. 罗奇
  105. </view>
  106. <view class="npc">
  107. <image src="../../static/island/npc.png"></image>
  108. </view>
  109. </view>
  110. </view>
  111. </template>
  112. <script>
  113. export default {
  114. name: 'TaskDialog',
  115. components: {
  116. },
  117. props: {
  118. visible: {
  119. type: Boolean,
  120. default: false
  121. }
  122. },
  123. data() {
  124. return {
  125. // 用户ID
  126. ssoId: 0,
  127. // 田地数据
  128. fields: Array(20).fill().map(() => ({
  129. id: 0, // 后端数据库ID
  130. tid: 0, // 对应的土地表ID
  131. isUnlocked: false,
  132. hasWeed: false,
  133. flower: false,
  134. flowerType: null,
  135. isGrowing: false,
  136. growthCompleted: false,
  137. canHarvest: false,
  138. remainingTime: 0,
  139. timer: null,
  140. seedStage: false,
  141. plantTime: '',
  142. state: 0 // 与后端对应的状态
  143. })),
  144. // 花的数据
  145. flowers: [],
  146. // 种子数据
  147. seeds: [],
  148. // 用户铃钱
  149. money: 0,
  150. // 下一块要解锁的土地价格
  151. nextLandPrice: 100,
  152. // 选择状态
  153. selectedFlowerIndex: -1,
  154. showFlowerSelection: false,
  155. showGuide: false,
  156. plantMode: false,
  157. showUnlockDialog: false, // 解锁确认对话框显示状态
  158. // 当前选择的土地ID
  159. selectedLandId: 0,
  160. // 成长检查定时器
  161. growthCheckTimer: null,
  162. weedMode: false, // 添加除草模式状态
  163. weedyFields: [], // 添加可除草的地块列表
  164. harvestMode: false, // 添加收获模式状态
  165. harvestableFields: [], // 添加可收获的地块列表
  166. }
  167. },
  168. onLoad() {
  169. // 初始化获取数据
  170. this.fetchData();
  171. },
  172. onShow() {
  173. // 重新获取数据
  174. this.fetchData();
  175. },
  176. beforeDestroy() {
  177. // 清除所有定时器
  178. this.fields.forEach(field => {
  179. if (field.timer) {
  180. clearInterval(field.timer);
  181. }
  182. });
  183. // 清除生长检查定时器
  184. if (this.growthCheckTimer) {
  185. clearInterval(this.growthCheckTimer);
  186. }
  187. },
  188. methods: {
  189. // 关闭界面
  190. onClose() {
  191. this.$emit('close');
  192. },
  193. // 初始化获取数据
  194. async fetchData() {
  195. console.log("uuid", getApp().globalData.uuid)
  196. try {
  197. uni.request({
  198. url: this.$apiHost + '/Game/huatian/index',
  199. method: 'POST',
  200. data: {
  201. uuid: getApp().globalData.uuid,
  202. },
  203. header: {
  204. 'Content-Type': 'application/x-www-form-urlencoded',
  205. 'sign': getApp().globalData.headerSign,
  206. },
  207. success: (res) => {
  208. if (res.data && res.data.code === 0) {
  209. // 更新用户铃钱
  210. this.money = res.data.money;
  211. // 更新种子数据
  212. this.seeds = res.data.seeds || [];
  213. // 更新背包数据
  214. const bagItems = res.data.bag_list || [];
  215. // 更新花的数据(背包中type=1的是种子)
  216. this.flowers = [];
  217. for (let i = 1; i <= 7; i++) {
  218. // 找到对应的种子数量
  219. const seedBag = bagItems.find(item => item.type === 1 && item.tid ===
  220. i);
  221. this.flowers.push({
  222. id: i,
  223. name: this.seeds[i - 1]?.name || `花${i}`,
  224. count: seedBag ? seedBag.num : 0
  225. });
  226. }
  227. // 更新土地数据
  228. const tudiList = res.data.tudi_list || [];
  229. // 重置田地数据
  230. this.fields = Array(20).fill().map((_, index) => ({
  231. id: 0,
  232. tid: index + 1,
  233. isUnlocked: false,
  234. hasWeed: false,
  235. flower: false,
  236. flowerType: null,
  237. isGrowing: false,
  238. growthCompleted: false,
  239. canHarvest: false,
  240. remainingTime: 0,
  241. timer: null,
  242. seedStage: false,
  243. plantTime: '',
  244. state: 0
  245. }));
  246. // 更新已解锁的田地状态
  247. tudiList.forEach(land => {
  248. const fieldIndex = land.tid - 1;
  249. if (fieldIndex >= 0 && fieldIndex < this.fields.length) {
  250. const field = this.fields[fieldIndex];
  251. field.id = land.id;
  252. field.tid = land.tid;
  253. field.isUnlocked = true;
  254. field.state = land.state;
  255. field.plantTime = land.plant_time;
  256. // 根据状态设置不同的显示
  257. if (land.state === -1) {
  258. // 杂草地
  259. field.hasWeed = true;
  260. } else if (land.state === 0) {
  261. // 待播种的空地
  262. field.hasWeed = false;
  263. } else if (land.state >= 1 && land.state <= 7) {
  264. // 种植了花
  265. field.hasWeed = false;
  266. field.flowerType = land.zzid;
  267. // 计算是否已经成熟
  268. if (land.plant_time) {
  269. const plantTime = new Date(land.plant_time);
  270. const now = new Date();
  271. const growthHours = (now - plantTime) / (1000 *
  272. 60 * 60);
  273. // 获取对应种子的生长周期
  274. const seed = this.seeds.find(s => s.id === land
  275. .state);
  276. const limitHours = seed ? seed.limit_time : 0;
  277. if (growthHours >= limitHours) {
  278. // 已成熟
  279. field.flower = true;
  280. field.growthCompleted = true;
  281. field.canHarvest = true;
  282. } else {
  283. // 未成熟,显示生长中
  284. field.isGrowing = true;
  285. field.seedStage = true;
  286. field.remainingTime = Math.ceil((limitHours -
  287. growthHours) * 60 * 60);
  288. this.startGrowthTimer(fieldIndex);
  289. }
  290. }
  291. } else if (land.state === 99) {
  292. // 成熟待收割
  293. field.hasWeed = false;
  294. field.flower = true;
  295. field.flowerType = land.flowerType || land
  296. .zzid;
  297. field.growthCompleted = true;
  298. field.canHarvest = true;
  299. }
  300. }
  301. });
  302. // 找到下一块可解锁土地的价格
  303. for (let i = 0; i < this.fields.length; i++) {
  304. if (!this.fields[i].isUnlocked) {
  305. console.log("this.landpric", i + 1)
  306. // 从服务端获取土地价格
  307. uni.request({
  308. url: this.$apiHost + '/Game/huatian/get_land_price',
  309. method: 'POST',
  310. data: {
  311. uuid: getApp().globalData.uuid,
  312. land_id: i + 1
  313. },
  314. header: {
  315. 'Content-Type': 'application/x-www-form-urlencoded',
  316. 'sign': getApp().globalData.headerSign,
  317. uuid: getApp().globalData.uuid,
  318. },
  319. success: (res) => {
  320. console.log('res.data', res.data);
  321. if (res.data && res.data.code === 0) {
  322. this.nextLandPrice = res.data.data.price;
  323. } else {
  324. // 如果获取失败,使用默认值
  325. this.nextLandPrice = 100;
  326. }
  327. },
  328. fail: (err) => {
  329. console.error('获取土地价格异常', err);
  330. // 如果获取失败,使用默认值
  331. this.nextLandPrice = 100;
  332. }
  333. });
  334. break;
  335. }
  336. }
  337. // 启动定时检查植物生长状态
  338. this.startGrowthCheck();
  339. } else {
  340. uni.showToast({
  341. title: res.data?.msg || '获取数据失败',
  342. icon: 'none'
  343. });
  344. }
  345. },
  346. fail: (err) => {
  347. console.error('获取数据异常', err);
  348. uni.showToast({
  349. title: '网络异常,请重试',
  350. icon: 'none'
  351. });
  352. }
  353. });
  354. } catch (error) {
  355. console.error('获取数据异常', error);
  356. uni.showToast({
  357. title: '网络异常,请重试',
  358. icon: 'none'
  359. });
  360. }
  361. },
  362. // 显示解锁确认对话框
  363. showUnlockConfirm() {
  364. this.showUnlockDialog = true;
  365. },
  366. // 取消解锁
  367. cancelUnlock() {
  368. this.showUnlockDialog = false;
  369. },
  370. // 确认解锁
  371. async confirmUnlock() {
  372. try {
  373. uni.request({
  374. url: this.$apiHost + '/Game/huatian/unlock_land',
  375. method: 'POST',
  376. data: {
  377. uuid: getApp().globalData.uuid,
  378. },
  379. header: {
  380. 'Content-Type': 'application/x-www-form-urlencoded',
  381. 'sign': getApp().globalData.headerSign,
  382. uuid: getApp().globalData.uuid,
  383. },
  384. success: (res) => {
  385. if (res.data && res.data.code === 0) {
  386. uni.showToast({
  387. title: '解锁成功',
  388. icon: 'success'
  389. });
  390. // 更新用户铃钱
  391. this.money = res.data.data.money;
  392. // 重新获取数据
  393. this.fetchData();
  394. } else {
  395. uni.showToast({
  396. title: res.data?.msg || '解锁失败',
  397. icon: 'none'
  398. });
  399. }
  400. },
  401. fail: (err) => {
  402. console.error('解锁土地异常', err);
  403. uni.showToast({
  404. title: '网络异常,请重试',
  405. icon: 'none'
  406. });
  407. }
  408. });
  409. } catch (error) {
  410. console.error('解锁土地异常', error);
  411. uni.showToast({
  412. title: '网络异常,请重试',
  413. icon: 'none'
  414. });
  415. }
  416. this.showUnlockDialog = false;
  417. },
  418. // 除草
  419. async removeWeed() {
  420. if (this.weedMode) {
  421. // 如果已经在除草模式,则退出
  422. this.weedMode = false;
  423. this.weedyFields = [];
  424. return;
  425. }
  426. // 找到所有有杂草的已解锁土地
  427. this.weedyFields = this.fields.filter(field => field.isUnlocked && field.hasWeed);
  428. if (this.weedyFields.length === 0) {
  429. uni.showToast({
  430. title: '没有需要除草的土地',
  431. icon: 'none'
  432. });
  433. return;
  434. }
  435. // 进入除草模式
  436. this.weedMode = true;
  437. uni.showToast({
  438. title: '请点击需要除草的土地',
  439. icon: 'none'
  440. });
  441. },
  442. // 进入收获模式
  443. enterHarvestMode() {
  444. if (this.harvestMode) {
  445. // 如果已经在收获模式,则退出
  446. this.harvestMode = false;
  447. this.harvestableFields = [];
  448. return;
  449. }
  450. // 找到所有可收获的土地
  451. this.harvestableFields = this.fields.filter(field => field.canHarvest);
  452. if (this.harvestableFields.length === 0) {
  453. uni.showToast({
  454. title: '没有可收获的花',
  455. icon: 'none'
  456. });
  457. return;
  458. }
  459. // 进入收获模式
  460. this.harvestMode = true;
  461. uni.showToast({
  462. title: '请点击需要收获的土地',
  463. icon: 'none'
  464. });
  465. },
  466. // 显示花的选择
  467. showFlowerSelect() {
  468. if (this.showFlowerSelection) {
  469. this.showFlowerSelection = false;
  470. return;
  471. }
  472. // 检查是否有可播种的土地
  473. const canPlantFields = this.fields.filter(field => field.isUnlocked && !field.hasWeed && !field.flower && !
  474. field.isGrowing);
  475. if (canPlantFields.length === 0) {
  476. uni.showToast({
  477. title: '没有可播种的土地',
  478. icon: 'none'
  479. });
  480. return;
  481. }
  482. this.showFlowerSelection = true;
  483. this.plantMode = false;
  484. this.selectedFlowerIndex = -1;
  485. },
  486. // 选择花
  487. selectFlower(idx) {
  488. this.selectedFlowerIndex = idx;
  489. this.showFlowerSelection = false;
  490. this.plantMode = true;
  491. },
  492. // 点击田地
  493. handleFieldClick(index) {
  494. if (this.weedMode) {
  495. // 在除草模式下,只处理有草的地块
  496. if (this.fields[index].hasWeed) {
  497. this.removeWeedFromField(index);
  498. }
  499. return;
  500. }
  501. if (this.harvestMode) {
  502. // 在收获模式下,只处理可收获的地块
  503. if (this.fields[index].canHarvest) {
  504. this.harvestField(this.fields[index].id);
  505. }
  506. return;
  507. }
  508. if (this.plantMode) {
  509. if (this.fields[index].canHarvest) {
  510. }
  511. this.handleFieldClickBZ(index);
  512. return;
  513. }
  514. // 原有的种植逻辑
  515. if (!this.fields[index].isUnlocked) {
  516. uni.showToast({
  517. title: '该地块未解锁',
  518. icon: 'none'
  519. });
  520. return;
  521. }
  522. if (this.fields[index].flower) {
  523. if (this.fields[index].canHarvest) {
  524. this.harvestField(this.fields[index].id);
  525. } else {
  526. uni.showToast({
  527. title: '花朵正在生长中',
  528. icon: 'none'
  529. });
  530. }
  531. return;
  532. }
  533. // 打开种子选择弹窗
  534. this.currentFieldIndex = index;
  535. this.showSeedSelector = true;
  536. },
  537. // 点击田地
  538. async handleFieldClickBZ(index) {
  539. const field = this.fields[index];
  540. // 已解锁土地且处于播种模式
  541. if (this.plantMode && field.isUnlocked && !field.hasWeed && !field.flower && !field.isGrowing) {
  542. // 设置当前选中的土地
  543. this.selectedLandId = field.id;
  544. // 播种
  545. try {
  546. uni.request({
  547. url: this.$apiHost + '/Game/huatian/plant_seed',
  548. method: 'POST',
  549. data: {
  550. uuid: getApp().globalData.uuid,
  551. land_id: field.id,
  552. seed_id: this.selectedFlowerIndex + 1
  553. },
  554. header: {
  555. 'Content-Type': 'application/x-www-form-urlencoded',
  556. 'sign': getApp().globalData.headerSign,
  557. uuid: getApp().globalData.uuid,
  558. },
  559. success: (res) => {
  560. if (res.data && res.data.code === 0) {
  561. // 更新土地状态
  562. field.isGrowing = true;
  563. field.seedStage = true;
  564. field.flowerType = this.selectedFlowerIndex + 1;
  565. field.state = this.selectedFlowerIndex + 1;
  566. field.plantTime = res.data.data.plantTime;
  567. // 计算剩余生长时间
  568. const limitTimeHours = res.data.data.limitTime;
  569. field.remainingTime = limitTimeHours * 60 * 60;
  570. // 开始计时
  571. this.startGrowthTimer(index);
  572. // 更新种子数量
  573. const flowerData = this.flowers[this.selectedFlowerIndex];
  574. if (flowerData && flowerData.count > 0) {
  575. flowerData.count--;
  576. }
  577. // 退出播种模式
  578. this.plantMode = false;
  579. uni.showToast({
  580. title: '播种成功',
  581. icon: 'success'
  582. });
  583. } else {
  584. uni.showToast({
  585. title: res.data?.msg || '播种失败',
  586. icon: 'none'
  587. });
  588. this.plantMode = false;
  589. }
  590. },
  591. fail: (err) => {
  592. console.error('播种异常', err);
  593. uni.showToast({
  594. title: '网络异常,请重试',
  595. icon: 'none'
  596. });
  597. this.plantMode = false;
  598. }
  599. });
  600. } catch (error) {
  601. console.error('播种异常', error);
  602. uni.showToast({
  603. title: '网络异常,请重试',
  604. icon: 'none'
  605. });
  606. this.plantMode = false;
  607. }
  608. } else if (field.canHarvest) {
  609. // 收获花朵
  610. this.harvestField(field.id);
  611. }
  612. },
  613. // 从特定地块移除杂草
  614. removeWeedFromField(index) {
  615. // 调用除草API
  616. uni.request({
  617. url: this.$apiHost + '/Game/huatian/remove_weed',
  618. method: 'POST',
  619. data: {
  620. uuid: getApp().globalData.uuid,
  621. land_id: this.fields[index].id
  622. },
  623. header: {
  624. 'Content-Type': 'application/x-www-form-urlencoded',
  625. 'sign': getApp().globalData.headerSign,
  626. uuid: getApp().globalData.uuid,
  627. },
  628. success: (res) => {
  629. if (res.data && res.data.code === 0) {
  630. // 更新地块状态
  631. this.fields[index].hasWeed = false;
  632. this.fields[index].state = 0;
  633. // 从可除草地块列表中移除
  634. this.weedyFields = this.weedyFields.filter(f => f.id !== this.fields[index].id);
  635. uni.showToast({
  636. title: '除草成功',
  637. icon: 'success'
  638. });
  639. // 如果所有草都已清除,退出除草模式
  640. if (this.weedyFields.length === 0) {
  641. this.weedMode = false;
  642. uni.showToast({
  643. title: '所有杂草已清除',
  644. icon: 'success'
  645. });
  646. }
  647. } else {
  648. uni.showToast({
  649. title: res.data?.msg || '除草失败',
  650. icon: 'none'
  651. });
  652. }
  653. },
  654. fail: (err) => {
  655. console.error('除草异常', err);
  656. uni.showToast({
  657. title: '网络异常,请重试',
  658. icon: 'none'
  659. });
  660. }
  661. });
  662. },
  663. // 开始生长计时器
  664. startGrowthTimer(fieldIndex) {
  665. const field = this.fields[fieldIndex];
  666. // 清除可能存在的旧定时器
  667. if (field.timer) {
  668. clearInterval(field.timer);
  669. }
  670. field.timer = setInterval(() => {
  671. field.remainingTime--;
  672. if (field.remainingTime <= 0) {
  673. // 生长完成
  674. clearInterval(field.timer);
  675. field.isGrowing = false;
  676. field.seedStage = false;
  677. field.growthCompleted = true;
  678. field.flower = true;
  679. field.canHarvest = true;
  680. }
  681. }, 1000);
  682. },
  683. // 收获特定地块
  684. async harvestField(landId) {
  685. console.log("land", landId);
  686. try {
  687. uni.request({
  688. url: this.$apiHost + '/Game/huatian/harvest',
  689. method: 'POST',
  690. data: {
  691. uuid: getApp().globalData.uuid,
  692. land_id: landId
  693. },
  694. header: {
  695. 'Content-Type': 'application/x-www-form-urlencoded',
  696. 'sign': getApp().globalData.headerSign,
  697. uuid: getApp().globalData.uuid,
  698. },
  699. success: (res) => {
  700. if (res.data && res.data.code === 0) {
  701. // 找到对应的田地
  702. const fieldIndex = this.fields.findIndex(f => f.id === landId);
  703. if (fieldIndex >= 0) {
  704. const field = this.fields[fieldIndex];
  705. // 重置状态
  706. field.flower = false;
  707. field.flowerType = null;
  708. field.growthCompleted = false;
  709. field.canHarvest = false;
  710. field.hasWeed = true;
  711. field.state = -1;
  712. // 如果有定时器,清除它
  713. if (field.timer) {
  714. clearInterval(field.timer);
  715. field.timer = null;
  716. }
  717. }
  718. // 重新获取数据以更新背包
  719. this.fetchData();
  720. uni.showToast({
  721. title: `收获了${res.data.data.harvestNum}朵花`,
  722. icon: 'success'
  723. });
  724. // 从可收获地块列表中移除
  725. this.harvestableFields = this.harvestableFields.filter(f => f.id !==
  726. landId);
  727. // 如果所有花都已收获,退出收获模式
  728. if (this.harvestableFields.length === 0) {
  729. this.harvestMode = false;
  730. uni.showToast({
  731. title: '所有花已收获',
  732. icon: 'success'
  733. });
  734. }
  735. } else {
  736. uni.showToast({
  737. title: res.data?.msg || '收获失败',
  738. icon: 'none'
  739. });
  740. }
  741. },
  742. fail: (err) => {
  743. console.error('收获异常', err);
  744. uni.showToast({
  745. title: '网络异常,请重试',
  746. icon: 'none'
  747. });
  748. }
  749. });
  750. } catch (error) {
  751. console.error('收获异常', error);
  752. uni.showToast({
  753. title: '网络异常,请重试',
  754. icon: 'none'
  755. });
  756. }
  757. },
  758. // 收获所有可收获的花
  759. async harvest() {
  760. const harvestFields = this.fields.filter(field => field.canHarvest);
  761. if (harvestFields.length === 0) {
  762. uni.showToast({
  763. title: '没有可收获的花',
  764. icon: 'none'
  765. });
  766. return;
  767. }
  768. try {
  769. for (const field of harvestFields) {
  770. await this.harvestField(field.id);
  771. }
  772. } catch (error) {
  773. console.error('批量收获异常', error);
  774. }
  775. },
  776. // 格式化时间显示
  777. formatTime(seconds) {
  778. const hours = Math.floor(seconds / 3600);
  779. const minutes = Math.floor((seconds % 3600) / 60);
  780. const secs = seconds % 60;
  781. // 获取当前时间,用于控制冒号闪烁
  782. const now = new Date();
  783. const blinkState = now.getSeconds() % 2 === 0;
  784. // 格式化为 "HH:MM" 格式
  785. return `${hours.toString().padStart(2, '0')}${blinkState ? ':' : ' '}${minutes.toString().padStart(2, '0')}`;
  786. },
  787. // 开始定时检查植物生长状态
  788. startGrowthCheck() {
  789. // 清除可能存在的旧定时器
  790. if (this.growthCheckTimer) {
  791. clearInterval(this.growthCheckTimer);
  792. }
  793. // 每分钟检查一次植物生长状态
  794. this.growthCheckTimer = setInterval(async () => {
  795. try {
  796. uni.request({
  797. url: this.$apiHost + '/Game/huatian/check_growth',
  798. method: 'POST',
  799. data: {
  800. uuid: getApp().globalData.uuid,
  801. },
  802. header: {
  803. 'Content-Type': 'application/x-www-form-urlencoded',
  804. 'sign': getApp().globalData.headerSign,
  805. uuid: getApp().globalData.uuid,
  806. },
  807. success: (res) => {
  808. if (res.data && res.data.code === 0) {
  809. const growthData = res.data.data || [];
  810. // 更新每块土地的状态
  811. growthData.forEach(item => {
  812. if (item.isReady) {
  813. // 找到对应的田地
  814. const field = this.fields.find(f => f.id ===
  815. item.landId);
  816. if (field) {
  817. // 更新为可收获状态
  818. field.isGrowing = false;
  819. field.seedStage = false;
  820. field.growthCompleted = true;
  821. field.flower = true;
  822. field.canHarvest = true;
  823. field.state = 99;
  824. // 清除定时器
  825. if (field.timer) {
  826. clearInterval(field.timer);
  827. field.timer = null;
  828. }
  829. }
  830. }
  831. });
  832. }
  833. },
  834. fail: (err) => {
  835. console.error('检查生长状态异常', err);
  836. }
  837. });
  838. } catch (error) {
  839. console.error('检查生长状态异常', error);
  840. }
  841. }, 60000); // 60秒检查一次
  842. },
  843. },
  844. }
  845. </script>
  846. <style lang="scss">
  847. @import './HuaTian.scss';
  848. @keyframes weedBlink {
  849. 0% {
  850. opacity: 1;
  851. transform: scale(1);
  852. }
  853. 50% {
  854. opacity: 0.5;
  855. transform: scale(1.1);
  856. }
  857. 100% {
  858. opacity: 1;
  859. transform: scale(1);
  860. }
  861. }
  862. .weed-blink {
  863. animation: weedBlink 1s infinite;
  864. z-index: 10;
  865. }
  866. @keyframes harvestBlink {
  867. 0% {
  868. opacity: 1;
  869. transform: scale(1);
  870. }
  871. 50% {
  872. opacity: 0.5;
  873. transform: scale(1.1);
  874. }
  875. 100% {
  876. opacity: 1;
  877. transform: scale(1);
  878. }
  879. }
  880. .harvest-blink {
  881. animation: harvestBlink 1s infinite;
  882. z-index: 10;
  883. }
  884. </style>