InfoController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /**
  3. * 订单列表
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-07-12 08:32:14
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Order;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\OrderInfoModel;
  12. use App\Models\RefundInfoModel;
  13. use App\Models\SettingInfoModel;
  14. use App\Services\Base\Attachment;
  15. use Carbon\Carbon;
  16. use EasyWeChat\Factory;
  17. use Illuminate\Http\Request;
  18. use App\Repositories\Base\Criteria\OrderBy;
  19. use App\Repositories\Order\Criteria\MultiWhere;
  20. use App\Repositories\Order\InfoRepository;
  21. use Illuminate\Pagination\LengthAwarePaginator;
  22. use Illuminate\Pagination\Paginator;
  23. use Illuminate\Support\Collection;
  24. use Cache;
  25. class InfoController extends Controller
  26. {
  27. private $repository;
  28. public function __construct(InfoRepository $repository)
  29. {
  30. if (!$this->repository) $this->repository = $repository;
  31. }
  32. function index(Request $request)
  33. {
  34. $search['keyword'] = $request->input('keyword');
  35. $search['status'] = $request->input('status');
  36. $order = array();
  37. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  38. $order[$request['sort_field']] = $request['sort_field_by'];
  39. } else {
  40. $order['id'] = 'DESC';
  41. //$order['status'] = 'ASC';
  42. }
  43. $list = $this->repository->with(['refundinfo','paidinfo'])->searchOrder($search, $order);
  44. $list = $list->get()->filter(function ($value) use ($search) {
  45. if ($search['status'] == 3 && count($value->refundinfo)) {
  46. return false;
  47. }
  48. return true;
  49. });
  50. $list = $this->paginate($list);
  51. return view('admin.order.info.index', compact('list'));
  52. }
  53. public function paginate($items, $perPage = 10, $page = null, $options = [])
  54. {
  55. $page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
  56. $items = $items instanceof Collection ? $items : Collection::make($items);
  57. return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
  58. }
  59. function check(Request $request)
  60. {
  61. $request = $request->all();
  62. $search['keyword'] = $request->input('keyword');
  63. $orderby = array();
  64. if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  65. $orderby[$request['sort_field']] = $request['sort_field_by'];
  66. }
  67. $list = $this->repository->search($search, $orderby);
  68. return view('admin.order.info.check', compact('list'));
  69. }
  70. /**
  71. * 添加
  72. *
  73. */
  74. public function create(Request $request)
  75. {
  76. if ($request->method() == 'POST') {
  77. return $this->_createSave();
  78. }
  79. return view('admin.order.info.edit');
  80. }
  81. /**
  82. * 保存修改
  83. */
  84. private function _createSave()
  85. {
  86. $data = (array)request('data');
  87. $id = $this->repository->create($data);
  88. if ($id) {
  89. $url[] = array('url' => U('Order/Info/index'), 'title' => '返回列表');
  90. $url[] = array('url' => U('Order/Info/create'), 'title' => '继续添加');
  91. $this->showMessage('添加成功', $url);
  92. } else {
  93. $url[] = array('url' => U('Order/Info/index'), 'title' => '返回列表');
  94. return $this->showWarning('添加失败', $url);
  95. }
  96. }
  97. /**
  98. *
  99. * 修改
  100. *
  101. *
  102. */
  103. public function update(Request $request)
  104. {
  105. $id = request('id');
  106. $order = OrderInfoModel::find($id);
  107. $order->status = 2;
  108. $ok = $order->save();
  109. if ($ok) {
  110. return $this->showMessage('操作成功', urldecode(request('_referer')));
  111. } else {
  112. return $this->showWarning('操作失败', urldecode(request('_referer')));
  113. }
  114. }
  115. /**
  116. * 保存修改
  117. */
  118. private function _updateSave()
  119. {
  120. $data = (array)request('data');
  121. $ok = $this->repository->update(request('id'), $data);
  122. if ($ok) {
  123. $url[] = array('url' => U('Order/Info/index'), 'title' => '返回列表');
  124. return $this->showMessage('操作成功', urldecode(request('_referer')));
  125. } else {
  126. $url[] = array('url' => U('Order/Info/index'), 'title' => '返回列表');
  127. return $this->showWarning('操作失败', $url);
  128. }
  129. }
  130. public function view(Request $request)
  131. {
  132. $data = $this->repository->find(request('id'));
  133. if($data->photo) $photo = explode(',',$data->photo);
  134. return view('admin.order.info.view', compact('data','photo'));
  135. }
  136. /**
  137. *
  138. * 状态改变
  139. *
  140. */
  141. public function status(Request $request)
  142. {
  143. $ok = $this->repository->updateStatus(request('id'), request('status'));
  144. if ($ok) {
  145. return $this->showMessage('操作成功');
  146. } else {
  147. return $this->showWarning('操作失败');
  148. }
  149. }
  150. /**
  151. * 删除
  152. */
  153. public function destroy(Request $request)
  154. {
  155. $bool = $this->repository->destroy($request->get('id'));
  156. if ($bool) {
  157. return $this->showMessage('操作成功');
  158. } else {
  159. return $this->showWarning("操作失败");
  160. }
  161. }
  162. public function upload(Request $request){
  163. if ($request->method() == 'POST') {
  164. return $this->_uploadSave();
  165. }
  166. $order = OrderInfoModel::find(request('id'));
  167. if($order->photo) $photo = explode(',',$order->photo);
  168. return view('admin.order.info.upload',compact('photo','order'));
  169. }
  170. public function _uploadSave(){
  171. $array_photo = (array)request('photo');
  172. if ($array_photo && isset($array_photo['url'])) {
  173. $data['photo'] = '';
  174. foreach ($array_photo['url'] as $item) {
  175. if (substr($item, 0, 7) == 'http://' || substr($item, 0, 8) == 'https://') {
  176. $data['photo'] .= $item . ',';
  177. } else {
  178. $data['photo'] .= env('APP_URL', '') . $item . ',';
  179. }
  180. }
  181. if ($data['photo']) $data['photo'] = rtrim($data['photo'], ',');
  182. }else{
  183. $data['photo'] = '';
  184. }
  185. $order = OrderInfoModel::find(request('id'));
  186. $ok = $this->repository->update(request('id'), $data);
  187. if ($ok) {
  188. //操作成功,删除原来的图片
  189. if ($order && $order->photo) {
  190. $photos = explode(',', $order->photo);
  191. foreach ($photos as $photo) {
  192. if (!in_array($photo, $array_photo['url'])) {
  193. $md5 = $this->getarea($photo);
  194. $attache = new Attachment();
  195. $attache->deleteAttachment($md5);
  196. }
  197. }
  198. }
  199. $url[] = array('url' => U('Order/Info/index?status=2'), 'title' => '返回列表');
  200. return $this->showMessage('操作成功',$url);
  201. } else {
  202. return $this->showWarning('操作失败');
  203. }
  204. }
  205. /***
  206. * 退款
  207. */
  208. public function refund(Request $request)
  209. {
  210. $settinig = SettingInfoModel::first();
  211. $config = [
  212. // 必要配置
  213. 'app_id' => $settinig->app_id,
  214. 'mch_id' => $settinig->mch_id,
  215. 'key' => $settinig->api_key, // API 密钥
  216. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  217. 'cert_path' => $settinig->cert_path, // XXX: 绝对路径!!!!
  218. 'key_path' => $settinig->key_path, // XXX: 绝对路径!!!!
  219. ];
  220. $app = Factory::payment($config);
  221. $orderid = request('id');
  222. $order = OrderInfoModel::with('paidinfo')->find($orderid);
  223. $out_trade_no = $order->out_trade_no;
  224. $total_price = $order->paidinfo->paid_price * 100;
  225. $refund_time = strtotime($order->updated_at);
  226. $schedule_time = strtotime($order->schedule_time);
  227. if ($schedule_time < $refund_time) {
  228. $refund_price = $total_price * 0.75;
  229. } else {
  230. $time = $schedule_time - $refund_time / 3600;
  231. $hour = 12;
  232. if ($time > $hour) {
  233. $refund_price = $total_price;
  234. } else {
  235. $refund_price = $total_price * 0.85;
  236. }
  237. }
  238. \Log::info($total_price);
  239. \Log::info($refund_price);
  240. $result = $app->refund->byOutTradeNumber($out_trade_no, $out_trade_no, $total_price, $refund_price, [
  241. 'refund_desc' => $out_trade_no . '退款',
  242. ]);
  243. \Log::info($result);
  244. if ($result) {
  245. $data = [
  246. 'refund_no' => $out_trade_no,
  247. 'order_id' => $orderid,
  248. 'refund_price' => $refund_price
  249. ];
  250. $ok = RefundInfoModel::create($data);
  251. if ($ok['return_code'] == 'SUCCESS') {
  252. return $this->showMessage('退款成功');
  253. } else {
  254. return $this->showWarning('操作失败,请稍后再试');
  255. }
  256. }
  257. }
  258. public function checkNewOrder(){
  259. $order = OrderInfoModel::where('status',1)->orderBy('id','desc')->first();
  260. $new_id = $order?$order->id:0;
  261. $old_id= Cache::store('database')->get('order_id');
  262. $count = OrderInfoModel::where('status',1)->count();
  263. \Log::info($new_id);
  264. \Log::info($old_id);
  265. if($new_id == $old_id){
  266. Cache::store('database')->forget('order_id');
  267. Cache::store('database')->put('order_id',$new_id,10);
  268. $data = [
  269. 'code' => 1,
  270. 'msg' => '没有新的订单',
  271. 'count' => $count
  272. ];
  273. return json_encode($data);
  274. }else{
  275. Cache::store('database')->forget('order_id');
  276. Cache::store('database')->put('order_id',$new_id,10);
  277. $data = [
  278. 'code' => 0,
  279. 'msg' => '你有新的订单待处理!',
  280. 'count' => $count
  281. ];
  282. return json_encode($data);
  283. }
  284. }
  285. /*
  286. * 获取md5码
  287. */
  288. public function getarea($str)
  289. {
  290. $start = strripos($str, '/');
  291. $first = substr($str, $start + 1);
  292. $end = strripos($first, '.');
  293. $last = substr($first, 0, $end);
  294. return $last;
  295. }
  296. }