homepage.js 8.3 KB

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