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.

186 lines
4.9 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
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Admin\CommonController;
  4. use DB;
  5. class ProductController 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('product', $where);
  29. foreach($posts as $key=>$value)
  30. {
  31. $info = DB::table('product_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.product.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.product.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['addtime'] = $_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(DB::table('product')->insert(array_filter($_POST)))
  68. {
  69. success_jump('添加成功!', route('admin_product'));
  70. }
  71. else
  72. {
  73. error_jump('添加失败!请修改后重新添加');
  74. }
  75. }
  76. public function edit()
  77. {
  78. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  79. $data['id'] = $id;
  80. $data['post'] = object_to_array(DB::table('product')->where('id', $id)->first(), 1);
  81. return view('admin.product.edit', $data);
  82. }
  83. public function doedit()
  84. {
  85. if(!empty($_POST["id"])){$id = $_POST["id"];}else {$id="";exit;}
  86. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  87. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}}//description
  88. $_POST['pubdate'] = time();//更新时间
  89. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 修改者id
  90. //关键词
  91. if(!empty($_POST["keywords"]))
  92. {
  93. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  94. }
  95. else
  96. {
  97. if(!empty($_POST["title"]))
  98. {
  99. $title=$_POST["title"];
  100. $title=str_replace("","",$title);
  101. $title=str_replace(",","",$title);
  102. $_POST['keywords']=get_keywords($title);//标题分词
  103. }
  104. }
  105. unset($_POST["_token"]);
  106. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  107. if(DB::table('product')->where('id', $id)->update($_POST))
  108. {
  109. success_jump('修改成功!', route('admin_product'));
  110. }
  111. else
  112. {
  113. error_jump('修改失败!');
  114. }
  115. }
  116. public function del()
  117. {
  118. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');}
  119. if(DB::table('product')->whereIn("id", explode(',', $id))->delete())
  120. {
  121. success_jump("$id ,删除成功");
  122. }
  123. else
  124. {
  125. error_jump("$id ,删除失败!请重新提交");
  126. }
  127. }
  128. //商品推荐
  129. public function recommendarc()
  130. {
  131. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');}
  132. $data['tuijian'] = 1;
  133. if(DB::table('product')->whereIn("id", explode(',', $id))->update($data))
  134. {
  135. success_jump("$id ,推荐成功");
  136. }
  137. else
  138. {
  139. error_jump("$id ,推荐失败!请重新提交");
  140. }
  141. }
  142. //商品是否存在
  143. public function productexists()
  144. {
  145. $res = '';
  146. $where = function ($query) use ($res) {
  147. if(isset($_REQUEST["title"]))
  148. {
  149. $query->where('title', $_REQUEST["title"]);
  150. }
  151. if(isset($_REQUEST["id"]))
  152. {
  153. $query->where('id', '<>', $_REQUEST["id"]);
  154. }
  155. };
  156. return DB::table("product")->where($where)->count();
  157. }
  158. }