Guest User

Untitled

a guest
Mar 2nd, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import {TabsPage} from '../tabs/tabs';
  2. import {LoginPage} from '../login/login';
  3. import { Component, Input } from '@angular/core';
  4. import { NavController } from 'ionic-angular';
  5. import { Storage } from '@ionic/storage';
  6.  
  7. import { User } from '../../model/user';
  8. import { UserService } from '../../service/user.service';
  9. import {Md5} from 'ts-md5/dist/md5';
  10.  
  11. import firebase from 'firebase'
  12.  
  13. declare var require: any;
  14. let QRCode = require('qrcode');
  15.  
  16.  
  17. @Component({
  18. selector: 'page-signup',
  19. templateUrl: 'signup.html'
  20. })
  21. export class SignupPage {
  22.  
  23. @Input('qrc-value') value = 'https://www.techiediaries.com';
  24. @Input('qrc-version') version : '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10' | '11' | '12' | '13' | '14' | '15' | '16' | '17' | '18' | '19' | '20' | '21' | '22' | '23' | '24' | '25' | '26' | '27' | '28' | '29' | '30' | '31' | '32' | '33' | '34' | '35' | '36' | '37' | '38' | '39'|'40' | '' = '';
  25.  
  26. @Input('qrc-errorCorrectionLevel') errorCorrectionLevel : 'L' | 'M' | 'Q' | 'H' = 'M';
  27.  
  28. user:User=new User();
  29. error:string;
  30. constructor(public navCtrl: NavController,public storage:Storage,private userService:UserService){
  31. }
  32. signup(){
  33. // this.userService.pushUser(this.user);
  34. if(this.matchPassword(this.user)){
  35. console.log("Error :Don't metch password");
  36. this.error ="Don't metch between Password and confirm password";
  37. }else if(this.user.password.length<6){
  38. console.log("Error : passwrod short");
  39. this.error ="Password is too short (minimum is 6 characters).";
  40. }else if(this.checkValidEmail(this.user.email)){
  41. console.log("Error : Already has this email address ");
  42. this.error ="Already has this email address in out system";
  43. }else{
  44. this.deleteconfirm(this.user);
  45. var pass=Md5.hashStr(this.user.password);
  46. this.user.password=pass.toString();
  47. if(this.userService.pushUser(this.user)){
  48. this.storage.get('id').then((val) => {
  49. // this.user=db.list('User').valueChanges();
  50. // console.log("user id: ",val)
  51. if (val != this.user.id) {
  52. this.user.add_date = new Date().toDateString();
  53. console.log(this.user);
  54. this.toDataURL().then((v : string)=>{
  55. var base64parts = v.split(',');
  56. base64parts[0] = "base64:Qrcode.png";
  57. var imagedata = base64parts.join("");
  58. var storageRef = firebase.storage().ref();
  59. var baseQR = storageRef.child('base64:Qrcode.png');
  60. this.navCtrl.push(TabsPage);
  61. }).catch((e)=>{
  62. console.error(e);
  63. })
  64. }
  65. }
  66. )}
  67.  
  68.  
  69. }
  70. }
  71.  
  72. matchPassword(group): boolean {
  73. if (group.password==group.password2) {
  74. return false;
  75. }
  76. return true;
  77. }
  78. deleteconfirm(group):any{
  79. delete group.password2;
  80. }
  81. checkValidEmail(email):any{
  82. let result=this.userService.getUserByEmail(email);
  83. console.log(result);
  84. result.subscribe((k:User[])=>{
  85. if(k.length>0){
  86. return false;
  87. }else{
  88. return true;
  89. }
  90. });
  91. }
  92. toDataURL(){
  93. return new Promise((resolve,reject)=>{
  94. QRCode.toDataURL(this.value, { version : this.version , errorCorrectionLevel : this.errorCorrectionLevel } ,function (err, url) {
  95. if(err){
  96. console.error(err);
  97. reject(err);
  98. }
  99. else{
  100. console.log(url);
  101. resolve(url);
  102. }
  103. })
  104. });
  105. }
  106. }
  107.  
  108. import { Injectable } from '@angular/core';
  109. import { Observable } from 'rxjs/Observable';
  110.  
  111. import { AngularFireDatabase } from 'angularfire2/database';
  112.  
  113. import { User } from ".././model/user";
  114.  
  115.  
  116. @Injectable()
  117. export class UserService {
  118. constructor(public db:AngularFireDatabase){
  119.  
  120. }
  121. getUsers():Observable<User[]>{
  122. console.log("User Service : get users list");
  123. return this.db.list('User').valueChanges();;
  124. }
  125. getUser(id:number):Observable<User[]>{
  126. console.log("User Service : get user");
  127. return this.db.list('User',ref => ref.orderByChild('id').equalTo(id)).valueChanges();
  128. }
  129. getUserByEmail(email:string):Observable<User[]>{
  130. console.log("User Service : get user");
  131. return this.db.list('User',ref => ref.orderByChild('email').equalTo(email)).valueChanges();
  132. }
  133. pushUser(data:User):boolean{
  134. var aRef = this.db.list('User').push(null);
  135. console.log(aRef.key);
  136. data.id=aRef.key;
  137. aRef.set(data);
  138. return true
  139. }
  140.  
  141. pushQR(data:User):string{
  142. console.log("Student Service: Add new student information");
  143. var aRef = this.db.list('Students').push(null);
  144. console.log(aRef.key);
  145. data.qr_code=aRef.key;
  146. aRef.set(data);
  147. return aRef.key
  148. }
  149.  
  150. pushQRimage(qr:User):string{
  151. console.log("Student Service: Add new student information");
  152. this.db.list('QR').push(qr.urlqr);
  153. return qr.urlqr
  154. }
  155.  
  156. updatehUser(data:User):boolean{
  157. console.log(JSON.stringify(data));
  158. this.db.list('User').set(data.id+"",data);
  159. return true
  160. }
  161. }
Add Comment
Please, Sign In to add comment