matchdetail.js 8.6 KB

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