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

356 lines
11 KiB

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