Goods.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\Goods as GoodsModel;
  11. use app\admin\validate\GoodsValidate;
  12. use think\exception\ValidateException;
  13. use HTMLPurifier;
  14. use HTMLPurifier_Config;
  15. use think\facade\Db;
  16. use think\facade\View;
  17. class Goods extends BaseController
  18. {
  19. /**
  20. * 构造函数
  21. */
  22. public function __construct()
  23. {
  24. $this->model = new GoodsModel();
  25. $this->uid = get_login_admin('id');
  26. }
  27. /**
  28. * 数据列表
  29. */
  30. public function datalist()
  31. {
  32. if (request()->isAjax()) {
  33. $param = get_params();
  34. $where = [];
  35. if (!empty($param['keywords'])) {
  36. $where[] = ['a.id|a.title|a.desc|a.content|c.title', 'like', '%' . $param['keywords'] . '%'];
  37. }
  38. if (!empty($param['cate_id'])) {
  39. $where[] = ['a.cate_id', '=', $param['cate_id']];
  40. }
  41. $where[] = ['a.delete_time', '=', 0];
  42. $param['order'] = 'a.sort asc';
  43. $list = $this->model->getGoodsList($where, $param);
  44. return table_assign(0, '', $list);
  45. }
  46. else{
  47. return view();
  48. }
  49. }
  50. /**
  51. * 添加
  52. */
  53. public function add()
  54. {
  55. if (request()->isAjax()) {
  56. $param = get_params();
  57. // 检验完整性
  58. try {
  59. validate(GoodsValidate::class)->check($param);
  60. } catch (ValidateException $e) {
  61. // 验证失败 输出错误信息
  62. return to_assign(1, $e->getError());
  63. }
  64. if (isset($param['tag_values']) && $param['tag_values']) {
  65. $param['tag_values'] = implode(',',$param['tag_values']);
  66. }
  67. if(empty($param['desc'])){
  68. $param['desc'] = getDescriptionFromContent($param['content'], 100);
  69. }
  70. // 创建HTMLPurifier配置对象
  71. $config = HTMLPurifier_Config::createDefault();
  72. $config->set('HTML.DefinitionID', 'html5-definitions');
  73. $config->set('HTML.DefinitionRev', 1);
  74. $config->set('HTML.ForbiddenAttributes', ['width', 'height']);
  75. //$config->set('HTML.Allowed', 'p,b,a[href],pre[class],code,blockquote,img[src],table,tr,th,td,ul,li,ol,dl,dt,dd');
  76. $config->set('HTML.ForbiddenElements',array('script'),true);//设置拒绝使用的tagname
  77. if ($def = $config->maybeGetRawHTMLDefinition()) {
  78. $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
  79. 'src' => 'URI',
  80. 'type' => 'Text',
  81. 'poster' => 'URI',
  82. 'preload' => 'Enum#auto,metadata,none',
  83. 'controls' => 'Bool',
  84. ]);
  85. $def->addElement('source', 'Block', 'Flow', 'Common', [
  86. 'src' => 'URI',
  87. 'type' => 'Text',
  88. ]);
  89. }
  90. // 创建HTMLPurifier对象
  91. $purifier = new HTMLPurifier($config);
  92. //防止xss,过滤输入并输出结果
  93. //$param['content'] = '测试<script>alert(0);</script>';
  94. $param['content'] = $purifier->purify($param['content']);
  95. $param['admin_id'] = $this->uid;
  96. $this->model->addGoods($param);
  97. }else{
  98. return view();
  99. }
  100. }
  101. /**
  102. * 编辑
  103. */
  104. public function edit()
  105. {
  106. $param = get_params();
  107. if (request()->isAjax()) {
  108. // 检验完整性
  109. try {
  110. validate(GoodsValidate::class)->check($param);
  111. } catch (ValidateException $e) {
  112. // 验证失败 输出错误信息
  113. return to_assign(1, $e->getError());
  114. }
  115. if (isset($param['tag_values']) && $param['tag_values']) {
  116. $param['tag_values'] = implode(',',$param['tag_values']);
  117. }
  118. // 创建HTMLPurifier配置对象
  119. $config = HTMLPurifier_Config::createDefault();
  120. $config->set('HTML.DefinitionID', 'html5-definitions');
  121. $config->set('HTML.DefinitionRev', 1);
  122. $config->set('HTML.ForbiddenAttributes', ['width', 'height']);
  123. //$config->set('HTML.Allowed', 'p,b,a[href],pre[class],code,blockquote,img[src],table,tr,th,td,ul,li,ol,dl,dt,dd');
  124. $config->set('HTML.ForbiddenElements',array('script'),true);//设置拒绝使用的tagname
  125. if ($def = $config->maybeGetRawHTMLDefinition()) {
  126. $def->addElement('video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [
  127. 'src' => 'URI',
  128. 'type' => 'Text',
  129. 'poster' => 'URI',
  130. 'preload' => 'Enum#auto,metadata,none',
  131. 'controls' => 'Bool',
  132. ]);
  133. $def->addElement('source', 'Block', 'Flow', 'Common', [
  134. 'src' => 'URI',
  135. 'type' => 'Text',
  136. ]);
  137. }
  138. // 创建HTMLPurifier对象
  139. $purifier = new HTMLPurifier($config);
  140. //防止xss,过滤输入并输出结果
  141. //$param['content'] = '测试<script>alert(0);</script>';
  142. $param['content'] = $purifier->purify($param['content']);
  143. $this->model->editGoods($param);
  144. }else{
  145. $id = isset($param['id']) ? $param['id'] : 0;
  146. $detail = $this->model->getGoodsById($id);
  147. if (!empty($detail)) {
  148. //轮播图
  149. if(!empty($detail['banner'])) {
  150. $detail['banner_array'] = explode(',',$detail['banner']);
  151. }
  152. //关键字
  153. $keywrod_array = Db::name('GoodsKeywords')
  154. ->field('i.aid,i.keywords_id,k.title')
  155. ->alias('i')
  156. ->join('keywords k', 'k.id = i.keywords_id', 'LEFT')
  157. ->order('i.create_time asc')
  158. ->where(array('i.aid' => $id, 'k.status' => 1))
  159. ->select()->toArray();
  160. $detail['keyword_ids'] = implode(",", array_column($keywrod_array, 'keywords_id'));
  161. $detail['keyword_names'] = implode(',', array_column($keywrod_array, 'title'));
  162. //标签设置
  163. $detail['tag1'] = $detail['tag2'] = $detail['tag3'] = $detail['tag4'] = $detail['tag5'] = $detail['tag6'] =0;
  164. if(!empty($detail['tag_values'])) {
  165. $tag_values_array = explode(',', $detail['tag_values']);
  166. if(in_array('1', $tag_values_array)){
  167. $detail['tag1'] = 1;
  168. }
  169. if(in_array('2', $tag_values_array)){
  170. $detail['tag2'] = 1;
  171. }
  172. if(in_array('3', $tag_values_array)){
  173. $detail['tag3'] = 1;
  174. }
  175. if(in_array('4', $tag_values_array)){
  176. $detail['tag4'] = 1;
  177. }
  178. if(in_array('5', $tag_values_array)){
  179. $detail['tag5'] = 1;
  180. }
  181. if(in_array('6', $tag_values_array)){
  182. $detail['tag6'] = 1;
  183. }
  184. }
  185. View::assign('detail', $detail);
  186. return view();
  187. }
  188. else{
  189. throw new \think\exception\HttpException(404, '找不到页面');
  190. }
  191. }
  192. }
  193. /**
  194. * 查看信息
  195. */
  196. public function read()
  197. {
  198. $param = get_params();
  199. $id = isset($param['id']) ? $param['id'] : 0;
  200. $detail = $this->model->getGoodsById($id);
  201. if (!empty($detail)) {
  202. //分类名
  203. $detail['cate_name'] = Db::name('GoodsCate')->where('id',$detail['cate_id'])->value('title');
  204. //轮播图
  205. if(!empty($detail['banner'])) {
  206. $detail['banner_array'] = explode(',',$detail['banner']);
  207. }
  208. //关键字
  209. $keywrod_array = Db::name('GoodsKeywords')
  210. ->field('i.aid,i.keywords_id,k.title')
  211. ->alias('i')
  212. ->join('keywords k', 'k.id = i.keywords_id', 'LEFT')
  213. ->order('i.create_time asc')
  214. ->where(array('i.aid' => $id, 'k.status' => 1))
  215. ->select()->toArray();
  216. $detail['keyword_ids'] = implode(",", array_column($keywrod_array, 'keywords_id'));
  217. $detail['keyword_names'] = implode(',', array_column($keywrod_array, 'title'));
  218. //标签设置
  219. $detail['tag1'] = $detail['tag2'] = $detail['tag3'] = $detail['tag4'] = $detail['tag5'] = $detail['tag6'] =0;
  220. if(!empty($detail['tag_values'])) {
  221. $tag_values_array = explode(',', $detail['tag_values']);
  222. if(in_array('1', $tag_values_array)){
  223. $detail['tag1'] = 1;
  224. }
  225. if(in_array('2', $tag_values_array)){
  226. $detail['tag2'] = 1;
  227. }
  228. if(in_array('3', $tag_values_array)){
  229. $detail['tag3'] = 1;
  230. }
  231. if(in_array('4', $tag_values_array)){
  232. $detail['tag4'] = 1;
  233. }
  234. if(in_array('5', $tag_values_array)){
  235. $detail['tag5'] = 1;
  236. }
  237. if(in_array('6', $tag_values_array)){
  238. $detail['tag6'] = 1;
  239. }
  240. }
  241. View::assign('detail', $detail);
  242. return view();
  243. }
  244. else{
  245. throw new \think\exception\HttpException(404, '找不到页面');
  246. }
  247. }
  248. /**
  249. * 删除
  250. */
  251. public function del()
  252. {
  253. $param = get_params();
  254. $id = isset($param['id']) ? $param['id'] : 0;
  255. $type = isset($param['type']) ? $param['type'] : 0;
  256. $this->model->delGoodsById($id,$type);
  257. }
  258. }