You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.7 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Admin\CommonController;
  4. use DB;
  5. class GoodsController extends CommonController
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function index()
  12. {
  13. $res = '';
  14. $where = function ($query) use ($res) {
  15. if(isset($_REQUEST["keyword"]))
  16. {
  17. $query->where('title', 'like', '%'.$_REQUEST['keyword'].'%');
  18. }
  19. if(isset($_REQUEST["typeid"]) && $_REQUEST["typeid"]!=0)
  20. {
  21. $query->where('typeid', $_REQUEST["typeid"]);
  22. }
  23. if(isset($_REQUEST["id"]))
  24. {
  25. $query->where('typeid', $_REQUEST["id"]);
  26. }
  27. };
  28. $posts = parent::pageList('goods', $where);
  29. foreach($posts as $key=>$value)
  30. {
  31. $info = DB::table('goods_type')->select('name')->where("id", $value->typeid)->first();
  32. $posts[$key]->name = $info->name;
  33. $posts[$key]->body = '';
  34. }
  35. $data['posts'] = $posts;
  36. return view('admin.goods.index', $data);
  37. }
  38. public function add()
  39. {
  40. $data = [];
  41. if(!empty($_GET["catid"])){$data['catid'] = $_GET["catid"];}else{$data['catid'] = 0;}
  42. return view('admin.goods.add', $data);
  43. }
  44. public function doadd()
  45. {
  46. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  47. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  48. $_POST['add_time'] = $_POST['pubdate'] = time(); //添加&更新时间
  49. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 发布者id
  50. //关键词
  51. if(!empty($_POST["keywords"]))
  52. {
  53. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  54. }
  55. else
  56. {
  57. if(!empty($_POST["title"]))
  58. {
  59. $title=$_POST["title"];
  60. $title=str_replace("","",$title);
  61. $title=str_replace(",","",$title);
  62. $_POST['keywords']=get_keywords($title);//标题分词
  63. }
  64. }
  65. unset($_POST["_token"]);
  66. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  67. if(isset($_POST['promote_start_date'])){$_POST['promote_start_date'] = strtotime($_POST['promote_start_date']);}
  68. if(isset($_POST['promote_end_date'])){$_POST['promote_end_date'] = strtotime($_POST['promote_end_date']);}
  69. if(empty($_POST['promote_price'])){unset($_POST['promote_price']);}
  70. if(DB::table('goods')->insert(array_filter($_POST)))
  71. {
  72. success_jump('添加成功!', route('admin_goods'));
  73. }
  74. else
  75. {
  76. error_jump('添加失败!请修改后重新添加');
  77. }
  78. }
  79. public function edit()
  80. {
  81. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  82. $data['id'] = $id;
  83. $goods = DB::table('goods')->where('id', $id)->first();
  84. if($goods->promote_start_date != 0){$goods->promote_start_date = date('Y-m-d H:i:s',$goods->promote_start_date);}
  85. if($goods->promote_end_date != 0){$goods->promote_end_date = date('Y-m-d H:i:s',$goods->promote_end_date);}
  86. $data['post'] = object_to_array($goods, 1);
  87. return view('admin.goods.edit', $data);
  88. }
  89. public function doedit()
  90. {
  91. if(!empty($_POST["id"])){$id = $_POST["id"];}else {$id="";exit;}
  92. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  93. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}}//description
  94. $_POST['pubdate'] = time();//更新时间
  95. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 修改者id
  96. //关键词
  97. if(!empty($_POST["keywords"]))
  98. {
  99. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  100. }
  101. else
  102. {
  103. if(!empty($_POST["title"]))
  104. {
  105. $title=$_POST["title"];
  106. $title=str_replace("","",$title);
  107. $title=str_replace(",","",$title);
  108. $_POST['keywords']=get_keywords($title);//标题分词
  109. }
  110. }
  111. unset($_POST["_token"]);
  112. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  113. if(isset($_POST['promote_start_date'])){$_POST['promote_start_date'] = strtotime($_POST['promote_start_date']);}
  114. if(isset($_POST['promote_end_date'])){$_POST['promote_end_date'] = strtotime($_POST['promote_end_date']);}
  115. if(empty($_POST['promote_price'])){unset($_POST['promote_price']);}
  116. if(DB::table('goods')->where('id', $id)->update($_POST))
  117. {
  118. success_jump('修改成功!', route('admin_goods'));
  119. }
  120. else
  121. {
  122. error_jump('修改失败!');
  123. }
  124. }
  125. public function del()
  126. {
  127. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');}
  128. if(DB::table('goods')->whereIn("id", explode(',', $id))->delete())
  129. {
  130. success_jump("$id ,删除成功");
  131. }
  132. else
  133. {
  134. error_jump("$id ,删除失败!请重新提交");
  135. }
  136. }
  137. //商品推荐
  138. public function recommendarc()
  139. {
  140. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');}
  141. $data['tuijian'] = 1;
  142. if(DB::table('goods')->whereIn("id", explode(',', $id))->update($data))
  143. {
  144. success_jump("$id ,推荐成功");
  145. }
  146. else
  147. {
  148. error_jump("$id ,推荐失败!请重新提交");
  149. }
  150. }
  151. //商品是否存在
  152. public function goodsexists()
  153. {
  154. $res = '';
  155. $where = function ($query) use ($res) {
  156. if(isset($_REQUEST["title"]))
  157. {
  158. $query->where('title', $_REQUEST["title"]);
  159. }
  160. if(isset($_REQUEST["id"]))
  161. {
  162. $query->where('id', '<>', $_REQUEST["id"]);
  163. }
  164. };
  165. return DB::table("goods")->where($where)->count();
  166. }
  167. }