Orders.php 28 KB

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