## 系统组件
目录
- 基本组件
- 获取单个配置信息
- 获取全部配置信息
- 打印调试
- 行为日志记录
- 微信接口验证及报错获取
- 解析 model 报错
- 文件上传
- 生成二维码
- IP地址转地区
- 快递查询
- 小票打印
- 易联云
- Curl
- 中文转拼音
- 爬虫
- Glide
### 基本组件
读取后台的配置
```
// 后台配置
Yii::$app->debris->backendConfig($fildName);
// 强制不从缓存读取配置
Yii::$app->debris->backendConfig($fildName, true);
// 强制不从缓存读取所有配置
Yii::$app->debris->backendConfigAll(true);
```
读取商户端的配置
```
// 商户端配置
Yii::$app->debris->merchantConfig($fildName);
// 强制不从缓存读取配置
Yii::$app->debris->merchantConfig($fildName, true);
// 强制不从缓存读取所有配置
Yii::$app->debris->merchantConfigAll(true);
```
自动读取对应配置信息
> 不了解其机制的话请谨慎使用
> 规则:如果有 merchant_id 的话,则直接读取后台配置,没有的话会去读取商户端配置
```
// 注意$fildName 为你的配置标识,默认从缓存读取
Yii::$app->debris->config($fildName);
// 强制不从缓存读取
Yii::$app->debris->config($fildName, true);
// 从缓存中强制读取商户 ID 为 1 配置(注意: 1 为总后台的 ID)
Yii::$app->debris->config($fildName, false);
```
##### 获取全部配置信息
```
// 注意默认从缓存读取
Yii::$app->debris->configAll();
// 强制不从缓存读取
Yii::$app->debris->configAll(true);
// 从缓存中强制读取商户 ID 为 1 全部配置(注意: 1 为总后台的 ID)
Yii::$app->debris->configAll(false, 1);
```
读取某一端的配置
```
// 商户端配置
Yii::$app->debris->merchantConfigAll();
```
##### 打印调试
```
Yii::$app->debris->p();
```
##### 行为日志记录
```
/**
* 行为日志
*
* @param string $behavior 行为标识
* @param string $remark 备注 注意长度为255
* @param bool $noRecordData 是否记录 post 数据 [true||false]
* @throws \yii\base\InvalidConfigException
*/
Yii::$app->services->actionLog->create($behavior, $remark, $noRecordData)
```
##### 微信接口验证及报错
```
// 默认直接报错
Yii::$app->debris->getWechatError($message);
// 如果想不直接报错并返回报错信息
$error = Yii::$app->debris->getWechatError($message, false);
```
##### 解析 model 报错
```
// 注意 $firstErrors 为 $model->getFirstErrors();
Yii::$app->debris->analyErr($firstErrors);
```
### 文件上传
获取组件全部实现 `League\Flysystem\Filesystem` 接口
```
// 支持 oss/cos/qiniu/local, 配置不传默认使用总后台
$entity = Yii::$app->uploadDrive->local($config)->entity();
```
使用案例
```
$entity = Yii::$app->uploadDrive->local()->entity();
$stream = fopen('文件绝对路径', 'r+');
$result = $entity->writeStream('存储相对路径', $stream);
// 直接写入base64数据
$entity->write('存储相对路径', $base64Data);
```
更多说明:新增驱动请放入 `common\components\uploaddrive` 目录, 并在 `common\components\UploadDrive` 类内实现可实例化的方法
### 生成二维码
```
$qr = Yii::$app->get('qr');
Yii::$app->response->format = Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', $qr->getContentType());
return $qr->setText('www.rageframe.com')
->setLabel('2amigos consulting group llc')
->setSize(150)
->setMargin(7)
->writeString();
```
or
```
use Da\QrCode\QrCode;
$qrCode = (new QrCode('This is my text'))
->setSize(250)
->setMargin(5)
->useForegroundColor(51, 153, 255);
// 把图片保存到文件中:
$qrCode->writeFile(Yii::getAlias('@attachment') . '/code.png'); // 没有指定的时候默认为png格式
// 直接显示在浏览器
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();
```
[二维码文档](http://qrcode-library.readthedocs.io/en/latest/)
### IP地址转地区
```
use Zhuzhichao\IpLocationZh\Ip;
var_dump(Ip::find('171.12.10.156'));
```
输出结果
```
array (size=4)
0 => string '中国' (length=6)
1 => string '河南' (length=6)
2 => string '郑州' (length=6)
3 => string '' (length=0)
4 => string '410100' (length=6)
```
### 快递查询
```
// 查询所有的可用快递公司
$companies = Yii::$app->logistics->companies('aliyun');
/**
* 支持 aliyun(阿里云)、juhe(聚合)、kdniao(快递鸟)、kd100(快递100)
*
* @param string $no 快递单号
* @param null $company 快递公司
* @param bool $isCache 是否缓存读取默认缓存1小时
* @return OrderInterface
*/
$order = Yii::$app->logistics->aliyun($no, $company, $isCache);
```
### 小票打印
#### 易联云
用法1
```
$orderSn = rand(1, 9);
$content = "
| 烤土豆(超级辣) | x3 | 5.96 |
| 烤豆干(超级辣) | x2 | 3.88 |
| 烤鸡翅(超级辣) | x3 | 17.96 |
| 烤排骨(香辣) | x3 | 12.44 |
| 烤韭菜(超级辣) | x3 | 8.96 |