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