Orders.php 26 KB

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