WechatQrcode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. /**
  24. * 创建临时二维码 有效期 30天
  25. *
  26. * 修改时 要使用的主键id $qtcode_id
  27. * @param $id
  28. * @param $type
  29. * @param string $qtcode_id
  30. */
  31. public static function createTemporaryQrcode($id,$type,$qtcode_id=''){
  32. $qrcode = WechatService::qrcodeService();
  33. $data = $qrcode->temporary($id,30*24*3600)->toArray();
  34. $data['qrcode_url'] = $data['url'];
  35. $data['expire_seconds'] = $data['expire_seconds']+time();
  36. $data['url'] = $qrcode->url($data['ticket']);
  37. $data['status'] = 1;
  38. $data['third_id'] = $id;
  39. $data['third_type'] = $type;
  40. if($qtcode_id){
  41. self::edit($data,$qtcode_id);
  42. }else{
  43. $data['add_time'] = time();
  44. self::set($data);
  45. }
  46. }
  47. /**
  48. * 创建永久二维码
  49. * @param $id
  50. * @param $type
  51. */
  52. public static function createForeverQrcode($id,$type){
  53. $qrcode = WechatService::qrcodeService();
  54. $data = $qrcode->forever($id)->toArray();
  55. $data['qrcode_url'] = $data['url'];
  56. $data['url'] = $qrcode->url($data['ticket']);
  57. $data['expire_seconds'] = 0;
  58. $data['status'] = 1;
  59. $data['third_id'] = $id;
  60. $data['third_type'] = $type;
  61. $data['add_time'] = time();
  62. self::set($data);
  63. }
  64. /**
  65. * 获取临时二维码
  66. * @param $type
  67. * @param $id
  68. * @return array|false|\PDOStatement|string|\think\Model
  69. */
  70. public static function getTemporaryQrcode($type,$id){
  71. $res = self::where('third_id',$id)->where('third_type',$type)->find();
  72. if(empty($res)){
  73. self::createTemporaryQrcode($id,$type);
  74. $res = self::getTemporaryQrcode($type,$id);
  75. }else if(empty($res['expire_seconds']) || $res['expire_seconds'] < time()){
  76. self::createTemporaryQrcode($id,$type,$res['id']);
  77. $res = self::getTemporaryQrcode($type,$id);
  78. }
  79. if(!$res['ticket']) exception('临时二维码获取错误');
  80. return $res;
  81. }
  82. /**
  83. * 获取永久二维码
  84. * @param $type
  85. * @param $id
  86. * @return array|false|\PDOStatement|string|\think\Model
  87. */
  88. public static function getForeverQrcode($type,$id){
  89. $res = self::where('third_id',$id)->where('third_type',$type)->find();
  90. if(empty($res)) {
  91. self::createForeverQrcode($id, $type);
  92. $res = self::getForeverQrcode($type, $id);
  93. }
  94. if(!$res['ticket']) exception('临时二维码获取错误');
  95. return $res;
  96. }
  97. public static function getQrcode($id,$type = 'id')
  98. {
  99. return self::where($type,$id)->find();
  100. }
  101. public static function scanQrcode($id,$type = 'id')
  102. {
  103. return self::where($type,$id)->setInc('scan');
  104. }
  105. }