WechatQrcode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\wechat;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\WechatService;
  15. /**
  16. * 获取二维码
  17. * Class WechatQrcode
  18. * @package app\admin\model\wechat
  19. */
  20. class WechatQrcode extends ModelBasic
  21. {
  22. use ModelTrait;
  23. const times = 2592000;
  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, self::times)->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::set($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::set($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)->setInc('scan');
  109. }
  110. }