Orders.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Orders as Request;
  3. use App\Models\AdminUser;
  4. use App\Models\Custom;
  5. use App\Models\CustomAddr;
  6. use App\Models\CustomScore;
  7. use App\Models\FilesManager;
  8. use App\Models\Product;
  9. use App\Models\Orders as Model;
  10. use App\Models\Orders\Receipt;
  11. use App\Models\OrdersAddr;
  12. use App\Models\OrdersProduct;
  13. use App\Models\Business;
  14. use Illuminate\Support\Facades\DB;
  15. use PhpOffice\PhpSpreadsheet\Cell\DataType;
  16. use PhpOffice\PhpSpreadsheet\IOFactory;
  17. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  18. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  19. use PhpOffice\PhpSpreadsheet\Style\Fill;
  20. use Intervention\Image\Facades\Image;
  21. use Intervention\Image\Gd\Font;
  22. use App\Models\Custom\Shoptype;
  23. /**
  24. * 订单管理
  25. *
  26. * @author 刘相欣
  27. *
  28. */
  29. class Orders extends Auth{
  30. protected function _initialize(){
  31. parent::_initialize();
  32. $this->assign('breadcrumb1','销售管理');
  33. $this->assign('breadcrumb2','订单管理');
  34. }
  35. /**
  36. * 首页列表
  37. *
  38. * */
  39. public function index(Model $Model,OrdersProduct $OrdersProduct,Product $Product,Custom $Custom,Shoptype $Shoptype,Business $Business){
  40. // 接受参数
  41. $code = request('order_code','');
  42. $orders_other = request('orders_other',0);
  43. $productCode = request('product_code','');
  44. $phone = request('phone','');
  45. $customCode = request('custom_code','');
  46. $productName = request('product_name','');
  47. $province = request('contact_province','');
  48. $city = request('contact_city','');
  49. $area = request('contact_area','');
  50. $status = request('status',0);
  51. $startTime = request('start_time','');
  52. $endTime = request('end_time','');
  53. $is_regiment = request('is_regiment','');
  54. $businessId = request('business_id','');
  55. // 编码转ID
  56. $id = $code ? $Model->codeToId($code) : 0;
  57. $productId = $productCode ? $Product->codeToId($productCode) : 0;
  58. $uid = $customCode ? $Custom->codeToId($customCode) : 0;
  59. // 查询条件
  60. $map = [];
  61. // 编码ID
  62. if( $id ) $map[] = ['orders_product.order_id','=',$id];
  63. // 编码ID
  64. if( $orders_other ) $map[] = $orders_other == 1 ? ['orders_product.product_id','>',0] : ['orders_product.product_id','=',0];
  65. if( $uid ) $map[] = ['custom.uid','=',$uid];
  66. if( $productId ) $map[] = ['orders_product.product_id','=',$productId];
  67. if( $productName ) $map[] = ['orders_product.product_name','LIKE','%'.$productName.'%'];
  68. if( $phone ) $map[] = ['orders_addr.contact_phone','=',$phone];
  69. if( $province ) $map[] = ['orders_addr.contact_province','LIKE','%'.$province.'%'];
  70. if( $city ) $map[] = ['orders_addr.contact_city','LIKE','%'.$city.'%'];
  71. if( $area ) $map[] = ['orders_addr.contact_area','LIKE','%'.$area.'%'];
  72. if( $startTime ) $map[] = ['orders_product.insert_time','>=',strtotime($startTime)];
  73. if( $endTime ) $map[] = ['orders_product.insert_time','<=',strtotime($endTime)];
  74. if( $status ) $map[] = ['orders_product.status','=',$status];
  75. if( $businessId ) $map[] = ['orders_product.business_id','=',$businessId];
  76. if($is_regiment){
  77. if( $is_regiment == 1) $map[] = ['orders.regiment_id','>',0];
  78. if( $is_regiment == 2) $map[] = ['orders.regiment_id','=',0];
  79. }
  80. // 当前登录的角色数据
  81. $session = session('userRule') ? session('userRule') : ['business_id'=>0,'menu_type'=>0,'data_type'=>0];
  82. // 是否存在对应的商业公司ID
  83. if ( $session['business_id'] ) $map[] = ['orders_product.business_id','=',$session['business_id']];
  84. // 数据类型
  85. $shopIds = ($session['menu_type'] == 1 && $session['data_type'] == 2) ? Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray() : [];
  86. // 查询数据
  87. $list = $OrdersProduct->query()
  88. ->join('custom','orders_product.custom_uid','=','custom.uid')
  89. ->join('orders_addr','orders_addr.order_id','=','orders_product.order_id')
  90. ->leftJoin('orders','orders.id','=','orders_product.order_id');
  91. if ( $shopIds ) $list = $list->whereIn('orders_product.business_id',$shopIds);
  92. $list = $list->where($map)
  93. ->orderByDesc('id')
  94. ->select([
  95. 'orders_product.*','custom.username as custom_name','orders.regiment_id as regiment_id',
  96. 'orders_addr.contact_name','orders_addr.contact_shop','orders_addr.shop_type','orders_addr.contact_phone','orders_addr.contact_province','orders_addr.contact_city','orders_addr.contact_area','orders_addr.contact_addr'
  97. ])
  98. ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  99. // 循环处理数据
  100. foreach ($list as $key => $value) {
  101. // id转编号
  102. $value['order_code'] = $Model->idToCode($value['order_id']);
  103. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  104. $value['state'] = $Model->getState($value['status'],'state');
  105. $value['shop_type'] = $Shoptype->getOne($value['shop_type'],'name');
  106. $value['business_name']= Business::query()->where('id',$value['business_id'])->value('name');
  107. $value['product_code'] = $value['product_id'] ? $Product->idToCode($value['product_id']) : '— —';
  108. // 重组
  109. $list[$key] = $value;
  110. }
  111. // 查询商业公司数据
  112. $businessList = $Business->getListByAdmin();
  113. // 分配数据
  114. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  115. $this->assign('list', $list);
  116. $this->assign('businessList',$businessList);
  117. // 加载模板
  118. return $this->fetch();
  119. }
  120. /**
  121. * 首页列表
  122. *
  123. * */
  124. public function detail(Model $Model,AdminUser $AdminUser,OrdersProduct $OrdersProduct,Product $Product,Custom $Custom,OrdersAddr $OrdersAddr,Receipt $Receipt){
  125. // 接受参数
  126. $id = request('order_id','');
  127. // 查询数据
  128. $order = $Model->query()->find($id);
  129. // 查询不到订单
  130. if( !$order ) return $this->error('订单数据不存在');
  131. // id转编号
  132. $order['order_code'] = $Model->idToCode($order['id']);
  133. $order['custom_code'] = $Custom->idToCode($order['custom_uid']);
  134. $order['custom_name'] = $Custom->getValue($order['custom_uid'],'username');
  135. $order['state'] = $Model->getState($order['status'],'state');
  136. // 查询子订单数据
  137. $orderItems = $OrdersProduct->query()->where([['order_id','=',$id]])->select(['id as item_id','order_id','product_id','buy_num','pay_total','is_rebate','sku_attr_names as product_spec','product_name','product_thumb'])->get()->toArray();
  138. // 地址
  139. $orderAddr = $OrdersAddr->query()->where([['order_id','=',$id]])->first(['contact_name','contact_phone','contact_province','contact_city','contact_area','contact_addr','contact_shop']);
  140. // 审核记录
  141. $orderReceipt = $Receipt->query()->where([['order_id','=',$id]])->orderByDesc('id')->get()->toArray();
  142. // 循环数据
  143. foreach ($orderItems as $key => $value) {
  144. $value['product_code'] = $Product->idToCode($value['product_id']);
  145. $orderItems[$key] = $value;
  146. }
  147. // 循环数据
  148. foreach ($orderReceipt as $key => $value) {
  149. // 操作人员
  150. $value['admin_name']= $AdminUser->getOne($value['admin_uid'],'username');
  151. $value['image'] = path_compat($value['image']);
  152. $orderReceipt[$key] = $value;
  153. }
  154. // 积分
  155. $score = $orderReceipt ? max(array_column($orderReceipt,'give_score')) : 0;
  156. // 恭喜
  157. $shopName = $orderAddr['contact_shop'] ? $orderAddr['contact_shop'] : $order['custom_name'];
  158. // 结果
  159. $shareImage = $this->getShareImage($shopName,$score);
  160. // 分配数据
  161. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  162. $this->assign('order', $order);
  163. $this->assign('orderAddr', $orderAddr);
  164. $this->assign('orderItems', $orderItems);
  165. $this->assign('orderReceipt', $orderReceipt);
  166. $this->assign('shareImage', $shareImage);
  167. // 加载模板
  168. return $this->fetch();
  169. }
  170. /**
  171. * 状态
  172. *
  173. * */
  174. public function set_status( Request $request, Model $Model,OrdersProduct $OrdersProduct,CustomScore $CustomScore){
  175. // 验证参数
  176. $request->scene('set_status')->validate();
  177. // 接收参数
  178. $id = request('id',0);
  179. $status = request('status',0);
  180. // 获取产品和数量
  181. $oldData = $Model->query()->find($id,['id','order_score','custom_uid','insert_time']);
  182. // 如果用户不存在
  183. if( !$oldData ) return json_send(['code'=>'error','msg'=>'订单不存在']);
  184. // 组合数据,写入订单表,子表
  185. DB::beginTransaction();
  186. try{
  187. // 查询数据
  188. $result = $Model->setOrderStatus($id,$status,$OrdersProduct);
  189. // 提示新增失败
  190. if( isset($result['error']) ) {
  191. // 回退数据
  192. DB::rollBack();
  193. // 提示信息
  194. return json_send(['code'=>'error','msg'=>$result['error'],'data'=>['error'=>$result['error']]]);
  195. }
  196. if( $status == 4 ){
  197. // 取消积分
  198. if( $oldData['order_score'] > 0 ) {
  199. // 如果扣减失败
  200. $result = $CustomScore->trade($oldData['custom_uid'],$oldData['id'],($oldData['order_score']*-1),6,1);
  201. // 提示新增失败
  202. if( isset($result['error']) ) {
  203. // 回退数据
  204. DB::rollBack();
  205. // 提示信息
  206. return json_send(['code'=>'error','msg'=>'取消赠送积分失败','data'=>['error'=>$result['error']]]);
  207. }
  208. }
  209. // 取消关联订单
  210. $result = $Model->cancelRelate($oldData['insert_time'],$oldData['custom_uid'],$OrdersProduct,$CustomScore);
  211. // 提示新增失败
  212. if( isset($result['error']) ) {
  213. // 回退数据
  214. DB::rollBack();
  215. // 提示信息
  216. return json_send(['code'=>'error','msg'=>'取消关联订单失败','data'=>['error'=>$result['error']]]);
  217. }
  218. }
  219. // 提交数据
  220. DB::commit();
  221. // 告知结果
  222. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  223. // 返回结果
  224. } catch (\Throwable $th) {
  225. // 回退数据
  226. DB::rollBack();
  227. // 下单失败提示
  228. return json_send(['code'=>'error','msg'=>'设置失败','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  229. }
  230. }
  231. /**
  232. * 表格导入
  233. *
  234. * */
  235. public function import_execl( Request $request,Model $Model,Custom $Custom,OrdersAddr $OrdersAddr,OrdersProduct $OrdersProduct, FilesManager $FilesManager,CustomAddr $CustomAddr){
  236. // 验证参数
  237. $request->scene('import_execl')->validate();
  238. // 获取表格信息
  239. $file = request()->file('order_file');
  240. // 返回结果
  241. $sheetList = $FilesManager->excelToOrder($file);
  242. // 如果不存在结果
  243. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  244. // 订单列表
  245. $orderList = [];
  246. // 当前时间
  247. $time = time();
  248. // 循环表格数据
  249. foreach ($sheetList as $value) {
  250. // 状态更改
  251. $value['status'] = $Model->getWeibanStatus($value['status']);
  252. // 客户手机号
  253. $orderList[$value['weizan_orderid']]['custom'] = ['phone'=>$value['contact_phone'],'username'=>$value['buyer_nick']];
  254. // 组合成订单的收件地址
  255. $orderList[$value['weizan_orderid']]['contact'] = [
  256. 'contact_name'=>trim($value['contact_name']),
  257. 'contact_phone'=>trim($value['contact_phone']),
  258. 'contact_province'=>trim($value['contact_province']),
  259. 'contact_city'=>trim($value['contact_city']),
  260. 'contact_area'=>trim($value['contact_area']),
  261. 'contact_addr'=>trim($value['contact_addr'])
  262. ];
  263. // 组合成订单的收件地址
  264. $orderList[$value['weizan_orderid']]['product'][] = [
  265. 'status'=>$value['status'],
  266. 'product_name'=>$value['product_name'],
  267. 'sku_attr_names'=>$value['sku_attr_names'],
  268. 'buy_num'=>$value['buy_num'],
  269. 'pay_total'=>$value['pay_total'],
  270. 'price_total'=>$value['pay_total'],
  271. 'insert_time'=>$value['insert_time'],
  272. 'update_time'=>$time,
  273. ];
  274. // 组合成订单的需要的数据
  275. if( !isset($orderList[$value['weizan_orderid']]['order']) ) $orderList[$value['weizan_orderid']]['order'] = ['weizan_orderid'=>$value['weizan_orderid'],'status'=>$value['status'],'pay_total'=>0,'price_total'=>0,'insert_time'=>$value['insert_time']];
  276. // 价格
  277. $orderList[$value['weizan_orderid']]['order']['pay_total'] = $orderList[$value['weizan_orderid']]['order']['pay_total'] + $value['pay_total'];
  278. $orderList[$value['weizan_orderid']]['order']['price_total'] = $orderList[$value['weizan_orderid']]['order']['pay_total'];
  279. }
  280. // 新增地址
  281. $newAddrList = [];
  282. // 要更新的订单子表
  283. $orderProduct = [];
  284. // 循环订单列表
  285. foreach ($orderList as $value) {
  286. // 获取手机号,查询是否用客户
  287. $custom = $Custom->getOneByPhone($value['custom']['phone']);
  288. // 如果存在手机号
  289. $uid = $custom ? $custom['uid'] : $Custom->add($value['custom']);
  290. // 如果客户存在
  291. if( !$uid ) return json_send(['code'=>'error','msg'=>$value['custom']['username'].'【'.$value['custom']['phone'].'】'.'无法创建用户']);
  292. // 通过订单号查询是否存在系统订单
  293. $orderId = $Model->query()->where([['weizan_orderid','=',$value['order']['weizan_orderid']]])->value('id');
  294. // 客户ID
  295. $value['order']['custom_uid'] = $uid;
  296. // 存在订单获取订单ID,不存在则新增
  297. $orderId = $orderId ? $Model->edit($orderId,$value['order']) : $Model->add($value['order']);
  298. // 如果客户存在
  299. if( !$orderId ) return json_send(['code'=>'error','msg'=>$orderId.'订单写入失败']);
  300. // 联系地址
  301. $contact = $value['contact'];
  302. // 存在详细地址,才创建地址库
  303. if( $contact['contact_name'] && $contact['contact_phone'] && $contact['contact_province'] && $contact['contact_city'] && $contact['contact_area'] && $contact['contact_addr'] ) {
  304. // 收件地址是否存在
  305. $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first();
  306. // 如果不存在地址
  307. if( !$oldAddr ) $CustomAddr->add(['custom_uid'=>$uid,'contact_name'=>$contact['contact_name'],'contact_phone'=>$contact['contact_phone'],'contact_province'=>$contact['contact_province'],'contact_city'=>$contact['contact_city'],'contact_area'=>$contact['contact_area'],'contact_addr'=>$contact['contact_addr']]);
  308. }
  309. // 订单地址库
  310. $addrId = $OrdersAddr->query()->where([['order_id','=',$orderId]])->value('id');
  311. // 订单ID
  312. $value['contact']['order_id'] = $orderId;
  313. // 订单地址ID
  314. $value['contact']['id'] = (int)$addrId;
  315. // 不存在地址的话
  316. $newAddrList[] = $value['contact'];
  317. // 循环子订单
  318. foreach ( $value['product'] as $product ) {
  319. // 数据结果
  320. $product['order_id'] = $orderId;
  321. $product['custom_uid'] = $uid;
  322. $product['id'] = (int) $OrdersProduct->query()->where([['order_id','=',$orderId],'product_name'=>$product['product_name'],'sku_attr_names'=>$product['sku_attr_names']])->value('id');
  323. $orderProduct[] = $product;
  324. }
  325. }
  326. // 新地址写入
  327. $OrdersProduct->query()->upsert($orderProduct,'id',['product_name','sku_attr_names','buy_num','pay_total','price_total','update_time']);
  328. // 新地址写入
  329. $OrdersAddr->query()->upsert($newAddrList,'id',['contact_name','contact_phone','contact_province','contact_city','contact_area','contact_addr']);
  330. // 提示成功
  331. return json_send(['code'=>'success','msg'=>'订单导入成功','path'=>'']);
  332. }
  333. /**
  334. * 批量修改状态
  335. *
  336. * */
  337. public function import_execl_status( Request $request,Model $Model,OrdersProduct $OrdersProduct,FilesManager $FilesManager){
  338. // 验证参数
  339. $request->scene('import_execl_status')->validate();
  340. // 获取表格信息
  341. $file = request()->file('order_file');
  342. // 返回结果
  343. $sheetList = $FilesManager->excelToOrderStatus($file);
  344. // 如果不存在结果
  345. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  346. // 循环表格数据
  347. foreach ($sheetList as $key=>$value) {
  348. // 客户ID
  349. $orderId = $Model->codeToId($value['order_code']);
  350. // 如果客户ID有误
  351. if( !$orderId ) return json_send(['code'=>'error','msg'=>$value['order_code'].'编码有误']);
  352. // 订单状态
  353. $status = $Model->getWeibanStatus($value['status']);
  354. // 状态提示
  355. if( $status < 0 ) return json_send(['code'=>'error','msg'=>$value['order_code'].'状态有误']);
  356. // 修改数据
  357. $result = $Model->setOrderStatus($orderId,$status,$OrdersProduct);
  358. // 提示
  359. if( isset($result['error']) ) return json_send(['code'=>'error','msg'=>$value['order_code'].$result['error']]);
  360. }
  361. // 提示成功
  362. return json_send(['code'=>'success','msg'=>'订单导入成功','path'=>'']);
  363. }
  364. /**
  365. * 导出表格导入
  366. *
  367. * */
  368. public function down_excel(Model $Model,OrdersProduct $OrdersProduct,Product $Product,Custom $Custom,Shoptype $Shoptype,Business $Business){
  369. // 接受参数
  370. $code = request('order_code','');
  371. $productCode = request('product_code','');
  372. $orders_other = request('orders_other',0);
  373. $customCode = request('custom_code','');
  374. $productName = request('product_name','');
  375. $phone = request('phone','');
  376. $province = request('contact_province','');
  377. $city = request('contact_city','');
  378. $area = request('contact_area','');
  379. $status = request('status',0);
  380. $startTime = request('start_time','');
  381. $endTime = request('end_time','');
  382. // 编码转ID
  383. $id = $code ? $Model->codeToId($code) : 0;
  384. $productId = $productCode ? $Product->codeToId($productCode) : 0;
  385. $uid = $customCode ? $Custom->codeToId($customCode) : 0;
  386. // 查询条件
  387. $map = [];
  388. // 编码ID
  389. if( $id ) $map[] = ['orders_product.order_id','=',$id];
  390. if( $uid ) $map[] = ['custom.uid','=',$uid];
  391. // 编码ID
  392. if( $orders_other ) $map[] = $orders_other == 1 ? ['orders_product.product_id','>',0] : ['orders_product.product_id','=',0];
  393. if( $productId ) $map[] = ['orders_product.product_id','=',$productId];
  394. if( $productName ) $map[] = ['orders_product.product_name','=',$productName];
  395. if( $phone ) $map[] = ['orders_addr.contact_phone','=',$phone];
  396. if( $province ) $map[] = ['orders_addr.contact_province','LIKE','%'.$province.'%'];
  397. if( $city ) $map[] = ['orders_addr.contact_city','LIKE','%'.$city.'%'];
  398. if( $area ) $map[] = ['orders_addr.contact_area','LIKE','%'.$area.'%'];
  399. if( $startTime ) $map[] = ['orders_product.insert_time','>=',strtotime($startTime)];
  400. if( $endTime ) $map[] = ['orders_product.insert_time','<=',strtotime($endTime)];
  401. if( $status ) $map[] = ['orders_product.status','=',$status];
  402. // 当前登录的角色数据
  403. $session = session('userRule') ? session('userRule') : ['business_id'=>0,'menu_type'=>0,'data_type'=>0];
  404. // 是否存在对应的商业公司ID
  405. if ( $session['business_id'] ) $map[] = ['orders_product.business_id','=',$session['business_id']];
  406. // 数据类型
  407. $shopIds = ($session['menu_type'] == 1 && $session['data_type'] == 2) ? Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray() : [];
  408. // 查询数据
  409. $list = $OrdersProduct->query()
  410. ->join('custom','orders_product.custom_uid','=','custom.uid')
  411. ->join('orders_addr','orders_addr.order_id','=','orders_product.order_id');
  412. if ( $shopIds ) $list = $list->whereIn('orders_product.business_id',$shopIds);
  413. $list = $list->where($map)
  414. ->orderByDesc('id')
  415. ->select([
  416. 'orders_product.id as id',
  417. 'orders_product.order_id',
  418. 'orders_product.custom_uid',
  419. 'custom.username as custom_name',
  420. 'orders_product.status',
  421. 'orders_addr.contact_name',
  422. 'orders_addr.contact_shop',
  423. 'orders_addr.contact_phone',
  424. 'orders_addr.contact_province',
  425. 'orders_addr.contact_city',
  426. 'orders_addr.contact_area',
  427. 'orders_addr.contact_addr',
  428. 'orders_addr.shop_type',
  429. 'orders_product.product_id',
  430. 'orders_product.product_name',
  431. 'orders_product.sku_attr_names as product_spec',
  432. 'orders_product.product_thumb',
  433. 'orders_product.buy_num',
  434. 'orders_product.price_total',
  435. 'orders_product.coupon_total',
  436. 'orders_product.pay_total',
  437. 'orders_product.insert_time',
  438. 'custom.weiban_extid as weiban_extid',
  439. ])
  440. ->get()->toArray();
  441. $data = [];
  442. // 循环处理数据
  443. foreach ($list as $key => $value) {
  444. // id转编号
  445. $value['order_id'] = $Model->idToCode($value['order_id']);
  446. $value['status'] = $Model->getState($value['status'],'state');
  447. $value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
  448. $value['product_price'] = $value['buy_num'] ? ($value['price_total'] / $value['buy_num']) : $value['buy_num'];
  449. $value['pay_price'] = $value['buy_num'] ? ($value['pay_total'] / $value['buy_num']) : $value['buy_num'];
  450. // 重组
  451. $data[$key] = [
  452. 'order_id' => $value['order_id'],
  453. 'custom_uid' => $value['custom_uid'],
  454. 'custom_name' => $value['custom_name'],
  455. 'status' => $value['status'],
  456. 'contact_name' => $value['contact_name'],
  457. 'contact_phone' => $value['contact_phone'],
  458. 'contact_province' => $value['contact_province'],
  459. 'contact_city' => $value['contact_city'],
  460. 'contact_area' => $value['contact_area'],
  461. 'contact_addr' => $value['contact_addr'],
  462. 'shop_type' => $value['shop_type'] ? $Shoptype->getOne($value['shop_type'],'name') : '',
  463. 'product_id' => $value['product_id'] ? $Product->idToCode($value['product_id']) : '— —',
  464. 'product_name' => $value['product_name'],
  465. 'product_spec' => $value['product_spec'],
  466. 'product_price' => $value['product_price'],
  467. 'pay_price' => $value['pay_price'],
  468. 'buy_num' => $value['buy_num'],
  469. 'coupon_total' => $value['coupon_total'],
  470. 'pay_total' => $value['pay_total'],
  471. 'weiban_extid' => $value['weiban_extid'],
  472. 'insert_time' => date('Y-m-d H:i:s',$value['insert_time']),
  473. ];
  474. }
  475. try {
  476. // 去下载
  477. $this->toDown($data);
  478. } catch (\Throwable $th) {
  479. echo $th->getMessage();
  480. }
  481. }
  482. /**
  483. * 去下载
  484. */
  485. private function toDown($data){
  486. try {
  487. $config = [
  488. 'path' =>public_path().'/uploads/' // xlsx文件保存路径
  489. ];
  490. $excel = new \Vtiful\Kernel\Excel($config);
  491. $header = [
  492. '订单ID',
  493. '客户ID',
  494. '客户昵称',
  495. '订单状态',
  496. '收货人',
  497. '收货人手机号',
  498. '省',
  499. '市',
  500. '区县',
  501. '收货地址',
  502. '终端类型',
  503. '产品编码',
  504. '产品名称',
  505. '产品规格',
  506. '产品单价',
  507. '折后单价',
  508. '产品数量',
  509. '优惠金额',
  510. '产品金额',
  511. '微伴ID',
  512. '下单时间',
  513. ];
  514. $filePath = $excel->fileName('tutorial01.xlsx', 'sheet1')
  515. ->header($header)
  516. ->data($data)
  517. ->output();
  518. $filename = '订单下载.xlsx';
  519. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  520. header('Content-Disposition: attachment;filename="' . $filename . '"');
  521. header('Content-Length: ' . filesize($filePath));
  522. header('Content-Transfer-Encoding: binary');
  523. header('Cache-Control: must-revalidate');
  524. header('Cache-Control: max-age=0');
  525. header('Pragma: public');
  526. ob_clean();
  527. flush();
  528. if (copy($filePath, 'php://output') === false) {
  529. return json_send(['code'=>'error','msg'=>'下载失败']);
  530. }
  531. @unlink($filePath);
  532. return json_send(['code'=>'success','msg'=>'下载成功','path'=>'']);
  533. }catch (\Exception $exception) {
  534. return json_send(['code'=>'error','msg'=>'下载失败']);
  535. }
  536. }
  537. /**
  538. * 设置表格样式
  539. *
  540. */
  541. private function setStyle(Spreadsheet $spreadsheet){
  542. // 选择当前活动的工作表
  543. $sheet = $spreadsheet->getActiveSheet();
  544. // 宽
  545. $sheet->getColumnDimension('A')->setWidth(15);
  546. $sheet->getColumnDimension('B')->setWidth(15);
  547. $sheet->getColumnDimension('C')->setWidth(15);
  548. $sheet->getColumnDimension('D')->setWidth(15);
  549. $sheet->getColumnDimension('E')->setWidth(15);
  550. $sheet->getColumnDimension('F')->setWidth(15);
  551. $sheet->getColumnDimension('G')->setWidth(15);
  552. $sheet->getColumnDimension('H')->setWidth(15);
  553. $sheet->getColumnDimension('I')->setWidth(15);
  554. $sheet->getColumnDimension('J')->setWidth(50);
  555. $sheet->getColumnDimension('K')->setWidth(15);
  556. $sheet->getColumnDimension('L')->setWidth(20);
  557. $sheet->getColumnDimension('M')->setWidth(80);
  558. $sheet->getColumnDimension('N')->setWidth(80);
  559. $sheet->getColumnDimension('O')->setWidth(10);
  560. $sheet->getColumnDimension('P')->setWidth(10);
  561. $sheet->getColumnDimension('Q')->setWidth(10);
  562. $sheet->getColumnDimension('R')->setWidth(10);
  563. $sheet->getColumnDimension('S')->setWidth(10);
  564. $sheet->getColumnDimension('T')->setWidth(50);
  565. $sheet->getColumnDimension('U')->setWidth(20);
  566. // 默认高度
  567. $sheet->getDefaultRowDimension()->setRowHeight(18);
  568. // 加粗第一行
  569. $sheet->getStyle('A:U')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  570. $sheet->getStyle('A1:U1')->getFont()->setBold(true);
  571. $sheet->getStyle('A1:U1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
  572. // 设置表格标题
  573. $sheet
  574. ->setCellValue('A1', '订单ID')
  575. ->setCellValue('B1', '客户ID')
  576. ->setCellValue('C1', '客户昵称')
  577. ->setCellValue('D1', '订单状态')
  578. ->setCellValue('E1', '收货人')
  579. ->setCellValue('F1', '收货人手机号')
  580. ->setCellValue('G1', '省')
  581. ->setCellValue('H1', '市')
  582. ->setCellValue('I1', '区县')
  583. ->setCellValue('J1', '收货地址')
  584. ->setCellValue('K1', '终端类型')
  585. ->setCellValue('L1', '产品编码')
  586. ->setCellValue('M1', '产品名称')
  587. ->setCellValue('N1', '产品规格')
  588. ->setCellValue('O1', '产品单价')
  589. ->setCellValue('P1', '折后单价')
  590. ->setCellValue('Q1', '产品数量')
  591. ->setCellValue('R1', '优惠金额')
  592. ->setCellValue('S1', '产品金额')
  593. ->setCellValue('T1', '微伴ID')
  594. ->setCellValue('U1', '下单时间');
  595. // 返回结果
  596. return $sheet;
  597. }
  598. /**
  599. * 分享图片
  600. *
  601. */
  602. private function getShareImage($shopName,$score=0){
  603. // 尝试执行
  604. try {
  605. // 加载图片
  606. $image = Image::make(public_path('uploads/images/default/').'order_receipt_share.jpg');
  607. // 加载图片
  608. $qrcode = Image::make(public_path('uploads/images/default/').'mp_qrcode.jpg')->resize(100,100);
  609. // 设置文字样式(字体、大小、颜色等)
  610. $fontPath = public_path().'/fonts/msyh14.ttf';// 指定字体文件路径
  611. // 文本
  612. $score = $score ? '即将获得'.$score.'积分' : '';
  613. // 给图片写入文字
  614. $image->text('恭喜 '.$shopName, 240,60,function (Font $font) use ($fontPath) {
  615. $font->file($fontPath); // 字体文件地址
  616. $font->size(24); // 字体大小
  617. $font->color('#FFFFFF');
  618. $font->align('center');
  619. });
  620. // 给图片写入文字
  621. $image->text('完成了一笔订单', 240,100,function (Font $font) use ($fontPath) {
  622. $font->file($fontPath); // 字体文件地址
  623. $font->size(24); // 字体大小
  624. $font->color('#FFFFFF');
  625. $font->align('center');
  626. });
  627. // 是否有积分
  628. if( $score ){
  629. // 给图片写入文字
  630. $image->text($score, 240,140,function (Font $font) use ($fontPath) {
  631. $font->file($fontPath); // 字体文件地址
  632. $font->size(24); // 字体大小
  633. $font->color('#FFFFFF');
  634. $font->align('center');
  635. });
  636. }
  637. // 给图片写入文字
  638. $image->text('你也来试试吧', 240,$score?180:140,function (Font $font) use ($fontPath) {
  639. $font->file($fontPath); // 字体文件地址
  640. $font->size(24); // 字体大小
  641. $font->color('#FFFFFF');
  642. $font->align('center');
  643. });
  644. // 插入图片
  645. $image->insert($qrcode,'bottom-right',10,10);
  646. // 转码成字符串
  647. $image = $image->encode('jpg', 90)->__toString();
  648. // 转base64
  649. $base64 = 'data:image/jpg;base64,' . base64_encode($image);
  650. return $base64;
  651. } catch (\Throwable $th) {
  652. // 返回路径
  653. return '';
  654. }
  655. }
  656. }