homepage.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // pages/today/today.js
  2. const app = getApp()
  3. import * as echarts from '../../ec-canvas/echarts';
  4. const $api = require('../../utils/api.js').API;
  5. var id, record_id, records = [], today_stock=[];
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. datas:{},
  12. mydata:{},
  13. date:'2021-11',
  14. days:[],
  15. match:[],
  16. index:0,
  17. id:'',
  18. ec: {
  19. onInit: null
  20. },
  21. ec1: {
  22. onInit: null
  23. }
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. // options.id = 7, options.record_id = 29014
  30. // options.id = 9, options.player_id = 3520
  31. id = options.id?options.id:null, record_id = options.record_id?options.record_id:null
  32. this.setData({
  33. type:options.type?options.type:'',
  34. id: id,
  35. record_id: record_id,
  36. player_id: options.player_id ? options.player_id:''
  37. })
  38. if(options.type){
  39. wx.setNavigationBarTitle({
  40. title: '我的主页',
  41. })
  42. }
  43. $api.getMatchList().then(res=>{
  44. let l = res.data.data.length - 1;
  45. this.setData({
  46. match:res.data.data,
  47. index: l,
  48. id: res.data.data[l].id
  49. })
  50. this.getData();
  51. })
  52. },
  53. getDays(){
  54. // match_id player_id month: "2021-12"
  55. $api.getCalendar({
  56. player_id: this.data.player_id,
  57. match_id:this.data.id,
  58. month: this.data.date
  59. }).then(res=>{
  60. var days = [], w = new Date(this.data.date + '-01').getDay();
  61. for (let i = 0; i < w; i++) {
  62. days.push({
  63. day: '',
  64. income: ''
  65. })
  66. }
  67. for (let i = 0; i < res.data.data.length; i++) {
  68. days.push({
  69. day: i+1,
  70. income: res.data.data[i].today_income
  71. })
  72. }
  73. this.setData({
  74. days: days
  75. })
  76. })
  77. },
  78. bindChange(e) {
  79. let match=this.data.match;
  80. this.setData({
  81. index: e.detail.value,
  82. id: match[e.detail.value].id
  83. })
  84. this.getData()
  85. },
  86. getData(){
  87. wx.showNavigationBarLoading();
  88. var data = {}
  89. if (this.data.id) {
  90. data.id = this.data.id
  91. }
  92. if (this.data.player_id) {
  93. data.player_id = this.data.player_id
  94. }
  95. if (this.data.record_id) {
  96. data.record_id = this.data.record_id
  97. }
  98. $api.getPlayerMatch(data).then(res=>{
  99. let stock_date=res.data.data.today_record.stock_date.split('-')
  100. this.setData({
  101. datas: res.data.data,
  102. date: stock_date[0] + '-' + stock_date[1],
  103. is_follow: res.data.data.is_follow,
  104. player_id: res.data.data.today_record.player_id,
  105. followers:res.data.data.followers,
  106. fans:res.data.data.fans,
  107. id: res.data.data.match.id,
  108. ec: {
  109. onInit: initChart
  110. },
  111. ec1: {
  112. onInit: initChart1
  113. }
  114. })
  115. records = res.data.data.records
  116. today_stock = res.data.data.today_record.today_stock
  117. this.getDays()
  118. wx.hideNavigationBarLoading()
  119. })
  120. .catch(err=>{
  121. wx.hideNavigationBarLoading()
  122. })
  123. //每日持股
  124. var parm={
  125. id: this.data.id, player_id: this.data.player_id?data.player_id:o
  126. }
  127. $api.getRecordList(parm).then(res=>{
  128. this.setData({
  129. stockList:res.data.data.list
  130. })
  131. })
  132. },
  133. bindDateChange(e){
  134. this.setData({
  135. date: e.detail.value
  136. })
  137. this.getDays()
  138. },
  139. prev(){
  140. var date=this.data.date.split('-'),y=date[0],m=date[1];
  141. if(m>1){
  142. m--
  143. }else{
  144. m=12
  145. y--
  146. }
  147. m=(m>9)?m:'0'+m
  148. this.setData({
  149. date: y+'-'+m
  150. })
  151. this.getDays()
  152. },
  153. logout() {
  154. wx.removeStorage({
  155. key: 'userInfo'
  156. })
  157. wx.switchTab({
  158. url: '../user/user',
  159. })
  160. },
  161. next() {
  162. var date = this.data.date.split('-'), y = date[0], m = date[1];
  163. if (m < 12) {
  164. m++
  165. } else {
  166. m = 1
  167. y++
  168. }
  169. m = (m > 9) ? m : '0' + m
  170. this.setData({
  171. date: y + '-' + m
  172. })
  173. this.getDays()
  174. },
  175. //预览
  176. preview(e) {
  177. wx.previewImage({
  178. urls: e.target.dataset.urls,
  179. current: e.target.dataset.src
  180. })
  181. },
  182. followPlayer(e){
  183. let action = e.target.dataset.action;
  184. $api.follow({ follow_id: this.data.player_id, action:action}).then(res=>{
  185. wx.showToast({
  186. title: action?'取消成功':'已关注',
  187. })
  188. this.setData({
  189. is_follow: !this.data.is_follow
  190. })
  191. app.globalData.follow=1
  192. })
  193. },
  194. /**
  195. * 生命周期函数--监听页面初次渲染完成
  196. */
  197. onReady: function () {
  198. },
  199. /**
  200. * 生命周期函数--监听页面显示
  201. */
  202. onShow: function () {
  203. },
  204. /**
  205. * 生命周期函数--监听页面隐藏
  206. */
  207. onHide: function () {
  208. },
  209. /**
  210. * 生命周期函数--监听页面卸载
  211. */
  212. onUnload: function () {
  213. },
  214. /**
  215. * 页面相关事件处理函数--监听用户下拉动作
  216. */
  217. onPullDownRefresh: function () {
  218. },
  219. /**
  220. * 页面上拉触底事件的处理函数
  221. */
  222. onReachBottom: function () {
  223. },
  224. /**
  225. * 用户点击右上角分享
  226. */
  227. onShareAppMessage: function () {
  228. }
  229. })
  230. /**折线图 */
  231. function initChart(canvas, width, height, dpr) {
  232. var xdata = [], ydata = [];
  233. for (let i = 0; i < records.length; i++) {
  234. let date = records[i].stock_date.split('-');
  235. xdata.push(date[1] + '/' + date[2])
  236. let y = records[i].total_income.replace('%', '')
  237. ydata.push(Number(y))
  238. }
  239. xdata = xdata.reverse()
  240. ydata = ydata.reverse()
  241. const chart = echarts.init(canvas, null, {
  242. width: width,
  243. height: height,
  244. devicePixelRatio: dpr // new
  245. });
  246. canvas.setChart(chart);
  247. var option = {
  248. legend: {
  249. show: false
  250. },
  251. grid: {
  252. x: 50,
  253. y: 40,
  254. x2: 10,
  255. y2: 35
  256. },
  257. tooltip: {
  258. show: true,
  259. trigger: 'axis',
  260. formatter: '{b0}: {c0}%'
  261. },
  262. xAxis: {
  263. type: 'category',
  264. data: xdata,
  265. axisLabel: {
  266. interval: 0,
  267. rotate: 40,
  268. color: '#999999',
  269. interval: 2
  270. }
  271. },
  272. yAxis: {
  273. axisLine: {
  274. show: true
  275. },
  276. type: 'value',
  277. name: '收益曲线',
  278. axisLabel: {
  279. formatter: function (value, index) {//隐藏 0
  280. let texts = [];
  281. texts.push(value + '%')
  282. return texts;
  283. },
  284. show: true
  285. },
  286. },
  287. series: [{
  288. name: 'A',
  289. type: 'line',
  290. smooth: true,
  291. symbolSize: 8,
  292. lineStyle: {
  293. color: '#FF2D68'
  294. // color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  295. // offset: 0,
  296. // color: '#FF2D68'
  297. // }, {
  298. // offset: 1,
  299. // color: '#4C4BFF'
  300. // }]),
  301. },
  302. itemStyle: {
  303. borderWidth: 5,
  304. borderColor: '#FFAD52',
  305. color: '#FFAD52'
  306. },
  307. data: ydata
  308. }]
  309. };
  310. chart.setOption(option);
  311. return chart;
  312. }
  313. function initChart1(canvas, width, height, dpr) {
  314. const chart = echarts.init(canvas, null, {
  315. width: width,
  316. height: height,
  317. devicePixelRatio: dpr // new
  318. });
  319. canvas.setChart(chart);
  320. today_stock.forEach(item=>{
  321. item.value = item.fund
  322. })
  323. var radius = today_stock.length<9?'65%':'50%';
  324. var option = {
  325. backgroundColor: "#ffffff",
  326. legend: {
  327. show: true,
  328. orient: 'vertical',
  329. right: '1%',
  330. formatter: function (name){
  331. var index = 0;
  332. today_stock.forEach(function (value, i) {
  333. if (value.name == name) {
  334. index = i;
  335. }
  336. });
  337. return name + " " + today_stock[index].value;
  338. }
  339. },
  340. series: [{
  341. label: {
  342. normal: {
  343. show: true,
  344. fontSize: 12,
  345. formatter: function (a) {
  346. return Math.round(a.percent)+'%'
  347. }
  348. }
  349. },
  350. type: 'pie',
  351. center: ['37%', '40%'],
  352. radius: ['0%', radius],
  353. data: today_stock
  354. }]
  355. };
  356. chart.setOption(option);
  357. return chart;
  358. }