QrcodeServices.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\services\system;
  4. use app\services\system\attachment\SystemAttachmentServices;
  5. use GuzzleHttp\Psr7\Utils;
  6. use qiniu\basic\BaseServices;
  7. use app\model\system\Qrcode;
  8. use qiniu\exceptions\AdminException;
  9. use qiniu\services\UploadService;
  10. use qiniu\services\UtilService;
  11. use qiniu\services\wechat\MiniProgram;
  12. use qiniu\services\wechat\OfficialAccount;
  13. use think\db\exception\DataNotFoundException;
  14. use think\db\exception\DbException;
  15. use think\db\exception\ModelNotFoundException;
  16. use think\Model;
  17. /**
  18. * 微信二维码管理服务
  19. * Class QrcodeServices
  20. * @package app\services\system
  21. * @mixin Qrcode
  22. */
  23. class QrcodeServices extends BaseServices
  24. {
  25. protected $exportHeader = [
  26. 'id' => '微信二维码ID',
  27. 'third_type' => '二维码类型',
  28. 'third_id' => '用户id',
  29. 'ticket' => '二维码参数',
  30. 'expire_seconds' => '二维码有效时间',
  31. 'status_chs' => '状态',
  32. 'add_time' => '添加时间',
  33. 'url' => '微信访问url',
  34. 'qrcode_url' => '微信二维码url',
  35. 'scan' => '被扫的次数',
  36. 'type_chs' => '二维码所属平台'
  37. ];
  38. /**
  39. * QrcodeServices constructor.
  40. * @param Qrcode $model
  41. */
  42. public function __construct(Qrcode $model)
  43. {
  44. $this->model = $model;
  45. }
  46. public function getQrcode($id, $type = 'id')
  47. {
  48. return $this->getModel()->where($type, $id)->find();
  49. }
  50. /**
  51. * 修改二维码使用状态
  52. * @param $id
  53. * @param string $type
  54. * @return mixed
  55. */
  56. public function scanQrcode($id, $type = 'id')
  57. {
  58. return $this->getModel()->where($type, $id)->inc('scan')->update();
  59. }
  60. /**
  61. * 获取临时二维码
  62. * @param $type
  63. * @param $id
  64. * @return array|Model|null
  65. * @throws DataNotFoundException
  66. * @throws DbException
  67. * @throws ModelNotFoundException
  68. */
  69. public function getTemporaryQrcode($type, $id)
  70. {
  71. $where['third_id'] = $id;
  72. $where['third_type'] = $type;
  73. $where['type'] = 2;
  74. $res = $this->getOne($where);
  75. if (!$res) {
  76. $this->createTemporaryQrcode($id, $type);
  77. $res = $this->getTemporaryQrcode($type, $id);
  78. } else if (empty($res['expire_seconds']) || $res['expire_seconds'] < time()) {
  79. $this->createTemporaryQrcode($id, $type, $res['id']);
  80. $res = $this->getTemporaryQrcode($type, $id);
  81. }
  82. if (!$res['ticket']) throw new AdminException('临时二维码获取错误');
  83. return $res;
  84. }
  85. /**
  86. * 临时二维码生成
  87. * @param $id
  88. * @param $type
  89. * @param string $qrcode_id
  90. */
  91. public function createTemporaryQrcode($id, $type, $qrcode_id = '')
  92. {
  93. $qrcode = OfficialAccount::qrcodeService();
  94. $data = $qrcode->temporary($id, 30 * 24 * 3600);
  95. $data['qrcode_url'] = $data['url'];
  96. $data['expire_seconds'] = $data['expire_seconds'] + time();
  97. $data['url'] = $qrcode->url($data['ticket']);
  98. $data['status'] = 1;
  99. $data['third_id'] = $id;
  100. $data['third_type'] = $type;
  101. $data['type'] = 2;
  102. if ($qrcode_id) {
  103. $this->update($qrcode_id, $data);
  104. } else {
  105. $data['add_time'] = time();
  106. $this->save($data);
  107. }
  108. }
  109. /**
  110. * 获取永久二维码
  111. * @param $type
  112. * @param $id
  113. * @return array|mixed|Model
  114. * @throws DataNotFoundException
  115. * @throws DbException
  116. * @throws ModelNotFoundException
  117. */
  118. public function getForeverQrcode($type, $id)
  119. {
  120. $where['third_id'] = $id;
  121. $where['third_type'] = $type;
  122. $where['type'] = 2;
  123. $res = $this->getOne($where);
  124. if (!$res) {
  125. $this->createForeverQrcode($id, $type);
  126. $res = $this->getForeverQrcode($type, $id);
  127. }
  128. if (!$res['ticket']) throw new AdminException('永久二维码获取错误');
  129. return $res;
  130. }
  131. /**
  132. * 永久二维码生成
  133. * @param $id
  134. * @param $type
  135. */
  136. public function createForeverQrcode($id, $type)
  137. {
  138. $qrcode = OfficialAccount::qrcodeService();
  139. $data = $qrcode->forever($id);
  140. $data['qrcode_url'] = $data['url'];
  141. $data['url'] = $qrcode->url($data['ticket']);
  142. $data['expire_seconds'] = 0;
  143. $data['status'] = 1;
  144. $data['third_id'] = $id;
  145. $data['third_type'] = $type;
  146. $data['add_time'] = time();
  147. $data['type'] = 2;
  148. $this->save($data);
  149. }
  150. /**
  151. * 获取小程序分享二维码
  152. * @param int $uid
  153. * @param string $namePath
  154. * @param bool $isSaveAttach
  155. * @param array $appendData
  156. * @param string $savePath
  157. * @return false|mixed|string
  158. */
  159. public function getRoutineQrcode(int $uid, string $namePath = '', bool $isSaveAttach = true, array $appendData = [], string $savePath = 'qrcode/routine')
  160. {
  161. $data = 'spread_uid=' . $uid;
  162. $page = 'pages/index/index';
  163. if (!$page || !$namePath) {
  164. return false;
  165. }
  166. try {
  167. /** @var SystemAttachmentServices $systemAttachmentService */
  168. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  169. if (!$isSaveAttach) {
  170. $imageInfo = "";
  171. } else {
  172. $imageInfo = $systemAttachmentService->getOne(['name' => $savePath . '/' . $namePath]);
  173. //检测远程文件是否存在
  174. if (isset($imageInfo['att_dir']) && strstr($imageInfo['att_dir'], 'http') !== false && curl_file_exist($imageInfo['att_dir']) === false) {
  175. $imageInfo = '';
  176. $systemAttachmentService->delete(['name' => $savePath . '/' . $namePath]);
  177. }
  178. }
  179. $siteUrl = sys_config('site_url');
  180. if (!$imageInfo) {
  181. $res = MiniProgram::appCodeUnlimit($data, $page, 280);
  182. if (!$res) return false;
  183. $uploadType = (int)sys_config('upload_type', 1);
  184. $upload = UploadService::init($uploadType);
  185. $res = (string)Utils::streamFor($res);
  186. $res = $upload->to($savePath)->validate()->stream($res, $namePath);
  187. if ($res === false) {
  188. return false;
  189. }
  190. $imageInfo = $upload->getUploadInfo();
  191. $imageInfo['image_type'] = $uploadType;
  192. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  193. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  194. if (!$remoteImage['status']) return false;
  195. if ($isSaveAttach) {
  196. $systemAttachmentService->save([
  197. 'name' => $imageInfo['name'],
  198. 'att_dir' => $imageInfo['dir'],
  199. 'satt_dir' => $imageInfo['thumb_path'],
  200. 'att_size' => $imageInfo['size'],
  201. 'att_type' => $imageInfo['type'],
  202. 'image_type' => $imageInfo['image_type'],
  203. 'module_type' => 2,
  204. 'time' => time(),
  205. 'pid' => 1,
  206. 'type' => 2
  207. ]);
  208. }
  209. $url = $imageInfo['dir'];
  210. } else $url = $imageInfo['att_dir'];
  211. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  212. return $url;
  213. } catch (\Throwable $e) {
  214. return false;
  215. }
  216. }
  217. }