GiftCardInfoLists.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\lists\gift_card;
  20. use app\adminapi\{
  21. lists\BaseAdminDataLists,
  22. };
  23. use app\common\{
  24. lists\ListsExcelInterface,
  25. lists\ListsExtendInterface,
  26. model\GiftCardInfo};
  27. use app\common\service\FileService;
  28. use app\common\service\GiftCardQrCodeService;
  29. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  30. use app\common\lists\ListsExcelTrait;
  31. use app\common\lists\ListsSearchTrait; // 修正命名空间
  32. /**
  33. * 批次礼品卡信息列表接口
  34. * Class GoodsLists
  35. * @package app\adminapi\lists\goods
  36. */
  37. class GiftCardInfoLists extends BaseAdminDataLists implements ListsExcelInterface
  38. {
  39. // ... existing code ...
  40. use ListsSearchTrait;
  41. use ListsExcelTrait;
  42. /**
  43. * 批量生成二维码
  44. * @return array
  45. */
  46. public function batchGenerateQrCode()
  47. {
  48. $giftCards = GiftCardInfo::where($this->setSearch())
  49. ->field('id,card_no,card_pass')
  50. ->select()
  51. ->toArray();
  52. return GiftCardQrCodeService::batchGenerateQrCode($giftCards);
  53. }
  54. // ... existing code ...
  55. /**
  56. * @notes 搜索条件
  57. * @return array
  58. * @author cjhao
  59. * @date 2021/7/22 10:51
  60. */
  61. public function setSearch(): array
  62. {
  63. $where = [];
  64. $params = $this->params;
  65. //下单时间
  66. if (isset($params['start_time']) && $params['start_time'] != '') {
  67. $where[] = ['create_time', '>=', strtotime($params['start_time'])];
  68. }
  69. if (isset($params['end_time']) && $params['end_time'] != '') {
  70. $where[] = ['create_time', '<=', strtotime($params['end_time'])];
  71. }
  72. $where[]=['gc_id','=',$params['id']];
  73. if(isset($params['card_no'])){
  74. $where[] = ['card_no', 'like', '%' . $params['card_no'] . '%'];
  75. }
  76. if(isset($params['is_used'])){
  77. $where[] = ['is_used', '=', $params['is_used']];
  78. }
  79. return $where;
  80. }
  81. /**
  82. * @notes 统计信息
  83. * @return array
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. * @author cjhao
  88. * @date 2021/7/22 10:51
  89. */
  90. public function extend(): array
  91. {
  92. return [];
  93. }
  94. /**
  95. * @notes 礼品卡列表
  96. * @return array
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. * @author cjhao
  101. * @date 2021/7/21 18:31
  102. */
  103. public function lists(): array
  104. {
  105. $field = 'id,gc_id,card_no,card_pass,card_money,is_used,user_id,used_time,create_time,qr_code_path';
  106. $lists = GiftCardInfo::field($field)->where($this->setSearch())
  107. ->with(['user'])
  108. ->append(['is_used_desc','used_user_name','batch_no','qr_code_url'])
  109. ->limit($this->limitOffset, $this->limitLength)
  110. ->order('id', 'desc')
  111. ->select()
  112. ->toArray();
  113. //
  114. // foreach ($lists as &$list) {
  115. // $list['qr_code_url'] = 'https://nongfa.sdshengyuekeji.cn/'.$list['qr_code_path'];
  116. // }
  117. return $lists;
  118. }
  119. /**
  120. * @notes 总数
  121. * @return int
  122. * @author cjhao
  123. * @date 2021/7/21 18:32
  124. */
  125. public function count(): int
  126. {
  127. return GiftCardInfo::where($this->setSearch())->count();
  128. }
  129. /**
  130. * @notes 设置excel表名
  131. * @return string
  132. * @author cjhao
  133. * @date 2021/9/23 9:52
  134. */
  135. public function setFileName(): string
  136. {
  137. return '礼品卡列表';
  138. }
  139. /**
  140. * @notes 设置导出字段
  141. * @return array
  142. * @author cjhao
  143. * @date 2021/9/23 9:59
  144. */
  145. public function setExcelFields(): array
  146. {
  147. return [
  148. 'batch_no' => '批次',
  149. 'card_no' => '礼品卡卡号',
  150. 'card_pass' => '礼品卡密码',
  151. 'card_money' => '礼品卡价值',
  152. 'is_used_desc' => '是否使用',
  153. 'used_user_name' => '使用人',
  154. 'used_time' => '使用时间',
  155. 'create_time'=> '创建时间',
  156. 'qr_code_url' => '小程序码', // 添加小程序码字段
  157. ];
  158. }
  159. /**
  160. * @notes 创建包含图片的excel
  161. * @param $excelFields
  162. * @param $lists
  163. * @return string
  164. * @throws \PhpOffice\PhpSpreadsheet\Exception
  165. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  166. */
  167. public function createExcel($excelFields, $lists)
  168. {
  169. $title = array_values($excelFields);
  170. $letter_column = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  171. $data = [];
  172. foreach ($lists as $row) {
  173. $temp = [];
  174. foreach ($excelFields as $key => $excelField) {
  175. $fieldData = $row[$key];
  176. if(is_numeric($fieldData) && strlen($fieldData) >= 11){
  177. $fieldData.="\t";
  178. }
  179. $temp[$key] = $fieldData;
  180. }
  181. $data[] = $temp;
  182. }
  183. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  184. $sheet = $spreadsheet->getActiveSheet();
  185. //设置单元格内容
  186. foreach ($title as $key => $value) {
  187. // 单元格内容写入
  188. $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
  189. }
  190. //设置行高
  191. $spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(25);
  192. $row = 2; //从第二行开始
  193. $qrCodeColumnIndex = array_search('qr_code_url', array_keys($excelFields)); // 获取二维码列的索引
  194. // 创建临时目录
  195. $tempDir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'excel_qr_codes';
  196. if (!is_dir($tempDir)) {
  197. mkdir($tempDir, 0755, true);
  198. }
  199. foreach ($data as $rowIndex => $item) {
  200. $column = 1;
  201. foreach ($item as $key => $value) {
  202. $columnIndex = $column - 1;
  203. // 如果是二维码列且有URL,插入图片
  204. if ($key === 'qr_code_url' && !empty($value)) {
  205. try {
  206. // 设置单元格为空,为图片腾出空间
  207. $sheet->setCellValueByColumnAndRow($column, $row, '');
  208. // 创建临时文件路径
  209. $tempFile = $tempDir . DIRECTORY_SEPARATOR . 'qr_' . $rowIndex . '_' . time() . '_' . mt_rand(1000, 9999) . '.png';
  210. // 下载图片到临时文件
  211. $context = stream_context_create([
  212. 'http' => [
  213. 'timeout' => 10,
  214. 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  215. ]
  216. ]);
  217. $imageContent = @file_get_contents($value, false, $context);
  218. if ($imageContent !== false && strlen($imageContent) > 0) {
  219. // 写入临时文件
  220. if (file_put_contents($tempFile, $imageContent) !== false && file_exists($tempFile)) {
  221. // 验证是否为有效图片
  222. $imageInfo = @getimagesize($tempFile);
  223. if ($imageInfo !== false) {
  224. // 创建图片对象
  225. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  226. $drawing->setName('QR Code');
  227. $drawing->setDescription('小程序码');
  228. $drawing->setPath($tempFile);
  229. $drawing->setHeight(60); // 设置图片高度
  230. $drawing->setWidth(60); // 设置图片宽度
  231. // 设置图片位置
  232. $cellCoordinate = $letter_column[$columnIndex] . $row;
  233. $drawing->setCoordinates($cellCoordinate);
  234. $drawing->setOffsetX(5); // X轴偏移
  235. $drawing->setOffsetY(5); // Y轴偏移
  236. // 将图片添加到工作表
  237. $drawing->setWorksheet($sheet);
  238. // 设置行高以适应图片
  239. $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(70);
  240. } else {
  241. // 图片无效,显示URL
  242. $sheet->setCellValueByColumnAndRow($column, $row, '图片无效');
  243. @unlink($tempFile);
  244. }
  245. } else {
  246. // 文件写入失败,显示URL
  247. $sheet->setCellValueByColumnAndRow($column, $row, '文件写入失败');
  248. }
  249. } else {
  250. // 如果图片下载失败,显示提示
  251. $sheet->setCellValueByColumnAndRow($column, $row, '图片下载失败');
  252. }
  253. } catch (\Exception $e) {
  254. // 如果图片插入失败,显示错误信息
  255. $sheet->setCellValueByColumnAndRow($column, $row, '图片处理失败: ' . $e->getMessage());
  256. }
  257. } else {
  258. // 普通文本数据
  259. $value = strpos($value, '=') === 0 ? " " . $value : $value;
  260. $sheet->setCellValueByColumnAndRow($column, $row, $value);
  261. }
  262. // 设置列宽
  263. $columnWidth = ($key === 'qr_code_url') ? 15 : max([strlen($value) * 1.2, 15]);
  264. $spreadsheet->getActiveSheet()->getColumnDimension($letter_column[$columnIndex])->setWidth($columnWidth);
  265. $column++;
  266. }
  267. // 设置默认行高
  268. if (!isset($qrCodeColumnIndex) || empty($data[$rowIndex]['qr_code_url'])) {
  269. $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(25);
  270. }
  271. $row++;
  272. }
  273. $getHighestRowAndColumn = $sheet->getHighestRowAndColumn();
  274. $HighestRow = $getHighestRowAndColumn['row'];
  275. $columnLetter = $getHighestRowAndColumn['column'];
  276. $titleScope = 'A1:' . $columnLetter . '1';//第一(标题)范围
  277. // 设置标题样式
  278. $sheet->getStyle($titleScope)
  279. ->getFill()
  280. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  281. ->getStartColor()
  282. ->setARGB('26956d');
  283. $sheet->getStyle($titleScope)->getFont()->getColor()
  284. ->setARGB('FFFFFF');
  285. $allScope = 'A1:' . $columnLetter . $HighestRow;
  286. // 设置居中对齐
  287. $styleArray = [
  288. 'alignment' => [
  289. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  290. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  291. ],
  292. ];
  293. $sheet->getStyle($allScope)->applyFromArray($styleArray);
  294. // 设置边框
  295. $sheet->getStyle($allScope)->getBorders()->getAllBorders()
  296. ->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
  297. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  298. //创建excel文件
  299. $exportCache = new \app\common\cache\ExportCache();
  300. $src = $exportCache->getSrc();
  301. if (!file_exists($src)) {
  302. mkdir($src, 0775, true);
  303. }
  304. $writer->save($src . $this->fileName);
  305. // 清理临时文件
  306. $this->cleanupTempFiles($tempDir);
  307. //设置本地excel缓存并返回下载地址
  308. return (string)(url('index/download/export', ['file' => $exportCache->setFile($this->fileName)], true, true));
  309. }
  310. /**
  311. * @notes 清理临时文件
  312. * @param string $tempDir
  313. */
  314. private function cleanupTempFiles($tempDir)
  315. {
  316. if (is_dir($tempDir)) {
  317. $files = glob($tempDir . DIRECTORY_SEPARATOR . '*');
  318. foreach ($files as $file) {
  319. if (is_file($file)) {
  320. @unlink($file);
  321. }
  322. }
  323. @rmdir($tempDir);
  324. }
  325. }
  326. }