import { Component , ViewChild } from '@angular/core' ; import { Platform , Loading , NavController , Popover , Alert , Toast , Storage , LocalStorage , Slides } from 'ionic-angular' ; import { PopoverPage } from '../home/PopoverPage' ; import { Http , Headers , RequestOptions } from '@angular/http' ; import { Helper } from '../helper/helper' ; import { IssuerPage } from '../issuer/issuer' ; @ Component ( { templateUrl : 'build/pages/home/home.html' } ) export class HomePage { @ ViewChild ( 'mySlider' ) slider : Slides ; mySlideOptions = { autoplay : 2000 , loop : true , direction : 'horizontal' , pager : true , } ; id : string ; public local : Storage ; private taskListUrl : string = 'http://119.29.140.85/index.php/task/task_list' ; private startTaskUrl : string = 'http://119.29.140.85/index.php/task/start_task' ; helper : Helper ; headers : Headers ; options : RequestOptions ; status : string ; express_code : string ; school_code : string ; page : number = 1 ; page_size : number ; public items = [ ] ; loading = Loading . create ( { content : '' , showBackdrop : false } ) ; constructor ( private nav , public platform , private http ) { this . local = new Storage ( LocalStorage ) ; this . helper = new Helper ( nav ) ; } presentPopover ( myEvent ) { let popover = Popover . create ( PopoverPage ) ; this . nav . present ( popover , { ev : myEvent } ) ; } shwoTastInfo ( event , item ) { let confirm = Alert . create ( { title : '确认领取任务' , message : '是否确认领取任务单[' + item . id + ']，一旦领取您将必须在规定时间内完成该订单' , buttons : [ { text : '取消' , handler : ( ) => { } } , { text : '确认' , handler : ( ) => { this . http . post ( this . startTaskUrl , this . helper . headleAjaxBodyParam ( { 'task_id' : item . id , 'complete_user_id' : this . id } ) , this . options ) . subscribe ( ( data ) => { let response = data . json ( ) ; if ( response . status ) { this . helper . presentToast ( '领取成功,可在我的任务查看任务信息' ) ; for ( let i = 0 ; i < this . items . length ; i ++ ) { if ( this . items [ i ] == item ) { this . items . splice ( i , 1 ) ; } } } else { this . helper . presentToast ( '任务领取失败:' + response . info ) ; } } , ( err ) => { this . helper . presentToast ( '网络错误，任务领取失败!' ) ; } ) ; } } ] } ) ; this . nav . present ( confirm ) ; } doRefresh ( refresher ) { this . http . post ( this . taskListUrl , this . helper . headleAjaxBodyParam ( { 'page_size' : 10 , 'status' : 0 } ) , this . options ) . subscribe ( ( data ) => { let response = data . json ( ) ; if ( response . status ) { this . items = [ ] ; for ( let i = 0 ; i < response . data . length ; i ++ ) { this . items [ i ] = { 'src' : 'http://119.29.140.85' + response . data [ i ] . launch_user_head_pic_url , 'name' : response . data [ i ] . launch_nick , 'KDName' : response . data [ i ] . express_name , 'address' : response . data [ i ] . school_name , 'id' : response . data [ i ] . id } ; } refresher . complete ( ) ; this . page = 1 ; } else { this . helper . presentToast ( '数据初始化失败：' + response . info ) ; } } , ( err ) => { this . helper . presentToast ( '网络错误，数据初始化失败!' ) ; } ) ; } presentToast ( ) { let message = this . local . get ( 'phone' ) . toString ( ) ; let toast = Toast . create ( { message : message , duration : 2000 , position : 'middle' } ) ; toast . onDismiss ( ( ) => { } ) ; this . nav . present ( toast ) ; } ionViewLoaded ( ) { this . nav . present ( this . loading ) ; this . headers = new Headers ( { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } ) ; this . options = new RequestOptions ( { headers : this . headers } ) ; let localData = JSON . stringify ( window . localStorage ) ; let localJson = eval ( "(" + localData + ")" ) ; this . id = localJson . id ; this . initData ( ) ; } initData ( ) { this . http . post ( this . taskListUrl , this . helper . headleAjaxBodyParam ( { 'page_size' : 10 , 'status' : 0 } ) , this . options ) . subscribe ( ( data ) => { let response = data . json ( ) ; if ( response . status ) { this . loading . dismiss ( ) ; this . items = [ ] ; for ( let i = 0 ; i < response . data . length ; i ++ ) { this . items [ i ] = { 'src' : 'http://119.29.140.85' + response . data [ i ] . launch_user_head_pic_url , 'name' : response . data [ i ] . launch_nick , 'KDName' : response . data [ i ] . express_name , 'address' : response . data [ i ] . school_name , 'id' : response . data [ i ] . id } ; } } else { this . helper . presentToast ( '数据初始化失败：' + response . info ) ; this . loading . dismiss ( ) ; } } , ( err ) => { this . helper . presentToast ( '网络错误，数据初始化失败!' ) ; this . loading . dismiss ( ) ; } ) ; } doInfinite ( infiniteScroll ) { this . page ++ ; this . http . post ( this . taskListUrl , this . helper . headleAjaxBodyParam ( { 'page_size' : 10 , 'page' : this . page , 'status' : 0 } ) , this . options ) . subscribe ( ( data ) => { let response = data . json ( ) ; if ( response . data . length > 0 ) { if ( response . status ) { for ( let i = 0 ; i < response . data . length ; i ++ ) { this . items [ i + ( this . page - 1 ) * 10 ] = { 'src' : 'http://119.29.140.85' + response . data [ i ] . launch_user_head_pic_url , 'name' : response . data [ i ] . launch_nick , 'KDName' : response . data [ i ] . express_name , 'address' : response . data [ i ] . school_name , 'id' : response . data [ i ] . id } ; } infiniteScroll . complete ( ) ; } else { this . helper . presentToast ( '数据初始化失败：' + response . info ) ; infiniteScroll . complete ( ) ; } } else { this . helper . presentToast ( '没有更多数据' ) ; infiniteScroll . enable ( false ) ; setTimeout ( ( ) => { infiniteScroll . enable ( true ) ; } , 1000 ) ; } } , ( err ) => { this . helper . presentToast ( '网络错误，数据初始化失败!' ) ; infiniteScroll . complete ( ) ; } ) ; } showIssuerPage ( ) { this . nav . push ( IssuerPage ) ; } }