WechatQrcodeRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-28
  7. *
  8. *
  9. */
  10. namespace app\common\repositories\wechat;
  11. use app\common\dao\BaseDao;
  12. use app\common\dao\wechat\WechatQrcodeDao;
  13. use app\common\repositories\BaseRepository;
  14. use crmeb\services\WechatService;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\exception\ValidateException;
  19. use think\Model;
  20. /**
  21. * Class WechatQrcodeRepository
  22. * @package app\common\repositories\wechat
  23. * @author xaboy
  24. * @day 2020-04-28
  25. * @mixin WechatQrcodeDao
  26. */
  27. class WechatQrcodeRepository extends BaseRepository
  28. {
  29. /**
  30. * WechatQrcodeRepository constructor.
  31. * @param WechatQrcodeDao $dao
  32. */
  33. public function __construct(WechatQrcodeDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * @param $id
  39. * @param $type
  40. * @param null $qtcode_id
  41. * @return BaseDao|int|Model
  42. * @throws DbException
  43. * @author xaboy
  44. * @day 2020-04-28
  45. */
  46. public function createTemporaryQrcode($id, $type, $qtcode_id = null)
  47. {
  48. $qrcode = WechatService::create()->getApplication()->qrcode;
  49. $data = $qrcode->temporary($id, 30 * 24 * 3600)->toArray();
  50. $data['qrcode_url'] = $data['url'];
  51. $data['expire_seconds'] = $data['expire_seconds'] + time();
  52. $data['url'] = $qrcode->url($data['ticket']);
  53. $data['status'] = 1;
  54. $data['third_id'] = $id;
  55. $data['third_type'] = $type;
  56. if ($qtcode_id) {
  57. return $this->dao->update($qtcode_id, $data);
  58. } else {
  59. return $this->dao->create($data);
  60. }
  61. }
  62. /**
  63. * @param $id
  64. * @param $type
  65. * @return BaseDao|Model
  66. * @author xaboy
  67. * @day 2020-04-28
  68. */
  69. public function createForeverQrcode($id, $type)
  70. {
  71. $qrcode = WechatService::create()->getApplication()->qrcode;
  72. $data = $qrcode->forever($id)->toArray();
  73. $data['qrcode_url'] = $data['url'];
  74. $data['url'] = $qrcode->url($data['ticket']);
  75. $data['expire_seconds'] = 0;
  76. $data['status'] = 1;
  77. $data['third_id'] = $id;
  78. $data['third_type'] = $type;
  79. return self::create($data);
  80. }
  81. /**
  82. * @param $type
  83. * @param $id
  84. * @return BaseDao|array|int|Model|null
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. * @author xaboy
  89. * @day 2020-04-28
  90. */
  91. public function getTemporaryQrcode($type, $id)
  92. {
  93. $qrInfo = $this->dao->getTemporaryQrcode($type, $id);
  94. if (!$qrInfo || (!$qrInfo['expire_seconds'] || $qrInfo['expire_seconds'] < time())) {
  95. $qrInfo = $this->createTemporaryQrcode($type, $id);
  96. }
  97. if (!isset($qrInfo['ticket']) || !$qrInfo['ticket']) throw new ValidateException('临时二维码获取错误');
  98. return $qrInfo;
  99. }
  100. /**
  101. * @param $type
  102. * @param $id
  103. * @return BaseDao|array|Model|null
  104. * @throws DataNotFoundException
  105. * @throws DbException
  106. * @throws ModelNotFoundException
  107. * @author xaboy
  108. * @day 2020-04-28
  109. */
  110. public function getForeverQrcode($type, $id)
  111. {
  112. $qrInfo = $this->dao->getForeverQrcode($type, $id);
  113. if (!$qrInfo) {
  114. $qrInfo = $this->createForeverQrcode($id, $type);
  115. }
  116. if (!isset($qrInfo['ticket']) || !$qrInfo['ticket']) throw new ValidateException('二维码获取错误');
  117. return $qrInfo;
  118. }
  119. }