WechatQrcode.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/22
  5. */
  6. namespace app\models\wechat;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\WechatService;
  10. /**
  11. * 获取二维码
  12. * Class WechatQrcode
  13. * @package app\models\wechat
  14. */
  15. class WechatQrcode extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'wechat_qrcode';
  27. use ModelTrait;
  28. /**
  29. * 创建临时二维码 有效期 30天
  30. *
  31. * 修改时 要使用的主键id $qtcode_id
  32. * @param $id
  33. * @param $type
  34. * @param string $qtcode_id
  35. */
  36. public static function createTemporaryQrcode($id, $type, $qtcode_id = '')
  37. {
  38. $qrcode = WechatService::qrcodeService();
  39. $data = $qrcode->temporary($id, 30 * 24 * 3600)->toArray();
  40. $data['qrcode_url'] = $data['url'];
  41. $data['expire_seconds'] = $data['expire_seconds'] + time();
  42. $data['url'] = $qrcode->url($data['ticket']);
  43. $data['status'] = 1;
  44. $data['third_id'] = $id;
  45. $data['third_type'] = $type;
  46. if ($qtcode_id) {
  47. self::edit($data, $qtcode_id);
  48. } else {
  49. $data['add_time'] = time();
  50. self::create($data);
  51. }
  52. }
  53. /**
  54. * 创建永久二维码
  55. * @param $id
  56. * @param $type
  57. */
  58. public static function createForeverQrcode($id, $type)
  59. {
  60. $qrcode = WechatService::qrcodeService();
  61. $data = $qrcode->forever($id)->toArray();
  62. $data['qrcode_url'] = $data['url'];
  63. $data['url'] = $qrcode->url($data['ticket']);
  64. $data['expire_seconds'] = 0;
  65. $data['status'] = 1;
  66. $data['third_id'] = $id;
  67. $data['third_type'] = $type;
  68. $data['add_time'] = time();
  69. self::create($data);
  70. }
  71. /**
  72. * 获取临时二维码
  73. * @param $type
  74. * @param $id
  75. * @return array|false|\PDOStatement|string|\think\Model
  76. */
  77. public static function getTemporaryQrcode($type, $id)
  78. {
  79. $res = self::where('third_id', $id)->where('third_type', $type)->find();
  80. if (empty($res)) {
  81. self::createTemporaryQrcode($id, $type);
  82. $res = self::getTemporaryQrcode($type, $id);
  83. } else if (empty($res['expire_seconds']) || $res['expire_seconds'] < time()) {
  84. self::createTemporaryQrcode($id, $type, $res['id']);
  85. $res = self::getTemporaryQrcode($type, $id);
  86. }
  87. if (!$res['ticket']) exception('临时二维码获取错误');
  88. return $res;
  89. }
  90. /**
  91. * 获取永久二维码
  92. * @param $type
  93. * @param $id
  94. * @return array|false|\PDOStatement|string|\think\Model
  95. */
  96. public static function getForeverQrcode($type, $id)
  97. {
  98. $res = self::where('third_id', $id)->where('third_type', $type)->find();
  99. if (empty($res)) {
  100. self::createForeverQrcode($id, $type);
  101. $res = self::getForeverQrcode($type, $id);
  102. }
  103. if (!$res['ticket']) exception('永久二维码获取错误');
  104. return $res;
  105. }
  106. public static function getQrcode($id, $type = 'id')
  107. {
  108. return self::where($type, $id)->find();
  109. }
  110. public static function scanQrcode($id, $type = 'id')
  111. {
  112. return self::where($type, $id)->inc('scan')->update();
  113. }
  114. }