WechatQrcode.php 3.3 KB

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