Title.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\admin\controller\manage;
  3. use app\common\controller\Backend;
  4. use app\common\service\BookService;
  5. /**
  6. * 运营管理-标题管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Title extends Backend
  11. {
  12. /**
  13. * ManageTitle模型对象
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = model('ManageTitle');
  20. $this->view->assign("statusList", $this->model->getStatusList());
  21. $this->view->assign("sexList", $this->model->getSexList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. //设置过滤方法
  34. $this->request->filter(['strip_tags']);
  35. if ($this->request->isAjax())
  36. {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('pkey_name'))
  39. {
  40. return $this->selectpage();
  41. }
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $whereArr = [];
  44. if (strtolower($this->request->action()) == 'select') {
  45. $whereArr[] = ['status', '=', 'normal'];
  46. $whereArr = function($query) use ($whereArr) {
  47. foreach ($whereArr as $k => $v)
  48. {
  49. if (is_array($v))
  50. {
  51. call_user_func_array([$query, 'where'], $v);
  52. }
  53. else
  54. {
  55. $query->where($v);
  56. }
  57. }
  58. };
  59. }
  60. $total = $this->model
  61. ->where($where)
  62. ->where($whereArr)
  63. ->order($sort, $order)
  64. ->count();
  65. $list = $this->model
  66. ->where($where)
  67. ->where($whereArr)
  68. ->order($sort, $order)
  69. ->limit($offset, $limit)
  70. ->select();
  71. $result = array("total" => $total, "rows" => $list);
  72. return json($result);
  73. }
  74. return $this->view->fetch();
  75. }
  76. /**
  77. * 选择标题
  78. */
  79. public function select()
  80. {
  81. if ($this->request->isAjax()) {
  82. return $this->index();
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * ajax方法转发index,用于渠道商/代理商API请求
  88. * @return string|\think\response\Json
  89. */
  90. public function ajax()
  91. {
  92. return $this->index();
  93. }
  94. public function batchaddtitle()
  95. {
  96. if ($this->request->isPost()) {
  97. $titles = explode("\r\n", trim($this->request->post('titles')));
  98. $titles = array_filter($titles, function ($value) {
  99. if (trim($value)) {
  100. return true;
  101. }
  102. return false;
  103. });
  104. $status = $this->request->post('status');
  105. $sex = $this->request->post('sex');
  106. $result = BookService::instance()->batchAddTitle($titles, $status, $sex);
  107. if ($result['code'] == 0) {
  108. $this->success();
  109. } else {
  110. $this->error($result['msg']);
  111. }
  112. } else {
  113. return $this->view->fetch();
  114. }
  115. }
  116. }