matchdetail.js 8.5 KB

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