Orders.php 28 KB

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