蛋蛋星球-客户端
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.
 
 
 
 
 

262 lines
7.6 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/lib/alipay"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  11. "encoding/json"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "time"
  15. )
  16. func GetRealNameAuthBase(c *gin.Context) {
  17. var res = &md.RealNameAuthBasicData{
  18. AuthState: "0",
  19. }
  20. res.AuthState = "0"
  21. //判断审核状态
  22. eg := db.Db
  23. user := GetUser(c)
  24. NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
  25. list, _ := NewUserRealNameAuthDb.GetRealNameAuthByUid(utils.Int64ToStr(user.Id))
  26. if list != nil {
  27. //如果是审核中的订单,判断是否人脸识别 调用人脸识别获取结果
  28. if list.State == 0 && list.CertifyId != "" {
  29. state, _, _, err := GetCertifyQuery(c, utils.Int64ToStr(user.Id))
  30. if err == nil {
  31. list.State = state
  32. }
  33. }
  34. res.AuthState = utils.IntToStr(list.State)
  35. if list.State == 0 { //人脸识别 支付了但是没扫脸的情况 设置成没审核状态
  36. res.AuthState = "0"
  37. }
  38. if list.IsPay == 0 {
  39. res.AuthState = "0"
  40. }
  41. }
  42. e.OutSuc(c, res, nil)
  43. return
  44. }
  45. func RealNameSave(c *gin.Context) {
  46. var args md.RealNameAuthAddRequest
  47. if err := c.ShouldBindJSON(&args); err != nil {
  48. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  49. return
  50. }
  51. eg := db.Db
  52. user := GetUser(c)
  53. NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
  54. realData, _ := NewUserRealNameAuthDb.GetRealNameAuthByUidWithState(utils.Int64ToStr(user.Id), "1")
  55. if realData != nil {
  56. e.OutErr(c, 400, e.NewErr(400, "您已审核通过"))
  57. return
  58. }
  59. //判断该身份证被多少个人绑定过
  60. count, _ := NewUserRealNameAuthDb.GetRealNameAuthCount(int(user.Id), args.CardNo)
  61. if count > 0 {
  62. e.OutErr(c, 400, e.NewErr(400, "该身份证已被绑定,绑定失败"))
  63. return
  64. }
  65. // 加锁 防止并发提取
  66. mutexKey := fmt.Sprintf("realname:%s", utils.Int64ToStr(user.Id))
  67. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  68. if err != nil {
  69. e.OutErr(c, 400, e.NewErr(400, "请稍后再试"))
  70. return
  71. }
  72. if withdrawAvailable != "OK" {
  73. e.OutErr(c, 400, e.NewErr(400000, "请稍后再试"))
  74. return
  75. }
  76. //调用初始化认证接口
  77. leave := 17 - len(utils.Int64ToStr(user.Id))
  78. outerOrderNo := "Egg" + utils.Int64ToStr(user.Id) + "T" + utils.Int64ToStr(time.Now().Unix()) + utils.RandPow(leave)
  79. var tmp = &model.UserRealNameAuth{
  80. Uid: int(user.Id),
  81. RealName: args.RealName,
  82. CardNo: args.CardNo,
  83. Amount: utils.StrToFloat64(GetSysCfgStr("user_real_name_money")),
  84. AuditType: 1,
  85. UpdateTime: time.Now(),
  86. Oid: outerOrderNo,
  87. }
  88. isNeedPay := "1"
  89. list, _ := NewUserRealNameAuthDb.GetRealNameAuthByUid(utils.Int64ToStr(user.Id))
  90. if list != nil {
  91. if args.RealName != list.RealName || args.CardNo != list.CardNo || list.AlipayOid == "" {
  92. list.AlipayOid = outerOrderNo
  93. list.CertifyId = ""
  94. }
  95. if list.IsPay != 1 && list.IsPay != 5 {
  96. //没付款的话,更新下订单号
  97. list.Oid = outerOrderNo
  98. list.Amount = utils.StrToFloat64(GetSysCfgStr("user_real_name_money"))
  99. }
  100. if list.IsPay != 1 && utils.InArr(utils.IntToStr(tmp.IsPay), []string{"5", "6"}) {
  101. list.IsPay = tmp.IsPay
  102. list.Amount = 0
  103. }
  104. if list.IsPay == 1 {
  105. isNeedPay = "0"
  106. }
  107. if list.State != 1 {
  108. list.State = 0
  109. }
  110. if args.RealName != "" {
  111. list.RealName = args.RealName
  112. }
  113. if args.CardNo != "" {
  114. list.CardNo = args.CardNo
  115. }
  116. list.UpdateTime = time.Now()
  117. eg.Where("id=?", list.Id).Update(list)
  118. } else {
  119. tmp.CreateTime = time.Now()
  120. tmp.AlipayOid = outerOrderNo
  121. db.InsertComm(eg, tmp)
  122. list = tmp
  123. }
  124. //TODO 扣钱 自动用能量值抵扣
  125. if isNeedPay == "1" {
  126. list.IsPay = 1
  127. eg.Where("id=?", list.Id).Update(list)
  128. }
  129. //调用初始化认证接口
  130. certifyID := list.CertifyId
  131. if certifyID != "" && list.State != 1 {
  132. c.Set("is_check", "1")
  133. state, _, _, _ := GetCertifyQuery(c, utils.Int64ToStr(user.Id))
  134. if state == 1 {
  135. e.OutErr(c, 400, e.NewErr(400, "已实名"))
  136. return
  137. }
  138. }
  139. var url = ""
  140. if certifyID == "" || list.State == 2 || list.CreateTime.Unix() < time.Now().Unix()-20*3600 {
  141. var param = &md.UserCertifyRequest{
  142. OrderId: outerOrderNo,
  143. RealName: list.RealName,
  144. IdentityNum: list.CardNo,
  145. Uid: utils.Int64ToStr(user.Id),
  146. }
  147. data, _ := AliFace(c, param)
  148. fmt.Println(data)
  149. tmpId, ok := data.AlipayUserCertifyOpenInitializeResponse.CertifyID.(string)
  150. if ok {
  151. certifyID = tmpId
  152. }
  153. } else {
  154. if list.AlipayOid != "" && list.State != 2 {
  155. outerOrderNo = list.AlipayOid
  156. }
  157. }
  158. //调用认证接口获取链接
  159. if certifyID != "" {
  160. var param = &md.UserCertifyRequest{CertifyId: certifyID, Uid: utils.Int64ToStr(user.Id)}
  161. url, _ = AliFaceCheck(c, param)
  162. list.CertifyId = certifyID
  163. list.AlipayOid = outerOrderNo
  164. eg.Where("id=?", list.Id).Update(list)
  165. }
  166. e.OutSuc(c, md.RealNameAuthResp{Oid: outerOrderNo, Url: url}, nil)
  167. return
  168. }
  169. func AliFace(c *gin.Context, pay *md.UserCertifyRequest) (alipay.UserCertifyOpenInitData, error) {
  170. orderId := pay.OrderId //支付单号
  171. payParams := map[string]string{
  172. "oid": orderId,
  173. "name": pay.RealName,
  174. "id_num": pay.IdentityNum,
  175. }
  176. var data = alipay.UserCertifyOpenInitData{}
  177. r, err := alipay.FacePrepareAlipayCode("face_init", payParams)
  178. if err != nil {
  179. return data, err
  180. }
  181. tmp, ok := r.(alipay.UserCertifyOpenInitData)
  182. if ok {
  183. data = tmp
  184. }
  185. return data, nil
  186. }
  187. func AliFaceCheck(c *gin.Context, pay *md.UserCertifyRequest) (string, error) {
  188. payParams := map[string]string{
  189. "certify_id": pay.CertifyId,
  190. }
  191. var data = ""
  192. r, err := alipay.FacePrepareAlipayCode("face_certify", payParams)
  193. utils.FilePutContents("face", utils.SerializeStr(r))
  194. utils.FilePutContents("face", utils.SerializeStr(err))
  195. if err != nil {
  196. return "", err
  197. }
  198. tmp, ok := r.(string)
  199. if ok {
  200. data = tmp
  201. }
  202. return data, nil
  203. }
  204. func GetCertifyQuery(c *gin.Context, uid string) (int, int, string, error) {
  205. eg := db.Db
  206. NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
  207. realNameAuth, _ := NewUserRealNameAuthDb.GetRealNameAuth(utils.StrToInt(uid))
  208. if realNameAuth == nil {
  209. return 0, 2, "未支付金额,认证失败", e.NewErr(400, "未支付金额,认证失败")
  210. }
  211. if realNameAuth.CertifyId == "" {
  212. return 0, 2, "校验失败", e.NewErr(400, "校验失败")
  213. }
  214. var param = &md.UserCertifyRequest{CertifyId: realNameAuth.CertifyId}
  215. data, _ := AliFaceQuery(c, param)
  216. isTrueStr := ""
  217. tmpData, ok := data.AlipayUserCertifyOpenQueryResponse.Passed.(string)
  218. if ok {
  219. isTrueStr = tmpData
  220. }
  221. tmpData1, ok := data.AlipayUserCertifyOpenQueryResponse.Passed.([]string)
  222. if ok {
  223. if len(tmpData1) > 0 {
  224. isTrueStr = tmpData1[0]
  225. }
  226. }
  227. fmt.Println(isTrueStr)
  228. msg, _ := json.Marshal(data)
  229. realNameAuth.Msg = string(msg)
  230. if isTrueStr == "T" {
  231. realNameAuth.State = 1
  232. eg.Where("id=?", realNameAuth.Id).Update(realNameAuth)
  233. } else if realNameAuth.State != 1 && isTrueStr == "F" && c.GetString("is_check") != "1" {
  234. realNameAuth.State = 2
  235. eg.Where("id=?", realNameAuth.Id).Update(realNameAuth)
  236. }
  237. return realNameAuth.State, realNameAuth.State, realNameAuth.Msg, nil
  238. }
  239. func AliFaceQuery(c *gin.Context, pay *md.UserCertifyRequest) (alipay.UserCertifyOpenQueryData, error) {
  240. payParams := map[string]string{
  241. "certify_id": pay.CertifyId,
  242. }
  243. var data = alipay.UserCertifyOpenQueryData{}
  244. r, err := alipay.FacePrepareAlipayCode("face_query", payParams)
  245. utils.FilePutContents("face", utils.SerializeStr(payParams))
  246. utils.FilePutContents("face", utils.SerializeStr(r))
  247. utils.FilePutContents("face", utils.SerializeStr(err))
  248. if err != nil {
  249. return data, err
  250. }
  251. tmp, ok := r.(alipay.UserCertifyOpenQueryData)
  252. if ok {
  253. data = tmp
  254. }
  255. return data, nil
  256. }