homepage.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if(this.data.player_id){
  205. this.getData();
  206. }
  207. },
  208. /**
  209. * 生命周期函数--监听页面隐藏
  210. */
  211. onHide: function () {
  212. },
  213. /**
  214. * 生命周期函数--监听页面卸载
  215. */
  216. onUnload: function () {
  217. },
  218. /**
  219. * 页面相关事件处理函数--监听用户下拉动作
  220. */
  221. onPullDownRefresh: function () {
  222. },
  223. /**
  224. * 页面上拉触底事件的处理函数
  225. */
  226. onReachBottom: function () {
  227. },
  228. /**
  229. * 用户点击右上角分享
  230. */
  231. onShareAppMessage: function () {
  232. }
  233. })
  234. /**折线图 */
  235. function initChart(canvas, width, height, dpr) {
  236. var xdata = [], ydata = [];
  237. for (let i = 0; i < records.length; i++) {
  238. let date = records[i].stock_date.split('-');
  239. xdata.push(date[1] + '/' + date[2])
  240. let y = records[i].total_income.replace('%', '')
  241. ydata.push(Number(y))
  242. }
  243. xdata = xdata.reverse()
  244. ydata = ydata.reverse()
  245. const chart = echarts.init(canvas, null, {
  246. width: width,
  247. height: height,
  248. devicePixelRatio: dpr // new
  249. });
  250. canvas.setChart(chart);
  251. var option = {
  252. legend: {
  253. show: false
  254. },
  255. grid: {
  256. x: 50,
  257. y: 40,
  258. x2: 10,
  259. y2: 35
  260. },
  261. tooltip: {
  262. show: true,
  263. trigger: 'axis',
  264. formatter: '{b0}: {c0}%'
  265. },
  266. xAxis: {
  267. type: 'category',
  268. data: xdata,
  269. axisLabel: {
  270. interval: 0,
  271. rotate: 40,
  272. color: '#999999',
  273. interval: 2
  274. }
  275. },
  276. yAxis: {
  277. axisLine: {
  278. show: true
  279. },
  280. type: 'value',
  281. name: '收益曲线',
  282. axisLabel: {
  283. formatter: function (value, index) {//隐藏 0
  284. let texts = [];
  285. texts.push(value + '%')
  286. return texts;
  287. },
  288. show: true
  289. },
  290. },
  291. series: [{
  292. name: 'A',
  293. type: 'line',
  294. smooth: true,
  295. symbolSize: 8,
  296. lineStyle: {
  297. color: '#FF2D68'
  298. // color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
  299. // offset: 0,
  300. // color: '#FF2D68'
  301. // }, {
  302. // offset: 1,
  303. // color: '#4C4BFF'
  304. // }]),
  305. },
  306. itemStyle: {
  307. borderWidth: 5,
  308. borderColor: '#FFAD52',
  309. color: '#FFAD52'
  310. },
  311. data: ydata
  312. }]
  313. };
  314. chart.setOption(option);
  315. return chart;
  316. }
  317. function initChart1(canvas, width, height, dpr) {
  318. const chart = echarts.init(canvas, null, {
  319. width: width,
  320. height: height,
  321. devicePixelRatio: dpr // new
  322. });
  323. canvas.setChart(chart);
  324. today_stock.forEach(item=>{
  325. item.value = item.fund
  326. })
  327. var radius = today_stock.length<9?'65%':'50%';
  328. var option = {
  329. backgroundColor: "#ffffff",
  330. legend: {
  331. show: true,
  332. orient: 'vertical',
  333. right: '1%',
  334. formatter: function (name){
  335. var index = 0;
  336. today_stock.forEach(function (value, i) {
  337. if (value.name == name) {
  338. index = i;
  339. }
  340. });
  341. return name + " " + today_stock[index].value;
  342. }
  343. },
  344. series: [{
  345. label: {
  346. normal: {
  347. show: true,
  348. fontSize: 12,
  349. formatter: function (a) {
  350. return Math.round(a.percent)+'%'
  351. }
  352. }
  353. },
  354. type: 'pie',
  355. center: ['37%', '40%'],
  356. radius: ['0%', radius],
  357. data: today_stock
  358. }]
  359. };
  360. chart.setOption(option);
  361. return chart;
  362. }