WechatQrcode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\model\wechat;
  3. use think\Model;
  4. use liuniu\WechatService;
  5. class WechatQrcode extends Model
  6. {
  7. // 表名
  8. protected $name = 'wechat_qrcode';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = false;
  11. // 定义时间戳字段名
  12. protected $createTime = false;
  13. protected $updateTime = false;
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'add_time_text'
  18. ];
  19. public function getAddTimeTextAttr($value, $data)
  20. {
  21. $value = $value ? $value : (isset($data['add_time']) ? $data['add_time'] : '');
  22. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  23. }
  24. protected function setAddTimeAttr($value)
  25. {
  26. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  27. }
  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. $qrcode = WechatService::qrcodeService();
  38. $data = $qrcode->temporary($id,30*24*3600)->toArray();
  39. $data['qrcode_url'] = $data['url'];
  40. $data['expire_seconds'] = $data['expire_seconds']+time();
  41. $data['url'] = $qrcode->url($data['ticket']);
  42. $data['status'] = 1;
  43. $data['third_id'] = $id;
  44. $data['third_type'] = $type;
  45. if($qtcode_id){
  46. self::edit($data,$qtcode_id);
  47. }else{
  48. $data['add_time'] = time();
  49. self::create($data);
  50. }
  51. }
  52. /**
  53. * 创建永久二维码
  54. * @param $id
  55. * @param $type
  56. */
  57. public static function createForeverQrcode($id,$type){
  58. $qrcode = WechatService::qrcodeService();
  59. $data = $qrcode->forever($id)->toArray();
  60. $data['qrcode_url'] = $data['url'];
  61. $data['url'] = $qrcode->url($data['ticket']);
  62. $data['expire_seconds'] = 0;
  63. $data['status'] = 1;
  64. $data['third_id'] = $id;
  65. $data['third_type'] = $type;
  66. $data['add_time'] = time();
  67. self::create($data);
  68. }
  69. /**
  70. * 获取临时二维码
  71. * @param $type
  72. * @param $id
  73. * @return array|false|\PDOStatement|string|\think\Model
  74. */
  75. public static function getTemporaryQrcode($type,$id){
  76. $res = self::where('third_id',$id)->where('third_type',$type)->find();
  77. if(empty($res)){
  78. self::createTemporaryQrcode($id,$type);
  79. $res = self::getTemporaryQrcode($type,$id);
  80. }else if(empty($res['expire_seconds']) || $res['expire_seconds'] < time()){
  81. self::createTemporaryQrcode($id,$type,$res['id']);
  82. $res = self::getTemporaryQrcode($type,$id);
  83. }
  84. if(!$res['ticket']) exception('临时二维码获取错误');
  85. return $res;
  86. }
  87. /**
  88. * 获取永久二维码
  89. * @param $type
  90. * @param $id
  91. * @return array|false|\PDOStatement|string|\think\Model
  92. */
  93. public static function getForeverQrcode($type,$id){
  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. }