Orders.php 32 KB

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