matchdetail.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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: '#D94B24',
  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<55;i++){
  149. weeks.push({
  150. name:'',
  151. data:[],
  152. total:0
  153. })
  154. }
  155. days.forEach(item=>{
  156. var w=this.getWeekNumber(item.stock_date)
  157. weeks[w-1].data.push(item)
  158. })
  159. weeks=weeks.filter(item=>item.data.length>0)
  160. var w_text=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
  161. weeks.forEach((item,index)=>{
  162. if(item.data.length>0){
  163. item.name='第'+w_text[index]+'周'
  164. var total=0
  165. var money = 10000.0
  166. item.data.forEach(i=>{
  167. var incom=Number(i.today_income.replace('%',''))
  168. money += money*incom/100.0
  169. i.date=(new Date(i.stock_date).getMonth()+1)+'/'+(new Date(i.stock_date).getDate())
  170. })
  171. total = (money-10000)*100.0/10000.0
  172. item.total=total.toFixed(2)
  173. }
  174. })
  175. this.setData({
  176. days:weeks.reverse()
  177. })
  178. },
  179. getDays(){
  180. $api.getCalendar({
  181. user_id:this.data.user_id,
  182. match_id:this.data.match_id,
  183. month: this.data.date
  184. }).then(res=>{
  185. var days = [], w = new Date(this.data.date + '-01').getDay();
  186. for (let i = 0; i < w; i++) {
  187. days.push({
  188. day: '',
  189. income: ''
  190. })
  191. }
  192. for (let i = 0; i < res.data.data.length; i++) {
  193. days.push({
  194. day: i+1,
  195. income: res.data.data[i].today_income,
  196. record_id:res.data.data[i].record_id,
  197. })
  198. }
  199. this.setData({
  200. days: days
  201. })
  202. })
  203. },
  204. preview(e){
  205. console.log(e)
  206. wx.previewImage({
  207. urls: e.target.dataset.urls,
  208. current: e.target.dataset.src
  209. })
  210. },
  211. bindDateChange(e){
  212. this.setData({
  213. date: e.detail.value
  214. })
  215. this.getDays()
  216. },
  217. prev(){
  218. var date=this.data.date.split('-'),y=date[0],m=date[1];
  219. if(m>1){
  220. m--
  221. }else{
  222. m=12
  223. y--
  224. }
  225. m=(m>9)?m:'0'+m
  226. this.setData({
  227. date: y+'-'+m
  228. })
  229. this.getDays()
  230. },
  231. next() {
  232. var date = this.data.date.split('-'), y = date[0], m = date[1];
  233. if (m < 12) {
  234. m++
  235. } else {
  236. m = 1
  237. y++
  238. }
  239. m = (m > 9) ? m : '0' + m
  240. this.setData({
  241. date: y + '-' + m
  242. })
  243. this.getDays()
  244. },
  245. })
  246. function initChart(canvas, width, height, dpr) {
  247. var xdata = [], ydata = [];
  248. records = records
  249. for (let i = 0; i < records.length; i++) {
  250. let date = records[i].stock_date.split('-');
  251. xdata.push(date[1] + '/' + date[2])
  252. let y = records[i].total_income.replace('%', '')
  253. ydata.push(Number(y))
  254. }
  255. xdata = xdata.reverse()
  256. ydata = ydata.reverse()
  257. const chart = echarts.init(canvas, null, {
  258. width: width,
  259. height: height,
  260. devicePixelRatio: dpr // new
  261. });
  262. canvas.setChart(chart);
  263. var option = {
  264. legend: {
  265. show: false
  266. },
  267. grid: {
  268. x: 50,
  269. y: 10,
  270. x2: 10,
  271. y2: 35
  272. },
  273. tooltip: {
  274. show: true,
  275. trigger: 'axis',
  276. formatter: '{b0}: {c0}%'
  277. },
  278. xAxis: {
  279. type: 'category',
  280. data: xdata,
  281. axisLabel: {
  282. interval: 0,
  283. rotate: 40,
  284. color: '#999999',
  285. interval: 2
  286. }
  287. },
  288. yAxis: {
  289. axisLine: {
  290. show: true
  291. },
  292. type: 'value',
  293. name: '',
  294. axisLabel: {
  295. formatter: function (value, index) {//隐藏 0
  296. let texts = [];
  297. texts.push(value + '%')
  298. return texts;
  299. },
  300. show: true
  301. },
  302. },
  303. series: [{
  304. name: 'A',
  305. type: 'line',
  306. smooth: true,
  307. symbolSize: 4,
  308. lineStyle: {
  309. color: '#FF2D68'
  310. // color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  311. // offset: 0,
  312. // color: '#FF2D68'
  313. // }, {
  314. // offset: 1,
  315. // color: '#4C4BFF'
  316. // }]),
  317. },
  318. itemStyle: {
  319. borderWidth: 5,
  320. borderColor: '#FFAD52',
  321. color: '#FFAD52'
  322. },
  323. data: ydata
  324. }]
  325. };
  326. chart.setOption(option);
  327. return chart;
  328. }
  329. function initChart1(canvas, width, height, dpr) {
  330. const chart = echarts.init(canvas, null, {
  331. width: width,
  332. height: height,
  333. devicePixelRatio: dpr // new
  334. });
  335. canvas.setChart(chart);
  336. today_stock.forEach(item=>{
  337. item.value = item.fund
  338. })
  339. var radius = today_stock.length<9?'65%':'50%';
  340. var option = {
  341. backgroundColor: "#ffffff",
  342. legend: {
  343. show: true,
  344. orient: 'vertical',
  345. right: '1%',
  346. formatter: function (name){
  347. var index = 0;
  348. today_stock.forEach(function (value, i) {
  349. if (value.name == name) {
  350. index = i;
  351. }
  352. });
  353. return name + " " + today_stock[index].value;
  354. }
  355. },
  356. series: [{
  357. label: {
  358. normal: {
  359. show: true,
  360. fontSize: 12,
  361. formatter: function (a) {
  362. return Math.round(a.percent)+'%'
  363. }
  364. }
  365. },
  366. type: 'pie',
  367. center: ['37%', '40%'],
  368. radius: ['0%', radius],
  369. data: today_stock
  370. }]
  371. };
  372. chart.setOption(option);
  373. return chart;
  374. }