index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const $api = require('../../utils/api.js').API;
  5. const $push = require('../../utils/api.js').PUSH;
  6. Page({
  7. data: {
  8. loading:0,
  9. notices:[],
  10. tabs:['今日留言','我的关注'],
  11. tabs1:['收益榜','盈利榜','亏损榜','跌幅榜'],
  12. cur:0,
  13. cur1:0,
  14. date:'2024-09-09',
  15. explist:[], //留言
  16. total1:0,
  17. page1:1,
  18. followList:[], //关注
  19. total2:-1,
  20. page2:1,
  21. banners:[],
  22. winLost:{},
  23. baikeList:[],
  24. articleList1:[],
  25. articleList2:[],
  26. articleList3:[],
  27. articleList4:[],
  28. groups:{},
  29. role:0,
  30. match_day:0,
  31. socore:[
  32. { name: '10', checked: false },
  33. { name: '9', checked: false },
  34. { name: '8', checked: false },
  35. { name: '7', checked: false },
  36. { name: '6', checked: false },
  37. { name: '5', checked: false },
  38. { name: '4', checked: false },
  39. { name: '3', checked: false },
  40. { name: '2', checked: false },
  41. { name: '1', checked: false }
  42. ],
  43. isScore:0,
  44. isWeek:0,
  45. scoredays:[],
  46. nochoose:false,
  47. },
  48. onLoad() {
  49. var w=new Date().getDay()
  50. if(w == 0 || w == 6){
  51. this.setData({
  52. isWeek:1
  53. })
  54. }
  55. wx.getStorage({
  56. key: 'user',
  57. complete: res => {
  58. this.setData({
  59. role: res.data.role,
  60. })
  61. }
  62. })
  63. $api.getDate().then(res=>{
  64. this.setData({
  65. date:res.data.data
  66. })
  67. $api.getNotoice().then(res=>{
  68. this.setData({
  69. notices:res.data.data
  70. })
  71. })
  72. // 是否打分
  73. var y=new Date().getFullYear()
  74. var m=Number(new Date().getMonth() + 1)
  75. var d = new Date().getDate()
  76. m=m>9?m:'0'+m;
  77. d=d>9?d:'0'+d
  78. var date = y+'-'+m+'-'+d
  79. if(!this.data.isWeek){
  80. $api.checkMark({stock_date:date}).then(res=>{
  81. if(res.data.data){
  82. this.setData({
  83. isScore:1
  84. })
  85. }
  86. })
  87. }
  88. })
  89. this.getFollow()
  90. this.getIndexData()
  91. this.getExp()
  92. this.getWeekAvg()
  93. },
  94. getWeekAvg:function(){
  95. $api.getMark().then(res=>{
  96. var days=res.data.data.reverse(),weeks=[]
  97. var pre=0
  98. console.log(days)
  99. days.forEach(item=>{
  100. item.week=getYearWeek(item.stock_date)
  101. if(item.week < pre){
  102. if(item.week == 1){
  103. item.week=pre
  104. }
  105. }else{
  106. pre=item.week
  107. }
  108. item.date=(new Date(item.stock_date).getMonth()+1)+'/'+(new Date(item.stock_date).getDate())
  109. })
  110. days=days.reverse()
  111. // days.forEach((item,i)=>{
  112. // })
  113. const result = days.reduce((acc, curr) => {
  114. let weekGroup = acc.find(item => item.name === curr.week);
  115. if (!weekGroup) {
  116. weekGroup = { name: curr.week, data: [] };
  117. acc.push(weekGroup);
  118. }
  119. weekGroup.data.push({ avg_score: curr.avg_score,date:curr.date});
  120. return acc;
  121. }, []);
  122. result.forEach(item=>{
  123. item.data=item.data.reverse()
  124. })
  125. // var scoredays=[]
  126. // for (let key in weeks) {
  127. // scoredays.push({
  128. // name:key,
  129. // data:weeks[key].reverse()
  130. // })
  131. // }
  132. this.setData({
  133. scoredays:result
  134. })
  135. })
  136. function getYearWeek(endDate) {
  137. //本年的第一天
  138. endDate=new Date(endDate)
  139. var beginDate = new Date(endDate.getFullYear(), 0, 1);
  140. //星期从0-6,0代表星期天,6代表星期六
  141. var endWeek = endDate.getDay();
  142. if (endWeek == 0) endWeek = 7;
  143. var beginWeek = beginDate.getDay();
  144. if (beginWeek == 0) beginWeek = 7;
  145. //计算两个日期的天数差
  146. var millisDiff = endDate.getTime() - beginDate.getTime();
  147. var dayDiff = Math.floor(( millisDiff + (beginWeek - endWeek) * (24 * 60 * 60 * 1000)) / 86400000);
  148. return Math.ceil(dayDiff / 7) + 1;
  149. }
  150. },
  151. radioChange:function(e){
  152. if(!$api.checkUser()){
  153. this.setData({
  154. socore:[
  155. { name: '10', checked: false },
  156. { name: '9', checked: false },
  157. { name: '8', checked: false },
  158. { name: '7', checked: false },
  159. { name: '6', checked: false },
  160. { name: '5', checked: false },
  161. { name: '4', checked: false },
  162. { name: '3', checked: false },
  163. { name: '2', checked: false },
  164. { name: '1', checked: false }
  165. ],
  166. })
  167. return
  168. }
  169. var y=new Date().getFullYear()
  170. var m=Number(new Date().getMonth() + 1)
  171. var d = new Date().getDate()
  172. m=m>9?m:'0'+m;
  173. d=d>9?d:'0'+d
  174. var date = y+'-'+m+'-'+d
  175. this.setData({
  176. s:e.detail.value
  177. })
  178. $api.markScore({stock_date:date,score:e.detail.value}).then(res=>{
  179. console.log(res)
  180. if(res.data.code == 0){
  181. wx.showToast({
  182. title: '打分成功',
  183. })
  184. this.setData({
  185. isScore:1
  186. })
  187. this.getWeekAvg()
  188. }
  189. })
  190. },
  191. fresh: function () {
  192. },
  193. // 获取首页数据
  194. getIndexData:function(){
  195. $api.getChampionlList({isbanner:1}).then(res=>{
  196. this.setData({
  197. banners:res.data.data.list
  198. })
  199. })
  200. $api.getWinList({new:1}).then(res=>{
  201. let day = res.data.data[0].valid_dates.indexOf(res.data.data[0].date)
  202. this.setData({
  203. winLost:res.data.data[0],
  204. match_day:day,
  205. })
  206. $api.getAvg({match_id:res.data.data[0].match_id}).then(res=>{
  207. var days=res.data.data.list,weeks=[]
  208. days=days.reverse()
  209. for(let i=0;i<100;i++){
  210. weeks.push({
  211. name:'',
  212. data:[],
  213. total:0
  214. })
  215. }
  216. var pre=0;
  217. days.forEach(item=>{
  218. var w=this.getWeekNumber(item.stock_date)
  219. if(w<pre){
  220. if(w==1){
  221. weeks[pre-1].data.push(item)
  222. }else{
  223. weeks[pre+w-2].data.push(item)
  224. }
  225. }else{
  226. weeks[w-1].data.push(item)
  227. pre=w
  228. }
  229. })
  230. weeks=weeks.filter(item=>item.data.length>0)
  231. var w_text=['一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五']
  232. weeks.forEach((item,index)=>{
  233. if(item.data.length>0){
  234. item.name='第'+w_text[index]+'周'
  235. var total=0
  236. var money = 10000.0
  237. item.data.forEach(i=>{
  238. var incom=Number(i.today_income.replace('%',''))
  239. money += money*incom/100.0
  240. i.date=(new Date(i.stock_date).getMonth()+1)+'/'+(new Date(i.stock_date).getDate())
  241. })
  242. total = (money-10000)*100.0/10000.0
  243. item.total=total.toFixed(2)
  244. }
  245. })
  246. this.setData({
  247. days:weeks.reverse()
  248. })
  249. })
  250. $api.getWinLose({match_id:res.data.data[0].match_id}).then(res1=>{
  251. this.setData({
  252. groups:res1.data.data
  253. })
  254. })
  255. })
  256. $api.getBaikeRandom().then(res=>{
  257. this.setData({
  258. baikeList:res.data.data
  259. })
  260. })
  261. $api.getChampionlList({category:'每日点评',page:1}).then(res=>{
  262. this.setData({
  263. articleList1:res.data.data.list
  264. })
  265. })
  266. $api.getChampionlList({category:'冠军交割',page:1}).then(res=>{
  267. this.setData({
  268. articleList2:res.data.data.list
  269. })
  270. })
  271. $api.getChampionlList({category:'牛人专场',page:1}).then(res=>{
  272. this.setData({
  273. articleList3:res.data.data.list
  274. })
  275. })
  276. $api.getChampionlList({category:'妖股列传',page:1}).then(res=>{
  277. this.setData({
  278. articleList4:res.data.data.list
  279. })
  280. })
  281. },
  282. //计算周数
  283. getWeekNumber:function(dateString) {
  284. const date = new Date(dateString);
  285. const year = date.getFullYear();
  286. // 将年份的第一个周一作为第一周的开始
  287. const firstWeekStart = new Date(year, 0, 1);
  288. firstWeekStart.setDate(firstWeekStart.getDate() - firstWeekStart.getDay() + 1);
  289. // 计算给定日期是一年中的第几周
  290. const currentDate = new Date(date);
  291. currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 1);
  292. let milliseconds = currentDate.getTime() - firstWeekStart.getTime();
  293. const weeks = Math.round(milliseconds / (7 * 24 * 60 * 60 * 1000));
  294. return weeks + 1; // 因为getWeek返回的是年初至今的周数,所以加1得到完整的周数
  295. },
  296. //获取今日留言
  297. getExp:function(){
  298. var list = this.data.explist
  299. $api.getExperience({page:this.data.page1,page_size:20}).then(res=>{
  300. res.data.data.list.forEach(item => {
  301. list.push(item)
  302. })
  303. this.setData({
  304. explist: list,
  305. total1: res.data.data.total
  306. })
  307. })
  308. },
  309. //留言分页
  310. next1: function () {
  311. if (this.data.explist.length < this.data.total1) {
  312. const page = this.data.page1 + 1
  313. this.setData({
  314. page1: page
  315. })
  316. this.getExp()
  317. }
  318. },
  319. //我的关注
  320. getFollow(){
  321. var followList = this.data.followList
  322. if(this.data.loading){
  323. followList=[]
  324. }
  325. $api.getMyFollowV3({
  326. page: this.data.page2, page_size: 20
  327. }).then(res => {
  328. res.data.data.list.forEach(item => {
  329. followList.push(item)
  330. })
  331. this.setData({
  332. followList: followList,
  333. total2:res.data.data.total,
  334. error:0,
  335. loading: 0
  336. })
  337. app.globalData.follow = 0
  338. })
  339. .catch(err=>{
  340. this.setData({
  341. error:1
  342. })
  343. })
  344. },
  345. //关注分页
  346. next2: function () {
  347. if (this.data.followList.length < this.data.total2) {
  348. const page = this.data.page2 + 1
  349. this.setData({
  350. page2: page
  351. })
  352. this.getFollow()
  353. }
  354. },
  355. tabChange(e){
  356. this.setData({
  357. cur:e.target.dataset.id
  358. })
  359. this.renew()
  360. },
  361. tabChange1(e){
  362. this.setData({
  363. cur1:e.target.dataset.id
  364. })
  365. },
  366. tapGroup(e){
  367. wx.navigateTo({
  368. url: '../avg/avg?index='+e.currentTarget.dataset.index + '&list=' + encodeURIComponent(JSON.stringify(this.data.groups.group_win_lose_list)),
  369. })
  370. },
  371. tapAvg(e){
  372. wx.navigateTo({
  373. url: '../avg/avg?index=0' + '&list=' + encodeURIComponent(JSON.stringify(this.data.groups.group_win_lose_list)),
  374. })
  375. },
  376. //预览
  377. preview(e){
  378. wx.previewImage({
  379. urls: e.target.dataset.urls,
  380. current: e.target.dataset.src
  381. })
  382. },
  383. curChange(e){
  384. if (e.detail.source == "touch"){
  385. this.setData({
  386. cur: e.detail.current
  387. })
  388. this.renew()
  389. }
  390. },
  391. curChange1(e){
  392. if (e.detail.source == "touch"){
  393. this.setData({
  394. cur1: e.detail.current
  395. })
  396. }
  397. },
  398. renew(){
  399. this.setData({
  400. followList:[],
  401. explist:[],
  402. page1: 1,
  403. total1: 0,
  404. page2: 1,
  405. total2: -1
  406. })
  407. switch(this.data.cur){
  408. case 0:
  409. this.data.page1 = 1
  410. this.getExp()
  411. break;
  412. case 1:
  413. this.data.page2 = 1
  414. this.getFollow()
  415. break;
  416. }
  417. },
  418. pushToday(e){
  419. $push.pushToday(e.currentTarget.dataset)
  420. },
  421. pushArticleWeb(e){
  422. if($api.checkUserRole()){
  423. wx.navigateTo({
  424. url: '../articleweb/articleweb?id=' + e.currentTarget.dataset.id,
  425. })
  426. }
  427. },
  428. pushBaike(e){
  429. if($api.checkUserRole()){
  430. wx.navigateTo({
  431. url: '../detail/detail?type=百科&id=' + e.currentTarget.dataset.id,
  432. })
  433. }
  434. },
  435. pushOther(e){
  436. switch(e.currentTarget.dataset.id){
  437. case "0":{
  438. wx.navigateTo({
  439. url: '../stock/stock?type=1&stock_date='+this.data.date,
  440. })
  441. }
  442. break;
  443. case "1":{
  444. wx.navigateTo({
  445. url: '../hotuser/hotuser',
  446. })
  447. }
  448. break;
  449. case "2":{
  450. wx.navigateTo({
  451. url: '../tlb/tlb?match_id='+this.data.winLost.match_id,
  452. })
  453. }
  454. break;
  455. case "3":{
  456. wx.navigateTo({
  457. url: '../baike/baike',
  458. })
  459. }
  460. break;
  461. case "4":{
  462. wx.navigateTo({
  463. url: '../todayExp/todayExp',
  464. })
  465. }
  466. break;
  467. case "5":{
  468. wx.navigateTo({
  469. url: '../myfollow/myfollow',
  470. })
  471. }
  472. break;
  473. }
  474. },
  475. onShow: function (e) {
  476. $api.initUser()
  477. $api.getsignup().then(res => {
  478. this.setData({
  479. info: res.data.data
  480. })
  481. })
  482. if(this.data.cur == 0 && app.globalData.follow == 1){
  483. this.data.loading = 1
  484. this.data.page2 = 1
  485. this.data.total2 = -1,
  486. this.getFollow()
  487. }
  488. },
  489. })