Helpers.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. use App\Service\Util\Support\Trace\CustomizeLogger;
  3. use Illuminate\Http\UploadedFile;
  4. use Modules\Common\Support\Upload\Uploader;
  5. use PHPMailer\PHPMailer\PHPMailer;
  6. if(!function_exists("str_decode")){
  7. /**
  8. * 字符解密
  9. * name: str_decode
  10. * @param $str
  11. * @return int
  12. * date 2022/09/27 11:38
  13. */
  14. function str_decode($str){
  15. $decode = Hashids::decode($str);
  16. $decode = is_array($decode) ? array_shift($decode) : $decode;
  17. if(empty($decodeBid)){
  18. myLog("StrDecodeError")->info("str : {$str}, decode : ".var_export($decode,true));
  19. }
  20. return intval($decode);
  21. }
  22. }
  23. if (!function_exists('str_encode')){
  24. /***
  25. * 字符加密
  26. * name: str_decode
  27. * @param mixed $str
  28. * @return mixed
  29. * date 2022/09/27 11:38
  30. */
  31. function str_encode($str)
  32. {
  33. return Hashids::encode($str);
  34. }
  35. }
  36. if (!function_exists('is_empty')){
  37. /**
  38. * 判断数据是否为空
  39. * name: is_empty
  40. * @param $data
  41. * @return bool 空返回true 非空返回false
  42. * date 2022/11/18 11:39
  43. */
  44. function is_empty($data): bool
  45. {
  46. if (is_object($data)){
  47. if (method_exists($data,'isEmpty')){
  48. return $data->isEmpty();
  49. }
  50. return empty(get_object_vars($data));
  51. }
  52. return empty($data);
  53. }
  54. }
  55. if (!function_exists('column_str')) {
  56. /**
  57. * 列转化
  58. * @param $key
  59. * @return string
  60. */
  61. function column_str($key)
  62. {
  63. $array = array(
  64. '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',
  65. 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ',
  66. 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ',
  67. 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ',
  68. 'DA', 'DB', 'DC', 'DD', 'DE', 'DF', 'DG', 'DH', 'DI', 'DJ', 'DK', 'DL', 'DM', 'DN', 'DO', 'DP', 'DQ', 'DR', 'DS', 'DT', 'DU', 'DV', 'DW', 'DX', 'DY', 'DZ',
  69. 'EA', 'EB', 'EC', 'ED', 'EE', 'EF', 'EG', 'EH', 'EI', 'EJ', 'EK', 'EL', 'EM', 'EN', 'EO', 'EP', 'EQ', 'ER', 'ES', 'ET', 'EU', 'EV', 'EW', 'EX', 'EY', 'EZ'
  70. );
  71. return $array[$key];
  72. }
  73. }
  74. if (!function_exists('column')) {
  75. /**
  76. * Excel 列转化
  77. * @param $key
  78. * @param mixed $columns
  79. * @return string
  80. */
  81. function column($key, $columns = 1)
  82. {
  83. return column_str($key) . $columns;
  84. }
  85. }
  86. if (!function_exists('export')) {
  87. /***
  88. * @param mixed $list 导出的数据
  89. * @param mixed $params 标题设置
  90. * @throws PHPExcel_Exception
  91. * @throws PHPExcel_Reader_Exception
  92. */
  93. function export($list, $params = array())
  94. {
  95. if (PHP_SAPI == 'cli') {
  96. die('This example should only be run from a Web Browser');
  97. }
  98. $excel = new \PHPExcel();
  99. $excel->getProperties()->setCreator("植宇")->setLastModifiedBy("植宇")->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")->setCategory("report file");
  100. $sheet = $excel->setActiveSheetIndex(0);
  101. $rowNum = 1;
  102. foreach ($params['columns'] as $key => $column) {
  103. $sheet->setCellValue(column($key, $rowNum), $column['title']);
  104. if (!empty($column['width'])) {
  105. $sheet->getColumnDimension(column_str($key))->setWidth($column['width']);
  106. }
  107. }
  108. $rowNum++;
  109. $len = count($params['columns']);;
  110. foreach ($list as $row) {
  111. for ($i = 0; $i < $len; $i++) {
  112. $value = $row[$params['columns'][$i]['field']] ?? '';
  113. $sheet->setCellValue(column($i, $rowNum), $value);
  114. }
  115. $rowNum++;
  116. }
  117. $excel->getActiveSheet()->setTitle($params['title']);
  118. $filename = urlencode($params['title'] . '-' . date('Y-m-d H:i', time()));
  119. ob_end_clean();
  120. header('Content-Type: application/octet-stream');
  121. header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
  122. header('Cache-Control: max-age=0');
  123. $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  124. $writer->save("php://output");
  125. }
  126. }
  127. /**
  128. * 发送邮件
  129. * @param $to_email 收件人
  130. * <pre>
  131. * [
  132. * ['address' => 'aa@bb.com', 'name' => 'aa'],
  133. * ......,
  134. * ['address' => 'aa@bb.com', 'name' => 'aa'],
  135. * ]
  136. * </pre>
  137. * @param $param
  138. * <pre>
  139. * [
  140. * 'subject' => "xxxxxx" , // 标题
  141. * 'body' => 'xxxx', // 邮件内容,如果内容需要换行,使用 <br>
  142. * ]
  143. * </pre>
  144. * @param string $accessory
  145. */
  146. function sendEmail($to_email, $param, $accessory = '')
  147. {
  148. $mail = new PHPMailer(true);// Passing `true` enables exceptions
  149. $mail->CharSet = 'UTF-8';//'UTF-8';
  150. try {
  151. $mail->SMTPDebug = 0; // Enable verbose debug output
  152. $mail->isSMTP(); // Set mailer to use SMTP
  153. $mail->Host = 'smtp.exmail.qq.com'; //$mail->Host = 'smtp.126.com'; Specify main and backup SMTP servers
  154. $mail->SMTPAuth = true; // Enable SMTP authentication
  155. $mail->Username = 'sendemail01@zkanshu.com'; //$mail->Username = 'tushengxiang@126.com'; // SMTP username
  156. $mail->Password = '5jBekvU2jJ2Ketue'; // SMTP password
  157. $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
  158. $mail->Port = 465; // TCP port to connect to
  159. //Recipients
  160. $mail->setFrom('sendemail01@zkanshu.com', 'System');
  161. $mail->addAddress($to_email[0]['address'], $to_email[0]['name']);
  162. array_shift($to_email);
  163. foreach ($to_email as $item) {
  164. $mail->addCC($item['address'], $item['name']); // Add a recipient
  165. }
  166. //Attachments
  167. if ($accessory) $mail->addAttachment($accessory);// Add attachments
  168. $mail->isHTML(true);
  169. $mail->Subject = $param['subject'];
  170. $mail->Body = $param['body'];
  171. $mail->send();
  172. } catch (\Exception $e) {
  173. \Log::warning('发送邮件失败:' . $mail->ErrorInfo, $e->getTrace());
  174. }
  175. }
  176. /**
  177. * 获取日志对象
  178. * @param $fileName 保存日志的名称
  179. * @param string $logLevel 日志等级
  180. * @param int $logDays 日志保留的天数
  181. * @return \Psr\Log\LoggerInterface
  182. */
  183. function myLog($fileName, $logLevel='info', $logDays = 7) :\Psr\Log\LoggerInterface{
  184. return CustomizeLogger::getLogger($fileName, $logLevel, $logDays);
  185. }
  186. if (!function_exists('get_date')) {
  187. /**
  188. * 获取时间
  189. * @param mixed $time
  190. * @param mixed $format
  191. * @return false|string
  192. */
  193. function get_date($time = 0, $format = "Y-m-d H:i:s")
  194. {
  195. $time = intval($time);
  196. if ($time <= 0) {
  197. $time = time();
  198. }
  199. return date($format, $time);
  200. }
  201. }
  202. /**
  203. * 华为obs文件上传
  204. * @param UploadedFile $file
  205. * @return array
  206. */
  207. function huaweiObsUpload(UploadedFile $file) {
  208. $uploader = new Uploader();
  209. $uploader->setDriver('HuaweiOBS');
  210. return $uploader->upload($file);
  211. }
  212. if (!function_exists("random")) {
  213. /**
  214. * [random 生成随机字符串]
  215. * @wzq
  216. * @DtuateTime 2020-09-08
  217. * @param [type] $length [长度]
  218. * @param mixed $numeric [是否仅是数字]
  219. * @return string [type] [string]
  220. * @version v1
  221. */
  222. function random($length, $numeric = false): string
  223. {
  224. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  225. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  226. if ($numeric) {
  227. $hash = '';
  228. } else {
  229. $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
  230. $length--;
  231. }
  232. $max = strlen($seed) - 1;
  233. for ($i = 0; $i < $length; $i++) {
  234. $hash .= $seed[mt_rand(0, $max)] ?? "";
  235. }
  236. return $hash;
  237. }
  238. }