GiftCardInfoLists.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /**
  31. * 批次礼品卡信息列表接口
  32. * Class GoodsLists
  33. * @package app\adminapi\lists\goods
  34. */
  35. class GiftCardInfoLists extends BaseAdminDataLists
  36. {
  37. // ... existing code ...
  38. use ListsSearchTrait;
  39. use ListsExcelTrait;
  40. /**
  41. * 批量生成二维码
  42. * @return array
  43. */
  44. public function batchGenerateQrCode()
  45. {
  46. $giftCards = GiftCardInfo::where($this->setSearch())
  47. ->field('id,card_no,card_pass')
  48. ->select()
  49. ->toArray();
  50. return GiftCardQrCodeService::batchGenerateQrCode($giftCards);
  51. }
  52. // ... existing code ...
  53. /**
  54. * @notes 搜索条件
  55. * @return array
  56. * @author cjhao
  57. * @date 2021/7/22 10:51
  58. */
  59. public function setSearch(): array
  60. {
  61. $where = [];
  62. $params = $this->params;
  63. //下单时间
  64. if (isset($params['start_time']) && $params['start_time'] != '') {
  65. $where[] = ['create_time', '>=', strtotime($params['start_time'])];
  66. }
  67. if (isset($params['end_time']) && $params['end_time'] != '') {
  68. $where[] = ['create_time', '<=', strtotime($params['end_time'])];
  69. }
  70. $where[]=['gc_id','=',$params['id']];
  71. if(isset($params['card_no'])){
  72. $where[] = ['card_no', 'like', '%' . $params['card_no'] . '%'];
  73. }
  74. if(isset($params['is_used'])){
  75. $where[] = ['is_used', '=', $params['is_used']];
  76. }
  77. return $where;
  78. }
  79. /**
  80. * @notes 统计信息
  81. * @return array
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. * @author cjhao
  86. * @date 2021/7/22 10:51
  87. */
  88. public function extend(): array
  89. {
  90. return [];
  91. }
  92. /**
  93. * @notes 礼品卡列表
  94. * @return array
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @author cjhao
  99. * @date 2021/7/21 18:31
  100. */
  101. public function lists(): array
  102. {
  103. $field = 'id,gc_id,card_no,card_pass,card_money,is_used,user_id,used_time,create_time,qr_code_path';
  104. $lists = GiftCardInfo::field($field)->where($this->setSearch())
  105. ->with(['user'])
  106. ->append(['is_used_desc','used_user_name','batch_no','qr_code_url'])
  107. ->limit($this->limitOffset, $this->limitLength)
  108. ->order('id', 'desc')
  109. ->select()
  110. ->toArray();
  111. //
  112. foreach ($lists as &$list) {
  113. $list['qr_code_url'] = 'https://nongfa.sdshengyuekeji.cn/'.$list['qr_code_path'];
  114. }
  115. return $lists;
  116. }
  117. /**
  118. * @notes 总数
  119. * @return int
  120. * @author cjhao
  121. * @date 2021/7/21 18:32
  122. */
  123. public function count(): int
  124. {
  125. return GiftCardInfo::where($this->setSearch())->count();
  126. }
  127. /**
  128. * @notes 设置excel表名
  129. * @return string
  130. * @author cjhao
  131. * @date 2021/9/23 9:52
  132. */
  133. public function setFileName(): string
  134. {
  135. return '礼品卡列表';
  136. }
  137. /**
  138. * @notes 设置导出字段
  139. * @return array
  140. * @author cjhao
  141. * @date 2021/9/23 9:59
  142. */
  143. public function setExcelFields(): array
  144. {
  145. return [
  146. 'batch_no' => '批次',
  147. 'card_no' => '礼品卡卡号',
  148. 'card_pass' => '礼品卡密码',
  149. 'card_money' => '礼品卡价值',
  150. 'is_used_desc' => '是否使用',
  151. 'used_user_name' => '使用人',
  152. 'used_time' => '使用时间',
  153. 'create_time'=> '创建时间',
  154. 'qr_code_url' => '小程序码', // 添加小程序码字段
  155. ];
  156. }
  157. /**
  158. * @notes 创建包含图片的excel
  159. * @param $excelFields
  160. * @param $lists
  161. * @return string
  162. * @throws \PhpOffice\PhpSpreadsheet\Exception
  163. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  164. */
  165. public function createExcel($excelFields, $lists)
  166. {
  167. $title = array_values($excelFields);
  168. $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'];
  169. $data = [];
  170. foreach ($lists as $row) {
  171. $temp = [];
  172. foreach ($excelFields as $key => $excelField) {
  173. $fieldData = $row[$key];
  174. if(is_numeric($fieldData) && strlen($fieldData) >= 11){
  175. $fieldData.="\t";
  176. }
  177. $temp[$key] = $fieldData;
  178. }
  179. $data[] = $temp;
  180. }
  181. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  182. $sheet = $spreadsheet->getActiveSheet();
  183. //设置单元格内容
  184. foreach ($title as $key => $value) {
  185. // 单元格内容写入
  186. $sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
  187. }
  188. //设置行高
  189. $spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(25);
  190. $row = 2; //从第二行开始
  191. $qrCodeColumnIndex = array_search('qr_code_url', array_keys($excelFields)); // 获取二维码列的索引
  192. foreach ($data as $rowIndex => $item) {
  193. $column = 1;
  194. foreach ($item as $key => $value) {
  195. $columnIndex = $column - 1;
  196. // 如果是二维码列且有URL,插入图片
  197. if ($key === 'qr_code_url' && !empty($value)) {
  198. try {
  199. // 设置单元格为空,为图片腾出空间
  200. $sheet->setCellValueByColumnAndRow($column, $row, '');
  201. // 下载图片到临时文件
  202. $imageContent = @file_get_contents($value);
  203. if ($imageContent !== false) {
  204. $tempFile = sys_get_temp_dir() . '/qr_' . $rowIndex . '_' . time() . '.png';
  205. file_put_contents($tempFile, $imageContent);
  206. // 创建图片对象
  207. $drawing = new Drawing();
  208. $drawing->setName('QR Code');
  209. $drawing->setDescription('小程序码');
  210. $drawing->setPath($tempFile);
  211. $drawing->setHeight(60); // 设置图片高度
  212. $drawing->setWidth(60); // 设置图片宽度
  213. // 设置图片位置
  214. $cellCoordinate = $letter_column[$columnIndex] . $row;
  215. $drawing->setCoordinates($cellCoordinate);
  216. $drawing->setOffsetX(5); // X轴偏移
  217. $drawing->setOffsetY(5); // Y轴偏移
  218. // 将图片添加到工作表
  219. $drawing->setWorksheet($sheet);
  220. // 设置行高以适应图片
  221. $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(70);
  222. // 清理临时文件
  223. @unlink($tempFile);
  224. } else {
  225. // 如果图片下载失败,显示URL
  226. $sheet->setCellValueByColumnAndRow($column, $row, $value);
  227. }
  228. } catch (\Exception $e) {
  229. // 如果图片插入失败,显示URL
  230. $sheet->setCellValueByColumnAndRow($column, $row, $value);
  231. }
  232. } else {
  233. // 普通文本数据
  234. $value = strpos($value, '=') === 0 ? " " . $value : $value;
  235. $sheet->setCellValueByColumnAndRow($column, $row, $value);
  236. }
  237. // 设置列宽
  238. $columnWidth = ($key === 'qr_code_url') ? 15 : max([strlen($value) * 1.2, 15]);
  239. $spreadsheet->getActiveSheet()->getColumnDimension($letter_column[$columnIndex])->setWidth($columnWidth);
  240. $column++;
  241. }
  242. // 设置默认行高
  243. if (!isset($qrCodeColumnIndex) || empty($data[$rowIndex]['qr_code_url'])) {
  244. $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(25);
  245. }
  246. $row++;
  247. }
  248. $getHighestRowAndColumn = $sheet->getHighestRowAndColumn();
  249. $HighestRow = $getHighestRowAndColumn['row'];
  250. $columnLetter = $getHighestRowAndColumn['column'];
  251. $titleScope = 'A1:' . $columnLetter . '1';//第一(标题)范围
  252. // 设置标题样式
  253. $sheet->getStyle($titleScope)
  254. ->getFill()
  255. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  256. ->getStartColor()
  257. ->setARGB('26956d');
  258. $sheet->getStyle($titleScope)->getFont()->getColor()
  259. ->setARGB('FFFFFF');
  260. $allScope = 'A1:' . $columnLetter . $HighestRow;
  261. // 设置居中对齐
  262. $styleArray = [
  263. 'alignment' => [
  264. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  265. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  266. ],
  267. ];
  268. $sheet->getStyle($allScope)->applyFromArray($styleArray);
  269. // 设置边框
  270. $sheet->getStyle($allScope)->getBorders()->getAllBorders()
  271. ->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
  272. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  273. //创建excel文件
  274. $exportCache = new \app\common\cache\ExportCache();
  275. $src = $exportCache->getSrc();
  276. if (!file_exists($src)) {
  277. mkdir($src, 0775, true);
  278. }
  279. $writer->save($src . $this->fileName);
  280. //设置本地excel缓存并返回下载地址
  281. return (string)(url('index/download/export', ['file' => $exportCache->setFile($this->fileName)], true, true));
  282. }
  283. }