暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Order.php 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Db;
  15. use think\Page;
  16. use think\Config;
  17. class Order extends Base
  18. {
  19. public function _initialize() {
  20. parent::_initialize();
  21. }
  22. public function index()
  23. {
  24. // 商城中心开启状态
  25. $shopOpen = getUsersConfigData('shop.shop_open');
  26. // 会员中心开启状态
  27. $webUsersSwitch = tpCache('global.web_users_switch');
  28. // 如果商城中心开启则执行跳转到商城中心,默认待发货订单列表
  29. if (!empty($shopOpen) && 1 === intval($shopOpen) && 1 == is_check_access('Shop@index')) {
  30. $url = url('Shop/index', ["order_status"=>1], true, true);
  31. $url = preg_replace('/^http(s?)/i', $this->request->scheme(), $url);
  32. $this->redirect($url);
  33. exit;
  34. }
  35. // 如果商城中心未开启,会员中心开启已开启则执行跳转到会员充值订单列表
  36. else if (!empty($webUsersSwitch) && 1 === intval($webUsersSwitch) && 1 == is_check_access('Member@money_index')) {
  37. $url = url('Member/money_index', ['status'=>2], true, true);
  38. $url = preg_replace('/^http(s?)/i', $this->request->scheme(), $url);
  39. $this->redirect($url);
  40. exit;
  41. } else {
  42. $this->error('您没有操作权限,请联系超级管理员分配权限');
  43. }
  44. // 其他逻辑待添加...
  45. }
  46. // AJAX下载订单Excel文档
  47. public function ajax_order_excel_export()
  48. {
  49. // 设置最大内存
  50. ini_set("memory_limit", "-1");
  51. // 防止php超时
  52. function_exists('set_time_limit') && set_time_limit(0);
  53. if (file_exists('./vendor/PHPExcel.zip') && !is_dir('./vendor/PHPExcel/')) {
  54. $zip = new \ZipArchive();//新建一个ZipArchive的对象
  55. if ($zip->open(ROOT_PATH.'vendor'.DS.'PHPExcel.zip') === true) {
  56. $zip->extractTo(ROOT_PATH.'vendor'.DS.'PHPExcel'.DS);
  57. $zip->close();//关闭处理的zip文件
  58. if (is_dir('./vendor/PHPExcel/')) {
  59. @unlink('./vendor/PHPExcel.zip');
  60. }
  61. }
  62. }
  63. // 执行操作
  64. if (IS_AJAX_POST) {
  65. $post = input('post.');
  66. $ResultUrl = null;
  67. if (1 == $post['export_type']) {
  68. // 查询商城订单并处理
  69. $ExportData = $this->GetShopOrder($post);
  70. } else if (2 == $post['export_type']) {
  71. // 查询充值订单并处理
  72. $ExportData = $this->GetMoneyOrder(1, $post);
  73. } else if (3 == $post['export_type']) {
  74. // 查询升级订单并处理
  75. $ExportData = $this->GetMoneyOrder(0, $post);
  76. } else if (4 == $post['export_type']) {
  77. // 查询视频订单并处理
  78. $ExportData = $this->GetMediaOrder($post);
  79. } else if (5 == $post['export_type']) {
  80. // 查询文章订单并处理
  81. $ExportData = $this->GetArticleOrder($post);
  82. } else if (6 == $post['export_type']) {
  83. // 查询文章订单并处理
  84. $ExportData = $this->GetServiceOrder($post);
  85. }
  86. if (!empty($ExportData['ExcelData'])) {
  87. // 执行导出下载
  88. $ResultUrl = $this->PerformExport($ExportData['ExcelData'], $ExportData['ExcelField'], $ExportData['ExcelTitle'], $post['export_type']);
  89. } else if (isset($ExportData['ExcelData'])) {
  90. $this->error('没有导出的数据');
  91. }
  92. }
  93. if (!empty($ResultUrl)) {
  94. $this->success('正在下载', $ResultUrl);
  95. } else {
  96. $this->error('导出失败');
  97. }
  98. }
  99. // 查询商城订单并处理
  100. private function GetShopOrder($post = [])
  101. {
  102. // 查询条件
  103. $where = [
  104. 'a.lang' => $this->admin_lang
  105. ];
  106. // 订单号查询
  107. $order_code = !empty($post['order_code']) ? $post['order_code'] : '';
  108. if (!empty($order_code)) $where['a.order_code'] = ['LIKE', "%{$order_code}%"];
  109. // 支付方式查询
  110. $pay_name = !empty($post['pay_name']) ? $post['pay_name'] : '';
  111. if (!empty($pay_name)) $where['a.pay_name'] = $pay_name;
  112. // 订单下单终端查询
  113. $order_terminal = !empty($post['order_terminal']) ? $post['order_terminal'] : 0;
  114. if (!empty($order_terminal)) $where['a.order_terminal'] = $order_terminal;
  115. // 商品类型查询
  116. $contains_virtual = !empty($post['contains_virtual']) ? $post['contains_virtual'] : 0;
  117. if (!empty($contains_virtual)) $where['a.contains_virtual'] = $contains_virtual;
  118. // 指定订单状态导出
  119. $order_status = !empty($post['order_status']) ? $post['order_status'] : 0;
  120. if (!empty($order_status)) $where['a.order_status'] = 10 == $order_status ? 0 : $order_status;
  121. // 根据日期导出
  122. $start_time = $post['start_time'];
  123. $end_time = $post['end_time'];
  124. if (!empty($start_time) && !empty($end_time)) {
  125. $start_time = strtotime($start_time);
  126. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  127. $where['a.add_time'] = ['between', [$start_time, $end_time]];
  128. } elseif (!empty($start_time) && empty($end_time)) {
  129. $start_time = strtotime($start_time);
  130. $where['a.add_time'] = ['>=', $start_time];
  131. } elseif (empty($start_time) && !empty($end_time)) {
  132. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  133. $where['a.add_time'] = ['<=', $end_time];
  134. }
  135. // 查询字段
  136. $Field = 'a.order_id, a.order_code, a.order_status, a.pay_time, a.pay_name, a.express_name, a.express_order, a.consignee, a.order_amount, a.order_total_num, a.province, a.city, a.district, a.address, a.mobile, a.add_time, b.username';
  137. // 执行查询
  138. $OrderData = Db::name('shop_order')
  139. ->alias('a')
  140. ->field($Field)
  141. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  142. ->where($where)
  143. ->order('a.order_id desc')
  144. ->select();
  145. if (empty($OrderData) && !empty($order_code)) {
  146. // 通过商品名称查询订单号
  147. $where_1['product_name'] = ['LIKE', "%{$order_code}%"];
  148. $order_details_list = Db::name('shop_order_details')->where($where_1)->order('order_id asc, details_id asc')->select();
  149. $order_details_list = group_same_key($order_details_list, 'order_id');
  150. $order_ids = array_keys($order_details_list);
  151. // 重新查询订单主表
  152. unset($where['a.order_code']);
  153. $where['a.order_id'] = ['IN', $order_ids];
  154. // 订单主表数据查询
  155. $OrderData = Db::name('shop_order')
  156. ->alias('a')
  157. ->field($Field)
  158. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  159. ->where($where)
  160. ->order('a.order_id desc')
  161. ->select();
  162. } else {
  163. if (!empty($OrderData)) {
  164. $order_ids = get_arr_column($OrderData, 'order_id');
  165. $where_1 = [];
  166. $where_1['order_id'] = ['IN', $order_ids];
  167. $order_details_list = Db::name('shop_order_details')->where($where_1)->order('order_id asc, details_id asc')->select();
  168. $order_details_list = group_same_key($order_details_list, 'order_id');
  169. }
  170. }
  171. // 获取订单状态,后台专用
  172. $admin_order_status_arr = Config::get('global.admin_order_status_arr');
  173. // 获取订单方式名称
  174. $pay_method_arr = Config::get('global.pay_method_arr');
  175. // 处理订单导出数据
  176. $ExcelData = [];
  177. foreach ($OrderData as $key => $value) {
  178. // 匹配省、市中文
  179. $Province = get_province_name($value['province']);
  180. // 匹配市、县中文
  181. $City = get_city_name($value['city']);
  182. // 匹配县、区、镇中文
  183. $District = get_area_name($value['district']);
  184. // 订单商品列表
  185. $product_name_all = '';
  186. $order_details_info = !empty($order_details_list[$value['order_id']]) ? $order_details_list[$value['order_id']] : [];
  187. if (!empty($order_details_info)) {
  188. foreach ($order_details_info as $k => $v) {
  189. if (!empty($product_name_all)) {
  190. $product_name_all .= PHP_EOL;
  191. }
  192. $product_name_all .= $v['product_name']. "\t";
  193. }
  194. }
  195. // 拼装追加数据
  196. $PushData = [
  197. // 订单信息
  198. 'order_code' => $value['order_code']. "\t",
  199. 'product_name' => $product_name_all,
  200. 'order_num' => $value['order_total_num'],
  201. 'order_amount' => $value['order_amount'],
  202. 'add_time' => date('Y-m-d H:i:s', $value['add_time']),
  203. 'order_status' => $admin_order_status_arr[$value['order_status']],
  204. // 支付信息
  205. 'pay_time' => !empty($value['pay_time']) ? date('Y-m-d H:i:s', $value['pay_time']) : '',
  206. 'pay_name' => in_array($value['order_status'], [1, 2, 3]) ? $pay_method_arr[$value['pay_name']] : '',
  207. // 快递信息
  208. 'express_name' => $value['express_name'],
  209. 'express_order' => $value['express_order']. "\t",
  210. // 会员账号
  211. 'username' => $value['username'],
  212. // 收货信息
  213. 'consignee' => $value['consignee'],
  214. 'mobile' => $value['mobile'],
  215. 'addr_info' => $Province . ' ' . $City . ' ' . $District . ' ' . $value['address'],
  216. ];
  217. // 追加数据,用于导出
  218. array_push($ExcelData, $PushData);
  219. }
  220. // 导出字段设置
  221. $ExcelField = ['order_code', 'product_name', 'order_num', 'order_amount', 'add_time', 'order_status', 'pay_time', 'pay_name', 'express_name', 'express_order', 'username', 'consignee', 'mobile', 'addr_info'];
  222. // 导出标题设置
  223. $ExcelTitle = ['订单号', '商品名称', '商品数量', '订单总额', '下单时间', '订单状态', '支付时间', '支付方式', '快递公司', '快递单号', '会员账号', '收货人', '联系电话', '收货地址'];
  224. // 返回导出所需数据及参数
  225. $ReturnData = [
  226. 'ExcelData' => $ExcelData,
  227. 'ExcelField' => $ExcelField,
  228. 'ExcelTitle' => $ExcelTitle
  229. ];
  230. return $ReturnData;
  231. }
  232. // 查询充值订单并处理
  233. private function GetMoneyOrder($cause_type = 0, $post = [])
  234. {
  235. // 查询条件
  236. $where = [
  237. // 多语言
  238. 'a.lang' => $this->admin_lang,
  239. // 查询充值订单
  240. 'a.cause_type' => $cause_type
  241. ];
  242. // 订单号或会员名查询
  243. $keywords = !empty($post['keywords']) ? $post['keywords'] : '';
  244. if (!empty($keywords)) $where['a.order_number|b.username'] = array('LIKE', "%{$keywords}%");
  245. // 支付方式查询
  246. $pay_method = !empty($post['pay_method']) ? $post['pay_method'] : '';
  247. if (!empty($pay_method)) $where['a.pay_method'] = $pay_method;
  248. // 会员级别查询
  249. $level = !empty($post['level']) ? $post['level'] : 0;
  250. if (!empty($level)) $where['b.level'] = $level;
  251. // 会员级别查询
  252. $level_id = !empty($post['level_id']) ? $post['level_id'] : 0;
  253. if (!empty($level_id)) $where['a.level_id'] = $level_id;
  254. // 指定订单状态导出
  255. $status = !empty($post['status']) ? $post['status'] : 0;
  256. if (!empty($post['status'])) $where['a.status'] = in_array($status, [2, 3]) ? ['IN', [2, 3]] : $status;
  257. // 如果传入的类型是0则强制查询已完成的订单
  258. if (0 === $cause_type) $where['a.status'] = ['IN', [2, 3]];
  259. // 根据日期导出
  260. $start_time = $post['start_time'];
  261. $end_time = $post['end_time'];
  262. if (!empty($start_time) && !empty($end_time)) {
  263. $start_time = strtotime($start_time);
  264. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  265. $where['a.add_time'] = ['between', [$start_time, $end_time]];
  266. } elseif (!empty($start_time) && empty($end_time)) {
  267. $start_time = strtotime($start_time);
  268. $where['a.add_time'] = ['>=', $start_time];
  269. } elseif (empty($start_time) && !empty($end_time)) {
  270. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  271. $where['a.add_time'] = ['<=', $end_time];
  272. }
  273. // 查询字段
  274. $Field = 'a.order_number, a.money, b.username, a.add_time, a.status, a.update_time, a.pay_method, a.cause, b.level';
  275. // 执行查询
  276. $MoneyData = Db::name('users_money')
  277. ->alias('a')
  278. ->field($Field)
  279. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  280. ->where($where)
  281. ->order('a.status desc, a.moneyid desc')
  282. ->select();
  283. // 充值状态
  284. $pay_status_arr = config('global.pay_status_arr');
  285. // 支付方式
  286. $pay_method_arr = config('global.pay_method_arr');
  287. // 处理订单导出数据
  288. $ExcelData = [];
  289. if (0 === $cause_type) {
  290. // 升级订单
  291. foreach ($MoneyData as $key => $value) {
  292. $value['cause'] = unserialize($value['cause']);
  293. // 拼装追加数据
  294. $PushData = [
  295. // 订单信息
  296. 'order_number' => $value['order_number']. "\t",
  297. 'type_name' => $value['cause']['type_name'],
  298. 'money' => $value['money'],
  299. 'add_time' => date('Y-m-d H:i:s', $value['add_time']),
  300. 'status' => $pay_status_arr[$value['status']],
  301. // 会员账号
  302. 'username' => $value['username'],
  303. // 支付信息
  304. 'update_time' => in_array($value['status'], [2, 3]) ? date('Y-m-d H:i:s', $value['update_time']) : '',
  305. 'pay_method' => in_array($value['status'], [2, 3]) ? $pay_method_arr[$value['pay_method']] : ''
  306. ];
  307. // 追加数据,用于导出
  308. array_push($ExcelData, $PushData);
  309. // 导出字段设置
  310. $ExcelField = ['order_number', 'type_name', 'money', 'username', 'add_time', 'status', 'update_time', 'pay_method'];
  311. // 导出标题设置
  312. $ExcelTitle = ['订单号', '产品名称', '订单金额', '会员账号', '升级时间', '订单状态', '支付时间', '支付方式'];
  313. }
  314. } else {
  315. // 充值订单
  316. foreach ($MoneyData as $key => $value) {
  317. // 拼装追加数据
  318. $PushData = [
  319. // 订单信息
  320. 'order_number' => $value['order_number']. "\t",
  321. 'money' => $value['money'],
  322. 'add_time' => date('Y-m-d H:i:s', $value['add_time']),
  323. 'status' => $pay_status_arr[$value['status']],
  324. // 会员账号
  325. 'username' => $value['username'],
  326. // 支付信息
  327. 'update_time' => in_array($value['status'], [2, 3]) ? date('Y-m-d H:i:s', $value['update_time']) : '',
  328. 'pay_method' => in_array($value['status'], [2, 3]) ? $pay_method_arr[$value['pay_method']] : ''
  329. ];
  330. // 追加数据,用于导出
  331. array_push($ExcelData, $PushData);
  332. }
  333. // 导出字段设置
  334. $ExcelField = ['order_number', 'money', 'username', 'add_time', 'status', 'update_time', 'pay_method'];
  335. // 导出标题设置
  336. $ExcelTitle = ['订单号', '充值金额', '会员账号', '充值时间', '订单状态', '支付时间', '支付方式'];
  337. }
  338. // 返回导出所需数据及参数
  339. $ReturnData = [
  340. 'ExcelData' => $ExcelData,
  341. 'ExcelField' => $ExcelField,
  342. 'ExcelTitle' => $ExcelTitle
  343. ];
  344. return $ReturnData;
  345. }
  346. // 查询视频订单并处理
  347. private function GetMediaOrder($post = [])
  348. {
  349. // 查询条件
  350. $where = [
  351. 'a.lang' => $this->admin_lang
  352. ];
  353. // 订单状态搜索
  354. $order_status = !empty($post['order_status']) ? $post['order_status'] : 0;
  355. if (!empty($order_status)) $where['a.order_status'] = intval($order_status) === 1 ? intval($order_status) : 0;
  356. // 订单号或用户名搜索
  357. $keywords = !empty($post['keywords']) ? $post['keywords'] : '';
  358. if (!empty($keywords)) $where['a.order_code|b.username'] = ['LIKE', "%{$keywords}%"];
  359. // 支付方式查询
  360. $pay_name = !empty($post['pay_name']) ? $post['pay_name'] : '';
  361. if (!empty($pay_name)) $where['a.pay_name'] = $pay_name;
  362. // 根据日期导出
  363. $start_time = $post['start_time'];
  364. $end_time = $post['end_time'];
  365. if (!empty($start_time) && !empty($end_time)) {
  366. $start_time = strtotime($start_time);
  367. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  368. $where['a.add_time'] = ['between', [$start_time, $end_time]];
  369. } elseif (!empty($start_time) && empty($end_time)) {
  370. $start_time = strtotime($start_time);
  371. $where['a.add_time'] = ['>=', $start_time];
  372. } elseif (empty($start_time) && !empty($end_time)) {
  373. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  374. $where['a.add_time'] = ['<=', $end_time];
  375. }
  376. // 查询字段
  377. $Field = 'a.order_code, a.order_amount, b.username, a.mobile, a.add_time, a.order_status';
  378. // 执行查询
  379. $MediaData = Db::name('media_order')
  380. ->alias('a')
  381. ->field($Field)
  382. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  383. ->where($where)
  384. ->order('a.order_status desc, a.order_id desc')
  385. ->select();
  386. // 处理订单导出数据
  387. $ExcelData = [];
  388. foreach ($MediaData as $key => $value) {
  389. // 拼装追加数据
  390. $PushData = [
  391. // 订单信息
  392. 'order_code' => $value['order_code']. "\t",
  393. 'order_amount' => $value['order_amount'],
  394. 'add_time' => date('Y-m-d H:i:s', $value['add_time']),
  395. 'order_status' => 1 == $value['order_status'] ? '已付款' : '未付款',
  396. // 会员账号
  397. 'mobile' => $value['mobile'],
  398. 'username' => $value['username']
  399. ];
  400. // 追加数据,用于导出
  401. array_push($ExcelData, $PushData);
  402. }
  403. // 导出字段设置
  404. $ExcelField = ['order_code', 'order_amount', 'username', 'mobile', 'add_time', 'order_status'];
  405. // 导出标题设置
  406. $ExcelTitle = ['订单号', '订单金额', '会员账号', '会员手机', '下单时间', '订单状态'];
  407. // 返回导出所需数据及参数
  408. $ReturnData = [
  409. 'ExcelData' => $ExcelData,
  410. 'ExcelField' => $ExcelField,
  411. 'ExcelTitle' => $ExcelTitle
  412. ];
  413. return $ReturnData;
  414. }
  415. // 查询文章订单并处理
  416. private function GetArticleOrder($post = [])
  417. {
  418. // 查询条件
  419. $where = [
  420. 'a.lang' => $this->admin_lang
  421. ];
  422. // 订单状态搜索
  423. $order_status = !empty($post['order_status']) ? $post['order_status'] : 0;
  424. if (!empty($order_status)) $where['a.order_status'] = intval($order_status) === 1 ? intval($order_status) : 0;
  425. // 订单号或用户名搜索
  426. $keywords = !empty($post['keywords']) ? $post['keywords'] : '';
  427. if (!empty($keywords)) $where['a.order_code|b.username'] = ['LIKE', "%{$keywords}%"];
  428. // 支付方式查询
  429. $pay_name = !empty($post['pay_name']) ? $post['pay_name'] : '';
  430. if (!empty($pay_name)) $where['a.pay_name'] = $pay_name;
  431. // 根据日期导出
  432. $start_time = $post['start_time'];
  433. $end_time = $post['end_time'];
  434. if (!empty($start_time) && !empty($end_time)) {
  435. $start_time = strtotime($start_time);
  436. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  437. $where['a.add_time'] = ['between', [$start_time, $end_time]];
  438. } elseif (!empty($start_time) && empty($end_time)) {
  439. $start_time = strtotime($start_time);
  440. $where['a.add_time'] = ['>=', $start_time];
  441. } elseif (empty($start_time) && !empty($end_time)) {
  442. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  443. $where['a.add_time'] = ['<=', $end_time];
  444. }
  445. // 查询字段
  446. $Field = 'a.order_code, a.order_amount, b.username, a.add_time, a.order_status';
  447. // 执行查询
  448. $ArticleData = Db::name('article_order')
  449. ->alias('a')
  450. ->field($Field)
  451. ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
  452. ->where($where)
  453. ->order('a.order_status desc, a.order_id desc')
  454. ->select();
  455. // 处理订单导出数据
  456. $ExcelData = [];
  457. foreach ($ArticleData as $key => $value) {
  458. // 拼装追加数据
  459. $PushData = [
  460. // 订单信息
  461. 'order_code' => $value['order_code']. "\t",
  462. 'order_amount' => $value['order_amount'],
  463. 'add_time' => date('Y-m-d H:i:s', $value['add_time']),
  464. 'order_status' => 1 == $value['order_status'] ? '已付款' : '未付款',
  465. // 会员账号
  466. 'username' => $value['username']
  467. ];
  468. // 追加数据,用于导出
  469. array_push($ExcelData, $PushData);
  470. }
  471. // 导出字段设置
  472. $ExcelField = ['order_code', 'order_amount', 'username', 'add_time', 'order_status'];
  473. // 导出标题设置
  474. $ExcelTitle = ['订单号', '订单金额', '会员账号', '下单时间', '订单状态'];
  475. // 返回导出所需数据及参数
  476. $ReturnData = [
  477. 'ExcelData' => $ExcelData,
  478. 'ExcelField' => $ExcelField,
  479. 'ExcelTitle' => $ExcelTitle
  480. ];
  481. return $ReturnData;
  482. }
  483. // 查询售后订单并处理
  484. private function GetServiceOrder($post = [])
  485. {
  486. // 初始化数组和条件
  487. $where =[];
  488. // 订单号查询
  489. $order_code = !empty($post['order_code']) ? trim($post['order_code']) : '';
  490. if (!empty($order_code)) $where['a.order_code|a.product_name'] = ['LIKE', "%{$order_code}%"];
  491. // 支付方式查询
  492. $pay_name = !empty($post['pay_name']) ? $post['pay_name'] : '';
  493. if (!empty($pay_name)) $where['c.pay_name'] = $pay_name;
  494. // 订单下单终端查询
  495. $order_terminal = !empty($post['order_terminal']) ? $post['order_terminal'] : 0;
  496. if (!empty($order_terminal)) $where['c.order_terminal'] = $order_terminal;
  497. // 根据日期导出
  498. $start_time = $post['start_time'];
  499. $end_time = $post['end_time'];
  500. if (!empty($start_time) && !empty($end_time)) {
  501. $start_time = strtotime($start_time);
  502. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  503. $where['a.add_time'] = ['between', [$start_time, $end_time]];
  504. } elseif (!empty($start_time) && empty($end_time)) {
  505. $start_time = strtotime($start_time);
  506. $where['a.add_time'] = ['>=', $start_time];
  507. } elseif (empty($start_time) && !empty($end_time)) {
  508. $end_time = strtotime("+1 day", strtotime($end_time)) - 1;
  509. $where['a.add_time'] = ['<=', $end_time];
  510. }
  511. $Service = Db::name('shop_order_service')->alias('a')
  512. ->field('a.*, b.product_price, b.num as product_num, d.username')
  513. ->join('__SHOP_ORDER_DETAILS__ b', 'a.details_id = b.details_id', 'LEFT')
  514. ->join('__SHOP_ORDER__ c', 'a.order_id = c.order_id', 'LEFT')
  515. ->join('__USERS__ d', 'a.users_id = d.users_id', 'LEFT')
  516. ->where($where)
  517. ->order('a.service_id desc')
  518. ->select();
  519. // 获取订单状态
  520. $ServiceStatus = Config::get('global.order_service_status');
  521. // 处理订单导出数据
  522. $ExcelData = [];
  523. foreach ($Service as $key => $value) {
  524. $service_type = '换货';
  525. if (2 === intval($value['service_type'])) {
  526. $service_type = '退货';
  527. } else if (3 === intval($value['service_type'])) {
  528. $service_type = '维修';
  529. }
  530. // 拼装追加数据
  531. $PushData = [
  532. // 订单信息
  533. 'order_code' => $value['order_code'] . "\t",
  534. 'refund_code' => $value['refund_code'] . "\t",
  535. 'product_name' => $value['product_name'],
  536. 'product_price' => $value['product_price'],
  537. 'product_num' => $value['product_num'],
  538. 'refund_price' => $value['refund_price'],
  539. 'service_type' => $service_type,
  540. 'status' => $ServiceStatus[$value['status']],
  541. 'add_time' => date('Y-m-d H:i:s', $value['add_time']),
  542. // 会员账号
  543. 'username' => $value['username'],
  544. // 收货信息
  545. 'consignee' => $value['consignee'],
  546. 'mobile' => $value['mobile'],
  547. 'address' => trim($value['address']),
  548. ];
  549. // 追加数据,用于导出
  550. array_push($ExcelData, $PushData);
  551. }
  552. // 导出字段设置
  553. $ExcelField = ['order_code', 'refund_code', 'product_name', 'product_price', 'product_num', 'refund_price', 'service_type', 'status', 'add_time', 'username', 'consignee', 'mobile', 'address'];
  554. // 导出标题设置
  555. $ExcelTitle = ['订单号', '服务单号', '商品名称', '商品价格', '商品数量', '退还金额', '售后类型', '处理状态', '售后时间', '会员账号', '收货人', '联系电话', '收货地址'];
  556. // 返回导出所需数据及参数
  557. $ReturnData = [
  558. 'ExcelData' => $ExcelData,
  559. 'ExcelField' => $ExcelField,
  560. 'ExcelTitle' => $ExcelTitle
  561. ];
  562. return $ReturnData;
  563. }
  564. // 执行导出下载
  565. private function PerformExport($ExcelData = [], $ExcelField = [], $ExcelTitle = [], $export_type = [])
  566. {
  567. // 引入SDK
  568. vendor("PHPExcel.Classes.PHPExcel");
  569. // Excel表格坐标
  570. $cell_arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  571. // 执行导出
  572. $objPHPExcel = new \PHPExcel();
  573. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(12);
  574. // 设置表格标题栏长度
  575. $objActSheet = $objPHPExcel->getActiveSheet();
  576. if (1 == $export_type) {// 商城订单
  577. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(22);
  578. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
  579. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(8);
  580. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(8);
  581. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
  582. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(8);
  583. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(18);
  584. $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
  585. $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(8);
  586. $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(22);
  587. $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(8);
  588. $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(8);
  589. $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(13);
  590. $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(40);
  591. // 设置导出表格名称
  592. $FileName = 'shop-order-'.date("YmdHis");
  593. } else if (2 == $export_type) {// 充值订单
  594. // 设置导出表格标题宽度
  595. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(22);
  596. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
  597. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
  598. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
  599. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
  600. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(18);
  601. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
  602. // 设置导出表格名称
  603. $FileName = 'money-order-'.date("YmdHis");
  604. } else if (3 == $export_type) {// 充值订单
  605. // 设置导出表格标题宽度
  606. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(22);
  607. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  608. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
  609. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
  610. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
  611. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
  612. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(18);
  613. $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
  614. // 设置导出表格名称
  615. $FileName = 'upgrade-order-'.date("YmdHis");
  616. } else if (4 == $export_type) {// 视频订单
  617. // 设置导出表格标题宽度
  618. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(18);
  619. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
  620. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
  621. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(13);
  622. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
  623. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
  624. // 设置导出表格名称
  625. $FileName = 'media-order-'.date("YmdHis");
  626. } else if (5 == $export_type) {// 文章订单
  627. // 设置导出表格标题宽度
  628. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(18);
  629. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
  630. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
  631. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(13);
  632. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
  633. // 设置导出表格名称
  634. $FileName = 'article-order-'.date("YmdHis");
  635. } else if (6 == $export_type) {// 售后订单
  636. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(22);
  637. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
  638. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(22);
  639. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(8);
  640. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(8);
  641. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(8);
  642. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(8);
  643. $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
  644. $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(18);
  645. $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(8);
  646. $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(8);
  647. $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(13);
  648. $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(40);
  649. // 设置导出表格名称
  650. $FileName = 'service-order-'.date("YmdHis");
  651. }
  652. // 循环设置表格标题栏数据
  653. $startRow = 1;
  654. if(!empty($ExcelTitle) || count($ExcelTitle) > 0) {
  655. foreach($ExcelTitle as $k => $v) {
  656. $objActSheet->setCellValue($cell_arr[$k] . $startRow, $v);
  657. }
  658. $startRow = 2;
  659. }
  660. // 循环设置表格字段内容数据
  661. foreach($ExcelData as $v) {
  662. $columnField = 'A';
  663. foreach($ExcelField as $key => $value) {
  664. $objActSheet->setCellValue($cell_arr[$key] . $startRow, $v[$value]);
  665. if (1 == $export_type) {// 商城订单
  666. $product_name_arr = explode(PHP_EOL, $v['product_name']);
  667. $product_name_num = count($product_name_arr);
  668. $height = ($product_name_num * 15) + 5;
  669. $objActSheet->getRowDimension($startRow)->setRowHeight($height);// 设置行高
  670. $objActSheet->getStyle($columnField.($startRow))->getAlignment()->setWrapText(true);//设置自动换行
  671. $objActSheet->getStyle($columnField. $startRow)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
  672. $columnField++;
  673. }
  674. }
  675. $startRow++;
  676. }
  677. $objPHPExcel->setActiveSheetIndex(0);
  678. header('Content-Type: application/vnd.ms-excel');
  679. header('Content-Disposition: attachment;filename="' . $FileName . '.xlsx"');
  680. header('Cache-Control: max-age=0');
  681. header('Cache-Control: max-age=1');
  682. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
  683. header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  684. header('Cache-Control: cache, must-revalidate');
  685. header('Pragma: public');
  686. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  687. // 文件目录
  688. $ExcelPath = UPLOAD_PATH . 'excel/';
  689. // 保存前清空删除原先的excel
  690. delFile(UPLOAD_PATH . 'excel/', true);
  691. // 创建文件夹
  692. @mkdir($ExcelPath, 0777, true);
  693. // excel文件路径
  694. $filePath = $ExcelPath . $FileName . '.xlsx';
  695. // 保存excel文件
  696. $objWriter->save($filePath);
  697. // 返回excel文件路径到AJAX下载
  698. return request()->domain() . ROOT_DIR . '/' . $filePath;
  699. }
  700. }