WechatQrcode.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/22
  5. */
  6. namespace app\admin\model\wechat;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\WechatService;
  10. /**
  11. * 获取二维码
  12. * Class WechatQrcode
  13. * @package app\admin\model\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. $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. }