123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- declare (strict_types=1);
- namespace app\services\system;
- use app\services\system\attachment\SystemAttachmentServices;
- use GuzzleHttp\Psr7\Utils;
- use qiniu\basic\BaseServices;
- use app\model\system\Qrcode;
- use qiniu\exceptions\AdminException;
- use qiniu\services\UploadService;
- use qiniu\services\UtilService;
- use qiniu\services\wechat\MiniProgram;
- use qiniu\services\wechat\OfficialAccount;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Model;
- /**
- * 微信二维码管理服务
- * Class QrcodeServices
- * @package app\services\system
- * @mixin Qrcode
- */
- class QrcodeServices extends BaseServices
- {
- protected $exportHeader = [
- 'id' => '微信二维码ID',
- 'third_type' => '二维码类型',
- 'third_id' => '用户id',
- 'ticket' => '二维码参数',
- 'expire_seconds' => '二维码有效时间',
- 'status_chs' => '状态',
- 'add_time' => '添加时间',
- 'url' => '微信访问url',
- 'qrcode_url' => '微信二维码url',
- 'scan' => '被扫的次数',
- 'type_chs' => '二维码所属平台'
- ];
- /**
- * QrcodeServices constructor.
- * @param Qrcode $model
- */
- public function __construct(Qrcode $model)
- {
- $this->model = $model;
- }
- public function getQrcode($id, $type = 'id')
- {
- return $this->getModel()->where($type, $id)->find();
- }
- /**
- * 修改二维码使用状态
- * @param $id
- * @param string $type
- * @return mixed
- */
- public function scanQrcode($id, $type = 'id')
- {
- return $this->getModel()->where($type, $id)->inc('scan')->update();
- }
- /**
- * 获取临时二维码
- * @param $type
- * @param $id
- * @return array|Model|null
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getTemporaryQrcode($type, $id)
- {
- $where['third_id'] = $id;
- $where['third_type'] = $type;
- $where['type'] = 2;
- $res = $this->getOne($where);
- if (!$res) {
- $this->createTemporaryQrcode($id, $type);
- $res = $this->getTemporaryQrcode($type, $id);
- } else if (empty($res['expire_seconds']) || $res['expire_seconds'] < time()) {
- $this->createTemporaryQrcode($id, $type, $res['id']);
- $res = $this->getTemporaryQrcode($type, $id);
- }
- if (!$res['ticket']) throw new AdminException('临时二维码获取错误');
- return $res;
- }
- /**
- * 临时二维码生成
- * @param $id
- * @param $type
- * @param string $qrcode_id
- */
- public function createTemporaryQrcode($id, $type, $qrcode_id = '')
- {
- $qrcode = OfficialAccount::qrcodeService();
- $data = $qrcode->temporary($id, 30 * 24 * 3600);
- $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;
- $data['type'] = 2;
- if ($qrcode_id) {
- $this->update($qrcode_id, $data);
- } else {
- $data['add_time'] = time();
- $this->save($data);
- }
- }
- /**
- * 获取永久二维码
- * @param $type
- * @param $id
- * @return array|mixed|Model
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function getForeverQrcode($type, $id)
- {
- $where['third_id'] = $id;
- $where['third_type'] = $type;
- $where['type'] = 2;
- $res = $this->getOne($where);
- if (!$res) {
- $this->createForeverQrcode($id, $type);
- $res = $this->getForeverQrcode($type, $id);
- }
- if (!$res['ticket']) throw new AdminException('永久二维码获取错误');
- return $res;
- }
- /**
- * 永久二维码生成
- * @param $id
- * @param $type
- */
- public function createForeverQrcode($id, $type)
- {
- $qrcode = OfficialAccount::qrcodeService();
- $data = $qrcode->forever($id);
- $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();
- $data['type'] = 2;
- $this->save($data);
- }
- /**
- * 获取小程序分享二维码
- * @param int $uid
- * @param string $namePath
- * @param bool $isSaveAttach
- * @param array $appendData
- * @param string $savePath
- * @return false|mixed|string
- */
- public function getRoutineQrcode(int $uid, string $namePath = '', bool $isSaveAttach = true, array $appendData = [], string $savePath = 'qrcode/routine')
- {
- $data = 'spread_uid=' . $uid;
- $page = 'pages/index/index';
- if (!$page || !$namePath) {
- return false;
- }
- try {
- /** @var SystemAttachmentServices $systemAttachmentService */
- $systemAttachmentService = app()->make(SystemAttachmentServices::class);
- if (!$isSaveAttach) {
- $imageInfo = "";
- } else {
- $imageInfo = $systemAttachmentService->getOne(['name' => $savePath . '/' . $namePath]);
- //检测远程文件是否存在
- if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
- $imageInfo = '';
- $systemAttachmentService->delete(['name' => $savePath . '/' . $namePath]);
- }
- }
- $siteUrl = sys_config('site_url');
- if (!$imageInfo) {
- $res = MiniProgram::appCodeUnlimit($data, $page, 280);
- if (!$res) return false;
- $uploadType = (int)sys_config('upload_type', 1);
- $upload = UploadService::init($uploadType);
- $res = (string)Utils::streamFor($res);
- $res = $upload->to($savePath)->validate()->stream($res, $namePath);
- if ($res === false) {
- return false;
- }
- $imageInfo = $upload->getUploadInfo();
- $imageInfo['image_type'] = $uploadType;
- if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
- else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
- if (!$remoteImage['status']) return false;
- if ($isSaveAttach) {
- $systemAttachmentService->save([
- 'name' => $imageInfo['name'],
- 'att_dir' => $imageInfo['dir'],
- 'satt_dir' => $imageInfo['thumb_path'],
- 'att_size' => $imageInfo['size'],
- 'att_type' => $imageInfo['type'],
- 'image_type' => $imageInfo['image_type'],
- 'module_type' => 2,
- 'time' => time(),
- 'pid' => 1,
- 'type' => 2
- ]);
- }
- $url = $imageInfo['dir'];
- } else $url = $imageInfo['att_dir'];
- if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
- return $url;
- } catch (\Throwable $e) {
- return false;
- }
- }
- }
|