level_list.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. } else {
  54. this.lab_title.getComponent(Label).string = '关卡列表'
  55. }
  56. this.m_back_call_back = back_call_back;
  57. this.m_click_callback = call_back;
  58. this.requestData()
  59. }
  60. initView(list:level_list_item_data[]){
  61. for (let index = 0; index < list.length; index++) {
  62. const element = list[index];
  63. let node = instantiate(this.item)
  64. node.parent = this.content;
  65. node.getComponent(level_list_item).initView(element,this.m_click_callback)
  66. }
  67. }
  68. requestData() {
  69. this.content.removeAllChildren()
  70. let stype = 0 //0:短篇 1:长篇
  71. let book_id = 0
  72. if(this.m_long_story_list_data) {
  73. stype = 1
  74. book_id = this.m_long_story_list_data.id
  75. }
  76. let opt = {"stype":stype,"book_id":book_id,"page":this.m_cur_page,"limit":this.m_every_page_count}
  77. // console.log('opt=',opt)
  78. http.post("/tool/mysnote/levels",opt,(err,data)=>{
  79. if(!err){
  80. // console.log(data)
  81. let _data = JSON.parse(data);
  82. let __data = _data.content
  83. if(this.m_cur_page == 1) {
  84. this.page_number.active = true
  85. this.m_total_page_count = Math.ceil(__data.tool_number/this.m_every_page_count)
  86. }
  87. this.setupLabNumber()
  88. this.initView(__data.list)
  89. config.last_id = parseInt(_data.content.zujian_id)
  90. console.log("level_list",config.last_id)
  91. }
  92. })
  93. }
  94. setupLabNumber() {
  95. let string = this.m_cur_page + ' / ' + this.m_total_page_count
  96. this.lab_current_page.getComponent(Label).string = string
  97. }
  98. }