一物一码
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_qrcode.go 3.3 KiB

1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/enum"
  6. "applet/app/lib/wx"
  7. "applet/app/md"
  8. "applet/app/utils"
  9. "errors"
  10. "time"
  11. "xorm.io/xorm"
  12. )
  13. func StatisticsQrcodeData() (qrcodeTotalNums, waitUseQrcodeNums, alreadyUseQrcodeNums, allowCreateQrcodeNums int64, err error) {
  14. qrcodeTotalNums = md.QrcodeTotalNums //二维码总量
  15. qrcodeWithBatchRecordsDb := db.QrcodeWithBatchRecordsDb{}
  16. qrcodeWithBatchRecordsDb.Set()
  17. qrcodeWithBatchRecordsForUseWait, err := qrcodeWithBatchRecordsDb.FindQrcodeWithBatchRecordsByState(enum.QrcodeWithBatchRecordsStateForWait)
  18. if err != nil {
  19. return
  20. }
  21. waitUseQrcodeNums = int64(len(qrcodeWithBatchRecordsForUseWait)) //待使用二维码数量
  22. qrcodeWithBatchRecordsForUseAlready, err := qrcodeWithBatchRecordsDb.FindQrcodeWithBatchRecordsByState(enum.QrcodeWithBatchRecordsStateForAlready)
  23. if err != nil {
  24. return
  25. }
  26. alreadyUseQrcodeNums = int64(len(qrcodeWithBatchRecordsForUseAlready)) //已使用二维码数量
  27. allowCreateQrcodeNums = qrcodeTotalNums - waitUseQrcodeNums //可生成二维码数量
  28. return
  29. }
  30. func createQrcodeIndex() string {
  31. date := utils.Int64ToStr(time.Now().UnixMicro())
  32. sceneStr := date + "_" + utils.RandString(6) //根据当前时间戳(微秒)+ 随机6位字符串 作为唯一标识符
  33. return sceneStr
  34. }
  35. func CreateQrcode(createNums int) (err error) {
  36. now := time.Now()
  37. var insertData []*model.Qrcode
  38. //1、调用微信 `cgi-bin/qrcode/create` 生成带参的永久二维码
  39. wxOfficial := wx.OfficialAccount{}
  40. wxOfficial.Set()
  41. for i := 0; i < createNums; i++ {
  42. sceneStr := createQrcodeIndex()
  43. qrcodeUrl, err1 := wxOfficial.QrcodeCreate(sceneStr)
  44. if err1 != nil {
  45. return err1
  46. }
  47. insertData = append(insertData, &model.Qrcode{
  48. Url: qrcodeUrl,
  49. State: enum.QrcodeSateAllowUse,
  50. Identity: sceneStr,
  51. CreateAt: now.Format("2006-01-02 15:00:00"),
  52. UpdateAt: now.Format("2006-01-02 15:00:00"),
  53. })
  54. }
  55. //2、批量新增二维码
  56. qrcodeDb := db.QrcodeDb{}
  57. qrcodeDb.Set()
  58. _, err = qrcodeDb.BatchAddQrcode(insertData)
  59. return
  60. }
  61. func OperateQrcode(batchId, totalNums int, args md.QrcodeBatchAddReq, session *xorm.Session) (err error) {
  62. qrcodeDb := db.QrcodeDb{}
  63. qrcodeDb.Set()
  64. //1、获取当前可用二维码
  65. allowUseQrcodeList, allowUseQrcodeTotal, err := qrcodeDb.FindQrcodeForAllowUse()
  66. if int(allowUseQrcodeTotal) < totalNums {
  67. err = errors.New("可用二维码不足")
  68. return
  69. }
  70. now := time.Now()
  71. var insertData []*model.QrcodeWithBatchRecords
  72. var updateQrcodeIds []int
  73. var k = 0
  74. for _, v := range args.List {
  75. for i := 0; i < v.Num; i++ {
  76. insertData = append(insertData, &model.QrcodeWithBatchRecords{
  77. QrcodeId: allowUseQrcodeList[k].Id,
  78. BatchId: batchId,
  79. Amount: v.Amount,
  80. State: enum.QrcodeWithBatchRecordsStateForWait,
  81. CreateAt: now.Format("2006-01-02 15:00:00"),
  82. UpdateAt: now.Format("2006-01-02 15:00:00"),
  83. })
  84. updateQrcodeIds = append(updateQrcodeIds, allowUseQrcodeList[k].Id)
  85. k++
  86. }
  87. }
  88. //2、新增“二维码-批次”记录
  89. qrcodeWithBatchRecordsDb := db.QrcodeWithBatchRecordsDb{}
  90. qrcodeWithBatchRecordsDb.Set()
  91. if _, err = qrcodeWithBatchRecordsDb.BatchAddQrcodeWithBatchRecordsBySession(session, insertData); err != nil {
  92. return
  93. }
  94. //3、修改"二维码状态"为不可用
  95. _, err = qrcodeDb.BatchUpdateQrcodeBySession(session, updateQrcodeIds, enum.QrcodeSateAllowNotUse)
  96. return
  97. }