ng8 go管理

一、angular

1、总目录
ng8 go管理_第1张图片
2、代码

manage.comment.html

<div class="friends">
    <div class="friendword">
        Pagge~hello,
        <button>
            <a [routerLink]="['/friends', userid]">返回a>
        button>
    div>
    <div class="friendlist">
         <div *ngFor="let users of userlist index as i"  class="friendlist-one"  >
            <hr>
            
            <dl>
                <dt>用户名:
                    <input type="text"  [(ngModel)]="upname[i]"  placeholder="{
      {users.userfriendname}}" >
                dt>
                <dt>手机号:
                    <input type="text"   [(ngModel)]="upphone[i]"   placeholder="{
      {users.userfriendphone}}">
                dt>
                <dt>  邮箱  :
                    <input type="text"   [(ngModel)]="upmail[i]"  placeholder="{
      {users.userfriendmail}}">
                dt>
                <dt>  密码  :
                    <input type="text"    [(ngModel)]="uppassword[i]"  placeholder="{
      {users.userfriendpassword}}">
                dt>
            dl>
            
            <button  (click)="userdelete(users.userfriendid)">
                删除该用户
            button>
            <button  (click)="userupdate(i, users.userfriendid)">
                确认修改
            button>
        div>
            
        <hr>
        <div>
            <dl>
                <dt>用户名:
                    <input type="text" placeholder="由数字,字母,下划线组成的10位字符" [(ngModel)]="newname" >
                dt>
                <dt>手机号:
                    <input type="text" placeholder="中国大陆手机号" [(ngModel)]="newphone" >
                dt>
                <dt>  邮箱  :
                    <input type="text" placeholder="***@域名" [(ngModel)]="newmail" >
                dt>
                <dt>  密码  :
                    <input type="text" placeholder=""  [(ngModel)]="newpassword" >
                dt>
            dl>
            <button (click)="newuser()">
                创建该用户
            button>
        div>
    div>
div>

manage.comment.scss

h2{
     

    text-align: left;

}

.friends{
     

    width: 440px;

    height: 550px;

    margin: 100px auto;

    background-color: #ffffff42;

    padding: 15px;

    border: 1px solid #0066da;

    img{
     

        // max-width:100px;

        max-width:50px;

    }

}

button{
     

    width: 83px;

    height: 26px;

    float: right;

    margin: 4px;

}

.friendword{
     

    font-size: 25px;

    font-weight: bold;

    width: 423px;

    height: 48px;

    padding: 7px;

    border: 1px solid #ffffff;

    background-color: #ffffff;

}

  

.friendlist{
     

    width: 394px;

    height: 417px;

    background-color: #fefeff;

    padding: 15px;

    border: 1px solid #15070736;

    margin: 13px 5px 0px 6px;

    overflow: scroll;

    overflow-x: hidden;

}

.friendlist-one{
     

    width: 370px;

    height: 220px;

}

dt{
     

    margin: 4px 0px 4px 0px;

}

manage.comment.ts

import {
      Component, OnInit } from '@angular/core';
import {
      CommonService } from '../../services/common.service';
import {
      ActivatedRoute } from '@angular/router';
// import {ChangeDetectorRef, ChangeDetectionStrategy } from '@angular/core';

@Component({
     
  selector: 'app-manage',
  templateUrl: './manage.component.html',
  styleUrls: ['./manage.component.scss'],
})
export class ManageComponent implements OnInit {
     

  constructor(//public changeDetectorRef:ChangeDetectorRef,
                          public route:ActivatedRoute,
                          public common:CommonService) {
     
  }

  public userlist:any[]=[];
  public userid:any=""
  ngOnInit() {
     
    this.route.params.subscribe((value:any)=>{
     
      this.userid=value.id
      this.userShowfriends(value.id)
    })
  }

  userShowfriends(id){
     
    this.common.show(id).then((Response:any)=>{
     
      console.log(Response);
      if(Response.status==-1){
     
          alert("获取用户列表失败")
          return
      }
      this.userlist = Response.userfriendList
    })
    return
  }

  userdelete(fid){
     
    if(fid==this.userid){
     
      alert("错误,不能删除自己")
      return
    }
    this.common.delete(fid).then((Response:any)=>{
     
      console.log(Response);
      if(Response.status==0){
     
        // this.link="['/friends?id="+Response.id+"']"
        alert("已删除该用户以及相关聊天记录")
      }else{
     
        alert("删除失败 : "+Response.msg)
      }
    })
    window.location.reload()

    // this.changeDetectorRef.markForCheck();
    // this.changeDetectorRef.detectChanges();
    return 
  }

  public newname=""
  public newphone=""
  public newmail=""
  public newpassword=""
  newuser(){
     
    this.common.register(this.newname, this.newphone, this.newmail, this.newpassword).then((Response:any)=>{
     
      console.log(Response);
      if(Response.status==0){
     
        alert("添加用户成功")
      }else{
     
        alert("添加用户失败:"+Response.msg)
      }
    })
    window.location.reload()
    // this.changeDetectorRef.markForCheck();
    // this.changeDetectorRef.detectChanges();
  }
  

  public upname:any=[]
  public upphone:any=[]
  public upmail:any=[]
  public uppassword:any=[]
  setvalue(i, dname, dphone, dmail, dpassword){
     
    this.upname[i]=dname
    this.upphone[i]=dphone
    this.upmail[i]=dmail
    this.uppassword[i]=dpassword
  }
  userupdate(i, fid){
     
    console.log("i : "+i)
    this.common.update(this.upname[i], this.upphone[i], this.upmail[i], this.uppassword[i], fid).then((Response:any)=>{
     
      console.log(Response);
      if(Response.status==0){
     
        alert("更新成功")
      }else{
     
        alert("更新失败:"+Response.msg)
        return
      }
    })
    window.location.reload()
  }

  
}

app.module.ts

comment.service.ts

参考博文 《 ng8 go 图片&聊天》

3、运行效果
ng8 go管理_第2张图片

4、bug

本页面未实现轮询。

二、golang

1、总目录
(数据库为 postgresql)
ng8 go管理_第3张图片
2、代码

friendgetchadel.go

package servewr

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	// 使用相对路径引用dbtable
	"../dbtable"
)

// ShowResp show接口的响应
type ShowResp struct {
     
	Status         int                      `json:"status"`
	UserfriendList []dbtable.UserfriendList `json:"userfriendList"` // 消息列表
}

// Show 好友列表接口的处理函数
func Show(w http.ResponseWriter, r *http.Request) {
     
	w.Header().Set("Access-Control-Allow-Origin", "*")                          //允许访问所有域
	w.Header().Add("Access-Control-Allow-Headers", "Content-TypeAuthorization") //header的类型
	w.Header().Set("content-type", "application/json")

	// 只处理POST请求
	if r.Method != "POST" {
     
		return
	}

	resp := ShowResp{
     }

	// 读取请求实体中的数据
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
     
		log.Printf("read http request failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	if body == nil || len(body) == 0 {
     
		log.Println("http request body is null")
		sendErrToClient("http request body is null", w)
		return
	}

	defer r.Body.Close()

	data := make(map[string]interface{
     })

	// json反序列化请求实体中的数据
	if err = json.Unmarshal(body, &data); err != nil {
     
		log.Printf("json unmarshal failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	if data == nil {
     
		log.Println("json unmarshal data is null")
		sendErrToClient("json unmarshal data is null", w)
		return
	}

	// 类型断言
	userid, ok := data["userid"].(string)
	if !ok {
     
		log.Println("type assert failed")
		sendErrToClient("type assert failed", w)
		return
	}

	if userid == "" {
     
		log.Println("the userid from http request is empty string")
		sendErrToClient("the userid from http request is empty string", w)
		return
	}

	// 根据id从数据库读取好友列表
	userfriendList, err := dbtable.GetUserfriendList(userid)
	if err != nil {
     
		log.Printf("getfriend list failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	resp.Status = 0
	resp.UserfriendList = userfriendList

	for _, v := range userfriendList {
     
		fmt.Printf("name: %s,  phone:%s ,  mail: %s ,  password:%s\n", v.Userfriendname, v.Userfriendphone, v.Userfriendmail, v.Userfriendpassword)
	}

	// json序列化receive接口的响应
	respByte, err := json.Marshal(resp)
	if err != nil {
     
		log.Printf("json marshal failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	// 响应数据
	_, err = w.Write(respByte)
	if err != nil {
     
		log.Printf("write response failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}
}

//DeleteResp  delete接口的响应
type DeleteResp struct {
     
	Status int `json:"status"`
}

//Delete  删除用户接口处理函数
func Delete(w http.ResponseWriter, r *http.Request) {
     
	w.Header().Set("Access-Control-Allow-Origin", "*")                          //允许访问所有域
	w.Header().Add("Access-Control-Allow-Headers", "Content-TypeAuthorization") //header的类型
	w.Header().Set("content-type", "application/json")
	// 如果请求类型不是POST, 不处理该请求
	if r.Method != "POST" {
     
		return
	}

	resp := &DeleteResp{
     
		Status: 0,
	}
	// 读取请求实体中的数据
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
     
		log.Printf("read http request failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	if body == nil || len(body) == 0 {
     
		log.Println("http request body is null")
		sendErrToClient("http request body is null", w)
		return
	}

	defer r.Body.Close()

	data := make(map[string]interface{
     })

	// json反序列化请求的数据
	if err = json.Unmarshal(body, &data); err != nil {
     
		log.Printf("json unmarshal failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	// 类型断言
	userfriendid, ok := data["userfriendid"].(string)
	if !ok {
     
		log.Println("type assertion failed")
		sendErrToClient("type assertion failed", w)
		return
	}

	if userfriendid == "" {
     
		log.Println("the userfriendid  is empty string")
		sendErrToClient("the userfriendid is empty string", w)
		return
	}

	err = dbtable.DeleteUserFromDB(userfriendid)
	if err != nil {
     
		log.Printf("Delete  User  From  DB failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	// 返回成功状态码0
	resp.Status = 0

	// json序列化login接口的响应
	respByte, err := json.Marshal(resp)
	if err != nil {
     
		log.Printf("json marshal failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	// 响应数据
	_, err = w.Write(respByte)
	if err != nil {
     
		log.Printf("write the response failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	return
}

//UpdateResp  update接口的响应
type UpdateResp struct {
     
	Status int `json:"status"`
}

// Update 更新接口
func Update(w http.ResponseWriter, r *http.Request) {
     
	w.Header().Set("Access-Control-Allow-Origin", "*")                          //允许访问所有域
	w.Header().Add("Access-Control-Allow-Headers", "Content-TypeAuthorization") //header的类型
	w.Header().Set("content-type", "application/json")
	// 如果请求类型不是POST, 不处理该请求
	if r.Method != "POST" {
     
		return
	}

	resp := &UpdateResp{
     
		Status: 0,
	}
	// 读取请求实体中的数据
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
     
		log.Printf("read http request failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	if body == nil || len(body) == 0 {
     
		log.Println("http request body is null")
		sendErrToClient("http request body is null", w)
		return
	}

	defer r.Body.Close()

	data := make(map[string]interface{
     })

	// json反序列化请求的数据
	if err = json.Unmarshal(body, &data); err != nil {
     
		log.Printf("json unmarshal failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	// 类型断言
	username, ok := data["username"].(string)
	// if !ok {
     
	//     log.Println("type assertion failed")
	//     sendErrToClient("type assertion failed", w)
	//     return
	// }
	userphone, ok := data["userphone"].(string)
	// if !ok {
     
	//     log.Println("type assertion failed")
	//     sendErrToClient("type assertion failed", w)
	//     return
	// }
	usermail, ok := data["usermail"].(string)
	// if !ok {
     
	//     log.Println("type assertion failed")
	//     sendErrToClient("type assertion failed", w)
	//     return
	// }
	password, ok := data["password"].(string)
	// if !ok {
     
	//     log.Println("type assertion failed")
	//     sendErrToClient("type assertion failed", w)
	//     return
	// }

	userfriendid, ok := data["userfriendid"].(string)
	if !ok {
     
		log.Println("type assertion failed")
		sendErrToClient("type assertion failed", w)
		return
	}

	err = dbtable.UpdateUserInfo(username, userphone, usermail, password, userfriendid)
	if err != nil {
     
		log.Println(err)
		x := fmt.Sprintf("%s", err)
		sendErrToClient(x, w)
		return
	}

	// 返回成功状态码0
	resp.Status = 0

	// json序列化register接口的响应
	respByte, err := json.Marshal(resp)
	if err != nil {
     
		log.Printf("json marshal failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	// 响应数据
	_, err = w.Write(respByte)
	if err != nil {
     
		log.Printf("write the response failed, err: %v", err)
		sendErrToClient(err.Error(), w)
		return
	}

	return
}

你可能感兴趣的:(ng8 go管理)