level_list.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { _decorator, Component, instantiate, Label, Node, Prefab, utils } from 'cc';
  2. import { http } from '../http';
  3. import { config } from '../config';
  4. import { level_list_item_data, long_story_list_item_data } from '../../data/data';
  5. import { level_list_item } from './level_list_item';
  6. import { tools } from '../tools';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('level_list')
  9. export class level_list extends Component {
  10. @property(Node) btn_back:Node = null;
  11. @property(Node) lab_title:Node = null;
  12. @property(Node) content:Node = null;
  13. @property(Prefab) item:Prefab = null;
  14. @property(Node) page_number:Node = null;
  15. @property(Node) btn_previous_page:Node = null;
  16. @property(Node) lab_current_page:Node = null;
  17. @property(Node) btn_next_page:Node = null;
  18. private m_long_story_list_data: long_story_list_item_data = null
  19. private m_back_call_back = null;
  20. private m_click_callback = null;
  21. private m_cur_page:number = 1;
  22. private m_every_page_count:number = 18;
  23. private m_total_page_count:number = 0;
  24. protected start(): void {
  25. this.btn_back.on(Node.EventType.TOUCH_END,()=>{
  26. if(this.m_back_call_back) {
  27. this.m_back_call_back()
  28. }
  29. })
  30. this.page_number.active = false
  31. let self = this
  32. this.btn_previous_page.on(Node.EventType.TOUCH_END, ()=> {
  33. if(self.m_cur_page == 1) {
  34. tools.showToast("已是第一页")
  35. return
  36. }
  37. self.m_cur_page-=1
  38. self.requestData()
  39. },this)
  40. this.btn_next_page.on(Node.EventType.TOUCH_END, ()=> {
  41. if(self.m_cur_page >= self.m_total_page_count) {
  42. tools.showToast("已是最后一页")
  43. return
  44. }
  45. self.m_cur_page+=1
  46. self.requestData()
  47. },this)
  48. }
  49. public init(long_story_list_data: long_story_list_item_data, back_call_back, call_back){
  50. this.m_long_story_list_data = long_story_list_data
  51. if(this.m_long_story_list_data) {
  52. this.lab_title.getComponent(Label).string = this.m_long_story_list_data.name
  53. }
  54. this.m_back_call_back = back_call_back;
  55. this.m_click_callback = call_back;
  56. this.requestData()
  57. }
  58. initView(list:level_list_item_data[]){
  59. for (let index = 0; index < list.length; index++) {
  60. const element = list[index];
  61. let node = instantiate(this.item)
  62. node.parent = this.content;
  63. node.getComponent(level_list_item).initView(element,this.m_click_callback)
  64. }
  65. }
  66. requestData() {
  67. this.content.removeAllChildren()
  68. if(this.m_long_story_list_data==null) {
  69. let request_data = {"page":this.m_cur_page,"limit":this.m_every_page_count}
  70. http.post("/tool/mysnote/levels",request_data,(err,data)=>{
  71. if(!err){
  72. // console.log(data)
  73. let _data = JSON.parse(data);
  74. let __data = _data.content
  75. if(this.m_cur_page == 1) {
  76. this.page_number.active = true
  77. this.m_total_page_count = Math.ceil(__data.tool_number/this.m_every_page_count)
  78. }
  79. this.setupLabNumber()
  80. config.last_id = parseInt(_data.content.zujian_id)
  81. console.log("level_list",config.last_id )
  82. this.initView(__data.list)
  83. }
  84. })
  85. } else {
  86. }
  87. }
  88. setupLabNumber() {
  89. let string = this.m_cur_page + ' / ' + this.m_total_page_count
  90. this.lab_current_page.getComponent(Label).string = string
  91. }
  92. }