index.js 9.2 KB

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