You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.9 KiB

package users
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/siddontang/go-log/log"
"io/ioutil"
"net/http"
"net/url"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/user"
"recook/tools"
"strings"
)
type RealNameReq struct {
UserID uint `json:"userId" validate:"numeric,required,gt=0"`
RealName string `json:"realName" validate:"min=2,max=30"`
IDNum string `json:"idNum" validate:"required,len=18"`
}
// 录入真实姓名和身份证
func RealInfo(c *gin.Context) {
var p RealNameReq
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
var userInfo user.Information
dbc.DB.Find(&userInfo, p.UserID)
if userInfo.RealInfoStatus == user.RealInfoPass {
back.Fail(c, "请勿重复验证")
return
}
err = dbc.DB.Model(&user.Information{}).Where("id = ?", p.UserID).Update(user.Information{RealName: p.RealName, IDCard: p.IDNum}).Error
if err != nil {
back.Fail(c, "错误101")
return
}
//二要素验证 1 成功 2 失败 3 异常情况(当作失败处理)
res, err := RealInfoVerify(p.RealName, p.IDNum)
if err != nil || res != "1" {
// 发生错误也是未通过验证的一种
err = dbc.DB.Model(&user.Information{}).Where("id = ?", p.UserID).Update(user.Information{RealInfoStatus: user.RealInfoErr}).Error
back.Fail(c, "认证失败")
return
} else {
// 通过验证
err = dbc.DB.Model(&user.Information{}).Where("id = ?", p.UserID).Update(user.Information{RealInfoStatus: user.RealInfoPass}).Error
back.Suc(c, "认证成功", nil)
return
}
}
// 实名认证
const AppKey = "203811861"
const AppSecret = "cqdaupmdvdfukns7evn3we125gy0v3dr"
const AppCode = "9d30e32dbf114d21ba03ab7e447a2157"
const RealInfoURL = `http://checkone.market.alicloudapi.com/chinadatapay/1882`
type RealInfoResp struct {
Code string `json:"code"`
Data struct {
Result string `json:"result"`
} `json:"data"`
Message string `json:"message"`
SeqNo string `json:"seqNo"`
}
func RealInfoVerify(name, idNum string) (res string, err error) {
client := &http.Client{}
req, err := http.NewRequest("POST", RealInfoURL, strings.NewReader(url.Values{
"name": {name},
"idcard": {idNum},
}.Encode()))
req.Header.Add("Authorization", "APPCODE "+AppCode)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
resp, err := client.Do(req)
if err != nil {
log.Println("err happened :", err)
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
// data := []byte(`{"code":"10000","message":"成功","data":{"result":"2"},"seqNo":"K8S05A332005061138"}`)
if err != nil {
log.Println("err happened :", err)
return
}
var respJson RealInfoResp
err = json.Unmarshal(data, &respJson)
if err != nil {
log.Println("err happened :", err)
return
}
if respJson.Code == "10000" {
res = respJson.Data.Result
} else {
res = "4"
}
return
}