|
- package svc
-
- import (
- "applet/app/cfg"
- "applet/app/db"
- "applet/app/e"
- "applet/app/lib/alipay"
- "applet/app/md"
- "applet/app/utils"
- "applet/app/utils/cache"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git"
- "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func GetRealNameAuthBase(c *gin.Context) {
- var res = &md.RealNameAuthBasicData{
- AuthState: "0",
- IsNeedGetEnergy: "1",
- TipStr: "请前往蛋蛋乐园获取能量",
- }
-
- res.AuthState = "0"
- //判断审核状态
- eg := db.Db
- user := GetUser(c)
- egg_system_rules.Init(cfg.RedisAddr)
- amount := GetSysCfgStr("user_real_name_money")
- _, can := egg_energy.CheckActivityCoinToAlipayRealName(db.Db, user.Id, amount)
- if can {
- res.IsNeedGetEnergy = "0"
- }
- NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
- list, _ := NewUserRealNameAuthDb.GetRealNameAuthByUid(utils.Int64ToStr(user.Id))
- if list != nil {
- //如果是审核中的订单,判断是否人脸识别 调用人脸识别获取结果
- if list.State == 0 && list.CertifyId != "" {
- state, _, _, err := GetCertifyQuery(c, utils.Int64ToStr(user.Id))
- if err == nil {
- list.State = state
- }
- }
- res.AuthState = utils.IntToStr(list.State)
- if list.State == 0 { //人脸识别 支付了但是没扫脸的情况 设置成没审核状态
- res.AuthState = "0"
- }
- if list.IsPay == 0 {
- res.AuthState = "0"
- }
- if list.IsPay == 1 {
- res.IsNeedGetEnergy = "0"
- }
- }
- if res.AuthState == "1" {
- res.IsNeedGetEnergy = "0"
- if user.IsRealName == 0 {
- user.IsRealName = 1
- eg.Where("id=?", user.Id).Cols("is_real_name").Update(user)
- }
- }
- e.OutSuc(c, res, nil)
- return
- }
- func GetRealNameAuthState(c *gin.Context) {
- var res = &md.RealNameAuthBasicData{
- AuthState: "0",
- IsNeedGetEnergy: "1",
- TipStr: "请前往蛋蛋乐园获取能量",
- }
- res.AuthState = "0"
- //判断审核状态
- eg := db.Db
- user := GetUser(c)
- egg_system_rules.Init(cfg.RedisAddr)
- amount := GetSysCfgStr("user_real_name_money")
- _, can := egg_energy.CheckActivityCoinToAlipayRealName(db.Db, user.Id, amount)
- if can {
- res.IsNeedGetEnergy = "0"
- }
- NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
- list, _ := NewUserRealNameAuthDb.GetRealNameAuthByUid(utils.Int64ToStr(user.Id))
- if list != nil {
- //如果是审核中的订单,判断是否人脸识别 调用人脸识别获取结果
- if list.State != 1 && list.CertifyId != "" {
- state, _, _, err := GetCertifyQuery(c, utils.Int64ToStr(user.Id))
- if err == nil {
- list.State = state
- }
- }
- res.AuthState = utils.IntToStr(list.State)
- if list.State == 0 { //人脸识别 支付了但是没扫脸的情况 设置成没审核状态
- res.AuthState = "0"
- }
- if list.IsPay == 0 {
- res.AuthState = "0"
- }
- if list.IsPay == 1 {
- res.IsNeedGetEnergy = "0"
- }
- }
- if res.AuthState == "1" {
- res.IsNeedGetEnergy = "0"
- if user.IsRealName == 0 {
- user.IsRealName = 1
- eg.Where("id=?", user.Id).Cols("is_real_name").Update(user)
- }
- }
- e.OutSuc(c, res, nil)
- return
- }
-
- func RealNameSave(c *gin.Context) {
- var args md.RealNameAuthAddRequest
- if err := c.ShouldBindJSON(&args); err != nil {
- e.OutErr(c, e.ERR_INVALID_ARGS, err)
- return
- }
- egg_system_rules.Init(cfg.RedisAddr)
- eg := db.Db
- user := GetUser(c)
- NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
- realData, _ := NewUserRealNameAuthDb.GetRealNameAuthByUidWithState(utils.Int64ToStr(user.Id), "1")
- if realData != nil {
- e.OutErr(c, 400, e.NewErr(400, "您已审核通过"))
- return
- }
- //判断该身份证被多少个人绑定过
- count, _ := NewUserRealNameAuthDb.GetRealNameAuthCount(int(user.Id), args.CardNo)
- if count > 0 {
- e.OutErr(c, 400, e.NewErr(400, "该身份证已被绑定,绑定失败"))
- return
- }
- // 加锁 防止并发提取
- mutexKey := fmt.Sprintf("realname:%s", utils.Int64ToStr(user.Id))
- withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
- if err != nil {
- e.OutErr(c, 400, e.NewErr(400, "请稍后再试"))
- return
- }
- if withdrawAvailable != "OK" {
- e.OutErr(c, 400, e.NewErr(400000, "请稍后再试"))
- return
- }
- //调用初始化认证接口
- leave := 17 - len(utils.Int64ToStr(user.Id))
- outerOrderNo := "Egg" + utils.Int64ToStr(user.Id) + "T" + utils.Int64ToStr(time.Now().Unix()) + utils.RandPow(leave)
- var tmp = &model.UserRealNameAuth{
- Uid: int(user.Id),
- RealName: args.RealName,
- CardNo: args.CardNo,
- Amount: utils.StrToFloat64(GetSysCfgStr("user_real_name_money")),
- AuditType: 1,
- UpdateTime: time.Now(),
- Oid: outerOrderNo,
- }
- isNeedPay := "1"
- list, _ := NewUserRealNameAuthDb.GetRealNameAuthByUid(utils.Int64ToStr(user.Id))
- amount := GetSysCfgStr("user_real_name_money")
- if list != nil && list.IsPay != 1 || list == nil {
- _, can := egg_energy.CheckActivityCoinToAlipayRealName(db.Db, user.Id, amount)
- if can == false {
- e.OutErr(c, 400, e.NewErr(400, "请先前往蛋蛋乐园获取能量值"))
- return
- }
- }
- if list != nil {
- if args.RealName != list.RealName || args.CardNo != list.CardNo || list.AlipayOid == "" {
- list.AlipayOid = outerOrderNo
- list.CertifyId = ""
- }
- if list.IsPay != 1 && list.IsPay != 5 {
- //没付款的话,更新下订单号
- list.Oid = outerOrderNo
- list.Amount = utils.StrToFloat64(GetSysCfgStr("user_real_name_money"))
- }
- if list.IsPay != 1 && utils.InArr(utils.IntToStr(tmp.IsPay), []string{"5", "6"}) {
- list.IsPay = tmp.IsPay
- list.Amount = 0
- }
- if list.IsPay == 1 {
- isNeedPay = "0"
- }
- if list.State != 1 {
- list.State = 0
- }
- if args.RealName != "" {
- list.RealName = args.RealName
- }
- if args.CardNo != "" {
- list.CardNo = args.CardNo
- }
- list.UpdateTime = time.Now()
- eg.Where("id=?", list.Id).Update(list)
- } else {
- tmp.CreateTime = time.Now()
- tmp.AlipayOid = outerOrderNo
- db.InsertComm(eg, tmp)
- list = tmp
- }
- //TODO 扣钱 自动用能量值抵扣
- if isNeedPay == "1" {
- err = egg_energy.ActivityCoinToAlipayRealName(db.Db, user.Id, amount)
- if err != nil {
- e.OutErr(c, 400, err)
- return
- }
- list.IsPay = 1
- eg.Where("id=?", list.Id).Update(list)
- }
- //调用初始化认证接口
- certifyID := list.CertifyId
- if certifyID != "" && list.State != 1 {
- c.Set("is_check", "1")
- state, _, _, _ := GetCertifyQuery(c, utils.Int64ToStr(user.Id))
- if state == 1 {
- e.OutErr(c, 400, e.NewErr(400, "已实名"))
- return
- }
- }
- var url = ""
- if certifyID == "" || list.State == 2 || list.CreateTime.Unix() < time.Now().Unix()-20*3600 {
- var param = &md.UserCertifyRequest{
- OrderId: outerOrderNo,
- RealName: list.RealName,
- IdentityNum: list.CardNo,
- Uid: utils.Int64ToStr(user.Id),
- }
- data, _ := AliFace(c, param)
- fmt.Println(data)
- tmpId, ok := data.AlipayUserCertifyOpenInitializeResponse.CertifyID.(string)
- if ok {
- certifyID = tmpId
- }
- } else {
- if list.AlipayOid != "" && list.State != 2 {
- outerOrderNo = list.AlipayOid
- }
- }
- //调用认证接口获取链接
- if certifyID != "" {
- var param = &md.UserCertifyRequest{CertifyId: certifyID, Uid: utils.Int64ToStr(user.Id)}
- url, _ = AliFaceCheck(c, param)
- list.CertifyId = certifyID
- list.AlipayOid = outerOrderNo
- eg.Where("id=?", list.Id).Update(list)
- }
- e.OutSuc(c, md.RealNameAuthResp{Oid: outerOrderNo, Url: url}, nil)
- return
- }
- func AliFace(c *gin.Context, pay *md.UserCertifyRequest) (alipay.UserCertifyOpenInitData, error) {
- orderId := pay.OrderId //支付单号
- payParams := map[string]string{
- "oid": orderId,
- "name": pay.RealName,
- "id_num": pay.IdentityNum,
- }
- var data = alipay.UserCertifyOpenInitData{}
- r, err := alipay.FacePrepareAlipayCode("face_init", payParams)
- if err != nil {
- return data, err
- }
- tmp, ok := r.(alipay.UserCertifyOpenInitData)
- if ok {
- data = tmp
- }
- return data, nil
- }
- func AliFaceCheck(c *gin.Context, pay *md.UserCertifyRequest) (string, error) {
- payParams := map[string]string{
- "certify_id": pay.CertifyId,
- }
- var data = ""
- r, err := alipay.FacePrepareAlipayCode("face_certify", payParams)
- utils.FilePutContents("face", utils.SerializeStr(r))
- utils.FilePutContents("face", utils.SerializeStr(err))
- if err != nil {
- return "", err
- }
- tmp, ok := r.(string)
- if ok {
- data = tmp
- }
- return data, nil
- }
-
- func GetCertifyQuery(c *gin.Context, uid string) (int, int, string, error) {
-
- eg := db.Db
- NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
- realNameAuth, _ := NewUserRealNameAuthDb.GetRealNameAuth(utils.StrToInt(uid))
- if realNameAuth == nil {
- return 0, 2, "未支付金额,认证失败", e.NewErr(400, "未支付金额,认证失败")
- }
- if realNameAuth.CertifyId == "" {
- return 0, 2, "校验失败", e.NewErr(400, "校验失败")
- }
- var param = &md.UserCertifyRequest{CertifyId: realNameAuth.CertifyId}
- data, _ := AliFaceQuery(c, param)
- isTrueStr := ""
- tmpData, ok := data.AlipayUserCertifyOpenQueryResponse.Passed.(string)
- if ok {
- isTrueStr = tmpData
- }
- tmpData1, ok := data.AlipayUserCertifyOpenQueryResponse.Passed.([]string)
- if ok {
- if len(tmpData1) > 0 {
- isTrueStr = tmpData1[0]
- }
- }
- fmt.Println(isTrueStr)
- msg, _ := json.Marshal(data)
- realNameAuth.Msg = string(msg)
- if isTrueStr == "T" {
- realNameAuth.State = 1
- eg.Where("id=?", realNameAuth.Id).Update(realNameAuth)
- } else if realNameAuth.State != 1 && isTrueStr == "F" && c.GetString("is_check") != "1" {
- realNameAuth.State = 2
- eg.Where("id=?", realNameAuth.Id).Update(realNameAuth)
- }
- return realNameAuth.State, realNameAuth.State, realNameAuth.Msg, nil
- }
- func AliFaceQuery(c *gin.Context, pay *md.UserCertifyRequest) (alipay.UserCertifyOpenQueryData, error) {
- payParams := map[string]string{
- "certify_id": pay.CertifyId,
- }
- var data = alipay.UserCertifyOpenQueryData{}
- r, err := alipay.FacePrepareAlipayCode("face_query", payParams)
- utils.FilePutContents("face", utils.SerializeStr(payParams))
- utils.FilePutContents("face", utils.SerializeStr(r))
- utils.FilePutContents("face", utils.SerializeStr(err))
- if err != nil {
- return data, err
- }
- tmp, ok := r.(alipay.UserCertifyOpenQueryData)
- if ok {
- data = tmp
- }
- return data, nil
- }
|