index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import {
  2. getStatisticsTime,
  3. getStatisticsMonth
  4. } from "../../../api/admin";
  5. import * as echarts from './../components/ec-canvas/echarts';
  6. const app = getApp();
  7. let chart = null;
  8. var option = {}
  9. Page({
  10. /**
  11. * 组件的初始数据
  12. */
  13. data: {
  14. ec: {
  15. lazyLoad: true
  16. },
  17. xData: [],
  18. yData: [],
  19. parameter: {
  20. 'navbar': '1',
  21. 'return': '1',
  22. 'title': '',
  23. 'color': false
  24. },
  25. start: '',
  26. stop: '',
  27. type: '',
  28. title: '',
  29. time: '',
  30. growth_rate: "", //增长率
  31. increase_time: "", //增长率
  32. increase_time_status: "", //增长率
  33. time_price: "", //增长率
  34. loading: false, //是否加载中
  35. loadend: false, //是否加载完毕
  36. loadTitle: '加载更多', //提示语
  37. page: 1,
  38. limit: 15,
  39. dataList: [],
  40. isClose: false,
  41. imageWidth: '',
  42. current: false,
  43. isRange: true,
  44. },
  45. touchHandler(e) {
  46. console.log(chartLine.getCurrentDataIndex(e))
  47. },
  48. /**
  49. * 日期选择
  50. */
  51. onPickDay(e) {
  52. let start = e.detail[0].split('-');
  53. let stop = e.detail[1].split('-');
  54. this.setData({
  55. start: new Date(Date.parse(start[0] + "/" + start[1] + "/" + start[2])).getTime() / 1000,
  56. stop: new Date(Date.parse(stop[0] + "/" + stop[1] + "/" + stop[2])).getTime() / 1000 + 24 * 60 * 60 - 1
  57. });
  58. },
  59. close() {
  60. this.setData({
  61. current: false
  62. });
  63. this.getIndex();
  64. this.setData({
  65. loadend: false,
  66. page: 1,
  67. dataList: []
  68. });
  69. this.getList();
  70. },
  71. // 2、进行初始化数据
  72. initChartOption: function (xData, yData) {
  73. return {
  74. backgroundColor: "#fff",
  75. color: ["#37A2DA", "#f2960d", "#67E0E3", "#9FE6B8"],
  76. title: {
  77. textStyle: {
  78. fontWeight: '500',
  79. fontSize: 15,
  80. color: '#000'
  81. },
  82. x: 'center',
  83. y: '0'
  84. },
  85. tooltip: {
  86. trigger: 'axis',
  87. },
  88. legend: {
  89. data: [""]
  90. },
  91. toolbox: {
  92. show: false,
  93. feature: {
  94. mark: {
  95. show: true
  96. },
  97. dataView: {
  98. show: true,
  99. readOnly: false
  100. },
  101. magicType: {
  102. show: true,
  103. type: ["line"]
  104. },
  105. restore: {
  106. show: true
  107. },
  108. saveAsImage: {
  109. show: true
  110. }
  111. }
  112. },
  113. calculable: true,
  114. xAxis: {
  115. type: 'category',
  116. data: xData,
  117. boundaryGap: false,
  118. splitLine: {
  119. show: false
  120. },
  121. axisLine: {
  122. lineStyle: {
  123. color: "#999",
  124. width: 1 //这里是为了突出显示加上的
  125. }
  126. }
  127. },
  128. yAxis: [{
  129. type: "value",
  130. splitLine: {
  131. show: true,
  132. lineStyle: {
  133. color: ["#f5f5f5"],
  134. width: 1,
  135. type: "solid"
  136. }
  137. },
  138. axisLine: {
  139. lineStyle: {
  140. color: "#999",
  141. width: 1 //这里是为了突出显示加上的
  142. }
  143. }
  144. }],
  145. series: [{
  146. data: yData,
  147. type: 'line',
  148. itemStyle: {
  149. normal: {
  150. color: "#2291f8", //折点颜色
  151. lineStyle: {
  152. color: "#2291f8" //折线颜色
  153. }
  154. }
  155. }
  156. }],
  157. grid: {
  158. left: '0%', //组件离容器左侧的距离
  159. right: '0%',
  160. bottom: '4%',
  161. containLabel: true
  162. },
  163. animationDuration: 1000
  164. }
  165. },
  166. /**
  167. * 生命周期函数--监听页面加载
  168. */
  169. onLoad: function (options) {
  170. this.barComponent = this.selectComponent('#mychart-one');
  171. // this.handleCanvarToImg();
  172. this.setData({
  173. type: options.type,
  174. time: options.time,
  175. parameter: {
  176. title: options.type === '1' ? '营业额统计' : '订单数据统计',
  177. navbar: '1',
  178. return: '1',
  179. color: false
  180. }
  181. });
  182. },
  183. /**
  184. * 登录回调
  185. */
  186. onLoadFun: function () {
  187. this.oneComponent = this.selectComponent('#mychart-one');
  188. this.setTime(this.data.time);
  189. },
  190. handleCanvarToImg() {
  191. wx.canvasToTempFilePath({
  192. x: 0,
  193. y: 0,
  194. width: 260,
  195. height: 180,
  196. canvasId: 'mychart-bar',
  197. success: function (res) {
  198. this.setData({
  199. radarImg: res.tempFilePath
  200. });
  201. }
  202. });
  203. },
  204. /**
  205. * 打开自定义
  206. */
  207. dateTitle: function () {
  208. wx.navigateTo({
  209. url: '/pages/admin/order_statistics_time/index'
  210. })
  211. },
  212. getIndex: function () {
  213. let that = this;
  214. getStatisticsTime({
  215. start: that.data.start,
  216. stop: that.data.stop,
  217. type: that.data.type
  218. }).then(
  219. res => {
  220. let _info = res.data.chart || [],
  221. day = [],
  222. num = [];
  223. _info.forEach(function (item) {
  224. day.push(item.time);
  225. num.push(item.num);
  226. });
  227. that.setData({
  228. growth_rate: res.data.growth_rate,
  229. increase_time: res.data.increase_time,
  230. increase_time_status: res.data.increase_time_status,
  231. time_price: res.data.time,
  232. xData: day,
  233. yData: num
  234. });
  235. that.barComponent.init((canvas, width, height) => {
  236. // 初始化图表
  237. const barChart = echarts.init(canvas, null, {
  238. width: width,
  239. height: height
  240. });
  241. barChart.setOption(that.initChartOption(day, num));
  242. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  243. return barChart;
  244. });
  245. },
  246. error => {
  247. app.Tips({
  248. title: error.msg
  249. });
  250. }
  251. );
  252. },
  253. setTime: function (e) {
  254. let time = '';
  255. if (e.currentTarget) {
  256. time = e.currentTarget.dataset.time;
  257. } else {
  258. time = e;
  259. }
  260. let year = new Date().getFullYear(),
  261. month = new Date().getMonth() + 1,
  262. day = new Date().getDate();
  263. switch (time) {
  264. case "today":
  265. this.setData({
  266. time: time,
  267. start: new Date(Date.parse(year + "/" + month + "/" + day)).getTime() / 1000,
  268. stop: new Date(Date.parse(year + "/" + month + "/" + day)).getTime() / 1000 + 24 * 60 * 60 - 1,
  269. title: "今日"
  270. });
  271. this.getIndex();
  272. this.setData({
  273. loadend: false,
  274. page: 1,
  275. dataList: []
  276. });
  277. this.getList();
  278. break;
  279. case "yesterday":
  280. this.setData({
  281. time: time,
  282. start: new Date(Date.parse(year + "/" + month + "/" + day)).getTime() / 1000 - 24 * 60 * 60,
  283. stop: new Date(Date.parse(year + "/" + month + "/" + day)).getTime() / 1000 - 1,
  284. title: "昨日"
  285. });
  286. this.getIndex();
  287. this.setData({
  288. loadend: false,
  289. page: 1,
  290. dataList: []
  291. });
  292. this.getList();
  293. break;
  294. case "month":
  295. this.setData({
  296. time: time,
  297. start: new Date(year, new Date().getMonth(), 1).getTime() / 1000,
  298. stop: new Date(year, month, 1).getTime() / 1000 - 1,
  299. title: "本月"
  300. });
  301. this.getIndex();
  302. this.setData({
  303. loadend: false,
  304. page: 1,
  305. dataList: []
  306. });
  307. this.getList();
  308. break;
  309. case "seven":
  310. this.setData({
  311. time: time,
  312. start: new Date(Date.parse(year + "/" + month + "/" + day)).getTime() / 1000 + 24 * 60 * 60 - 7 * 3600 * 24,
  313. stop: new Date(Date.parse(year + "/" + month + "/" + day)).getTime() / 1000 + 24 * 60 * 60 - 1,
  314. title: "七日"
  315. });
  316. this.getIndex();
  317. this.setData({
  318. loadend: false,
  319. page: 1,
  320. dataList: []
  321. });
  322. this.getList();
  323. break;
  324. }
  325. },
  326. /**
  327. * 获取详细数据
  328. */
  329. getList: function () {
  330. if (this.data.loadend) return;
  331. if (this.data.loading) return;
  332. this.setData({
  333. loading: true,
  334. loadTitle: ""
  335. });
  336. getStatisticsMonth({
  337. page: this.data.page,
  338. limit: this.data.limit,
  339. start: this.data.start,
  340. stop: this.data.stop
  341. }).then(res => {
  342. let list = res.data || [];
  343. let loadend = list.length < this.data.limit;
  344. this.data.dataList = app.SplitArray(list, this.data.dataList);
  345. this.setData({
  346. dataList: this.data.dataList,
  347. loadend: loadend,
  348. loading: false,
  349. loadTitle: loadend ? "我也是有底线的" : '加载更多',
  350. page: this.data.page + 1,
  351. });
  352. }).catch(err => {
  353. this.setData({
  354. loading: false,
  355. loadTitle: "加载更多"
  356. });
  357. })
  358. },
  359. /**
  360. * 生命周期函数--监听页面隐藏
  361. */
  362. onHide: function () {
  363. this.setData({
  364. isClose: true
  365. });
  366. },
  367. /**
  368. * 生命周期函数--监听页面显示
  369. */
  370. onShow: function () {
  371. if (app.globalData.isLog && this.data.isClose) {
  372. this.setData({
  373. loadend: false,
  374. page: 1,
  375. dataList: []
  376. });
  377. this.getList();
  378. }
  379. let pages = getCurrentPages();
  380. let currPage = pages[pages.length - 1]; //当前页面
  381. if (currPage.data.start && currPage.data.stop) {
  382. this.setData({
  383. start: currPage.data.start,
  384. stop: currPage.data.stop,
  385. title: "",
  386. time: 'date'
  387. });
  388. this.getIndex();
  389. }
  390. },
  391. /**
  392. * 页面上拉触底事件的处理函数
  393. */
  394. onReachBottom: function () {
  395. this.getList();
  396. },
  397. })