組件
只提供單一功能,不和其他功能產(chǎn)生耦合
復用性強,一般放在公共模塊,別的模塊都可以使用
注意:
1.簡單的功能,自己封裝一個組件,不要使用第三方.第三方組件的代碼冗余,學習成本,掌控程度都沒有自己寫好.
2.使用合適的第三方組件,可以節(jié)約大量時間
3.結合業(yè)務提前做好設計,例如使用策略模式.
4.盡量不要直接使用組件,自己在封裝一次,因為組件是可能更換,面向功能開發(fā),
一,支付組件
composer require riverslei/payment
很強大,支持支付寶,微信,PC支付,APP支付,公眾號支付,掃描支付等等.
新建pay.php
use Payment\Common\PayException;
use Payment\Client\Charge;
use Payment\Client\Refund;
use Payment\Client\Transfer;
/**
*
* @param object $payData 訂單數(shù)據(jù)
* @param string $type 支付類型
*/
public function payOrder($payData, $type='aliconfig')
{
$Config = config($type);//獲取配置信息
try {
$str = Charge::run($type, $Config, $payData);
} catch (PayException $e) {
$msg = $e->errorMessage();
throw new \Exception($msg);
}
return $str;
}
二.云存儲
composer require qiniu/php-sdk
核心代碼;默認使用七牛云存儲
public function __construct($type='qiniu')
{
$config = Config::get($type)
$accessKey = $this->config['accessKey'];
$secretKey = $this->config['secretKey'];
//初始化Auth狀態(tài):
$this->auth = new Auth($accessKey, $secretKey);
}
三,消息通知
極光消息通知
composer require jpush/jpush
核心代碼同上,使用策略模式,加載不同的配置文件,
消息通知是可能更換的
四,excel導入導出
namespace app\common\service;
class Phpexcel{
public function import_excel($filename){
// 判斷文件是什么格式
$file=ROOT_PATH.'runtime'. DS .$filename;
//$file=ROOT_PATH.'a.xlsx';
$type = pathinfo($file);
$type = strtolower($type["extension"]);
if($type==='xls'){
$type='Excel5';
}else{
$type='Excel2007';
}
ini_set('max_execution_time', '0');
Vendor('Classes.PHPExcel');
// 判斷使用哪種格式
$objReader = \PHPExcel_IOFactory::createReader($type);
$objPHPExcel = $objReader->load($file);
$sheet = $objPHPExcel->getSheet(0);
// 取得總行數(shù)
$highestRow = $sheet->getHighestRow();
// 取得總列數(shù)
$highestColumn = $sheet->getHighestColumn();
++$highestColumn;
//循環(huán)讀取excel文件,讀取一條,插入一條
$data=array();
//從第二行開始讀取數(shù)據(jù)
for($j=2;$j<=$highestRow;$j++){
//從A列讀取數(shù)據(jù)
for($k='A';$k!=$highestColumn;++$k){
// 讀取單元格
$data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
}
}
//die;
return $data;
}
public function create($data=[],$filename='simple.xls'){
ini_set('max_execution_time', '0');
Vendor('Classes.PHPExcel');
$filename=str_replace('.xls', '', $filename).'.xlsx';
$phpexcel = new \PHPExcel();
$phpexcel->getProperties()
->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->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("Test result file");
$phpexcel->getActiveSheet()->fromArray($data);
$phpexcel->getActiveSheet()->setTitle('Sheet1');
$phpexcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
return $objwriter = \PHPExcel_IOFactory::createWriter($phpexcel, 'Excel2007');
$objwriter->save('php://output');
exit;
}
}
五,百度地圖
注意:地圖可能更換,比如換成高德.
核心代碼
/*
*獲取行駛距離和時間
*/
public function getMileageTime($start_lat, $start_lng, $end_lat, $end_lng)
{
$origins = $start_lat . ',' . $start_lng;
$destinations = $end_lat . ',' . $end_lng;
$data['origins'] = $origins;
$data['destinations'] = $destinations;
$data['output'] = 'json';
$data['tactics'] = 11;
$data['ak'] = $this->config['ak'];
$uri = 'http://api.map.baidu.com/routematrix/v2/driving?' . http_build_query($data);
$address = $this->http_get($uri);
$da=[];
if ($address['status'] == 0) {
$da['distanc']=$address['result']['0']['distance']['text'];
$da['time']=$address['result']['0']['duration']['text'];
return $da;
} else {
throw new \Exception('百度地圖:'.$address['message']);
}
}
六,短信
短信可能使用夢網(wǎng)科技或者阿里大于
面向接口開發(fā)$smsSendConn是夢網(wǎng)的短信類庫或者阿里的短信類庫,看官方文檔就可以
try {
$result = $smsSendConn->singleSend($data);
return true;
}catch (Exception $e) {
//$e->getMessage()
return false;
}
感謝這些組件代碼的作者