WechatQrcodeRepository.php 3.4 KB

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