導(dǎo)出excel
進(jìn)入項(xiàng)目根目錄,composer 安裝
cd /www/wwwroot/dgtapi/
php73 /usr/bin/composer require mk-j/PHP_XLSXWriter
composer報(bào)錯(cuò)

降級(jí)
composer selfupdate --1
composer install
laravel驗(yàn)證參數(shù)
public function zhangyidebug()
{
//待驗(yàn)證的數(shù)據(jù)
$data = [
'a'=>123,
'b'=>'',
'c'=>null,
'd'=>0,
'e'=>false,
'f'=>'hello',
'g'=>'阿薩德,分為軟',
'h'=>999999999
];
//驗(yàn)證規(guī)則 ,key(要驗(yàn)證的字段) => value(字段需要滿足的規(guī)則)
$rules = [
'a'=>'required|numeric',
'b'=>'required',
'c'=>'required',
'd'=>'required',
'e'=>'required',
'sort'=>'sometimes|numeric', //數(shù)組中該字段存在時(shí)就驗(yàn)證
'ison'=>'nullable|numeric',//字段可為空, 不為空的時(shí)候必須是數(shù)字
];
//驗(yàn)證失敗的錯(cuò)誤提示 , key(規(guī)則) => value(規(guī)則不滿足時(shí)的提示)
$messages = [
'required' => '錯(cuò)誤 :attribute 必填',
'numeric' => '你輸入的 :attribute 不是數(shù)字'
];
//字段名 , key(字段) => value(字段的中文名)
$customAttributes = [
'a' => '第一個(gè)字段',
'b' => '第二個(gè)字段',
'c' => '第三個(gè)字段',
'd' => '第四個(gè)字段',
];
$validator = Validator::make($data, $rules,$messages,$customAttributes);
if ($validator->fails()){
$errorMessage = $validator->errors()->first();//拿到第一個(gè)錯(cuò)誤
dd($errorMessage);
}
//TODO 驗(yàn)證通過(guò) 正常業(yè)務(wù)流程
}
laravel框架自帶,可用驗(yàn)證規(guī)則列表
--end--
laravel中使用ide-helper為model生成字段注釋
php artisan ide-helper:models - 為數(shù)據(jù)模型生成注釋
導(dǎo)出csv
類
<?php
namespace App\Libs;
class CSVExport
{
/** @var null 查詢 MYSQL得到的生成器 */
private $generator = null;
/** @var array 表頭 */
private $tableHeader = array();
/** 表頭中的字段名 @var array */
private $tableHeaderFiledName = array();
/** 表頭中的輸出顯示漢字 @var array */
private $tableHeaderFiledNameDesc = array();
/** 下載的文件名,不帶后綴 @var string */
private $fileName = '';
/** 字段轉(zhuǎn)換的.回調(diào)函數(shù) @var null */
private $convert = null;
/** 直接下載@ var bool */
private $directDownload = true;
/** 腳本超時(shí)時(shí)間 @var int */
private $timeout = 300;
/**
* CSVExport constructor.
*
* @param $generator
* @param array $tableHeader 表頭,一維數(shù)組['field_name'=>'要展示的文字']
* @param string $fileName 下載時(shí)的文件名, 不含后綴
* @param callable $convert 回調(diào)函數(shù), 用來(lái)處理某些字段
*/
public function __construct ($generator, $tableHeader , $fileName = '' , $convert = null)
{
$this->generator = $generator;
$this->tableHeader = $tableHeader;
$this->tableHeaderFiledName = array_keys($tableHeader);
$this->tableHeaderFiledNameDesc = array_values($tableHeader);
$this->fileName = $fileName ? $fileName : md5(time());
$this->convert = $convert;
}
/**
* @return float|int
*/
public function getTimeout ()
{
return $this->timeout;
}
/**
* @param float|int $timeout
*/
public function setTimeout ($timeout)
{
$this->timeout = $timeout;
}
/**
* 執(zhí)行查詢導(dǎo)出操作
*/
function doExport(){
set_time_limit($this->timeout);//超時(shí)限制
// 向?yàn)g覽器發(fā)送 http 下載頭
$this->sendHTTPHeader($this->fileName);
echo $this->tableHeaderDescImplode(); //表頭
//遍歷生成器, 發(fā)送數(shù)據(jù)
foreach ($this->generator as $data) {
$data = get_object_vars($data);
if ($this->convert){
$data = call_user_func($this->convert,$data);
}
echo $this->dataFieldImplode($data);
}
}
/**
* 拼接表頭
* @return string 逗號(hào)分割的表頭字符串
*/
private function tableHeaderDescImplode(){
$str = implode("\t,", $this->tableHeaderFiledNameDesc);
return $str.PHP_EOL;
}
/**
* 按照表頭字段,從結(jié)果集取出對(duì)應(yīng)的字段,拼接
* @param array $data 數(shù)據(jù)庫(kù)結(jié)果集, 一維數(shù)組
*
* @return string 拼接后的字符傳
*/
private function dataFieldImplode($data){
$str = '';
foreach ($this->tableHeaderFiledName as $field){
$str .= $data[$field]."\t,";
}
return rtrim($str,',').PHP_EOL;
}
/**
* 發(fā)送http下載header
*
* @param $fileName
*/
private function sendHTTPHeader($fileName){
header("Cache-control: private");
header("Pragma: public");
header('Content-type: application/x-csv');
header("Content-Disposition:attachment;filename=" . $fileName. ".csv");
}
}
調(diào)用
$query = "select * from skyshop_member limit 10000 ";
$generator = DB::connection()->cursor($query);
$tableHeader = [
'id'=>'序號(hào)',
'full_name'=>'全名'
];
$fileName = 'csv導(dǎo)出測(cè)試';
$callable = function($data){
if($data['id'] == 1){
$data['id'] = 'yi';
}
return $data;
};
$CSVExport = new CSVExport($generator, $tableHeader, $fileName,$callable);
$CSVExport->doExport();
圖片上傳oss
前端通過(guò)form表單enctype="multipart/form-data"直接上傳文件,PHP可以在$_FILES中接收.直接傳到阿里OSS服務(wù)器
現(xiàn)在前端把圖片轉(zhuǎn)成base64(字符串)上傳,沒(méi)法用$_FILES接收
解決方案
步驟1. 輸出到本地
步驟2. 上傳到OSS服務(wù)器,得到url
步驟3. 刪除本地文件
/**
* 上傳base64圖片到OSS服務(wù)器
* @param string $imageBase64 從前端接收到的圖片資源
* @param string $serviceDir 想要保存在OSS服務(wù)器上的目錄
*
* @return string 圖片在OSS服務(wù)器上的地址
*/
public function uploadImageBase64($imageBase64,$serviceDir){
$URL = '';
/** 得到文件后綴 */
//data:image/png;base64,iVBORw0KGgoA..
list($head,$image_data) = explode(',',$imageBase64); // head = data:image/png;base64 image_data = iVBORw0KGgoA..
list($type,) = explode(';',$head);// type = data:image/png
list(,$suffix) = explode('/',$type);// suffix = png
/** 步驟1. 輸出到本地 */
$localDir = $_SERVER['DOCUMENT_ROOT'].'/Temp';
$localFilename = $this->makeUUID().'.'.$suffix;
file_put_contents($localDir.'/'.$localFilename,base64_decode($image_data));
/** 步驟2. 上傳到OSS服務(wù)器,得到url */
try{
//$this->oss_client->uploadFile 是阿里提供的PHP-SDK中的方法
$response = $this->oss_client->uploadFile($this->bucket,$serviceDir.'/'.$localFilename,$localDir.'/'.$localFilename);
$URL = $response['info']['url'];
}catch (\Exception $e){
Log::write('文件上傳錯(cuò)誤:'.$e->getMessage());
$URL = '';
} finally {
/** 步驟3. 刪除本地文件 */
unlink($localDir.'/'.$localFilename);
return $URL;
}
}
生成uuid方法
/**
* 生成uuid (8-4-4-4-12)
* @return string
*/
private function makeUUID(){
$chars = md5(uniqid(mt_rand(), true));
return substr ( $chars, 0, 8 ) . '-'
. substr ( $chars, 8, 4 ) . '-'
. substr ( $chars, 12, 4 ) . '-'
. substr ( $chars, 16, 4 ) . '-'
. substr ( $chars, 20, 12 );
}
調(diào)用時(shí)只需要
$serviceDir = 'Uploads/driver_report';//OSS服務(wù)器上保存的位置
$obj = new myClass();
$url = $obj->uploadImageBase64($post['img1'],$serviceDir)
laravel 發(fā)送郵件
- env中配置發(fā)送方的賬號(hào)密碼
MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=171913904@qq.com
MAIL_PASSWORD=授權(quán)碼在qq郵箱個(gè)人中心獲取
MAIL_ENCRYPTION=ssl
- 生成一個(gè)mail類
命令php73 artisan make:mail MailA
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MailA extends Mailable
{
use Queueable, SerializesModels;
public $code;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($code)
{
//
$this->code = $code;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('郵件標(biāo)題')->from('171913904@qq.com')->view('emails.mail_a');
}
}
3 寫(xiě)一個(gè)blade模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Verification Code</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
font-size: 16px;
line-height: 1.6;
}
.container {
max-width: 600px;
margin: 20px auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
h1 {
font-size: 24px;
color: #333;
}
p {
margin-bottom: 20px;
color: #666;
}
strong {
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>您正在找回密碼</h1>
<p>Dear 用戶</p>
<p>您的驗(yàn)證碼是: <strong>{{ $code }}</strong></p>
<p>如果非您本人操作可忽略。(請(qǐng)勿回復(fù)本郵件)</p>
</div>
</body>
</html>
4 發(fā)送
Mail::to('接受者的郵箱')->send(new MailA(666666));
php中的索引不連續(xù)的關(guān)聯(lián)數(shù)組 返給前端變成了對(duì)象
php中定義
const typeList =
[
10 => '首頁(yè)視頻',
20 => '教學(xué)視頻',
30 => '介紹視頻',
];

可以在js中處理 把對(duì)象轉(zhuǎn)成數(shù)組
state.typeList = Object.entries(data.typeList).map(([key, value]) => ({
key: parseInt(key, 10),
value,
}))

GPT對(duì)js代碼的解釋
詳細(xì)解釋
Object.entries:
Object.entries(typeListObject) 將對(duì)象的鍵值對(duì)轉(zhuǎn)換為一個(gè)二維數(shù)組,其中每個(gè)元素是一個(gè)包含鍵和值的數(shù)組。
例如,如果 typeListObject 是 { 1: '首頁(yè)視頻', 2: '教學(xué)視頻', 3: '介紹視頻' }
則 Object.entries(typeListObject) 生成 [[1, '首頁(yè)視頻'], [2, '教學(xué)視頻'], [3, '介紹視頻']]。
map:
map 方法用于遍歷數(shù)組中的每一個(gè)元素,并對(duì)每一個(gè)元素執(zhí)行給定的函數(shù),然后返回一個(gè)新數(shù)組。
在這個(gè)例子中,map 函數(shù)接收一個(gè)回調(diào)函數(shù)作為參數(shù)。
箭頭函數(shù):
箭頭函數(shù)的語(yǔ)法是 ([key, value]) => ({ key: parseInt(key, 10), value })。
([key, value]) 是解構(gòu)賦值語(yǔ)法,表示將數(shù)組的第一個(gè)元素解構(gòu)為 key,第二個(gè)元素解構(gòu)為 value。
parseInt(key, 10) 將字符串形式的鍵轉(zhuǎn)換為整數(shù)。parseInt 函數(shù)的第二個(gè)參數(shù) 10 表示使用十進(jìn)制進(jìn)行解析。
({ key: parseInt(key, 10), value }) 返回一個(gè)新的對(duì)象,其鍵為 key 的整數(shù)形式,值為原始值。
為什么箭頭函數(shù) => 后要用括號(hào) ()?
當(dāng)你需要返回一個(gè)對(duì)象字面量時(shí),必須用括號(hào)將對(duì)象括起來(lái)。
這是因?yàn)樵诩^函數(shù)的語(yǔ)法中,大括號(hào) {} 通常被解析為函數(shù)體的開(kāi)始,而不是對(duì)象字面量的開(kāi)始。
通過(guò)使用括號(hào) (),可以明確告訴 JavaScript 引擎這里返回的是一個(gè)對(duì)象字面量,而不是一個(gè)函數(shù)體。
示例
需要括號(hào)的情況
如果你直接寫(xiě)對(duì)象字面量而不使用括號(hào),JavaScript 會(huì)誤解你的意圖:
javascript
const getObject = () => { key: 'value' }; // 這樣寫(xiě)是錯(cuò)誤的
上面的代碼會(huì)拋出一個(gè)錯(cuò)誤,因?yàn)?JavaScript 會(huì)將大括號(hào) {} 視為函數(shù)體的開(kāi)始,而不是對(duì)象字面量。
正確的做法是使用括號(hào)將對(duì)象字面量括起來(lái):
javascript
const getObject = () => ({ key: 'value' });
這樣,JavaScript 就能正確地解析并返回一個(gè)對(duì)象字面量。
獲取本年的12個(gè)月
<?php
// 獲取當(dāng)前年份
$currentYear = date("Y");
// 初始化一個(gè)數(shù)組來(lái)存儲(chǔ)12個(gè)月的時(shí)間范圍
$monthRanges = array();
// 循環(huán)12個(gè)月
for ($i = 1; $i <= 12; $i++) {
// 獲取當(dāng)前月份的第一天
$startDate = date("Y-m-01", strtotime("$currentYear-$i-01"));
// 獲取當(dāng)前月份的最后一天
$endDate = date("Y-m-t", strtotime("$currentYear-$i-01"));
// 將時(shí)間范圍添加到數(shù)組中
$monthRanges[] = array(
"start_date" => $startDate,
"end_date" => $endDate
);
}
dd($monthRanges);

php 安裝擴(kuò)展
php有兩個(gè)配置文件 , 以當(dāng)前docker環(huán)境為例 ,安裝的php版本是7.3.32
php pecl安裝擴(kuò)展
去網(wǎng)站搜索想要的擴(kuò)展 https://pecl.php.net/
找到合適的版本 下載下來(lái)
linux 系統(tǒng)下載擴(kuò)展
cd ~
wget https://pecl.php.net/get/gmagick-2.0.5RC1.tgz
不用解壓,直接使用pecl命令安裝
pecl install gmagick-2.0.5RC1.tgz
出現(xiàn)
lease provide the prefix of GraphicsMagick installation [autodetect] :
直接回車
后續(xù)報(bào)錯(cuò)自行百度
php7.3 zip擴(kuò)展
composer安裝三方包時(shí)報(bào)錯(cuò)phpoffice/phpspreadsheet 1.10.1 requires ext-zip *

zip擴(kuò)展可以通過(guò)編譯 ,php7.3為例 zip的擴(kuò)展源碼位置在/www/server/php/73/src/ext/zip目錄
編譯完成后得到文件
/www/server/php/73/lib/php/extensions/no-debug-non-zts-20180731/zip.so
在php.ini和php-cli.ini中分別加入
extension = /www/server/php/73/lib/php/extensions/no-debug-non-zts-20180731/zip.so
注意:
用composer安裝三方擴(kuò)展時(shí) ,使用的是php-cli.ini ,

處理http請(qǐng)求的php-fpm的work進(jìn)程使用的是php.ini
替換/刪除 mysql某字段中的指定字符

update 表名 set 字段名 = replace(字段名,'被替換的字符','替換為')
update member set headpic = REPLACE(headpic,':33380','')
替換為空字符 ,則刪除了字段中的被替換的字符

model時(shí)間格式化
解決laravel數(shù)據(jù)庫(kù)取出的數(shù)據(jù),created_at,updated_at字段,時(shí)間格式問(wèn)題
/**
* 格式化時(shí)間
* @param \DateTimeInterface $date
* zhangyi [2022-12-12]
* @return string
*/
protected function serializeDate(\DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
生成遷移文件
php artisan make:migration create_table_demo
執(zhí)行單個(gè)遷移
php artisan migrate --path=database\migrations\2021_10_13_153021_create_jobs_table.php
回滾一步遷移
php artisan migrate:rollback --step=1
生成repository
php artisan core:model --table=users --name=User
php artisan core:model --table=
清除視圖緩存
php artisan view:clear
創(chuàng)建命令行腳本
php artisan make:command 自定義名稱(生成的文件名)
在app->console->commands目錄內(nèi)找到腳本文件 ,修改屬性 $signature = '自定義名稱'(腳本啟動(dòng)名稱) ,在handle方法內(nèi)寫(xiě)邏輯
啟動(dòng)腳本
php artisan 自定義名稱($signature)
composer
更新指定的包composer u 包名
composer autoload使用
自己寫(xiě)的vendor包 ,應(yīng)用到項(xiàng)目后 如何自動(dòng)加載
- 類文件需要聲明命名空間,并且根據(jù)規(guī)范 ,目錄結(jié)構(gòu)與命名控件對(duì)應(yīng)

- 在項(xiàng)目的composer.json中加入
autoload相關(guān)內(nèi)容 ,此處使用的psr-4規(guī)范
composer.json.png
"autoload": { "psr-4": { "utils\\": "./" } }
解釋:
"psr-4"規(guī)范,具體百度
"utils\\"自定義的頂級(jí)命名空間
"./"對(duì)應(yīng)的類文件路徑 - 命令行執(zhí)行
composer dumpautoload -o更新自動(dòng)加載文件
4.測(cè)試 可以正常執(zhí)行
圖片.png
說(shuō)明:
此處測(cè)試需要手動(dòng)require_once __DIR__."/vendor/autoload.php";
一般框架的index.php文件已經(jīng)引入了autoload.php,所以框架項(xiàng)目中只需通過(guò)composer安裝三方包 ,就能直接用.
重新生成autoload
composer dumpautoload
三方包網(wǎng)站
composer更換鏡像
查看鏡像
全局
composer config -l -g
當(dāng)前項(xiàng)目
composer config -l
更換當(dāng)前項(xiàng)目鏡像
全局
composer config -g repo.packagist composer ${鏡像地址}
當(dāng)前項(xiàng)目
composer config repo.packagist composer ${鏡像地址}
| 阿里云 Composer 鏡像 | https://mirrors.aliyun.com/composer/ | 阿里云 | 96 秒 | 推薦 |
| 騰訊云 Composer 鏡像 | https://mirrors.cloud.tencent.com/composer/ | 騰訊云 | 24 小時(shí) | - |
| PHP 國(guó)內(nèi) Composer 鏡像 | https://packagist.phpcomposer.com | 仁潤(rùn)股份 | 24 小時(shí) | 不穩(wěn)定 |
| 華為云 Composer 鏡像 | https://repo.huaweicloud.com/repository/php/ | 華為云 | 未知 | 未知 |
| php.cnpkg.org Composer 鏡像 | https://php.cnpkg.org | 安暢網(wǎng)絡(luò) | 60 秒 | - |
composer三方包的搜索網(wǎng)站
卡死
安裝三方包時(shí)卡在Updating dependencies (including require-dev)
一般都是鏡像問(wèn)題 ,composer官網(wǎng)給的鏡像不能用 ,換成阿里云的鏡像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
切換鏡像無(wú)效
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/不一定能切換成功
在項(xiàng)目根目錄下找到composer.json文件 ,切換成功應(yīng)該是

或者直接把下邊代碼粘貼到
composer.json文件
"repositories": {
"packagist": {
"type": "composer",
"url": "https://mirrors.aliyun.com/composer/"
}
}
內(nèi)存溢出
Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 67108872 bytes) in phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/DependencyResolver/RuleSet.php on line 84
官方有解決方案
注意 即使配置了composer全局環(huán)境變量也不行 必須用composer.phar文件絕對(duì)路徑
php -d memory_limit=-1 ${composer.phar 絕對(duì)路徑} require ${包名}
如:
php -d memory_limit=-1 C:\ProgramData\ComposerSetup\bin\composer.phar require phpoffice/phpspreadsheet
laravel創(chuàng)建命令行腳本
php artisan make:command 自定義名稱(生成的文件名)
在app->console->commands目錄內(nèi)找到腳本文件 ,修改屬性 $signature = '自定義名稱'(腳本啟動(dòng)名稱) ,在handle方法內(nèi)寫(xiě)邏輯
啟動(dòng)腳本
php artisan 自定義名稱($signature)
phpstorm多行變成一行
有時(shí)候拿到一部分?jǐn)?shù)據(jù)ID ,需要在數(shù)據(jù)庫(kù)進(jìn)行批量處理 .
比如對(duì)下邊的用戶ID進(jìn)行封號(hào)

解決
在phpstorm中新建一個(gè)普通文本文件 , 把excel中的一列粘貼進(jìn)去

1. Ctrl + a 全選
2. Ctrl + Shift + j 多行變成一行,空格分割
3. Ctrl + r 替換 空格為想要的字符

完成

phpspreadsheet 導(dǎo)出微信昵稱數(shù)據(jù)丟失
遇到昵稱帶表情的用戶數(shù)據(jù)后 ,接下來(lái)的很多表格都變成了空白 .實(shí)際上數(shù)據(jù)是存在的

解決方法 ,生成的excel文件后綴 ,由xsl 改為更高版本的 xslx ,便可恢復(fù)正常
判斷數(shù)組值是否存在
經(jīng)常需要判斷前端傳過(guò)來(lái)的篩選參數(shù), 如果存在有效值 sql中 就增加篩選條件
判斷前端是否傳了有效參數(shù) ,當(dāng)參數(shù)是數(shù)字 0 時(shí) 篩選狀態(tài)為0的數(shù)據(jù) 經(jīng)常出現(xiàn)問(wèn)題
$arr['status'] = 0;
判斷數(shù)組$arr中是否有某個(gè)參數(shù)
返回false
$arr['key'] = null
$arr['key'] = false
$arr['key'] = ''
$arr['未定義索引']
返回true
$arr['key'] = 0
$arr['key'] = '0'
$arr['key'] = 'null'
$arr['key'] = 'false'
/**
* 判斷數(shù)組中是否存在某個(gè)有效參數(shù)
* @param array $arr 數(shù)組
* @param string $key 索引
* @return bool
*/
function hasVal($arr,$key){
if (!is_array($arr)) return false;
if (array_key_exists($key , $arr)){
if (
$arr[$key] !== '' &&
$arr[$key] !== false &&
$arr[$key] !== null
){
return true;
}
}
return false;
}
找出一維數(shù)組中重復(fù)的值
通過(guò)in_array()判斷
$temp = [];
foreach ( $list as $val ) {
if ( !in_array($val , $temp) ) {
$temp[] = $val;
continue;
}
throw new \Exception("值:{$val}重復(fù)");
}

