import { asLiteral } from '@angular/compiler/src/render3/view/util';
import { Component, OnInit } from '@angular/core';
import { FormBuilder,FormControl, FormGroup,ValidationErrors, Validators } from '@angular/forms';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ActivatedRoute } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { User } from 'src/app/model/user';
import { environment } from 'src/environments/environment';
import { UserService } from '../../service/user.service';
import { WeixingService } from '../../service/weixing.service';
declare var wx: any;
@Component({
selector: 'app-user-info',
templateUrl: './user-info.component.html',
styleUrls: ['./user-info.component.css']
})
export class UserInfoComponent implements OnInit {
private protocal = environment.protocal
private host = environment.host
private port = environment.port
appid:string='wx39de1c568b28fb44';
code:string="";
openid:string="";
user:User|undefined;
title="";
desc=""
isReged=false;
validateForm: FormGroup ;
redirect_uri = `${environment.protocal}://${environment.host}/website/userInfo`;
constructor(
private cookieService: CookieService,
protected weixingService:WeixingService,
protected userService:UserService,
private fb: FormBuilder,
private route: ActivatedRoute,
private snackBar: MatSnackBar
) {
this.protocal = environment.protocal;
this.host = environment.host;
this.port = environment.port;
this.validateForm = this.fb.group({
userName: ['', [Validators.required]],
password: ['', [Validators.required]],
mobile: ['', [Validators.required]],
email: ['', [Validators.email, Validators.required]],
openId: ['', [Validators.email, Validators.required]],
});
}
ngOnInit() {
const that=this;
this.openid=this.cookieService.get('openid');
if(this.openid!=null&&this.openid!=""){
this.userService.getUserByOpenId(this.openid).subscribe(ret2=>{
if(ret2['code']==0&&ret2['user']!=null){
this.user=ret2['user'];
this.isReged=true;
this.title="用户信息";
this.desc="(该用户已注册)";
if(this.user){
this.validateForm.controls['userName'].setValue(this.user.userName);
this.validateForm.controls['mobile'].setValue(this.user.mobile);
this.validateForm.controls['email'].setValue(this.user.email);
this.validateForm.controls['openId'].setValue(this.openid);
}
}else{
this.isReged=false;
this.title="注册用户";
this.desc="(该微信用户还不是注册用户,请填写资料后,点击注册按钮)"
this.weixingService.getUserInfo(this.openid).subscribe(ret3=>{
if(ret3['code']==0&&ret3['userInfo']!=null){
const userInfo=ret3['userInfo'];
if(userInfo){
this.validateForm.controls['userName'].setValue(userInfo['nickname']);
this.validateForm.controls['openId'].setValue(this.openid);
}
}
});
}
});
}
}
regUser() {
if(this.validateForm.controls['userName'].value==""){
this.snackBar.open("用户名不能为空.");
return;
}
if(this.validateForm.controls['password'].value==""){
this.snackBar.open("密码不能为空.");
return;
}
if(this.validateForm.controls['mobile'].value==""){
this.snackBar.open("手机号码不能为空.");
return;
}
if(this.validateForm.controls['email'].value==""){
this.snackBar.open("电子邮箱不能为空.");
return;
}
for (const key in this.validateForm.controls) {
this.validateForm.controls[key].markAsDirty();
this.validateForm.controls[key].updateValueAndValidity();
}
this.user=new User();
this.user.userName = this.validateForm.controls['userName'].value;
this.user.password = this.validateForm.controls['password'].value;
this.user.mobile = this.validateForm.controls['mobile'].value;
this.user.openId=this.openid;
this.user.email = this.validateForm.controls['email'].value;
const ret = this.userService.regUser(this.user).subscribe(ret=>{
if(ret['code']==0){
this.isReged=true;
alert("注册成功.");
wx.closeWindow();
}else{
alert(ret['msg']);
}
});
return ret;
}
gotoMyDesktop(){
// this.getCurrentLocation();
wx.closeWindow();
}
private randomString(len:number) {
const charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var randomString = '';
for (var i = 0; i < len; i++) {
var randomPoz = Math.floor(Math.random() * charSet.length);
randomString += charSet.substring(randomPoz,randomPoz+1);
}
return randomString;
}
}
关闭微信窗口示例
最新推荐文章于 2024-04-26 17:03:05 发布