Orders.php 29 KB

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