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.

233 lines
7.2 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
6 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
6 years ago
7 years ago
6 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. $data['goodsbrand_list'] = object_to_array(DB::table('goods_brand')->where('status', 0)->orderBy('listorder', 'asc')->get()); //商品品牌
  43. return view('admin.goods.add', $data);
  44. }
  45. public function doadd()
  46. {
  47. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  48. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  49. $_POST['add_time'] = $_POST['pubdate'] = time(); //添加&更新时间
  50. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 发布者id
  51. //关键词
  52. if(!empty($_POST["keywords"]))
  53. {
  54. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  55. }
  56. else
  57. {
  58. if(!empty($_POST["title"]))
  59. {
  60. $title=$_POST["title"];
  61. $title=str_replace("","",$title);
  62. $title=str_replace(",","",$title);
  63. $_POST['keywords']=get_keywords($title);//标题分词
  64. }
  65. }
  66. unset($_POST["_token"]);
  67. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  68. if(isset($_POST['promote_start_date'])){$_POST['promote_start_date'] = strtotime($_POST['promote_start_date']);}
  69. if(isset($_POST['promote_end_date'])){$_POST['promote_end_date'] = strtotime($_POST['promote_end_date']);}
  70. if(empty($_POST['promote_price'])){unset($_POST['promote_price']);}
  71. if(!empty($_POST['goods_img']))
  72. {
  73. $goods_img = $_POST['goods_img'];
  74. $_POST['goods_img'] = $_POST['goods_img'][0];
  75. }
  76. if($goods_id = DB::table('goods')->insertGetId(array_filter($_POST)))
  77. {
  78. if(isset($goods_img))
  79. {
  80. $tmp = [];
  81. foreach($goods_img as $k=>$v)
  82. {
  83. $tmp[] = ['url'=>$v,'goods_id'=>$goods_id,'add_time'=>time()];
  84. }
  85. DB::table('goods_img')->insert($tmp);
  86. }
  87. success_jump('添加成功', route('admin_goods'));
  88. }
  89. else
  90. {
  91. error_jump('添加失败!请修改后重新添加');
  92. }
  93. }
  94. public function edit()
  95. {
  96. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  97. $data['id'] = $id;
  98. $goods = DB::table('goods')->where('id', $id)->first();
  99. if($goods->promote_start_date != 0){$goods->promote_start_date = date('Y-m-d H:i:s',$goods->promote_start_date);}
  100. if($goods->promote_end_date != 0){$goods->promote_end_date = date('Y-m-d H:i:s',$goods->promote_end_date);}
  101. $data['post'] = object_to_array($goods, 1);
  102. $data['goodsbrand_list'] = object_to_array(DB::table('goods_brand')->where('status', 0)->orderBy('listorder', 'asc')->get()); //商品品牌
  103. $data['goods_img_list'] = object_to_array(DB::table('goods_img')->where(array('goods_id'=>$id))->orderBy('listorder', 'asc')->get()); //商品图片
  104. return view('admin.goods.edit', $data);
  105. }
  106. public function doedit()
  107. {
  108. if(!empty($_POST["id"])){$id = $_POST["id"];}else {$id="";exit;}
  109. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  110. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}}//description
  111. $_POST['pubdate'] = time();//更新时间
  112. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 修改者id
  113. //关键词
  114. if(!empty($_POST["keywords"]))
  115. {
  116. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  117. }
  118. else
  119. {
  120. if(!empty($_POST["title"]))
  121. {
  122. $title=$_POST["title"];
  123. $title=str_replace("","",$title);
  124. $title=str_replace(",","",$title);
  125. $_POST['keywords']=get_keywords($title);//标题分词
  126. }
  127. }
  128. unset($_POST["_token"]);
  129. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  130. if(isset($_POST['promote_start_date'])){$_POST['promote_start_date'] = strtotime($_POST['promote_start_date']);}
  131. if(isset($_POST['promote_end_date'])){$_POST['promote_end_date'] = strtotime($_POST['promote_end_date']);}
  132. if(empty($_POST['promote_price'])){unset($_POST['promote_price']);}
  133. if(!empty($_POST['goods_img']))
  134. {
  135. $goods_img = $_POST['goods_img'];
  136. $_POST['goods_img'] = $_POST['goods_img'][0];
  137. }
  138. if(DB::table('goods')->where('id', $id)->update($_POST))
  139. {
  140. if(isset($goods_img))
  141. {
  142. $tmp = [];
  143. foreach($goods_img as $k=>$v)
  144. {
  145. $tmp[] = ['url'=>$v,'goods_id'=>$id,'add_time'=>time()];
  146. }
  147. DB::table('goods_img')->where(array('goods_id'=>$id))->delete();
  148. DB::table('goods_img')->insert($tmp);
  149. }
  150. success_jump('修改成功', route('admin_goods'));
  151. }
  152. else
  153. {
  154. error_jump('修改失败');
  155. }
  156. }
  157. public function del()
  158. {
  159. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');}
  160. if(DB::table('goods')->whereIn("id", explode(',', $id))->update(['status' => 1]))
  161. {
  162. success_jump("$id ,删除成功");
  163. }
  164. else
  165. {
  166. error_jump("$id ,删除失败!请重新提交");
  167. }
  168. }
  169. //商品推荐
  170. public function recommendarc()
  171. {
  172. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump('删除失败!请重新提交');}
  173. $data['tuijian'] = 1;
  174. if(DB::table('goods')->whereIn("id", explode(',', $id))->update($data))
  175. {
  176. success_jump("$id ,推荐成功");
  177. }
  178. else
  179. {
  180. error_jump("$id ,推荐失败!请重新提交");
  181. }
  182. }
  183. //商品是否存在
  184. public function goodsexists()
  185. {
  186. $res = '';
  187. $where = function ($query) use ($res) {
  188. if(isset($_REQUEST["title"]))
  189. {
  190. $query->where('title', $_REQUEST["title"]);
  191. }
  192. if(isset($_REQUEST["id"]))
  193. {
  194. $query->where('id', '<>', $_REQUEST["id"]);
  195. }
  196. };
  197. return DB::table("goods")->where($where)->count();
  198. }
  199. }