Skip to content

Commit

Permalink
add qzyq server request
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzt19 committed Feb 22, 2022
1 parent 9c99dc5 commit 073a4cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ go.work
*.db

water_sub

.vscode/


dist/
44 changes: 43 additions & 1 deletion controllers/reccords.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package controllers

import (
"encoding/json"
"net/http"
"net/url"
"strconv"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -32,6 +34,8 @@ func GetRecords(c *gin.Context) {
})
}

const API_URL = "http://dingshui.bjqzhd.com"

func PostRecord(c *gin.Context) {
var input models.Record
if err := c.BindJSON(&input); err != nil {
Expand All @@ -40,7 +44,45 @@ func PostRecord(c *gin.Context) {
}
input.IP = c.ClientIP()

models.DB.Create(&input)
// start transaction
tx := models.DB.Begin()
// add to db
tx.Create(&input)
// reqeust to api
// step 1: get user info
resp, err := http.PostForm(API_URL+"/auser/getuser.html", url.Values{
"param": {input.FileNumber},
"name": {"pw"},
})
if err != nil {
tx.Rollback()
c.JSON(http.StatusBadRequest, gin.H{"msg": err})
return
}
var userInfo map[string]string
json.NewDecoder(resp.Body).Decode(&userInfo)
// step 2: submit
resp, err = http.PostForm(API_URL+"/buy/subs.html", url.Values{
"pw": {userInfo["pw"]},
"name": {userInfo["name"]},
"num": {"1"},
"num1": {"0"},
"lid": {"6"},
"phone": {""},
"address": {""},
})
if err != nil {
tx.Rollback()
c.JSON(http.StatusBadRequest, gin.H{"msg": err})
return
}
if resp.StatusCode != 200 {
tx.Rollback()
c.JSON(http.StatusBadRequest, gin.H{"msg": "sub returned code: " + strconv.Itoa(resp.StatusCode)})
return
}
// success
tx.Commit()

c.JSON(http.StatusOK, gin.H{
"data": input,
Expand Down

0 comments on commit 073a4cf

Please sign in to comment.