Orders.php 26 KB

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