添加线程池
This commit is contained in:
@@ -49,38 +49,32 @@ func UserLogin(c *gin.Context) {
|
||||
fmt.Println("微信登录返回结果:", resp.Body)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 解析微信返回结果
|
||||
var wxResp usermodel.WxLoginResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&wxResp); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析微信登录响应失败: " + err.Error(), "code": "10026"})
|
||||
return
|
||||
}
|
||||
|
||||
// 检查微信返回的错误
|
||||
if wxResp.ErrCode != 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{ // http.StatusBadRequest 对应 400
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": fmt.Sprintf("微信登录失败: %s (错误码: %d)", wxResp.ErrCode, wxResp.ErrMsg),
|
||||
"code": "10027",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
//可选)使用session_key、encryptedData和iv解密用户信息
|
||||
// 步骤2:解密手机号(复用AES解密函数)
|
||||
phoneData, err := decryptWxData(wxResp.SessionKey, req.EncryptedData, req.Iv)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "解密失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 步骤3:解析为手机号结构体
|
||||
var phoneInfo usermodel.WxPhoneInfo
|
||||
if err := json.Unmarshal(phoneData, &phoneInfo); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "解析手机号失败"})
|
||||
return
|
||||
}
|
||||
|
||||
// 验证水印(确保数据有效性)
|
||||
if phoneInfo.Watermark.AppID != config.Conf.WxID {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "数据水印验证失败"})
|
||||
return
|
||||
@@ -89,11 +83,10 @@ func UserLogin(c *gin.Context) {
|
||||
fmt.Println("用户手机号为:", phoneInfo.PhoneNumber)
|
||||
|
||||
openid := wxResp.OpenID
|
||||
ctx := c.Request.Context() // 获取请求上下文,用于控制数据库操作超时
|
||||
ctx := c.Request.Context()
|
||||
|
||||
var username string
|
||||
|
||||
// 1. 查询用户是否存在
|
||||
var exists bool
|
||||
query := "SELECT EXISTS(SELECT 1 FROM user_info WHERE uid = ? LIMIT 1)"
|
||||
err = databaseInit.UserDB.QueryRowContext(ctx, query, openid).Scan(&exists)
|
||||
@@ -105,7 +98,6 @@ func UserLogin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 如果用户不存在,插入新用户
|
||||
if !exists {
|
||||
username = generateUsername()
|
||||
now := time.Now()
|
||||
@@ -119,8 +111,8 @@ func UserLogin(c *gin.Context) {
|
||||
insertSQL,
|
||||
openid, // uid(使用微信 openid)
|
||||
2,
|
||||
now, // createdtime
|
||||
now, // updatedtime
|
||||
now,
|
||||
now,
|
||||
username,
|
||||
phoneInfo.PhoneNumber,
|
||||
)
|
||||
@@ -159,10 +151,8 @@ func UserLogin(c *gin.Context) {
|
||||
}
|
||||
|
||||
func generateUsername() string {
|
||||
// 初始化随机数种子(确保每次运行生成不同随机数)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
// 生成8位随机数字(范围:10000000-99999999)
|
||||
randomNum := 10000000 + rand.Intn(90000000) // 8位随机数
|
||||
randomNum := 10000000 + rand.Intn(90000000)
|
||||
return "用户" + strconv.Itoa(randomNum)
|
||||
}
|
||||
|
||||
@@ -179,10 +169,9 @@ func decryptWxData(sessionKey, encryptedData, iv string) ([]byte, error) {
|
||||
mode := cipher.NewCBCDecrypter(block, ivBytes)
|
||||
mode.CryptBlocks(data, data)
|
||||
|
||||
return pkcs7Unpad(data), nil // 返回去除填充后的原始数据
|
||||
return pkcs7Unpad(data), nil
|
||||
}
|
||||
|
||||
// 去除PKCS#7填充(微信加密数据使用PKCS#7填充)
|
||||
func pkcs7Unpad(data []byte) []byte {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
|
||||
35
goroutine/connectPool/goroutine/readgo.go
Normal file
35
goroutine/connectPool/goroutine/readgo.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package goroutine
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Dataread(db *sql.DB, donetitle chan struct{}) {
|
||||
// 启动一个长事务(整个循环在一个事务内,而非每次查询一个事务)
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer tx.Rollback() // 确保退出时回滚(仅为测试)
|
||||
|
||||
// 随机种子(保持不变)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
// 只执行两次查询,方便观察结果
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case <-time.After(2 * time.Second): // 第一次查询后等待2秒,给Datawrite插入时间
|
||||
var count int
|
||||
err = tx.QueryRow("SELECT count(*) FROM article WHERE age > 50 FOR UPDATE ").Scan(&count)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("第%d次查询 count: %d\n", i+1, count)
|
||||
case <-donetitle:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
11
goroutine/connectPool/goroutine/updatego.go
Normal file
11
goroutine/connectPool/goroutine/updatego.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package goroutine
|
||||
|
||||
import "database/sql"
|
||||
|
||||
func DataUpdate(db *sql.DB) {
|
||||
_, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
45
goroutine/connectPool/goroutine/writego.go
Normal file
45
goroutine/connectPool/goroutine/writego.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package goroutine
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Datawrite(db *sql.DB, donetitle chan struct{}) {
|
||||
timetick := time.Tick(1 * time.Second)
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-timetick:
|
||||
uid, error := uuid.NewRandom()
|
||||
if error != nil {
|
||||
panic(error)
|
||||
}
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("开始事务")
|
||||
_, err = tx.Exec(`insert INTO article (uid,content,author,age) values (?,?,?,?)`, uid, "123", "用户"+strconv.Itoa(rand.Intn(1000)), rand.Intn(40)+60)
|
||||
if err != nil {
|
||||
if rbErr := tx.Rollback(); rbErr != nil {
|
||||
panic("回滚失败: " + rbErr.Error())
|
||||
}
|
||||
panic("插入失败: " + err.Error()) // 明确错误类型
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("插入数据成功")
|
||||
case <-donetitle:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
32
goroutine/connectPool/pool.go
Normal file
32
goroutine/connectPool/pool.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package connectPool
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"toutoukan/goroutine/connectPool/goroutine"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
func ConnectPool(donetitle chan struct{}) {
|
||||
dsn := "mayiming:Mydream5654my,@tcp(43.142.81.151:3306)/goLearn?charset-uft8mb4&parseTime=True"
|
||||
db, _ = sql.Open("mysql", dsn)
|
||||
|
||||
err := db.Ping()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("数据库连接成功")
|
||||
for i := 0; i < 20; i++ {
|
||||
go goroutine.Datawrite(db, donetitle)
|
||||
}
|
||||
go goroutine.Dataread(db, donetitle)
|
||||
}
|
||||
|
||||
func DisconnectPool() {
|
||||
err := db.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user