企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 第五课附录处理报表|支付 ### 对于少量数据采用Excel导入导出 - 导入 ``` <pre class="calibre14">``` <span class="token"><</span>form id<span class="token">=</span><span class="token4">"importform"</span> class<span class="token">=</span><span class="token4">"form-horizontal form"</span> action<span class="token">=</span><span class="token4">""</span> method<span class="token">=</span><span class="token4">"post"</span> enctype<span class="token">=</span><span class="token4">"multipart/form-data"</span><span class="token">></span> <span class="token"><</span>div class<span class="token">=</span><span class="token4">"form-group"</span><span class="token">></span> <span class="token"><</span>label class<span class="token">=</span><span class="token4">"col-sm-2 control-label must"</span><span class="token">></span>EXCEL<span class="token"><</span><span class="token">/</span>label<span class="token">></span> <span class="token"><</span>div class<span class="token">=</span><span class="token4">"col-sm-5"</span> style<span class="token">=</span><span class="token4">"padding-right:0;"</span> <span class="token">></span> <span class="token"><</span>input type<span class="token">=</span><span class="token4">"file"</span> name<span class="token">=</span><span class="token4">"excelfile"</span> class<span class="token">=</span><span class="token4">"form-control"</span> <span class="token">/</span><span class="token">></span> <span class="token"><</span>span class<span class="token">=</span><span class="token4">"help-block"</span><span class="token">></span>选择excel文件<span class="token"><</span><span class="token">/</span>span<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span>div class<span class="token">=</span><span class="token4">'form-group'</span><span class="token">></span> <span class="token"><</span>div class<span class="token">=</span><span class="token4">"col-sm-12"</span><span class="token">></span> <span class="token"><</span>div class<span class="token">=</span><span class="token4">"modal-footer"</span><span class="token">></span> <span class="token"><</span>button type<span class="token">=</span><span class="token4">"submit"</span> class<span class="token">=</span><span class="token4">"btn btn-primary"</span> name<span class="token">=</span><span class="token4">"cancelsend"</span> value<span class="token">=</span><span class="token4">"yes"</span><span class="token">></span>确认导入<span class="token"><</span><span class="token">/</span>button<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>div<span class="token">></span> <span class="token"><</span><span class="token">/</span>form<span class="token">></span> ``` ``` php处理 ``` <pre class="calibre14">``` <span class="token5">if</span> <span class="token2">(</span>$_W<span class="token2">[</span><span class="token4">'ispost'</span><span class="token2">]</span><span class="token2">)</span> <span class="token2">{</span> $rows <span class="token">=</span> <span class="token1">m</span><span class="token2">(</span><span class="token4">'excel'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">import</span><span class="token2">(</span><span class="token4">'excelfile'</span><span class="token2">)</span><span class="token2">;</span> $time <span class="token">=</span> <span class="token1">time</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> foreach <span class="token2">(</span>$rows as $rownum <span class="token">=</span><span class="token">></span> $col<span class="token2">)</span> <span class="token2">{</span> $data <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span> <span class="token4">'uniacid'</span> <span class="token">=</span><span class="token">></span> $_W<span class="token2">[</span><span class="token4">'uniacid'</span><span class="token2">]</span><span class="token2">,</span> <span class="token4">'createtime'</span> <span class="token">=</span><span class="token">></span> $time<span class="token2">,</span> <span class="token4">'category'</span> <span class="token">=</span><span class="token">></span> <span class="token1">trim</span><span class="token2">(</span>$col<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">'orderid'</span> <span class="token">=</span><span class="token">></span> <span class="token1">trim</span><span class="token2">(</span>$col<span class="token2">[</span><span class="token3">1</span><span class="token2">]</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">'isuse'</span> <span class="token">=</span><span class="token">></span> <span class="token1">trim</span><span class="token2">(</span>$col<span class="token2">[</span><span class="token3">2</span><span class="token2">]</span><span class="token2">)</span> <span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token1">empty</span><span class="token2">(</span>$col<span class="token2">[</span><span class="token3">0</span><span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">continue</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">pdo_insert</span><span class="token2">(</span><span class="token4">'ewei'</span><span class="token2">,</span>$data<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> $this<span class="token">-</span><span class="token">></span><span class="token1">message</span><span class="token2">(</span><span class="token4">'导入成功!'</span><span class="token2">,</span><span class="token1">webUrl</span><span class="token2">(</span><span class="token4">''</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">''</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` - 导出模板,调用以下方法即可 ``` <pre class="calibre14">``` public <span class="token5">function</span> <span class="token1">import</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">{</span> $columns <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $columns<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'物流类别'</span><span class="token2">,</span> <span class="token4">'field'</span> <span class="token">=</span><span class="token">></span> <span class="token4">''</span><span class="token2">,</span> <span class="token4">'width'</span> <span class="token">=</span><span class="token">></span> <span class="token3">32</span><span class="token2">)</span><span class="token2">;</span> $columns<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'物流单号'</span><span class="token2">,</span> <span class="token4">'field'</span> <span class="token">=</span><span class="token">></span> <span class="token4">''</span><span class="token2">,</span> <span class="token4">'width'</span> <span class="token">=</span><span class="token">></span> <span class="token3">32</span><span class="token2">)</span><span class="token2">;</span> $columns<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'是否使用'</span><span class="token2">,</span> <span class="token4">'field'</span> <span class="token">=</span><span class="token">></span> <span class="token4">''</span><span class="token2">,</span> <span class="token4">'width'</span> <span class="token">=</span><span class="token">></span> <span class="token3">32</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">m</span><span class="token2">(</span><span class="token4">'excel'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">temp</span><span class="token2">(</span><span class="token4">'文件名'</span><span class="token2">.</span> <span class="token1">date</span><span class="token2">(</span><span class="token4">'Y-m-d-H-i'</span><span class="token2">,</span> <span class="token1">time</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">,</span> $columns<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` - 导出excel ``` <pre class="calibre14">``` <span class="token"><</span>button type<span class="token">=</span><span class="token4">"submit"</span> name<span class="token">=</span><span class="token4">"export_order"</span> value<span class="token">=</span><span class="token4">"1"</span> class<span class="token">=</span><span class="token4">"btn btn-primary btn-sm"</span><span class="token">></span>导出订单<span class="token"><</span><span class="token">/</span>button<span class="token">></span> <span class="token6">//$list是数据</span> <span class="token5">if</span> <span class="token2">(</span>$_GPC<span class="token2">[</span><span class="token4">'export_order'</span><span class="token2">]</span> <span class="token">==</span> <span class="token3">1</span><span class="token2">)</span> <span class="token2">{</span> $earr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$list as $v<span class="token2">)</span><span class="token2">{</span> $earr<span class="token2">[</span><span class="token2">]</span><span class="token">=</span><span class="token1">array</span><span class="token2">(</span> <span class="token4">'openid'</span> <span class="token">=</span><span class="token">></span> $v<span class="token2">[</span><span class="token4">'openid'</span><span class="token2">]</span><span class="token2">,</span> <span class="token4">'price'</span> <span class="token">=</span><span class="token">></span> $v<span class="token2">[</span><span class="token4">'price'</span><span class="token2">]</span><span class="token2">,</span> <span class="token4">'mobile'</span><span class="token">=</span><span class="token">></span> $v<span class="token2">[</span><span class="token4">'mobile'</span><span class="token2">]</span><span class="token2">,</span> <span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> $titlearr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'openid'</span><span class="token2">,</span> <span class="token4">'field'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'openid'</span><span class="token2">,</span> <span class="token4">'width'</span> <span class="token">=</span><span class="token">></span> <span class="token3">24</span><span class="token2">)</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'手机号'</span><span class="token2">,</span> <span class="token4">'field'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'mobile'</span><span class="token2">,</span> <span class="token4">'width'</span> <span class="token">=</span><span class="token">></span> <span class="token3">12</span><span class="token2">)</span><span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'智慧卡金额'</span><span class="token2">,</span> <span class="token4">'field'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'price'</span><span class="token2">,</span> <span class="token4">'width'</span> <span class="token">=</span><span class="token">></span> <span class="token3">12</span><span class="token2">)</span><span class="token2">,</span> <span class="token2">)</span><span class="token2">;</span> <span class="token1">m</span><span class="token2">(</span><span class="token4">'excel'</span><span class="token2">)</span><span class="token">-</span><span class="token">></span><span class="token1">export</span><span class="token2">(</span>$earr<span class="token2">,</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'title'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'订单-'</span> <span class="token2">.</span> <span class="token1">date</span><span class="token2">(</span><span class="token4">'Y-m-d-H-i'</span><span class="token2">,</span> <span class="token1">time</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">'columns'</span> <span class="token">=</span><span class="token">></span> $titlearr<span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` ### 大数据导入导出 #### csv导出大数据使用方法fputcsv()函数; #### 步骤如下 - 计算出总数据数,大概测试出每次可导出数据量,比如1000条可导出,内存不会溢出;并设置初始导出;下面语句都是需要的,输出缓存,并且保证不会中文乱码 ``` <pre class="calibre14">``` $totalcount <span class="token">=</span> <span class="token1">pdo_fetchcolumn</span><span class="token2">(</span><span class="token4">"select count(id) from "</span><span class="token2">.</span><span class="token1">tablename</span><span class="token2">(</span><span class="token4">'ewei_shop_order'</span><span class="token2">)</span><span class="token2">.</span><span class="token4">' where uniacid='</span><span class="token2">.</span>$uniacid<span class="token2">)</span><span class="token2">;</span> $pagecount <span class="token">=</span> <span class="token3">1000</span><span class="token2">;</span> <span class="token1">set_time_limit</span><span class="token2">(</span><span class="token3">0</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">header</span><span class="token2">(</span><span class="token4">"Content-type:application/vnd.ms-excel"</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">header</span><span class="token2">(</span><span class="token4">"Content-Disposition:attachment;filename=全部订单-"</span><span class="token2">.</span><span class="token1">date</span><span class="token2">(</span><span class="token4">'Y-m-d-H-i-s'</span><span class="token2">)</span><span class="token2">.</span><span class="token4">".csv"</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">ob_start</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">ob_end_clean</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> echo <span class="token4">"\xEF\xBB\xBF"</span><span class="token2">;</span> $head <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'openid'</span><span class="token2">,</span><span class="token4">'粉丝昵称'</span><span class="token2">)</span><span class="token2">;</span> $fp <span class="token">=</span> <span class="token1">fopen</span><span class="token2">(</span><span class="token4">'php://output'</span><span class="token2">,</span> <span class="token4">'a'</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">fputcsv</span><span class="token2">(</span>$fp<span class="token2">,</span> $head<span class="token2">)</span><span class="token2">;</span> <span class="token5">for</span><span class="token2">(</span>$i<span class="token">=</span><span class="token3">0</span><span class="token2">;</span>$i<span class="token"><</span><span class="token1">intval</span><span class="token2">(</span>$totalcount<span class="token">/</span>$pagecount<span class="token2">)</span><span class="token">+</span><span class="token3">1</span><span class="token2">;</span>$i<span class="token">++</span><span class="token2">)</span><span class="token2">{</span> $sql<span class="token">=</span><span class="token4">""</span><span class="token2">;</span> $sql <span class="token2">.</span><span class="token">=</span> <span class="token4">' LIMIT '</span> <span class="token2">.</span> <span class="token1">strval</span><span class="token2">(</span>$i<span class="token">*</span>$pagecount<span class="token2">)</span> <span class="token2">.</span> <span class="token4">','</span> <span class="token2">.</span> $pagecount<span class="token2">;</span> $list <span class="token">=</span> <span class="token1">pdo_fetchall</span><span class="token2">(</span>$sql<span class="token2">)</span><span class="token2">;</span> <span class="token1">foreach</span><span class="token2">(</span>$list as <span class="token">&</span>$row<span class="token2">)</span><span class="token2">{</span> $export <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span>$row<span class="token2">[</span><span class="token4">'openid'</span><span class="token2">]</span><span class="token2">,</span>$row<span class="token2">[</span><span class="token4">'nickname'</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> <span class="token1">fputcsv</span><span class="token2">(</span>$fp<span class="token2">,</span> $export<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token1">unset</span><span class="token2">(</span>$row<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> ``` ``` 支付通知文件,返回如下信息,ewei\_shopv2\\payment\\wechat\\notify.php 通过 $this->get\['attach'\]获取 ``` <pre class="calibre16">``` Array <span class="token2">(</span> <span class="token2">[</span>get<span class="token2">]</span> <span class="token">=</span><span class="token">></span> Array <span class="token2">(</span> <span class="token2">[</span>appid<span class="token2">]</span> <span class="token">=</span><span class="token">></span> wx8a86306dcc35b538 <span class="token2">[</span>attach<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">5</span><span class="token2">:</span><span class="token3">17</span> <span class="token6">//前者为公众号id,后者是支付方式代码</span> <span class="token2">[</span>bank_type<span class="token2">]</span> <span class="token">=</span><span class="token">></span> CFT <span class="token2">[</span>cash_fee<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">1</span> <span class="token2">[</span>device_info<span class="token2">]</span> <span class="token">=</span><span class="token">></span> ewei_shopv2 <span class="token2">[</span>fee_type<span class="token2">]</span> <span class="token">=</span><span class="token">></span> CNY <span class="token2">[</span>is_subscribe<span class="token2">]</span> <span class="token">=</span><span class="token">></span> Y <span class="token2">[</span>mch_id<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">1485040312</span> <span class="token2">[</span>nonce_str<span class="token2">]</span> <span class="token">=</span><span class="token">></span> d1l11eOETpaY5AeRwC1E18e4T388O14o <span class="token2">[</span>openid<span class="token2">]</span> <span class="token">=</span><span class="token">></span> o2SfSwZusX6_wKAFOBGrBAiUpPNk <span class="token2">[</span>out_trade_no<span class="token2">]</span> <span class="token">=</span><span class="token">></span> OS20171128143422642364 <span class="token2">[</span>result_code<span class="token2">]</span> <span class="token">=</span><span class="token">></span> SUCCESS <span class="token2">[</span>return_code<span class="token2">]</span> <span class="token">=</span><span class="token">></span> SUCCESS <span class="token2">[</span>sign<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">5</span>B1B86703788788B021E51ED5D667E74 <span class="token2">[</span>time_end<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">20171128143428</span> <span class="token2">[</span>total_fee<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">1</span> <span class="token2">[</span>trade_type<span class="token2">]</span> <span class="token">=</span><span class="token">></span> JSAPI <span class="token2">[</span>transaction_id<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">4200000042201711287611180681</span> <span class="token2">)</span> <span class="token2">[</span>type<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">17</span> <span class="token2">[</span>total_fee<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">0.01</span> <span class="token2">[</span>set<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span>setting<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span>sec<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span>sign<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span>isapp<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token2">[</span>is_jie<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token2">)</span> ``` ```