Skip to content

Instantly share code, notes, and snippets.

@kanon755056
Created April 2, 2018 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanon755056/dba83d5075f7db9df98882943f4f1fa3 to your computer and use it in GitHub Desktop.
Save kanon755056/dba83d5075f7db9df98882943f4f1fa3 to your computer and use it in GitHub Desktop.
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { ScreenOrientation } from '@ionic-native/screen-orientation';
import * as moment from 'moment';
import { CouponArray } from '../../providers/couponArray';
import { Dandom_Fn } from '../../providers/random';
/**
* Generated class for the CouponPage page.
*
* See https://ionicframework.com/docs/components/#navigation for more info on
* Ionic pages and navigation.
*/
@IonicPage()
@Component({
selector: 'page-coupon',
templateUrl: 'coupon.html',
})
export class CouponPage {
nowTime: any;
couponTime: any;
invalidTimeOut: any;
cleanTimeOut: any;
timeText: {[k: string]: any} = {};
intervalSecond: any;
coupon: any;
couponArr: any;
intervalElement: any;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public statusBar: StatusBar,
public screenOrientation: ScreenOrientation,
public getCouponArr: CouponArray,
public dandom_Fn: Dandom_Fn,
) {
// this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT_PRIMARY)
/***************************************************************
* get password
***************************************************************/
let password = this.navParams.get('password');
console.log('password =', password)
if(!password){
password = 756;
}
/***************************************************************
* set every second
***************************************************************/
var everySecond = 1 * 1000;
/***************************************************************
* get coupon array
***************************************************************/
// let coupons;
let coupons = getCouponArr.re();
this.couponArr = [];
// this.couponArr = getCouponArr.re();
// console.log('CouponArray =', this.couponArr)
var arrLength
if(String(password).length == 3){
// coupons = getCouponArr.re();
// this.couponArr = [];
// this.couponArr = getCouponArr.re();
// console.log('CouponArray =', this.couponArr)
for(let i = 0; i < 6; i++){
this.couponArr.push(coupons[this.dandom_Fn.randomIntFromInterval(0, coupons.length -1)])
}
}else if(String(password).length > 3){
if(String(password).length > 6){
arrLength = 6;
}else {
arrLength = String(password).length
}
for(let i = 3; i < arrLength; i++){
// console.log(String(password).substring(i, i + 1))
let index = String(password).substring(i, i + 1)
this.couponArr.push(coupons[Number(index)])
}
for(let i = 0; i < 4; i++){
this.couponArr.push(coupons[this.dandom_Fn.randomIntFromInterval(0, coupons.length -1)])
}
}
console.log('this.couponArr =', this.couponArr)
// this.timeText = "AAAAAAAA"
// statusBar.overlaysWebView(true);
/***************************************************************
* set statusBar background color & font color
***************************************************************/
statusBar.styleBlackTranslucent();
statusBar.backgroundColorByHexString('#de1403')
/***************************************************************
* set time
***************************************************************/
// this.nowTime = new Date();
// this.nowTime = moment();
this.nowTime = moment();
console.log('time =', this.nowTime)
console.log('time =', String(this.nowTime))
console.log('time =', String(this.nowTime).substring(11, 15))
// this.cleanTimeOut = moment().add(3, 'seconds');
// this.invalidTimeOut = moment().add(7, 'seconds');
this.couponTime = moment(this.nowTime).add(3, 'days');
this.cleanTimeOut = moment(this.nowTime).add(3, 'days');
this.invalidTimeOut = moment(this.nowTime).add(7, 'days');
// let testTime = moment.duration(this.cleanTimeOut);
// console.log('testTime =', testTime)
// this.invalidTimeOut = moment().add(3, 'seconds');
// this.invalidTimeOut = moment.max(this.nowTime, this.invalidTimeOut);
console.log('invalidTimeOut =', this.invalidTimeOut)
console.log('invalidTimeOut =', String(this.invalidTimeOut))
// console.log('cleanTimeOut =', this.cleanTimeOut);
// console.log('invalidTimeOut =', this.invalidTimeOut)
// console.log('7 - 3 day =', moment(this.invalidTimeOut).subtract(this.cleanTimeOut))
// console.log('7 - 3 day =', String(this.invalidTimeOut))
this.intervalSecond = 0;
// var regexAgo = /ago&/;
// var regex;
/***************************************************************
* set random hours
***************************************************************/
let diffTime = this.dandom_Fn.randomIntFromInterval(0, 23)
this.cleanTimeOut = moment(this.cleanTimeOut).subtract(diffTime, 'hours')
this.invalidTimeOut = moment(this.invalidTimeOut).subtract(diffTime, 'hours')
/***************************************************************
* set Interval
***************************************************************/
this.intervalElement = setInterval(() => {
this.intervalSecond += 1;
// console.log('invalidTimeOut =', this.invalidTimeOut)
// console.log(moment.max(moment(), this.invalidTimeOut))
// console.log(moment.max(moment(), this.invalidTimeOut).isValid())
this.nowTime = this.nowTime.add(1, 'seconds')
// let momentLet = moment(this.nowTime).to(this.cleanTimeOut)
// let hours = moment(this.nowTime).to(this.cleanTimeOut)
console.log('this.nowTime =', this.nowTime)
// console.log(moment(this.nowTime).to(this.cleanTimeOut))
/***************************************************************
* set Array Add Time
***************************************************************/
let arrayTimeDiff = 0;
if(this.couponArr.length > 4){
arrayTimeDiff += (this.couponArr.length - 4)
}
let ArrayTime = this.nowTime;
/***************************************************************
* Moment Diff Judgment
***************************************************************/
let cleanTimeOut = this.cleanTimeOut
let invalidTimeOut = this.invalidTimeOut
let maxTime = moment(this.invalidTimeOut).subtract(4, 'days');
// console.log('cleanTimeOut =', cleanTimeOut)
// console.log('invalidTimeOut =', invalidTimeOut)
// console.log('maxTime =', maxTime)
for(let key in this.couponArr){
if(arrayTimeDiff > 0) {
arrayTimeDiff -= 1
}else {
// ArrayTime = moment(ArrayTime).add(1, 'days');
cleanTimeOut = moment(cleanTimeOut).subtract(1, 'days');
invalidTimeOut = moment(invalidTimeOut).subtract(1, 'days');
maxTime = moment(maxTime).subtract(1, 'days');
// console.log('ArrayTime =', ArrayTime)
}
// console.log('maxTime =', String(maxTime))
let momentDiff = moment(cleanTimeOut).diff(ArrayTime);
// console.log('diff =', momentDiff);
let dayLife = 0;
let hourLife = 0;
if(momentDiff > 0){
dayLife = Math.floor(((momentDiff/1000)/3600)/24)
hourLife = Math.floor(((momentDiff/1000)/3600)%24)
}
// console.log('dayLife =', dayLife)
// console.log('hourLife =', hourLife)
// let secondLife = Math.floor((momentDiff/1000)%60)
console.log('dayLife =', dayLife)
console.log('hourLife =', hourLife)
// console.log('maxTime =', String(maxTime))
this.setTimeText(key, maxTime, dayLife, hourLife)
// this.couponArr[key].timeText =
// '期限:' +
// String(maxTime).substring(11, 15) +
// ' 年 ' +
// this.monthJudgment(String(maxTime).substring(4, 7)) +
// ' 月 ' +
// String(maxTime).substring(8, 10) +
// ' 日 尚餘:' +
// dayLife +
// ' 天 ' +
// hourLife +
// ' 小時'
// console.log('this.intervalSecond =', this.intervalSecond)
// console.log(moment.duration(this.intervalSecond, 'seconds'))
console.log('key =', key)
console.log('this.couponArr =', this.couponArr[key])
// console.log('timeText =', this.timeText.text)
console.log('this.couponArr[' + key + '].timeText = ', this.couponArr[key].timeText)
console.log('this.couponArr =', this.couponArr)
// if(moment(ArrayTime).isBetween(cleanTimeOut, invalidTimeOut)){
// console.log('isBetween')
// this.couponArr[key].valid = false;
// }else if(moment(ArrayTime).isSameOrBefore(invalidTimeOut)){
// console.log('isSameOrBefore')
// }else{
// console.log('invalid')
// this.couponArr[key].valid = false;
// }
}
// console.log('this.couponArr =', this.couponArr)
// console.log(moment(this.nowTime).isSameOrBefore(this.invalidTimeOut));
}, everySecond)
}
ionViewDidLoad() {
console.log('ionViewDidLoad CouponPage');
}
ionViewWillLeave() {
if(this.intervalElement)
clearInterval(this.intervalElement);
}
monthJudgment(month){
switch(month){
case 'Jan':{return 1;};
case 'Feb':{return 2;};
case 'Mar':{return 3;};
case 'Apr':{return 4;};
case 'May':{return 5;};
case 'Jun':{return 6;};
case 'Jul':{return 7;};
case 'Aug':{return 8;};
case 'Sep':{return 9;};
case 'Oct':{return 10;};
case 'Nov':{return 11;};
case 'Dec':{return 12;};
default :{break;};
}
}
setTimeText(key, maxTime, dayLife, hourLife) {
console.log('maxTime =', String(maxTime))
this.couponArr[key].timeText =
'期限:' +
String(maxTime).substring(11, 15) +
' 年 ' +
this.monthJudgment(String(maxTime).substring(4, 7)) +
' 月 ' +
String(maxTime).substring(8, 10) +
' 日 尚餘:' +
dayLife +
' 天 ' +
hourLife +
' 小時'
}
// weekJudgment(week){
// switch(week){
// case :{
// break;
// }
// default :{
// break;
// }
// }
// }
useCoupon(event) {
console.log('click useCoupon')
console.log('event =', event)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment