PHP code example of dfer / tools

1. Go to this page and download the library: Download dfer/tools library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dfer / tools example snippets


php test.php


 = new \Dfer\Tools\Common;
echo $common->about();

php index.php

protected $common;
public function _initialize()
{
    parent::_initialize();
    $this->common =new \Dfer\Tools\Common();
}

$c=new TpCommon;
$name_arr=$c->getColName("cat_publish_info_comment");

$spService=new Office;

// 下载
$title=\sprintf('订单-%s', date("Ymd", time()));
$header = ['姓名',    '电话',    '地址',    '随机数字(两位)'    ,'省份(简称)',    '市(不要带市)',    '区县(不要带区县)'    ,'随机数字(三位)'];
$data=Db::query("SELECT * FROM dd_shop_paybill GROUP BY receive_id");
$file_src = $spService->setTableTitle($title)
->setStyle()
->setContent($header, $data)
->saveFile($title.'.xlsx');

$file_stream = $spService->setTableTitle('2021销售记录')
->setStyle()
->setVContent($header, $data)
->saveStream('2021销售记录.xlsx');


$title = \sprintf('模板-%s', date("Ymd", time()));
$common_item=['序号','组织机构代码','企业详细名称'];
$item=array_merge($common_item,['乡镇', '所属产业','本月;工业总产值;万元','1-本月;工业总产值;万元','上年同期;本月;工业总产值;万元','上年同期;1-本月;工业总产值;万元','行业大类','1-本月;行业增加值增速;%','是否为重点监测企业']);
$lists = YjComQiyeBaseInfosReportModel::where(['year' => 2023, 'month' => 9])->order('xuhao asc')->field("xuhao,qiye_dm,qiye_mc,xiangzhen_mc,chanye,hangye_mc,s_zhongdian")->select()->toArray();
$lists_type_1=[];
foreach($lists as $key=>$v){
    $v['s_zhongdian']=$v['s_zhongdian']==2 ? '是' : '';
    $lists_type_1[]=[$v['xuhao'],$v['qiye_dm'],$v['qiye_mc'],$v['xiangzhen_mc'],$v['chanye'],null,null,null,null,$v['hangye_mc'],null,$v['s_zhongdian']];
}
$spService->setStyle()->setWidthAndHeight(35)
->setTitle($title,true)->setContent($item, $lists_type_1,[10,20,50,15,20,null,null,null,null,50,null,25])->getFile($title . '.xlsx',0);




// 上传
$files = request()->file('file_data');
$savename = \think\facade\Filesystem::disk('public')->putFile('filexls', $files);
$spService = new Office;
$items=['xuhao','qiye_dm','qiye_mc','xiangzhen_mc','chanye','chanzhi','chanzhi_lj','chanzhi_tong','chanzhi_lj_tong','hangye_mc','hangye_zeng','s_zhongdian'];
$xls_data=$spService->readFile('./storage/' . $savename,$items,2);
foreach($xls_data as $key=>&$v){
}

<!-- 帮助 -->
php think dfer:console_create -h
<!-- 创建一个脚本 -->
php think dfer:console_create Test


// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
// 自定义指令
return [
    // 指令定义
    'commands' => [
           'dfer:console_create' => 'Dfer\Tools\TpConsole\Create'
    ]
];




// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <[email protected]>
// +----------------------------------------------------------------------

return [
    'Dfer\Tools\Console\Create'
];



/**
 * 钉钉登录
 *
 * @ApiMethod (POST)
 * @param string $authCode  授权码
 */
public function dd_login()
{
    $authCode=isset($_POST['authCode'])?$_POST['authCode']:"";
    $service=new DingTalk();
    $accessToken=$service->getUserAccessToken($authCode);
    $users=$service->getContactUsers($accessToken);
    $this->common->log([$_GET,$_POST,$accessToken,$users]);

    $account='[email protected]';
    $ret = $this->auth->dd_login($account);
    if ($ret) {
        $data = ['userinfo' => $this->auth->getUserinfo()];
        $this->success(__('Logged in successful'), $data);
    } else {
        $this->error($this->auth->getError());
    }
}

composer 

/**
 * 七牛云上传
 **/
public function uploadQN()
{
 $fileObj = $this->request->file('file');
 $result=\Dfer\Tools\QiNiuService::getInstance()->uploadFile($fileObj);

 if($result['code']==0){
  $this->error('缺少参数[file]',$result);
 }
 else{
  $this->success('',$result);
 }
}

public function index()
{

    $qr=new \Dfer\Tools\QrCode;
    $data=$qr->setStyle(500,30,new Color(255, 250, 232))
    ->setText("二维码测试",new Color(0, 0, 0))
    ->setData('http://www.baidu.com/')
    ->setLogo()
    ->getFile();
    $this->success('请求成功!', $data);
}

public function index()
{
    $mail=new \Dfer\Tools\Mail;
    $data=$mail->instance(['debug'=>true])
    ->send('[email protected]','邮件主题','邮件内容');
    $this->success('请求成功!', $data);
}

composer 

namespace app\api\controller;

use Dfer\Tools\Statics\{Common};
use Dfer\Tools\AliOss;
use Exception;

use app\api\model\OssUploadRecordModel;

class Oss extends Base
{
    private $userId=0;

    public function __construct()
    {
        parent::__construct(app());

        if (!class_exists('Dfer\Tools\AliOss')) {
            die("缺少`dfer/tools`组件");
        }
    }

    public function getRequestParams(){
        // 组件类型
        $type = $this->request->param('type','ueditor');
        // 资源加工列表
        $process_list = $this->request->param('process_list',[]);

        $access_id =  config('oss.access_id');
        $access_key =  config('oss.access_key');
        $callback_url =  config('oss.callback_url');

        $dir =  config('oss.dir');
        $user_id=$this->userId;
        $debug=1;
        Common::debug(compact('access_id','access_key','dir','callback_url','debug'),false);
        $oss=new AliOss(compact('access_id','access_key','dir','callback_url','debug'),false);
        $oss->getRequestParams(compact('user_id','type','process_list'));
    }


    public function uploadCallback($var = null)
    {
        $access_id =  config('oss.access_id');
        $access_key =  config('oss.access_key');
        $bucket =  config('oss.bucket');
        $endpoint =  config('oss.endpoint');
        $host =  config('oss.host');
        // 调试日志保存在`/data/logs/`
        $debug=1;
        Common::debug(compact('access_id','access_key','bucket','host','debug','endpoint'));
        $oss=new AliOss(compact('access_id','access_key','bucket','endpoint','host','debug'));
        $oss->uploadCallback(function($status,$data){return $this->callback($status,$data);});
    }

    public function callback($status,$post_arr = null)
    {
        if(intval($status)){
            OssUploadRecordModel::create([
                'user_id'=>$post_arr['user_id'],
                'file_path'=>$post_arr['filePath'],
                'url'=>$post_arr['host'].$post_arr['filePath']
            ]);
        }



        $return = [
            'code' => $status ? 1 : 0,
            'msg' => $status ? '上传成功!' : '上传失败!',
            'original' => $post_arr['fileName'],
            'state' => $status ? 'SUCCESS' : 'FAIL',
            'title' => $post_arr['fileName'],
            'url' => $post_arr['host'] . $post_arr['filePath']
        ];

        if (!$status) {
            $return['error'] = $post_arr['error'];
        }

        Common::showJsonBase($return);
    }
}


return [
    'access_id'=>'*************',
    'access_key'=>'*************',
    'callback_url'=>'https://www.dfer.site/api/oss/uploadCallback',
    'dir'=>'www_dfer_site',
    'host'=>'http://oss.dfer.site/',
    'bucket'=>'df-linux-oss',
    'endpoint'=>'oss-cn-hangzhou.aliyuncs.com',
];