homepage.js 8.0 KB

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