关闭微信窗口示例

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;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值