WechatQrcodeRepository.php 4.0 KB

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