Orders.php 27 KB

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