ng8 go好友

一、angular

1、总目录
ng8 go好友_第1张图片
2、代码

friends.comment.html

<div class="friends">
     <div class="friendword">
        Pagge~hello,
        <div [class]="colorflag" >
            未读: {
    {readflag}}条
        div>
        <button  (click)="usergotoManage()">
            管理
        button>
        <button (click)="deleteautologin()">
            
            注销
        button>
    div>
    <div class="friendlist">
         <div *ngFor="let users of userlist">
            <a [routerLink]="['/chat', userid, users.userfriendid, users.userfriendname]">   
                 
                 {
    {users.userfriendname}}
                 <hr>
            a>
         div>
    div>


    
div>


friends.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;

    }

}



.friendword{
     

    font-size: 25px;

    font-weight: bold;

    width: 423px;

    height: 48px;

    padding: 7px;

    border: 1px solid #ffffff;

    background-color: #ffffff;

    button{
     

        width: 65px;

        height: 30px;

        float: right;

        margin: 4px;

   }

   .newsnumred{
     

    background-color: rgb(238, 145, 145);

    width: 35px;

    font-size: 10px;

    float: right;

    height: 35px;

   }

   .newsnumblue{
     

    background-color: rgb(255, 255, 255);

    width: 35px;

    font-size: 10px;

    float: right;

    height: 35px;

   }

}

  

.friendlist{
     

    width: 394px;

    height: 417px;

    background-color: #fefeff;

    padding: 15px;

    border: 1px solid #15070736;

    margin: 13px 5px 0px 6px;

    overflow: scroll;

    overflow-x: hidden;

}





a:link {
     

    font-size: 20px;

    color: #000000;

    text-decoration: none;

}

a:visited {
     

         font-size: 20px;

         color: #000000;

          text-decoration: none;

}

a:hover {
     

         font-size: 20px;

         color: #999999;

         text-decoration: underline;

}

friends.comment.ts

import {
      Component, OnInit } from '@angular/core';
import {
      CommonService } from '../../services/common.service';
import {
      ActivatedRoute } from '@angular/router';
import {
     Router} from '@angular/router';
import {
      CookieService } from 'ngx-cookie-service';

@Component({
     
  selector: 'app-friends',
  templateUrl: './friends.component.html',
  styleUrls: ['./friends.component.scss']
})
export class FriendsComponent implements OnInit {
     
  // public list:any[]=[];
  // public domain:string='';
  public userid=""
  constructor(private router:Router,
                            public cookieService: CookieService,
                            public route:ActivatedRoute,
                            public common:CommonService) {
     
    // this.domain=this.common.domain;
   }
  ngOnInit() {
     
    // var api='api/productlist';
    // this.common.get(api).then((Response:any)=>{
     
    //   console.log(Response);
    //   // this.list=Response.result;
    //   this.list=Response.result[0].list;
    // })
    this.route.params.subscribe((value:any)=>{
     
      this.userid=value.id
      this.userreadnum(value.id)
      this.userShowfriends(value.id)
    })
  }
  public colorflag="newsnumblue"
  public readflag
  userreadnum(id){
     
   this.common.check(id).then((Response:any)=>{
     
     console.log(Response);
     if(Response.status==0){
     
       // this.link="['/friends?id="+Response.id+"']"
       this.readflag = Response.num;
       if ( this.readflag!=0){
     
         this.colorflag = "newsnumred"
       }else{
     
        this.colorflag="newsnumblue"
       }
     }
     
   })
   return 
  }
  public userlist :any[]=[]
  userShowfriends(id){
     
    this.common.show(id).then((Response:any)=>{
     
      console.log(Response);
      if(Response.status==-1){
     
          alert("获取用户列表失败")
          return
      }
      this.userlist = Response.userfriendList
    })
    return
  }
  usergotoManage(){
     
    if (this.userid!="ee755bdd4adb8ac6270ef476fefab245"){
     
      alert("没有权限访问")
      return
    }
    this.router.navigate(['/manage', this.userid]);

  }
  deleteautologin(){
     
    console.log("删除cookie")
    this.cookieService.delete("userid");
    this.router.navigate(['/login']);
  }
}


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好友)