组件
只提供单一功能,不和其他功能产生耦合
复用性强,一般放在公共模块,别的模块都可以使用
注意:
1.简单的功能,自己封装一个组件,不要使用第三方.第三方组件的代码冗余,学习成本,掌控程度都没有自己写好.
2.使用合适的第三方组件,可以节约大量时间
3.结合业务提前做好设计,例如使用策略模式.
4.尽量不要直接使用组件,自己在封装一次,因为组件是可能更换,面向功能开发,
一,支付组件
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 订单数据
* @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状态:
$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);
// 取得总行数
$highestRow = $sheet->getHighestRow();
// 取得总列数
$highestColumn = $sheet->getHighestColumn();
++$highestColumn;
//循环读取excel文件,读取一条,插入一条
$data=array();
//从第二行开始读取数据
for($j=2;$j<=$highestRow;$j++){
//从A列读取数据
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']);
}
}
六,短信
短信可能使用梦网科技或者阿里大于
面向接口开发$smsSendConn是梦网的短信类库或者阿里的短信类库,看官方文档就可以
try {
$result = $smsSendConn->singleSend($data);
return true;
}catch (Exception $e) {
//$e->getMessage()
return false;
}
感谢这些组件代码的作者