与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

658 lines
16 KiB

1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
  1. import http from '../../utils/api'
  2. // pages/market/market.js
  3. Page({
  4. data: {
  5. // 当前时间
  6. currentTime: '',
  7. // 销售市场数据
  8. salesData: [],
  9. salesUpdateTime: '',
  10. salesStatus: 'inactive',
  11. // 饲料市场数据
  12. feedData: [],
  13. feedUpdateTime: '',
  14. feedStatus: 'inactive',
  15. // 市场趋势数据
  16. trendData: [],
  17. unreadCount: 0,
  18. // 最后更新时间
  19. lastUpdateTime: '',
  20. // 刷新状态
  21. isRefreshing: false,
  22. // 动画数据
  23. headerAnimation: {},
  24. cardAnimation1: {},
  25. cardAnimation2: {},
  26. cardAnimation3: {},
  27. // 无缝滚动相关数据
  28. scrollOffset: 0, // 滚动偏移量
  29. scrollDuration: 0.5, // 滚动动画持续时间
  30. scrollTimer: null, // 滚动定时器
  31. itemHeight: 150, // 每个公告项的高度(估算值,单位:px)
  32. isPaused: false // 是否暂停滚动
  33. },
  34. onLoad: function () {
  35. // 初始化时间
  36. this.updateCurrentTime();
  37. // 初始化数据
  38. this.initMarketData();
  39. // 启动动画
  40. this.startPageAnimations();
  41. // 启动定时器
  42. this.startTimers();
  43. // 启动无缝滚动
  44. this.startAutoScroll();
  45. this.getsales()
  46. this.getfeed()
  47. this.gettrend()
  48. },
  49. onShow: function () {
  50. // 恢复滚动(如果之前暂停了)
  51. if (this.data.isPaused) {
  52. this.setData({
  53. isPaused: false
  54. });
  55. this.startAutoScroll();
  56. }
  57. },
  58. onHide: function () {
  59. // 页面隐藏时暂停滚动
  60. this.stopAutoScroll();
  61. this.setData({
  62. isPaused: true
  63. });
  64. },
  65. onUnload: function () {
  66. // 清理定时器
  67. this.clearTimers();
  68. // 停止滚动
  69. this.stopAutoScroll();
  70. },
  71. // 销售市场
  72. getsales() {
  73. http.sales({
  74. data: {},
  75. success: res => {
  76. console.log(11, res);
  77. this.setData({
  78. salesData:res.rows
  79. })
  80. }
  81. })
  82. },
  83. //饲料市场
  84. getfeed() {
  85. http.feed({
  86. data: {},
  87. success: res => {
  88. console.log(22, res);
  89. this.setData({
  90. feedData:res.rows
  91. })
  92. }
  93. })
  94. },
  95. // 市场趋势
  96. gettrend() {
  97. http.trend({
  98. data: {},
  99. success: res => {
  100. console.log(22, res);
  101. this.setData({
  102. trendData:res.rows
  103. })
  104. }
  105. })
  106. },
  107. onPullDownRefresh: function () {
  108. this.refreshAllData();
  109. },
  110. // 更新当前时间
  111. updateCurrentTime: function () {
  112. const now = new Date();
  113. const timeStr = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  114. this.setData({
  115. currentTime: timeStr
  116. });
  117. },
  118. // 启动定时器
  119. startTimers: function () {
  120. // 更新时间
  121. this.timeTimer = setInterval(() => {
  122. this.updateCurrentTime();
  123. }, 60000);
  124. // 模拟实时数据更新
  125. this.dataTimer = setInterval(() => {
  126. this.updateRandomData();
  127. }, 30000);
  128. },
  129. // 清理定时器
  130. clearTimers: function () {
  131. if (this.timeTimer) clearInterval(this.timeTimer);
  132. if (this.dataTimer) clearInterval(this.dataTimer);
  133. },
  134. // 启动自动滚动
  135. startAutoScroll: function () {
  136. // 清除已有的定时器
  137. if (this.data.scrollTimer) {
  138. clearInterval(this.data.scrollTimer);
  139. }
  140. // 计算总高度(一条数据的高度 * 数据条数)
  141. const totalHeight = this.data.itemHeight * this.data.trendData.length;
  142. // 设置滚动定时器,每5秒滚动一次
  143. const scrollTimer = setInterval(() => {
  144. this.autoScrollStep(totalHeight);
  145. }, 5000);
  146. this.setData({
  147. scrollTimer
  148. });
  149. },
  150. // 停止自动滚动
  151. stopAutoScroll: function () {
  152. if (this.data.scrollTimer) {
  153. clearInterval(this.data.scrollTimer);
  154. this.setData({
  155. scrollTimer: null
  156. });
  157. }
  158. },
  159. // 自动滚动步骤
  160. autoScrollStep: function (totalHeight) {
  161. // 计算下一个偏移量
  162. let nextOffset = this.data.scrollOffset + this.data.itemHeight;
  163. // 如果滚动到了第一组数据的末尾,重置到开始位置
  164. if (nextOffset >= totalHeight) {
  165. // 瞬间回到起点,然后继续滚动
  166. this.setData({
  167. scrollOffset: 0,
  168. scrollDuration: 0
  169. });
  170. // 下一帧继续正常滚动
  171. setTimeout(() => {
  172. this.setData({
  173. scrollOffset: this.data.itemHeight,
  174. scrollDuration: 0.5
  175. });
  176. }, 50);
  177. } else {
  178. // 正常滚动
  179. this.setData({
  180. scrollOffset: nextOffset,
  181. scrollDuration: 0.5
  182. });
  183. }
  184. },
  185. // 手动控制滚动(可选)
  186. pauseScroll: function () {
  187. this.stopAutoScroll();
  188. this.setData({
  189. isPaused: true
  190. });
  191. },
  192. resumeScroll: function () {
  193. if (this.data.isPaused) {
  194. this.setData({
  195. isPaused: false
  196. });
  197. this.startAutoScroll();
  198. }
  199. },
  200. // 初始化市场数据
  201. initMarketData: function () {
  202. // 模拟销售市场数据(6条数据)
  203. const salesData = [{
  204. id: 1,
  205. name: '优质肉牛',
  206. region: '内蒙古呼伦贝尔',
  207. price: 35.8,
  208. unit: '公斤',
  209. trend: 'up',
  210. change: '+2.3%',
  211. updateTime: '09:30'
  212. },
  213. {
  214. id: 2,
  215. name: '绵羊',
  216. region: '新疆阿勒泰',
  217. price: 28.5,
  218. unit: '公斤',
  219. trend: 'stable',
  220. change: '0%',
  221. updateTime: '09:25'
  222. },
  223. {
  224. id: 3,
  225. name: '山羊',
  226. region: '山东济宁',
  227. price: 32.2,
  228. unit: '公斤',
  229. trend: 'down',
  230. change: '-1.5%',
  231. updateTime: '09:20'
  232. },
  233. {
  234. id: 4,
  235. name: '奶牛',
  236. region: '黑龙江',
  237. price: 26.8,
  238. unit: '公斤',
  239. trend: 'up',
  240. change: '+1.2%',
  241. updateTime: '09:15'
  242. },
  243. {
  244. id: 5,
  245. name: '生猪',
  246. region: '河南',
  247. price: 18.5,
  248. unit: '公斤',
  249. trend: 'up',
  250. change: '+3.1%',
  251. updateTime: '09:10'
  252. },
  253. {
  254. id: 6,
  255. name: '肉羊',
  256. region: '甘肃',
  257. price: 30.2,
  258. unit: '公斤',
  259. trend: 'stable',
  260. change: '0%',
  261. updateTime: '09:05'
  262. }
  263. ];
  264. // 模拟饲料市场数据(5条数据)
  265. const feedData = [{
  266. id: 1,
  267. name: '玉米饲料',
  268. supplier: '内蒙古正大饲料有限公司',
  269. price: 2.85,
  270. unit: '公斤',
  271. trend: 'up',
  272. chartHeight: 85,
  273. updateTime: '09:28'
  274. },
  275. {
  276. id: 2,
  277. name: '豆粕',
  278. supplier: '山东六和集团股份有限公司',
  279. price: 3.42,
  280. unit: '公斤',
  281. trend: 'up',
  282. chartHeight: 92,
  283. updateTime: '09:25'
  284. },
  285. {
  286. id: 3,
  287. name: '青贮饲料',
  288. supplier: '河北牧原股份有限责任公司',
  289. price: 0.78,
  290. unit: '公斤',
  291. trend: 'stable',
  292. chartHeight: 65,
  293. updateTime: '09:22'
  294. },
  295. {
  296. id: 4,
  297. name: '麦麸',
  298. supplier: '河南双汇发展有限公司',
  299. price: 1.25,
  300. unit: '公斤',
  301. trend: 'down',
  302. chartHeight: 45,
  303. updateTime: '09:20'
  304. },
  305. {
  306. id: 5,
  307. name: '鱼粉',
  308. supplier: '广东海大集团',
  309. price: 5.8,
  310. unit: '公斤',
  311. trend: 'stable',
  312. chartHeight: 60,
  313. updateTime: '09:18'
  314. }
  315. ];
  316. // 模拟市场趋势通知公告数据(8条数据)
  317. const trendData = [{
  318. id: 1,
  319. type: 'report',
  320. title: '2023年Q3畜牧业市场分析报告发布',
  321. source: '国家统计局',
  322. date: '2023-10-15',
  323. summary: '第三季度畜产品价格整体上涨3.2%,饲料成本下降1.5%,养殖效益显著提升',
  324. isNew: true,
  325. isHot: true
  326. },
  327. {
  328. id: 2,
  329. type: 'prediction',
  330. title: '专家预测:四季度牛肉价格将稳中有升',
  331. source: '行业专家委员会',
  332. date: '2023-10-12',
  333. summary: '受季节因素影响,预计四季度牛肉价格上涨幅度在3-5%之间',
  334. isNew: true,
  335. isHot: false
  336. },
  337. {
  338. id: 3,
  339. type: 'report',
  340. title: '全国饲料价格指数月报(9月份)',
  341. source: '农业部监测中心',
  342. date: '2023-10-10',
  343. summary: '9月份全国饲料价格指数为108.5,环比上涨0.8%,同比上涨2.3%',
  344. isNew: false,
  345. isHot: true
  346. },
  347. {
  348. id: 4,
  349. type: 'prediction',
  350. title: '冬季饲草供应紧张预警',
  351. source: '气象局农业处',
  352. date: '2023-10-08',
  353. summary: '北方地区提前进入冬季,预计饲草储备量不足,价格可能上涨',
  354. isNew: true,
  355. isHot: true
  356. },
  357. {
  358. id: 5,
  359. type: 'report',
  360. title: '牛羊养殖效益分析报告',
  361. source: '畜牧业协会',
  362. date: '2023-10-05',
  363. summary: '2023年1-9月牛羊养殖效益同比增长8.5%,创近三年新高',
  364. isNew: false,
  365. isHot: false
  366. },
  367. {
  368. id: 6,
  369. type: 'report',
  370. title: '畜产品质量安全监测报告',
  371. source: '质量监督局',
  372. date: '2023-10-03',
  373. summary: '第三季度畜产品质量抽检合格率达99.2%,质量安全状况良好',
  374. isNew: false,
  375. isHot: false
  376. },
  377. {
  378. id: 7,
  379. type: 'prediction',
  380. title: '明年饲料原料价格走势分析',
  381. source: '农业经济研究所',
  382. date: '2023-10-01',
  383. summary: '预计明年玉米、豆粕等主要原料价格将保持稳定,波动幅度有限',
  384. isNew: false,
  385. isHot: false
  386. },
  387. {
  388. id: 8,
  389. type: 'report',
  390. title: '畜牧业数字化转型进展报告',
  391. source: '工信部',
  392. date: '2023-09-28',
  393. summary: '全国畜牧业数字化率已达45%,智能养殖设备普及率显著提升',
  394. isNew: false,
  395. isHot: true
  396. }
  397. ];
  398. // 计算未读数量
  399. const unreadCount = trendData.filter(item => item.isNew).length;
  400. const now = new Date();
  401. const timeStr = `${now.getMonth()+1}${now.getDate()}${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;
  402. const lastUpdateTime = `${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;
  403. this.setData({
  404. salesData,
  405. feedData,
  406. trendData,
  407. unreadCount,
  408. salesUpdateTime: timeStr,
  409. feedUpdateTime: timeStr,
  410. lastUpdateTime,
  411. salesStatus: 'inactive',
  412. feedStatus: 'inactive'
  413. });
  414. // 模拟数据加载完成
  415. setTimeout(() => {
  416. this.setData({
  417. salesStatus: 'active',
  418. feedStatus: 'active'
  419. });
  420. }, 1500);
  421. },
  422. // 启动页面动画
  423. startPageAnimations: function () {
  424. // 头部动画
  425. const headerAnimation = wx.createAnimation({
  426. duration: 800,
  427. timingFunction: 'ease-out'
  428. });
  429. headerAnimation.translateY(0).opacity(1).step();
  430. // 卡片动画
  431. const cardAnimation1 = wx.createAnimation({
  432. duration: 600,
  433. timingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)',
  434. delay: 200
  435. });
  436. cardAnimation1.translateY(0).opacity(1).step();
  437. const cardAnimation2 = wx.createAnimation({
  438. duration: 600,
  439. timingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)',
  440. delay: 350
  441. });
  442. cardAnimation2.translateY(0).opacity(1).step();
  443. const cardAnimation3 = wx.createAnimation({
  444. duration: 600,
  445. timingFunction: 'cubic-bezier(0.4, 0, 0.2, 1)',
  446. delay: 500
  447. });
  448. cardAnimation3.translateY(0).opacity(1).step();
  449. this.setData({
  450. headerAnimation: headerAnimation.export(),
  451. cardAnimation1: cardAnimation1.export(),
  452. cardAnimation2: cardAnimation2.export(),
  453. cardAnimation3: cardAnimation3.export()
  454. });
  455. },
  456. // 刷新所有数据
  457. refreshAllData: function () {
  458. if (this.data.isRefreshing) return;
  459. this.setData({
  460. isRefreshing: true,
  461. salesStatus: 'active',
  462. feedStatus: 'active'
  463. });
  464. // 显示加载动画
  465. wx.showLoading({
  466. title: '更新数据中...',
  467. mask: true
  468. });
  469. // 模拟网络请求
  470. setTimeout(() => {
  471. // 更新数据
  472. this.updateRandomData();
  473. // 更新时间戳
  474. const now = new Date();
  475. const timeStr = `${now.getMonth()+1}${now.getDate()}${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;
  476. const lastUpdateTime = `${now.getHours()}:${now.getMinutes().toString().padStart(2, '0')}`;
  477. this.setData({
  478. salesUpdateTime: timeStr,
  479. feedUpdateTime: timeStr,
  480. lastUpdateTime,
  481. isRefreshing: false,
  482. salesStatus: 'inactive',
  483. feedStatus: 'inactive'
  484. });
  485. // 显示成功提示
  486. wx.hideLoading();
  487. wx.showToast({
  488. title: '数据已更新',
  489. icon: 'success',
  490. duration: 1500
  491. });
  492. // 停止下拉刷新
  493. wx.stopPullDownRefresh();
  494. }, 1500);
  495. },
  496. // 更新随机数据(模拟实时变化)
  497. updateRandomData: function () {
  498. // 随机更新销售价格
  499. const updatedSalesData = this.data.salesData.map(item => {
  500. const random = Math.random();
  501. let newPrice, trend, change;
  502. if (random < 0.4) {
  503. // 上涨
  504. trend = 'up';
  505. change = +(Math.random() * 0.5 + 0.1).toFixed(1);
  506. newPrice = +(item.price * (1 + change / 100)).toFixed(1);
  507. } else if (random < 0.7) {
  508. // 稳定
  509. trend = 'stable';
  510. change = 0;
  511. newPrice = item.price;
  512. } else {
  513. // 下降
  514. trend = 'down';
  515. change = +(Math.random() * 0.5 + 0.1).toFixed(1);
  516. newPrice = +(item.price * (1 - change / 100)).toFixed(1);
  517. }
  518. // 更新时间
  519. const now = new Date();
  520. const updateTime = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  521. return {
  522. ...item,
  523. price: newPrice,
  524. trend,
  525. change: trend === 'stable' ? '0%' : (trend === 'up' ? `+${change}%` : `-${change}%`),
  526. updateTime
  527. };
  528. });
  529. // 随机更新饲料价格
  530. const updatedFeedData = this.data.feedData.map(item => {
  531. const random = Math.random();
  532. let trend, chartHeight;
  533. if (random < 0.4) {
  534. trend = 'up';
  535. chartHeight = Math.floor(Math.random() * 30 + 70);
  536. } else if (random < 0.7) {
  537. trend = 'stable';
  538. chartHeight = Math.floor(Math.random() * 20 + 50);
  539. } else {
  540. trend = 'down';
  541. chartHeight = Math.floor(Math.random() * 20 + 30);
  542. }
  543. // 轻微调整价格
  544. const priceChange = (Math.random() - 0.5) * 0.1;
  545. const newPrice = +(item.price + priceChange).toFixed(2);
  546. // 更新时间
  547. const now = new Date();
  548. const updateTime = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`;
  549. return {
  550. ...item,
  551. price: newPrice > 0 ? newPrice : item.price,
  552. trend,
  553. chartHeight,
  554. updateTime
  555. };
  556. });
  557. // 随机更新趋势数据(标记一些为已读,添加新公告)
  558. const updatedTrendData = [...this.data.trendData];
  559. // 随机标记一些为已读
  560. updatedTrendData.forEach((item, index) => {
  561. if (item.isNew && Math.random() > 0.7) {
  562. updatedTrendData[index] = {
  563. ...item,
  564. isNew: false
  565. };
  566. }
  567. });
  568. // 偶尔添加新公告
  569. if (Math.random() > 0.8) {
  570. const newNotice = {
  571. id: Date.now(),
  572. type: Math.random() > 0.5 ? 'report' : 'prediction',
  573. title: `实时快报:${Math.random() > 0.5 ? '价格波动提醒' : '行业动态更新'}`,
  574. source: '实时监测系统',
  575. date: new Date().toISOString().split('T')[0],
  576. summary: '系统监测到市场出现新的变化,请关注后续详细分析',
  577. isNew: true,
  578. isHot: Math.random() > 0.7
  579. };
  580. updatedTrendData.unshift(newNotice);
  581. }
  582. const unreadCount = updatedTrendData.filter(item => item.isNew).length;
  583. this.setData({
  584. salesData: updatedSalesData,
  585. feedData: updatedFeedData,
  586. trendData: updatedTrendData,
  587. unreadCount
  588. });
  589. }
  590. });