Orders.php 25 KB

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