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

361 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, err := NewUserWalletDb.GetUserVirtualWallet(user.Id)
  165. if err != nil {
  166. utils.FilePutContents("face", err.Error())
  167. e.OutErr(c, 400, e.NewErr(400, "请先前往积分中心使用部分能量值兑换余额~"))
  168. return
  169. }
  170. if wallet == nil {
  171. e.OutErr(c, 400, e.NewErr(400, "请先前往积分中心使用部分能量值兑换余额!"))
  172. return
  173. }
  174. if utils.StrToFloat64(wallet.Amount) < utils.StrToFloat64(amount) {
  175. e.OutErr(c, 400, e.NewErr(400, "请先前往积分中心使用部分能量值兑换余额."))
  176. return
  177. }
  178. }
  179. if list != nil {
  180. if args.RealName != list.RealName || args.CardNo != list.CardNo || list.AlipayOid == "" {
  181. list.AlipayOid = outerOrderNo
  182. list.CertifyId = ""
  183. }
  184. if list.IsPay != 1 && list.IsPay != 5 {
  185. //没付款的话,更新下订单号
  186. list.Oid = outerOrderNo
  187. list.Amount = utils.StrToFloat64(GetSysCfgStr("user_real_name_money"))
  188. }
  189. if list.IsPay != 1 && utils.InArr(utils.IntToStr(tmp.IsPay), []string{"5", "6"}) {
  190. list.IsPay = tmp.IsPay
  191. list.Amount = 0
  192. }
  193. if list.IsPay == 1 {
  194. isNeedPay = "0"
  195. }
  196. if list.State != 1 {
  197. list.State = 0
  198. }
  199. if args.RealName != "" {
  200. list.RealName = args.RealName
  201. }
  202. if args.CardNo != "" {
  203. list.CardNo = args.CardNo
  204. }
  205. list.UpdateTime = time.Now()
  206. eg.Where("id=?", list.Id).Update(list)
  207. } else {
  208. tmp.CreateTime = time.Now()
  209. tmp.AlipayOid = outerOrderNo
  210. db.InsertComm(eg, tmp)
  211. list = tmp
  212. }
  213. //TODO 扣钱 自动用能量值抵扣
  214. if isNeedPay == "1" {
  215. err = egg_energy.ActivityCoinToAlipayRealName(db.Db, user.Id, amount)
  216. if err != nil {
  217. if err.Error() == "用户钱包余额不足" {
  218. e.OutErr(c, 400, e.NewErr(400, "请先前往积分中心使用部分能量值兑换余额"))
  219. return
  220. }
  221. e.OutErr(c, 400, err.Error())
  222. return
  223. }
  224. list.IsPay = 1
  225. eg.Where("id=?", list.Id).Update(list)
  226. }
  227. //调用初始化认证接口
  228. certifyID := list.CertifyId
  229. if certifyID != "" && list.State != 1 {
  230. c.Set("is_check", "1")
  231. state, _, _, _ := GetCertifyQuery(c, utils.Int64ToStr(user.Id))
  232. if state == 1 {
  233. e.OutErr(c, 400, e.NewErr(400, "已实名"))
  234. return
  235. }
  236. }
  237. var url = ""
  238. if certifyID == "" || list.State == 2 || list.CreateTime.Unix() < time.Now().Unix()-20*3600 {
  239. var param = &md.UserCertifyRequest{
  240. OrderId: outerOrderNo,
  241. RealName: list.RealName,
  242. IdentityNum: list.CardNo,
  243. Uid: utils.Int64ToStr(user.Id),
  244. }
  245. data, _ := AliFace(c, param)
  246. fmt.Println(data)
  247. tmpId, ok := data.AlipayUserCertifyOpenInitializeResponse.CertifyID.(string)
  248. if ok {
  249. certifyID = tmpId
  250. }
  251. } else {
  252. if list.AlipayOid != "" && list.State != 2 {
  253. outerOrderNo = list.AlipayOid
  254. }
  255. }
  256. //调用认证接口获取链接
  257. if certifyID != "" {
  258. var param = &md.UserCertifyRequest{CertifyId: certifyID, Uid: utils.Int64ToStr(user.Id)}
  259. url, _ = AliFaceCheck(c, param)
  260. list.CertifyId = certifyID
  261. list.AlipayOid = outerOrderNo
  262. eg.Where("id=?", list.Id).Update(list)
  263. }
  264. e.OutSuc(c, md.RealNameAuthResp{Oid: outerOrderNo, Url: url}, nil)
  265. return
  266. }
  267. func AliFace(c *gin.Context, pay *md.UserCertifyRequest) (alipay.UserCertifyOpenInitData, error) {
  268. orderId := pay.OrderId //支付单号
  269. payParams := map[string]string{
  270. "oid": orderId,
  271. "name": pay.RealName,
  272. "id_num": pay.IdentityNum,
  273. }
  274. var data = alipay.UserCertifyOpenInitData{}
  275. r, err := alipay.FacePrepareAlipayCode("face_init", payParams)
  276. if err != nil {
  277. return data, err
  278. }
  279. tmp, ok := r.(alipay.UserCertifyOpenInitData)
  280. if ok {
  281. data = tmp
  282. }
  283. return data, nil
  284. }
  285. func AliFaceCheck(c *gin.Context, pay *md.UserCertifyRequest) (string, error) {
  286. payParams := map[string]string{
  287. "certify_id": pay.CertifyId,
  288. }
  289. var data = ""
  290. r, err := alipay.FacePrepareAlipayCode("face_certify", payParams)
  291. utils.FilePutContents("face", utils.SerializeStr(r))
  292. utils.FilePutContents("face", utils.SerializeStr(err))
  293. if err != nil {
  294. return "", err
  295. }
  296. tmp, ok := r.(string)
  297. if ok {
  298. data = tmp
  299. }
  300. return data, nil
  301. }
  302. func GetCertifyQuery(c *gin.Context, uid string) (int, int, string, error) {
  303. eg := db.Db
  304. NewUserRealNameAuthDb := implement.NewUserRealNameAuthDb(eg)
  305. realNameAuth, _ := NewUserRealNameAuthDb.GetRealNameAuth(utils.StrToInt(uid))
  306. if realNameAuth == nil {
  307. return 0, 2, "未支付金额,认证失败", e.NewErr(400, "未支付金额,认证失败")
  308. }
  309. if realNameAuth.CertifyId == "" {
  310. return 0, 2, "校验失败", e.NewErr(400, "校验失败")
  311. }
  312. var param = &md.UserCertifyRequest{CertifyId: realNameAuth.CertifyId}
  313. data, _ := AliFaceQuery(c, param)
  314. isTrueStr := ""
  315. tmpData, ok := data.AlipayUserCertifyOpenQueryResponse.Passed.(string)
  316. if ok {
  317. isTrueStr = tmpData
  318. }
  319. tmpData1, ok := data.AlipayUserCertifyOpenQueryResponse.Passed.([]string)
  320. if ok {
  321. if len(tmpData1) > 0 {
  322. isTrueStr = tmpData1[0]
  323. }
  324. }
  325. fmt.Println(isTrueStr)
  326. msg, _ := json.Marshal(data)
  327. realNameAuth.Msg = string(msg)
  328. if isTrueStr == "T" {
  329. realNameAuth.State = 1
  330. eg.Where("id=?", realNameAuth.Id).Update(realNameAuth)
  331. } else if realNameAuth.State != 1 && isTrueStr == "F" && c.GetString("is_check") != "1" {
  332. realNameAuth.State = 2
  333. eg.Where("id=?", realNameAuth.Id).Update(realNameAuth)
  334. }
  335. return realNameAuth.State, realNameAuth.State, realNameAuth.Msg, nil
  336. }
  337. func AliFaceQuery(c *gin.Context, pay *md.UserCertifyRequest) (alipay.UserCertifyOpenQueryData, error) {
  338. payParams := map[string]string{
  339. "certify_id": pay.CertifyId,
  340. }
  341. var data = alipay.UserCertifyOpenQueryData{}
  342. r, err := alipay.FacePrepareAlipayCode("face_query", payParams)
  343. utils.FilePutContents("face", utils.SerializeStr(payParams))
  344. utils.FilePutContents("face", utils.SerializeStr(r))
  345. utils.FilePutContents("face", utils.SerializeStr(err))
  346. if err != nil {
  347. return data, err
  348. }
  349. tmp, ok := r.(alipay.UserCertifyOpenQueryData)
  350. if ok {
  351. data = tmp
  352. }
  353. return data, nil
  354. }