Orders.php 29 KB

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