ManufacturerController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * 系统设置
  4. * @author system
  5. * @version 1.0
  6. * @date 2018-05-14 13:30:36
  7. *
  8. */
  9. namespace App\Http\Controllers\Admin\Album;
  10. use App\Http\Controllers\Admin\Controller;
  11. use App\Models\AgentBannerModel;
  12. use App\Models\AlbumManufacturerModel;
  13. use App\Models\AlbumUserModel;
  14. use App\Services\OSS;
  15. use Illuminate\Http\Request;
  16. use App\Repositories\Base\Criteria\OrderBy;
  17. use App\Repositories\Album\Criteria\MultiWhere;
  18. use App\Repositories\Album\ManufacturerRepository;
  19. class ManufacturerController extends Controller
  20. {
  21. private $repository;
  22. public function __construct(ManufacturerRepository $repository) {
  23. if(!$this->repository) $this->repository = $repository;
  24. }
  25. function list(Request $request){
  26. $search['keyword'] = $request->input('keyword');
  27. $isalbum = request('isalbum')?request('isalbum'):0;
  28. $query = $this->repository->pushCriteria(new MultiWhere($search));
  29. if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
  30. $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
  31. }
  32. $list = $query->paginate(10);
  33. return view('admin.album.manufacturer.index',compact('list','isalbum'));
  34. }
  35. function index(Request $request) {
  36. if ($request->method() == 'POST') {
  37. $iscreate = AlbumManufacturerModel::where('store_id', $this->getStoreId())->first();
  38. if ($iscreate) {
  39. return $this->_updateSave();
  40. } else {
  41. return $this->_createSave();
  42. }
  43. }
  44. $data = AlbumManufacturerModel::where('store_id', $this->getStoreId())->first();
  45. if ($data) {
  46. $data['banner'] = json_decode($data['banner'], true);
  47. }
  48. $banner = AgentBannerModel::where([['store_id',$this->getStoreId()],['type',0]])->get();
  49. $imgs = array();
  50. foreach ($banner as $key => $val) {
  51. $imgs[] = $val['url'];
  52. }
  53. $data['agent_banner'] = $imgs;
  54. $video = AgentBannerModel::where([['store_id',$this->getStoreId()],['type',1]])->first();
  55. return view('admin.album.manufacturer.edit', compact('data', 'video'));
  56. }
  57. function indexFurniture(Request $request) {
  58. if ($request->method() == 'POST') {
  59. $iscreate = AlbumManufacturerModel::where('store_id', $this->getStoreId())->first();
  60. if ($iscreate) {
  61. return $this->_updateSave();
  62. } else {
  63. return $this->_createSave();
  64. }
  65. }
  66. $data = AlbumManufacturerModel::where('store_id', $this->getStoreId())->first();
  67. if ($data) {
  68. $data['banner'] = json_decode($data['banner'], true);
  69. }
  70. return view('admin.album.manufacturer.edit-furniture', compact('data'));
  71. }
  72. function create(Request $request)
  73. {
  74. if ($request->method() == 'POST') {
  75. return $this->_createSave();
  76. }
  77. return view('admin.album.manufacturer.edit-all');
  78. }
  79. /**
  80. * 保存修改
  81. */
  82. private function _createSave(){
  83. $data = (array) request('data');
  84. $ma = AlbumManufacturerModel::where('id','!=',0)->orderByDesc('id')->first();
  85. $data['store_id'] =$ma->id+1;
  86. if (!empty($data['banner']['url'])) {
  87. foreach ($data['banner']['url'] as $key => $val) {
  88. $data['banner']['url'][$key] = $this->formatImgUrl($val);
  89. }
  90. $data['banner'] = json_encode($data['banner']['url']);
  91. }
  92. if (isset($data['banner_agent']['url'])) {
  93. $agent_banner = $data['banner_agent']['url'];
  94. unset($data['banner_agent']);
  95. if ($agent_banner) {
  96. foreach($agent_banner as $key => $val){
  97. $add['url'] = $this->formatImgUrl($val);
  98. $add['agent_id'] = request('id');
  99. $add['type'] = 0;
  100. AgentBannerModel::create($add);
  101. }
  102. }
  103. }
  104. if(!empty($data['avatar']))
  105. $data['avatar'] = $this->formatImgUrl($data['avatar']);
  106. if (!empty($data['logo'])) {
  107. $data['logo'] = $this->formatImgUrl($data['logo']);
  108. }
  109. if(!empty($data['background_pic']))
  110. $data['background_pic'] = $this->formatImgUrl($data['background_pic']);
  111. if(!empty($data['advertising_pic']))
  112. $data['advertising_pic'] = $this->formatImgUrl($data['advertising_pic']);
  113. if(!empty($data['notice_icon']))
  114. $data['notice_icon'] = $this->formatImgUrl($data['notice_icon']);
  115. if(!empty($data['furniture_ads_pic']))
  116. $data['furniture_ads_pic'] = $this->formatImgUrl($data['furniture_ads_pic']);
  117. $id = $this->repository->create($data);
  118. if ($id) {
  119. $this->showMessage('添加成功');
  120. } else {
  121. return $this->showWarning('添加失败');
  122. }
  123. }
  124. /**
  125. * 保存修改
  126. */
  127. private function _updateSave() {
  128. $data = (array) request('data');
  129. $saveData = AlbumManufacturerModel::where('store_id', $this->getStoreId())->first();
  130. if (!empty($data['avatar'])) {
  131. $data['avatar'] = $this->formatImgUrl($data['avatar']);
  132. //$this->deleteUrl($saveData->avatar);
  133. }
  134. $banner = AgentBannerModel::where([['store_id',$this->getStoreId()],['type',0]])->get();
  135. if($banner){
  136. foreach ($banner as $key=>$val){
  137. $url = str_replace(env('APP_URL'),public_path(),$val['url']);
  138. if(file_exists($url)) unlink($url);
  139. }
  140. }
  141. AgentBannerModel::where([['store_id',$this->getStoreId()],['type',0]])->delete();
  142. if(!empty($data['agent_banner']['url'])){
  143. foreach($data['agent_banner']['url'] as $key=>$val){
  144. $add['url'] = $this->formatImgUrl($val);
  145. $add['store_id'] = $this->getStoreId();
  146. $add['type'] = 0;
  147. AgentBannerModel::create($add);
  148. }
  149. }
  150. unset($data['agent_banner']);
  151. if (!empty($data['logo'])) {
  152. $data['logo'] = $this->formatImgUrl($data['logo']);
  153. }
  154. if (!empty($data['background_pic'])) {
  155. $data['background_pic'] = $this->formatImgUrl($data['background_pic']);
  156. //$this->deleteUrl($saveData->background_pic);
  157. }
  158. if (!empty($data['advertising_pic'])) {
  159. $data['advertising_pic'] = $this->formatImgUrl($data['advertising_pic']);
  160. // $this->deleteUrl($saveData->advertising_pic);
  161. }
  162. if (!empty($data['notice_icon'])) {
  163. $data['notice_icon'] = $this->formatImgUrl($data['notice_icon']);
  164. // $this->deleteUrl($saveData->notice_icon);
  165. }
  166. if (!empty($data['furniture_ads_pic'])) {
  167. $data['furniture_ads_pic'] = $this->formatImgUrl($data['furniture_ads_pic']);
  168. // $this->deleteUrl($saveData->furniture_ads_pic);
  169. }
  170. if (!empty($data['banner']['url'])) {
  171. foreach ($data['banner']['url'] as $key => $val) {
  172. $data['banner']['url'][$key] = $this->formatImgUrl($val);
  173. }
  174. $data['banner'] = json_encode($data['banner']['url']);
  175. /*$banner = json_decode($data['banner'], true);
  176. foreach ($banner as $val) {
  177. $this->deleteUrl($val);
  178. }*/
  179. }
  180. $id = $saveData->id;
  181. $ok = $this->repository->update($id,$data);
  182. if($ok) {
  183. return $this->showMessage('操作成功');
  184. }else{
  185. return $this->showWarning('操作失败');
  186. }
  187. }
  188. public function view(Request $request) {
  189. $data = $this->repository->find(request('id'));
  190. return view('admin.album.manufacturer.view',compact('data'));
  191. }
  192. /**
  193. *
  194. * 状态改变
  195. *
  196. */
  197. public function status(Request $request) {
  198. $ok = $this->repository->updateStatus(request('id'),request('status'));
  199. if($ok) {
  200. return $this->showMessage('操作成功');
  201. }else{
  202. return $this->showWarning('操作失败');
  203. }
  204. }
  205. /**
  206. * 删除
  207. */
  208. public function destroy(Request $request)
  209. {
  210. $bool = $this->repository->destroy($request->get('id'));
  211. if ($bool) {
  212. return $this->showMessage('操作成功');
  213. } else {
  214. return $this->showWarning("操作失败");
  215. }
  216. }
  217. public function uploadVideo(Request $request)
  218. {
  219. $video = $request->file('video');
  220. // 判断图片有效性
  221. if (!$video) {
  222. return back()->withErrors('上传视频无效..');
  223. }
  224. $check = AgentBannerModel::where([['store_id',$this->getStoreId()],['type',1]])->first();
  225. if($check) OSS::publicDeleteObject(config('alioss.BucketName'),$check->oss_key);
  226. // 获取图片在临时文件中的地址
  227. $videoPath = $video->getRealPath();
  228. // 制作文件名
  229. $ex = $video->getClientOriginalExtension();
  230. $key = time() . rand(10000, 99999999) . '.' . $ex;
  231. //阿里 OSS 文件上传
  232. $result = OSS::publicUpload(config('alioss.BucketName'), $key, $videoPath);
  233. if ($result) {
  234. $data['name'] = $video->getClientOriginalName();
  235. $data['type'] = 1;
  236. $data['store_id'] = $this->getStoreId();
  237. $data['oss_key'] = $key;
  238. $data['url'] = config('alioss.FileUrl') . $key;
  239. $video = AgentBannerModel::create($data);
  240. if (!$video) {
  241. return back()->withErrors('上传视频失败..');
  242. }
  243. return $this->showMessage('上传成功');
  244. } else {
  245. return back()->withErrors('上传视频失败..');
  246. }
  247. }
  248. public function videoDel(Request $request)
  249. {
  250. $check = AgentBannerModel::find($request->get('id'));
  251. if($check) $osskey = $check->oss_key;
  252. if(!$check) return $this->showWarning("操作失败");
  253. $bool = AgentBannerModel::where('id',$request->get('id'))->delete();
  254. if($bool) {
  255. OSS::publicDeleteObject(config('alioss.BucketName'),$osskey);
  256. return $this->showMessage('操作成功');
  257. }else{
  258. return $this->showWarning("操作失败");
  259. }
  260. }
  261. }