homepage.js 8.2 KB

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