Pages.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. namespace app\admin\model;
  8. use think\model;
  9. class Pages extends Model
  10. {
  11. //插入关键字
  12. public function insertKeyword($keywordArray = [], $aid = 0)
  13. {
  14. $insert = [];
  15. $time = time();
  16. foreach ($keywordArray as $key => $value) {
  17. if (!$value) {
  18. continue;
  19. }
  20. $keywords_id = (new Keywords())->increase($value);
  21. $insert[] = ['aid' => $aid,
  22. 'keywords_id' => $keywords_id,
  23. 'create_time' => $time,
  24. ];
  25. }
  26. $res = \think\facade\Db::name('PagesKeywords')->strict(false)->field(true)->insertAll($insert);
  27. return $res;
  28. }
  29. /**
  30. * 获取分页列表
  31. * @param $where
  32. * @param $param
  33. */
  34. public function getPagesList($where, $param)
  35. {
  36. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  37. $order = empty($param['order']) ? 'id desc' : $param['order'];
  38. $list = $this->where($where)
  39. ->field('a.*,u.nickname as admin_name')
  40. ->alias('a')
  41. ->leftJoin('Admin u', 'a.admin_id = u.id')
  42. ->order($order)
  43. ->paginate($rows, false, ['query' => $param]);
  44. return $list;
  45. }
  46. /**
  47. * 添加数据
  48. * @param $param
  49. */
  50. public function addPages($param)
  51. {
  52. $insertId = 0;
  53. try {
  54. $param['create_time'] = time();
  55. $insertId = $this->strict(false)->field(true)->insertGetId($param);
  56. //关联关键字
  57. if (isset($param['keyword_names']) && $param['keyword_names']) {
  58. $keywordArray = explode(',', $param['keyword_names']);
  59. $res_keyword = $this->insertKeyword($keywordArray,$insertId);
  60. }
  61. add_log('add', $insertId, $param);
  62. } catch(\Exception $e) {
  63. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  64. }
  65. return to_assign(0,'操作成功',['aid'=>$insertId]);
  66. }
  67. /**
  68. * 编辑信息
  69. * @param $param
  70. */
  71. public function editPages($param)
  72. {
  73. try {
  74. $param['update_time'] = time();
  75. $this->where('id', $param['id'])->strict(false)->field(true)->update($param);
  76. //关联关键字
  77. if (isset($param['keyword_names']) && $param['keyword_names']) {
  78. \think\facade\Db::name('PagesKeywords')->where(['aid'=>$param['id']])->delete();
  79. $keywordArray = explode(',', $param['keyword_names']);
  80. $res_keyword = $this->insertKeyword($keywordArray,$param['id']);
  81. }
  82. add_log('edit', $param['id'], $param);
  83. } catch(\Exception $e) {
  84. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  85. }
  86. return to_assign();
  87. }
  88. /**
  89. * 根据id获取信息
  90. * @param $id
  91. */
  92. public function getPagesById($id)
  93. {
  94. $info = $this->where('id', $id)->find();
  95. return $info;
  96. }
  97. /**
  98. * 删除信息
  99. * @param $id
  100. * @return array
  101. */
  102. public function delPagesById($id,$type=0)
  103. {
  104. if($type==0){
  105. //逻辑删除
  106. try {
  107. $param['delete_time'] = time();
  108. $this->where('id', $id)->update(['delete_time'=>time()]);
  109. add_log('delete', $id);
  110. } catch(\Exception $e) {
  111. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  112. }
  113. }
  114. else{
  115. //物理删除
  116. try {
  117. $this->where('id', $id)->delete();
  118. add_log('delete', $id);
  119. } catch(\Exception $e) {
  120. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  121. }
  122. }
  123. return to_assign();
  124. }
  125. }