Task.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\special;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\special\SpecialCourse;
  14. use app\admin\model\special\SpecialTask;
  15. use service\JsonService;
  16. use think\Url;
  17. /**
  18. * 任务控制器
  19. * Class Grade
  20. * @package app\admin\controller\special
  21. */
  22. class Task extends AuthController
  23. {
  24. /**
  25. * 任务列表展示
  26. * @return
  27. * */
  28. public function index($coures_id = 0)
  29. {
  30. $this->assign('coures_id', $coures_id);
  31. $this->assign('special_id', SpecialCourse::where('id', $coures_id)->value('special_id'));
  32. $this->assign('specialList', \app\admin\model\special\Special::PreWhere()->field(['id', 'title'])->select());
  33. return $this->fetch();
  34. }
  35. /**
  36. * 任务列表获取
  37. * @return json
  38. * */
  39. public function task_list()
  40. {
  41. $where = parent::getMore([
  42. ['page', 1],
  43. ['is_show', ''],
  44. ['limit', 20],
  45. ['title', ''],
  46. ['order', ''],
  47. ['special_id', 0],
  48. ]);
  49. return JsonService::successlayui(SpecialTask::getTaskList($where));
  50. }
  51. /**
  52. * 添加和修改任务
  53. * @param int $id 修改
  54. * @return
  55. * */
  56. public function add_task($id = 0)
  57. {
  58. $this->assign('id', $id);
  59. if ($id) {
  60. $task = SpecialTask::get($id);
  61. $task->image = get_key_attr($task->image);
  62. $task->link = get_key_attr($task->link);
  63. $this->assign('special_id', $task->special_id);
  64. $this->assign('task', $task->toArray());
  65. }
  66. $specialList = \app\admin\model\special\Special::PreWhere()->field(['id', 'title', 'is_live'])->select();
  67. $this->assign('specialList', $specialList);
  68. return $this->fetch();
  69. }
  70. /**
  71. * 添加和修改任务
  72. * @param int $id 修改
  73. * @return json
  74. * */
  75. public function save_task($id = 0)
  76. {
  77. $data = parent::postMore([
  78. ['title', ''],
  79. ['image', ''],
  80. ['link', ''],
  81. ['play_count', 0],
  82. ['special_id', 0],
  83. ['sort', 0],
  84. ['is_pay', 0],
  85. ['is_show', 1],
  86. ]);
  87. if ($data['special_id'] === 0) return JsonService::fail('请选择课程再尝试添加');
  88. if (!$data['title']) return JsonService::fail('请输入课程标题');
  89. if (!$data['image']) return JsonService::fail('请上传封面图');
  90. if (!$data['link']) return JsonService::fail('请上传或者添加视频');
  91. if ($id) {
  92. unset($data['is_show']);
  93. SpecialTask::update($data, ['id' => $id]);
  94. return JsonService::successful('修改成功');
  95. } else {
  96. $data['add_time'] = time();
  97. if (SpecialTask::set($data))
  98. return JsonService::successful('添加成功');
  99. else
  100. return JsonService::fail('添加失败');
  101. }
  102. }
  103. /**
  104. * 设置单个产品上架|下架
  105. * @param int $is_show 是否显示
  106. * @param int $id 修改的主键
  107. * @return json
  108. */
  109. public function set_show($is_show = '', $id = '')
  110. {
  111. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  112. $res = SpecialTask::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  113. if ($res) {
  114. return JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  115. } else {
  116. return JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  117. }
  118. }
  119. /**
  120. * 快速编辑
  121. * @param string $field 字段名
  122. * @param int $id 修改的主键
  123. * @param string value 修改后的值
  124. * @return json
  125. */
  126. public function set_value($field = '', $id = '', $value = '')
  127. {
  128. $field == '' || $id == '' || $value == '' && JsonService::fail('缺少参数');
  129. if (SpecialTask::where(['id' => $id])->update([$field => $value]))
  130. return JsonService::successful('保存成功');
  131. else
  132. return JsonService::fail('保存失败');
  133. }
  134. /**
  135. * 删除指定任务并删除指定资源
  136. * @param int $id 修改的主键
  137. * @return json
  138. * */
  139. public function delete($id = 0)
  140. {
  141. if (!$id) return JsonService::fail('缺少参数');
  142. if (SpecialTask::delTask($id))
  143. return JsonService::successful('删除成功');
  144. else
  145. return JsonService::fail(SpecialTask::getErrorInfo('删除失败'));
  146. }
  147. /**
  148. * 编辑详情
  149. * @return mixed
  150. */
  151. public function update_content($id = 0)
  152. {
  153. if (!$id) {
  154. return $this->failed('缺少id ');
  155. }
  156. $this->assign([
  157. 'action' => Url::build('save_content', ['id' => $id]),
  158. 'field' => 'content',
  159. 'content' => htmlspecialchars_decode(SpecialTask::where('id', $id)->value('content'))
  160. ]);
  161. return $this->fetch();
  162. }
  163. /**
  164. * @param $id
  165. * @throws \think\exception\DbException
  166. */
  167. public function save_content($id)
  168. {
  169. $content = $this->request->post('content', '');
  170. $task = SpecialTask::get($id);
  171. if (!$task) {
  172. return JsonService::fail('修改得课程不存在');
  173. }
  174. $task->content = htmlspecialchars($content);
  175. if ($task->save()) {
  176. return JsonService::successful('保存成功');
  177. } else {
  178. return JsonService::fail('保存失败或者您没有修改什么');
  179. }
  180. }
  181. }