123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\admin\model\wechat;
- use think\Model;
- use liuniu\WechatService;
- class WechatQrcode extends Model
- {
-
-
- // 表名
- protected $name = 'wechat_qrcode';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = false;
- // 定义时间戳字段名
- protected $createTime = false;
- protected $updateTime = false;
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'add_time_text'
- ];
-
-
- public function getAddTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['add_time']) ? $data['add_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setAddTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- /**
- * 创建临时二维码 有效期 30天
- *
- * 修改时 要使用的主键id $qtcode_id
- * @param $id
- * @param $type
- * @param string $qtcode_id
- */
- public static function createTemporaryQrcode($id,$type,$qtcode_id=''){
- $qrcode = WechatService::qrcodeService();
- $data = $qrcode->temporary($id,30*24*3600)->toArray();
- $data['qrcode_url'] = $data['url'];
- $data['expire_seconds'] = $data['expire_seconds']+time();
- $data['url'] = $qrcode->url($data['ticket']);
- $data['status'] = 1;
- $data['third_id'] = $id;
- $data['third_type'] = $type;
- if($qtcode_id){
- self::edit($data,$qtcode_id);
- }else{
- $data['add_time'] = time();
- self::create($data);
- }
- }
- /**
- * 创建永久二维码
- * @param $id
- * @param $type
- */
- public static function createForeverQrcode($id,$type){
- $qrcode = WechatService::qrcodeService();
- $data = $qrcode->forever($id)->toArray();
- $data['qrcode_url'] = $data['url'];
- $data['url'] = $qrcode->url($data['ticket']);
- $data['expire_seconds'] = 0;
- $data['status'] = 1;
- $data['third_id'] = $id;
- $data['third_type'] = $type;
- $data['add_time'] = time();
- self::create($data);
- }
- /**
- * 获取临时二维码
- * @param $type
- * @param $id
- * @return array|false|\PDOStatement|string|\think\Model
- */
- public static function getTemporaryQrcode($type,$id){
- $res = self::where('third_id',$id)->where('third_type',$type)->find();
- if(empty($res)){
- self::createTemporaryQrcode($id,$type);
- $res = self::getTemporaryQrcode($type,$id);
- }else if(empty($res['expire_seconds']) || $res['expire_seconds'] < time()){
- self::createTemporaryQrcode($id,$type,$res['id']);
- $res = self::getTemporaryQrcode($type,$id);
- }
- if(!$res['ticket']) exception('临时二维码获取错误');
- return $res;
- }
- /**
- * 获取永久二维码
- * @param $type
- * @param $id
- * @return array|false|\PDOStatement|string|\think\Model
- */
- public static function getForeverQrcode($type,$id){
- $res = self::where('third_id',$id)->where('third_type',$type)->find();
- if(empty($res)) {
- self::createForeverQrcode($id, $type);
- $res = self::getForeverQrcode($type, $id);
- }
- if(!$res['ticket']) exception('永久二维码获取错误');
- return $res;
- }
- public static function getQrcode($id,$type = 'id')
- {
- return self::where($type,$id)->find();
- }
- public static function scanQrcode($id,$type = 'id')
- {
- return self::where($type,$id)->inc('scan')->update();
- }
- }
|