Orders.php 25 KB

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