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

svc_user_real_name.go 10 KiB

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