first commit
This commit is contained in:
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
9
.idea/goinim.iml
generated
Normal file
9
.idea/goinim.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/goinim.iml" filepath="$PROJECT_DIR$/.idea/goinim.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
23
go.mod
Normal file
23
go.mod
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
module goinim
|
||||||
|
|
||||||
|
go 1.23.1
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||||
|
github.com/go-redis/redis/v8 v8.11.5 // indirect
|
||||||
|
github.com/golang/snappy v0.0.4 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
|
github.com/klauspost/compress v1.13.6 // indirect
|
||||||
|
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||||
|
github.com/redis/go-redis/v9 v9.7.0 // indirect
|
||||||
|
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||||
|
github.com/xdg-go/scram v1.1.2 // indirect
|
||||||
|
github.com/xdg-go/stringprep v1.0.4 // indirect
|
||||||
|
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
|
||||||
|
go.mongodb.org/mongo-driver v1.17.1 // indirect
|
||||||
|
golang.org/x/crypto v0.26.0 // indirect
|
||||||
|
golang.org/x/sync v0.8.0 // indirect
|
||||||
|
golang.org/x/sys v0.23.0 // indirect
|
||||||
|
golang.org/x/text v0.17.0 // indirect
|
||||||
|
)
|
||||||
62
go.sum
Normal file
62
go.sum
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
|
||||||
|
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||||
|
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||||
|
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||||
|
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||||
|
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||||
|
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||||
|
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
|
||||||
|
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||||
|
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
|
||||||
|
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
|
||||||
|
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
|
||||||
|
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
|
||||||
|
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
|
||||||
|
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
|
||||||
|
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
|
||||||
|
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
|
||||||
|
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
|
||||||
|
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
|
||||||
|
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
|
||||||
|
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM=
|
||||||
|
go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||||
|
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||||
|
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c h1:7dEasQXItcW1xKJ2+gg5VOiBnqWrJc+rq0DPKyvvdbY=
|
||||||
|
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:NQtJDoLvd6faHhE7m4T/1IY708gDefGGjR/iUW8yQQ8=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||||
|
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
|
||||||
|
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||||
|
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||||
|
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
15
main.go
Normal file
15
main.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"goinim/send"
|
||||||
|
"goinim/user"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/registerverify", user.Registerverify)
|
||||||
|
http.HandleFunc("/register", user.Userregister)
|
||||||
|
http.HandleFunc("/login", user.Userlogin)
|
||||||
|
http.HandleFunc("/fasong", send.Fasong)
|
||||||
|
http.ListenAndServe("[::]:8081", nil)
|
||||||
|
}
|
||||||
79
send/send.go
Normal file
79
send/send.go
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package send
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
var up = websocket.Upgrader{
|
||||||
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
}
|
||||||
|
var useronline = make(map[string]*websocket.Conn)
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
Receiver string `json:"receiver"`
|
||||||
|
Poster string `json:"poster"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
Time string `json:"time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fasong(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
connect, error := up.Upgrade(w, r, nil)
|
||||||
|
if error != nil {
|
||||||
|
log.Println(error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
connect.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
Username := r.URL.Query().Get("username")
|
||||||
|
if Username == "" {
|
||||||
|
log.Println("没有该用户")
|
||||||
|
connect.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("用户" + Username + "登录至服务器")
|
||||||
|
|
||||||
|
useronline[Username] = connect
|
||||||
|
|
||||||
|
for {
|
||||||
|
msgtype, msg, error := connect.ReadMessage()
|
||||||
|
if error != nil {
|
||||||
|
if websocket.IsUnexpectedCloseError(error, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||||
|
fmt.Printf("错误:%s 用户:%s退出服务器\n", error, Username)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if msgtype == websocket.TextMessage {
|
||||||
|
var message Message
|
||||||
|
fmt.Println(message)
|
||||||
|
|
||||||
|
if error := json.Unmarshal(msg, &message); error != nil {
|
||||||
|
log.Println("解析失败:", error)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Println(message)
|
||||||
|
|
||||||
|
if receiverconnect, ok := useronline[message.Receiver]; ok {
|
||||||
|
sendmessage, _ := json.Marshal(message)
|
||||||
|
if error := receiverconnect.WriteMessage(websocket.TextMessage, []byte(sendmessage)); error != nil {
|
||||||
|
log.Println("信息发送失败")
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if msgtype == websocket.BinaryMessage {
|
||||||
|
fmt.Println(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if msgtype == websocket.BinaryMessage {
|
||||||
|
fmt.Println(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
user/123
Normal file
14
user/123
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#定又训练亟
|
||||||
|
def train():
|
||||||
|
model.train()
|
||||||
|
for i, data in enumerate(train loader):
|
||||||
|
#获得一个batch的数据和对应的标签
|
||||||
|
inputs,labels = data
|
||||||
|
inputs,labels =inputs.to(device),labels.to(device)#将数据移到GPU或CPU上
|
||||||
|
#数据正向传播,(64,8)需要补全以下代码!!!
|
||||||
|
#计算代价(误差),out(batch,c),labels(batch)
|
||||||
|
需要补全以下代码!!
|
||||||
|
# 梯度凊θ
|
||||||
|
optimizer.zero grad()#误差反向传播---需要补全以下代码!!
|
||||||
|
#更新网络模型爹数
|
||||||
|
optimizer.step()
|
||||||
1
user/userforget.go
Normal file
1
user/userforget.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package user
|
||||||
90
user/userlogin.go
Normal file
90
user/userlogin.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Userloginstruct struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Loginresponse struct {
|
||||||
|
Success string `json:"success"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Token string `json:"token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Userlogin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body, error := io.ReadAll(r.Body)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println("数据读取失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(string(body))
|
||||||
|
var userinfo Userloginstruct
|
||||||
|
error = json.Unmarshal(body, &userinfo)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println("数据解析失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mongocli := options.Client().ApplyURI("mongodb://localhost:27017")
|
||||||
|
|
||||||
|
mongoclient, error := mongo.Connect(context.TODO(), mongocli)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println("数据库连接失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
finduser := mongoclient.Database("user").Collection("userlist")
|
||||||
|
|
||||||
|
filter := bson.M{"email": userinfo.Email, "password": userinfo.Password}
|
||||||
|
|
||||||
|
type theuser struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var oneuser theuser
|
||||||
|
|
||||||
|
error = finduser.FindOne(context.Background(), filter).Decode(&oneuser)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println("未找到该用户")
|
||||||
|
loginres := Loginresponse{
|
||||||
|
Success: "no",
|
||||||
|
Message: "登录失败",
|
||||||
|
Token: "0",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(loginres)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
loginres := Loginresponse{
|
||||||
|
Success: "yes",
|
||||||
|
Message: "登录成功",
|
||||||
|
Token: "1",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(loginres)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
133
user/userregister.go
Normal file
133
user/userregister.go
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Userreg struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Verifycode string `json:"verifycode"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Userregister(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body, error := io.ReadAll(r.Body)
|
||||||
|
if error != nil {
|
||||||
|
log.Println("数据读取失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var userinfo Userreg
|
||||||
|
error = json.Unmarshal(body, &userinfo)
|
||||||
|
if error != nil {
|
||||||
|
log.Println("数据读取失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var username = userinfo.Username
|
||||||
|
var password = userinfo.Password
|
||||||
|
var useremail = userinfo.Email
|
||||||
|
var verifycode = userinfo.Verifycode
|
||||||
|
|
||||||
|
mongodbcli := options.Client().ApplyURI("mongodb://localhost:27017").SetMaxPoolSize(100)
|
||||||
|
|
||||||
|
mongodbclient, error := mongo.Connect(context.TODO(), mongodbcli)
|
||||||
|
if error != nil {
|
||||||
|
log.Println("数据库连接失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
findtheoneuser := bson.M{"username": username}
|
||||||
|
|
||||||
|
finduser := mongodbclient.Database("users").Collection("userlist")
|
||||||
|
_, error = finduser.Find(context.Background(), findtheoneuser)
|
||||||
|
if error == nil {
|
||||||
|
log.Println("该用户名已存在")
|
||||||
|
loginres := Loginresponse{
|
||||||
|
Success: "no",
|
||||||
|
Message: "注册失败,用户已经存在",
|
||||||
|
Token: "2",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(loginres)
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
findtheoneemail := bson.M{"email": useremail}
|
||||||
|
_, error = finduser.Find(context.Background(), findtheoneemail)
|
||||||
|
if error == nil {
|
||||||
|
log.Println("该邮箱已存在")
|
||||||
|
loginres := Loginresponse{
|
||||||
|
Success: "no",
|
||||||
|
Message: "注册失败",
|
||||||
|
Token: "3",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(loginres)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
redisdb := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
Password: "",
|
||||||
|
DB: 0,
|
||||||
|
})
|
||||||
|
|
||||||
|
findveri, error := redisdb.HGet(context.Background(), "userverify", useremail).Result()
|
||||||
|
if error != nil {
|
||||||
|
log.Println("验证码验证失败")
|
||||||
|
loginres := Loginresponse{
|
||||||
|
Success: "no",
|
||||||
|
Message: "注册失败,验证码错误",
|
||||||
|
Token: "4",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(loginres)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if findveri != verifycode {
|
||||||
|
log.Println("验证码验证失败")
|
||||||
|
loginres := Loginresponse{
|
||||||
|
Success: "no",
|
||||||
|
Message: "注册失败,验证码错误",
|
||||||
|
Token: "4",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(loginres)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, error = finduser.InsertOne(context.Background(), bson.M{"username": username, "password": password, "email": useremail})
|
||||||
|
if error != nil {
|
||||||
|
log.Println("数据插入失败")
|
||||||
|
loginres := Loginresponse{
|
||||||
|
Success: "no",
|
||||||
|
Message: "用户数据错误",
|
||||||
|
Token: "5",
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(loginres)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
127
user/userregisterverify.go
Normal file
127
user/userregisterverify.go
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/smtp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Userregveri struct {
|
||||||
|
Email string `json:"email"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func randomCode() string {
|
||||||
|
source := rand.NewSource(time.Now().UnixNano())
|
||||||
|
r := rand.New(source)
|
||||||
|
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
||||||
|
b := make([]rune, 6)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = letters[r.Intn(len(letters))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Registerverify(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173")
|
||||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||||
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||||
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
|
||||||
|
if r.Method == "OPTIONS" {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bodyrec, error := io.ReadAll(r.Body)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println("接受数据错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var bodyjx Userregveri
|
||||||
|
error = json.Unmarshal(bodyrec, &bodyjx)
|
||||||
|
if error != nil {
|
||||||
|
fmt.Println("数据解析失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var useremail = bodyjx.Email
|
||||||
|
log.Println("邮箱是", useremail)
|
||||||
|
|
||||||
|
redisdb := redis.NewClient(&redis.Options{
|
||||||
|
Addr: "localhost:6379",
|
||||||
|
Password: "",
|
||||||
|
DB: 0,
|
||||||
|
})
|
||||||
|
var redisverifyhash = "userverify"
|
||||||
|
|
||||||
|
_, error = redisdb.Ping(context.Background()).Result()
|
||||||
|
if error != nil {
|
||||||
|
log.Println("redis连接失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, _ := redisdb.Exists(context.Background(), redisverifyhash, useremail).Result()
|
||||||
|
if result == 1 {
|
||||||
|
log.Println("用户验证码已经存在")
|
||||||
|
redisdb.HDel(context.Background(), redisverifyhash, useremail)
|
||||||
|
}
|
||||||
|
log.Println(result)
|
||||||
|
codesend := randomCode()
|
||||||
|
|
||||||
|
error = redisdb.HSet(context.Background(), redisverifyhash, map[string]string{
|
||||||
|
"useremail": useremail,
|
||||||
|
"userver": codesend,
|
||||||
|
}).Err()
|
||||||
|
if error != nil {
|
||||||
|
log.Println("redis键值设置失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, error = redisdb.Expire(context.Background(), redisverifyhash, 3*time.Minute).Result()
|
||||||
|
if error != nil {
|
||||||
|
log.Println("有效时长设置失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
from := "mayiming_person@163.com"
|
||||||
|
password := "VChadvNigu6TdzYH"
|
||||||
|
to := []string{useremail}
|
||||||
|
subject := "MS用户验证码 --MS助手"
|
||||||
|
|
||||||
|
htmlContent := `以下为您的验证码,3分钟内有效,请勿分享给别人. 验证码:` + string(codesend) + ``
|
||||||
|
t, err := template.New("emailBody").Parse(htmlContent)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
var bodyBuilder strings.Builder
|
||||||
|
err = t.Execute(&bodyBuilder, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
body := bodyBuilder.String()
|
||||||
|
|
||||||
|
msg := "From: " + from + "\r\n" +
|
||||||
|
"To: " + to[0] + "\r\n" +
|
||||||
|
"Subject: " + subject + "\r\n" +
|
||||||
|
"\r\n" +
|
||||||
|
body
|
||||||
|
|
||||||
|
smtpServer := "smtp.163.com:25"
|
||||||
|
auth := smtp.PlainAuth("", from, password, "smtp.163.com")
|
||||||
|
err = smtp.SendMail(smtpServer, auth, from, to, []byte(msg))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("邮箱发送成功!")
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user