Orders.php 30 KB

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