QrcodeServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. declare (strict_types=1);
  12. namespace app\services\other;
  13. use app\services\BaseServices;
  14. use app\dao\other\QrcodeDao;
  15. use app\services\system\attachment\SystemAttachmentServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\UploadService;
  18. use crmeb\services\UtilService;
  19. use crmeb\services\wechat\MiniProgram;
  20. use crmeb\services\wechat\OfficialAccount;
  21. use crmeb\traits\ServicesTrait;
  22. use GuzzleHttp\Psr7\Utils;
  23. use think\exception\ValidateException;
  24. /**
  25. *
  26. * Class QrcodeServices
  27. * @package app\services\other
  28. * @mixin QrcodeDao
  29. */
  30. class QrcodeServices extends BaseServices
  31. {
  32. use ServicesTrait;
  33. /**
  34. * QrcodeServices constructor.
  35. * @param QrcodeDao $dao
  36. */
  37. public function __construct(QrcodeDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /**
  42. * 获取临时二维码
  43. * @param $type
  44. * @param $id
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function getTemporaryQrcode($type, $id)
  50. {
  51. $where['third_id'] = $id;
  52. $where['third_type'] = $type;
  53. $res = $this->dao->getOne($where);
  54. if (!$res) {
  55. $this->createTemporaryQrcode($id, $type);
  56. $res = $this->getTemporaryQrcode($type, $id);
  57. } else if (empty($res['expire_seconds']) || $res['expire_seconds'] < time()) {
  58. $this->createTemporaryQrcode($id, $type, $res['id']);
  59. $res = $this->getTemporaryQrcode($type, $id);
  60. }
  61. if (!$res['ticket']) throw new AdminException('临时二维码获取错误');
  62. return $res;
  63. }
  64. /**
  65. * 临时二维码生成
  66. * @param $id
  67. * @param $type
  68. * @param string $qrcode_id
  69. */
  70. public function createTemporaryQrcode($id, $type, $qrcode_id = '')
  71. {
  72. $qrcode = OfficialAccount::qrcodeService();
  73. $data = $qrcode->temporary($id, 30 * 24 * 3600);
  74. $data['qrcode_url'] = $data['url'];
  75. $data['expire_seconds'] = $data['expire_seconds'] + time();
  76. $data['url'] = $qrcode->url($data['ticket']);
  77. $data['status'] = 1;
  78. $data['third_id'] = $id;
  79. $data['third_type'] = $type;
  80. if ($qrcode_id) {
  81. $this->dao->update($qrcode_id, $data);
  82. } else {
  83. $data['add_time'] = time();
  84. $this->dao->save($data);
  85. }
  86. }
  87. /**
  88. * 获取永久二维码
  89. * @param $type
  90. * @param $id
  91. * @return array|mixed|\think\Model
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function getForeverQrcode($type, $id)
  97. {
  98. $where['third_id'] = $id;
  99. $where['third_type'] = $type;
  100. $res = $this->dao->getOne($where);
  101. if (!$res) {
  102. $this->createForeverQrcode($id, $type);
  103. $res = $this->getForeverQrcode($type, $id);
  104. }
  105. if (!$res['ticket']) throw new AdminException('永久二维码获取错误');
  106. return $res;
  107. }
  108. /**
  109. * 永久二维码生成
  110. * @param $id
  111. * @param $type
  112. */
  113. public function createForeverQrcode($id, $type)
  114. {
  115. $qrcode = OfficialAccount::qrcodeService();
  116. $data = $qrcode->forever($id);
  117. $data['qrcode_url'] = $data['url'];
  118. $data['url'] = $qrcode->url($data['ticket']);
  119. $data['expire_seconds'] = 0;
  120. $data['status'] = 1;
  121. $data['third_id'] = $id;
  122. $data['third_type'] = $type;
  123. $data['add_time'] = time();
  124. $this->dao->save($data);
  125. }
  126. /**
  127. * 获取二维码完整路径,不存在则自动生成
  128. * @param string $name
  129. * @param string $link
  130. * @param bool $force
  131. * @return bool|mixed|string
  132. */
  133. public function getWechatQrcodePath(string $name, string $link, bool $force = false, bool $isSaveAttach = true)
  134. {
  135. /** @var SystemAttachmentServices $systemAttachmentService */
  136. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  137. try {
  138. if (!$isSaveAttach) {
  139. $imageInfo = "";
  140. } else {
  141. $imageInfo = $systemAttachmentService->getOne(['name' => $name]);
  142. }
  143. $siteUrl = sys_config('site_url');
  144. if (!$imageInfo) {
  145. $codeUrl = UtilService::setHttpType($siteUrl . $link, request()->isSsl() ? 0 : 1);//二维码链接
  146. $imageInfo = UtilService::getQRCodePath($codeUrl, $name);
  147. if (is_string($imageInfo) && $force)
  148. return false;
  149. if (is_array($imageInfo)) {
  150. if ($isSaveAttach) {
  151. $systemAttachmentService->save([
  152. 'name' => $imageInfo['name'],
  153. 'att_dir' => $imageInfo['dir'],
  154. 'satt_dir' => $imageInfo['thumb_path'],
  155. 'att_size' => $imageInfo['size'],
  156. 'att_type' => $imageInfo['type'],
  157. 'image_type' => $imageInfo['image_type'],
  158. 'module_type' => 2,
  159. 'time' => time(),
  160. 'pid' => 1,
  161. 'type' => 1
  162. ]);
  163. }
  164. $url = $imageInfo['dir'];
  165. } else {
  166. $url = '';
  167. $imageInfo = ['image_type' => 0];
  168. }
  169. } else $url = $imageInfo['att_dir'];
  170. if ($imageInfo['image_type'] == 1 && $url) $url = $siteUrl . $url;
  171. return $url;
  172. } catch (\Throwable $e) {
  173. if ($force)
  174. return false;
  175. else
  176. return '';
  177. }
  178. }
  179. /**
  180. * 获取小程序分享二维码
  181. * @param int $id
  182. * @param int $uid
  183. * @param int $type 1 = 拼团,2 = 秒杀
  184. * @return bool|string
  185. */
  186. public function getRoutineQrcodePath(int $id, int $uid, int $type, array $parame = [], bool $isSaveAttach = true)
  187. {
  188. /** @var SystemAttachmentServices $systemAttachmentService */
  189. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  190. $page = '';
  191. $namePath = '';
  192. $data = 'id=' . $id . '&spid=' . $uid;
  193. switch ($type) {
  194. case 0:
  195. $page = 'pages/goods_details/index';
  196. $namePath = $id . '_' . $uid . '_' . $parame['is_promoter'] . '_product.jpg';
  197. break;
  198. case 1:
  199. $page = 'pages/activity/goods_combination_details/index';
  200. $namePath = 'combination_' . $id . '_' . $uid . '.jpg';
  201. break;
  202. case 2:
  203. $page = 'pages/activity/goods_seckill_details/index';
  204. $namePath = 'seckill_' . $id . '_' . $uid . '.jpg';
  205. if (isset($parame['stop_time']) && $parame['stop_time']) {
  206. $data .= '&time=' . $parame['stop_time'];
  207. $namePath = $parame['stop_time'] . $namePath;
  208. }
  209. break;
  210. case 3:
  211. $page = 'pages/annex/offline_pay/index';
  212. $namePath = 'routine_offline_scan.jpg';
  213. break;
  214. case 4:
  215. $page = 'pages/annex/vip_active/index';
  216. $namePath = 'routine_member_card.jpg';
  217. break;
  218. case 5:
  219. $page = 'pages/annex/vip_paid/index';
  220. $namePath = 'routine_pay_vip_code.jpg';
  221. break;
  222. case 6:
  223. $page = 'pages/annex/special/index';
  224. $namePath = $id . 'routine_annex_index_code.jpg';
  225. break;
  226. case 7:
  227. $page = 'pages/goods/order_pay/index';
  228. $namePath = 'routine_cashier_pay' . $id . '.jpg';
  229. $data = 'store_id=' . $id;
  230. break;
  231. case 8:
  232. $page = 'pages/index/index';
  233. $namePath = $id . 'routine_index_code.jpg';
  234. break;
  235. case 9: //桌码二维码
  236. $page = 'pages/store/table_code/index';
  237. $namePath = 'routine_table_code_' . $parame['store_id'] . '_' . $parame['table_number'] . '_' . $id . '.jpg';
  238. $data = 'qrcode_id=' . $id. '&store_id=' . $parame['store_id'];
  239. break;
  240. case 10: //门店二维码
  241. $page = 'pages/store_cate/store_cate';
  242. $namePath = 'routine_store_cate_id_' . $id . '.jpg';
  243. $data = 'id=' . $id;
  244. break;
  245. }
  246. if (!$page || !$namePath) {
  247. return false;
  248. }
  249. try {
  250. $to = 'routine/product';
  251. if (!$isSaveAttach) {
  252. $imageInfo = "";
  253. } else {
  254. $imageInfo = $systemAttachmentService->getOne(['name' => $to . '/' . $namePath]);
  255. }
  256. $siteUrl = sys_config('site_url');
  257. if (!$imageInfo) {
  258. $res = MiniProgram::appCodeUnlimit($data, $page, 280);
  259. if (!$res) return false;
  260. $uploadType = (int)sys_config('upload_type', 1);
  261. $upload = UploadService::init($uploadType);
  262. $res = (string)Utils::streamFor($res);
  263. $res = $upload->to($to)->validate()->stream($res, $namePath);
  264. if ($res === false) {
  265. return false;
  266. }
  267. $imageInfo = $upload->getUploadInfo();
  268. $imageInfo['image_type'] = $uploadType;
  269. if ($imageInfo['image_type'] == 1) $remoteImage = UtilService::remoteImage($siteUrl . $imageInfo['dir']);
  270. else $remoteImage = UtilService::remoteImage($imageInfo['dir']);
  271. if (!$remoteImage['status']) return false;
  272. if ($isSaveAttach) {
  273. $systemAttachmentService->save([
  274. 'name' => $imageInfo['name'],
  275. 'att_dir' => $imageInfo['dir'],
  276. 'satt_dir' => $imageInfo['thumb_path'],
  277. 'att_size' => $imageInfo['size'],
  278. 'att_type' => $imageInfo['type'],
  279. 'image_type' => $imageInfo['image_type'],
  280. 'module_type' => 2,
  281. 'time' => time(),
  282. 'pid' => 1,
  283. 'type' => 2
  284. ]);
  285. }
  286. $url = $imageInfo['dir'];
  287. } else $url = $imageInfo['att_dir'];
  288. if ($imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  289. return $url;
  290. } catch (\Throwable $e) {
  291. return false;
  292. }
  293. }
  294. /**
  295. * 添加二维码 存在直接获取
  296. * @param int $thirdId
  297. * @param string $thirdType
  298. * @param string $page
  299. * @param string $qrCodeLink
  300. * @return array|false|object|\PDOStatement|string|\think\Model
  301. * @throws \think\Exception
  302. * @throws \think\db\exception\DataNotFoundException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. * @throws \think\exception\DbException
  305. */
  306. public function qrCodeForever($thirdId = 0, $thirdType = 'spread', $page = '', $qrCodeLink = '')
  307. {
  308. $qrcode = $this->dao->getOne(['third_id' => $thirdId, 'third_type' => $thirdType]);
  309. if ($qrcode) {
  310. return $qrcode;
  311. }
  312. return $this->setQrcodeForever($thirdId, $thirdType, $page, $qrCodeLink);
  313. }
  314. /**
  315. * 检测是否存在
  316. * @param int $thirdId
  317. * @param string $thirdType
  318. * @return int
  319. */
  320. public function qrCodeExist($thirdId = 0, $thirdType = 'spread')
  321. {
  322. return !!$this->dao->getCount(['third_id' => $thirdId, 'third_type' => $thirdType]);
  323. }
  324. /**
  325. * 添加二维码记录
  326. * @param int $thirdId
  327. * @param string $thirdType
  328. * @param string $page
  329. * @param string $qrCodeLink
  330. * @return object
  331. */
  332. public function setQrcodeForever($thirdId = 0, $thirdType = 'spread', $page = '', $qrCodeLink = '')
  333. {
  334. $data['third_type'] = $thirdType;
  335. $data['third_id'] = $thirdId;
  336. $data['status'] = 1;
  337. $data['add_time'] = time();
  338. $data['page'] = $page;
  339. $data['url_time'] = '';
  340. $data['qrcode_url'] = $qrCodeLink;
  341. if (!$re = $this->dao->save($data)) {
  342. throw new ValidateException('生成失败');
  343. }
  344. return $re;
  345. }
  346. /**
  347. * 修改二维码地址
  348. * @param int $id
  349. * @param array $data
  350. * @return bool
  351. */
  352. public function setQrcodeFind($id = 0, $data = array())
  353. {
  354. if (!$id) return false;
  355. if (!$this->dao->get((int)$id)) {
  356. throw new ValidateException('数据不存在');
  357. }
  358. if (!$re = $this->dao->update($id, $data, 'id')) {
  359. throw new ValidateException('修改数据失败');
  360. }
  361. return $re;
  362. }
  363. }