matchdetail.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // pages/matchdetail/matchdetail.js
  2. const app = getApp()
  3. import * as echarts from '../../ec-canvas/echarts';
  4. const $api = require('../../utils/api.js').API;
  5. const $push = require('../../utils/api.js').PUSH;
  6. var records = [], today_stock=[];
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. datas:{},
  13. lists:[],
  14. total_money:0,
  15. user_id:'',
  16. match_id:'',
  17. date:'',
  18. days:[],
  19. ec: {
  20. onInit: null
  21. },
  22. ec1: {
  23. onInit: null
  24. },
  25. role:0,
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad(options) {
  31. this.setData({
  32. user_id:options.user_id,
  33. match_id:options.match_id,
  34. })
  35. wx.getStorage({
  36. key: 'user',
  37. complete: res => {
  38. this.setData({
  39. role: res.data.role,
  40. })
  41. }
  42. })
  43. this.getMatchData()
  44. },
  45. pushToday(e){
  46. $push.pushToday(e.currentTarget.dataset)
  47. },
  48. /**
  49. * 生命周期函数--监听页面初次渲染完成
  50. */
  51. onReady() {
  52. },
  53. /**
  54. * 生命周期函数--监听页面显示
  55. */
  56. onShow() {
  57. },
  58. /**
  59. * 生命周期函数--监听页面隐藏
  60. */
  61. onHide() {
  62. },
  63. /**
  64. * 生命周期函数--监听页面卸载
  65. */
  66. onUnload() {
  67. },
  68. /**
  69. * 页面相关事件处理函数--监听用户下拉动作
  70. */
  71. onPullDownRefresh() {
  72. },
  73. /**
  74. * 页面上拉触底事件的处理函数
  75. */
  76. onReachBottom() {
  77. },
  78. /**
  79. * 用户点击右上角分享
  80. */
  81. onShareAppMessage() {
  82. },
  83. getMatchData(){
  84. wx.showNavigationBarLoading();
  85. var data = {}
  86. data.user_id = this.data.user_id
  87. data.id = this.data.match_id
  88. $api.getPlayerMatch(data).then(res=>{
  89. let stock_date=res.data.data.today_record.stock_date.split('-')
  90. records = res.data.data.records
  91. today_stock = res.data.data.today_record.today_stock
  92. // console.log(records,2323)
  93. let tmoney = ((res.data.data.today_record.today_fund - res.data.data.today_record.init_fund)*10000).toFixed(0)
  94. this.getWeek(records)
  95. this.setData({
  96. datas: res.data.data,
  97. lists: records.reverse(),
  98. total_money:tmoney,
  99. date: stock_date[0] + '-' + stock_date[1],
  100. ec: {
  101. onInit: initChart
  102. },
  103. // ec1: {
  104. // onInit: initChart1
  105. // }
  106. })
  107. // this.getDays()
  108. wx.hideNavigationBarLoading()
  109. wx.setNavigationBarTitle({
  110. title: res.data.data.userinfo.username,
  111. })
  112. if(this.data.total_money >= 0){
  113. wx.setNavigationBarColor({
  114. backgroundColor: '#ff0f03',
  115. frontColor: '#ffffff',
  116. })
  117. } else{
  118. wx.setNavigationBarColor({
  119. backgroundColor: '#07B20B',
  120. frontColor: '#ffffff',
  121. })
  122. }
  123. })
  124. .catch(err=>{
  125. wx.hideNavigationBarLoading()
  126. })
  127. },
  128. toThousandFilter(num) {
  129. return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
  130. },
  131. //计算周数
  132. getWeekNumber:function(dateString) {
  133. const date = new Date(dateString);
  134. const year = date.getFullYear();
  135. // 将年份的第一个周一作为第一周的开始
  136. const firstWeekStart = new Date(year, 0, 1);
  137. firstWeekStart.setDate(firstWeekStart.getDate() - firstWeekStart.getDay() + 1);
  138. // 计算给定日期是一年中的第几周
  139. const currentDate = new Date(date);
  140. currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1);
  141. let milliseconds = currentDate.getTime() - firstWeekStart.getTime();
  142. const weeks = Math.round(milliseconds / (7 * 24 * 60 * 60 * 1000));
  143. return weeks + 1; // 因为getWeek返回的是年初至今的周数,所以加1得到完整的周数
  144. },
  145. getWeek(list){
  146. var days=list,weeks=[]
  147. days=days.reverse()
  148. for(let i=0;i<100;i++){
  149. weeks.push({
  150. name:'',
  151. data:[],
  152. total:0
  153. })
  154. }
  155. var pre=0;
  156. days.forEach(item=>{
  157. var w=this.getWeekNumber(item.stock_date)
  158. if(w<pre){
  159. if(w==1){
  160. weeks[pre-1].data.push(item)
  161. } else{
  162. weeks[pre+w-2].data.push(item)
  163. }
  164. }else{
  165. weeks[w-1].data.push(item)
  166. pre=w
  167. }
  168. })
  169. weeks=weeks.filter(item=>item.data.length>0)
  170. var w_text=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
  171. weeks.forEach((item,index)=>{
  172. if(item.data.length>0){
  173. item.name='第'+w_text[index]+'周'
  174. var total=0
  175. var money = 10000.0
  176. item.data.forEach(i=>{
  177. var incom=Number(i.today_income.replace('%',''))
  178. money += money*incom/100.0
  179. i.date=(new Date(i.stock_date).getMonth()+1)+'/'+(new Date(i.stock_date).getDate())
  180. })
  181. total = (money-10000)*100.0/10000.0
  182. item.total=total.toFixed(2)
  183. }
  184. })
  185. this.setData({
  186. days:weeks.reverse()
  187. })
  188. },
  189. getDays(){
  190. $api.getCalendar({
  191. user_id:this.data.user_id,
  192. match_id:this.data.match_id,
  193. month: this.data.date
  194. }).then(res=>{
  195. var days = [], w = new Date(this.data.date + '-01').getDay();
  196. for (let i = 0; i < w; i++) {
  197. days.push({
  198. day: '',
  199. income: ''
  200. })
  201. }
  202. for (let i = 0; i < res.data.data.length; i++) {
  203. days.push({
  204. day: i+1,
  205. income: res.data.data[i].today_income,
  206. record_id:res.data.data[i].record_id,
  207. })
  208. }
  209. this.setData({
  210. days: days
  211. })
  212. })
  213. },
  214. preview(e){
  215. // console.log(e)
  216. wx.previewImage({
  217. urls: e.target.dataset.urls,
  218. current: e.target.dataset.src
  219. })
  220. },
  221. bindDateChange(e){
  222. this.setData({
  223. date: e.detail.value
  224. })
  225. this.getDays()
  226. },
  227. prev(){
  228. var date=this.data.date.split('-'),y=date[0],m=date[1];
  229. if(m>1){
  230. m--
  231. }else{
  232. m=12
  233. y--
  234. }
  235. m=(m>9)?m:'0'+m
  236. this.setData({
  237. date: y+'-'+m
  238. })
  239. this.getDays()
  240. },
  241. next() {
  242. var date = this.data.date.split('-'), y = date[0], m = date[1];
  243. if (m < 12) {
  244. m++
  245. } else {
  246. m = 1
  247. y++
  248. }
  249. m = (m > 9) ? m : '0' + m
  250. this.setData({
  251. date: y + '-' + m
  252. })
  253. this.getDays()
  254. },
  255. })
  256. function initChart(canvas, width, height, dpr) {
  257. var xdata = [], ydata = [];
  258. records = records
  259. for (let i = 0; i < records.length; i++) {
  260. let date = records[i].stock_date.split('-');
  261. xdata.push(date[1] + '/' + date[2])
  262. let y = records[i].total_income.replace('%', '')
  263. ydata.push(Number(y))
  264. }
  265. xdata = xdata.reverse()
  266. ydata = ydata.reverse()
  267. const chart = echarts.init(canvas, null, {
  268. width: width,
  269. height: height,
  270. devicePixelRatio: dpr // new
  271. });
  272. canvas.setChart(chart);
  273. var option = {
  274. legend: {
  275. show: false
  276. },
  277. grid: {
  278. x: 50,
  279. y: 10,
  280. x2: 10,
  281. y2: 35
  282. },
  283. tooltip: {
  284. show: true,
  285. trigger: 'axis',
  286. formatter: '{b0}: {c0}%'
  287. },
  288. xAxis: {
  289. type: 'category',
  290. data: xdata,
  291. axisLabel: {
  292. interval: 0,
  293. rotate: 40,
  294. color: '#999999',
  295. interval: 2
  296. }
  297. },
  298. yAxis: {
  299. axisLine: {
  300. show: true
  301. },
  302. type: 'value',
  303. name: '',
  304. axisLabel: {
  305. formatter: function (value, index) {//隐藏 0
  306. let texts = [];
  307. texts.push(value + '%')
  308. return texts;
  309. },
  310. show: true
  311. },
  312. },
  313. series: [{
  314. name: 'A',
  315. type: 'line',
  316. smooth: true,
  317. symbolSize: 4,
  318. lineStyle: {
  319. color: '#FF2D68'
  320. // color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  321. // offset: 0,
  322. // color: '#FF2D68'
  323. // }, {
  324. // offset: 1,
  325. // color: '#4C4BFF'
  326. // }]),
  327. },
  328. itemStyle: {
  329. borderWidth: 5,
  330. borderColor: '#FFAD52',
  331. color: '#FFAD52'
  332. },
  333. data: ydata
  334. }]
  335. };
  336. chart.setOption(option);
  337. return chart;
  338. }
  339. function initChart1(canvas, width, height, dpr) {
  340. const chart = echarts.init(canvas, null, {
  341. width: width,
  342. height: height,
  343. devicePixelRatio: dpr // new
  344. });
  345. canvas.setChart(chart);
  346. today_stock.forEach(item=>{
  347. item.value = item.fund
  348. })
  349. var radius = today_stock.length<9?'65%':'50%';
  350. var option = {
  351. backgroundColor: "#ffffff",
  352. legend: {
  353. show: true,
  354. orient: 'vertical',
  355. right: '1%',
  356. formatter: function (name){
  357. var index = 0;
  358. today_stock.forEach(function (value, i) {
  359. if (value.name == name) {
  360. index = i;
  361. }
  362. });
  363. return name + " " + today_stock[index].value;
  364. }
  365. },
  366. series: [{
  367. label: {
  368. normal: {
  369. show: true,
  370. fontSize: 12,
  371. formatter: function (a) {
  372. return Math.round(a.percent)+'%'
  373. }
  374. }
  375. },
  376. type: 'pie',
  377. center: ['37%', '40%'],
  378. radius: ['0%', radius],
  379. data: today_stock
  380. }]
  381. };
  382. chart.setOption(option);
  383. return chart;
  384. }