123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- namespace app\admin\controller\clientmanage\bookcity;
- use app\common\model\ManageBlock;
- use app\common\controller\Backend;
- use app\common\model\Attachment;
- use app\common\model\Book;
- use app\main\service\ActivityService;
- use think\Controller;
- use think\Exception;
- use think\exception\ErrorException;
- use think\exception\PDOException;
- use think\Request;
- /**
- * 书城管理-资源管理
- *
- * @icon fa fa-circle-o
- */
- class Blockresource extends Backend
- {
- /**
- * ManageBlockResource模型对象
- */
- protected $model = null;
- protected $blockData = [];
- // protected $searchFields = 'block_id';
- protected $relationSearch = true;
- /**
- * 是否开启Validate验证
- */
- protected $modelValidate = true;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('ClientManageBlockResource');
- $this->view->assign("typeList", $this->model->getTypeList($this->request->get('page_id')));
- $page_id = model('ClientManageBlock')->where('id','in',$this->request->get('block_id'))->find();
- $blockList = collection(model('ClientManageBlock')->where('page_id', 'in', $page_id['page_id'])->select())->toArray();
- foreach ($blockList as $k => $v) {
- $this->blockData[$v['id']] = [
- 'name' => $v['name'],
- 'type' => $v['type']
- ];
- }
- $this->view->assign('block_id', $this->request->get('block_id'));
- $this->assignconfig('block_id', $this->request->get('block_id'));
- $this->assignconfig('page_id', $this->request->get('page_id'));
- $this->view->assign('blockData', $this->blockData);
- }
- /**
- * 查看
- */
- public function index()
- {
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams(NULL);
- $total = $this->model
- ->where($where)
- ->with('Book')
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->with('Book')
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list, "extend" => ['id' => 1]);
- return json($result);
- }
- return $this->view->fetch();
- }
- /*
- * 新增块资源
- */
- public function add()
- {
- if($this->request->isPost()){
- $row = $this->request->post('row/a');
- if($row){
- try{
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $this->model->validate(true);
- }
- $this->model->startTrans();
- //保存资源信息
- if($this->model->allowField(true)->save($row) == false){
- throw new Exception($this->model->getError());
- }
- switch ($row['page_id']){
- case '1': //男频
- case '2': //女频
- $row['free_stime'] = 0;
- $row['free_etime'] = 0;
- break;
- case '3': //限免男频
- case '4': //限免女频
- if($row['type']==1){
- //修改对应书籍记录限免时间
- $book = Book::get($row['book_id']);
- $book->setAttr('free_stime', strtotime($row['free_stime']));
- $book->setAttr('free_etime', strtotime($row['free_etime']));
- if($book->allowField(true)->save() == false){
- throw new Exception($book->getError());
- }
- }
- break;
- default:
- throw new Exception('页类型不存在');
- }
- $this->model->commit();
- $this->success();
- }catch (Exception $e){
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign('page_id', $this->request->get('page_id'));
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get($ids);
- if (!$row)
- $this->error(__('No Results were found'));
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
- $row->validate($validate);
- }
- $row->startTrans();
- switch ($params['type']) {
- case "1";
- $params['url'] = '';
- $params['client_activity_id'] = '';
- break;
- case "2";
- $params['book_id'] = '';
- $params['client_activity_id'] = '';
- break;
- case "3";
- $params['book_id'] = '';
- $params['url'] = '';
- $params['client_activity_id'] = '';
- break;
- case "4";
- $params['book_id'] = '';
- $params['url'] = '';
- break;
- }
- $result = $row->allowField(true)->save($params);
- if($result === false){
- throw new Exception($row->getError());
- }
- switch ($params['page_id']){
- case '1': //男频
- case '2': //女频
- $params['free_stime'] = 0;
- $params['free_etime'] = 0;
- break;
- case '3': //限免男频
- case '4': //限免女频
- if($params['type'] == 1){
- //修改对应书籍记录限免时间
- $book = Book::get($params['book_id']);
- $book->setAttr('free_stime', strtotime($params['free_stime']));
- $book->setAttr('free_etime', strtotime($params['free_etime']));
- if($book->allowField(true)->save() == false){
- throw new Exception($book->getError());
- }
- }
- break;
- default:
- throw new Exception('页类型不存在');
- }
- $row->commit();
- $this->success();
- }catch (Exception $e){
- $row->rollback();
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- //清除无效的配置
- switch ($row['type']) {
- case "1":
- $row['url'] = '';
- $row['client_activity_id'] = '';
- break;
- case "2":
- $row['book_id'] = '';
- $row['client_activity_id'] = '';
- break;
- case "3":
- $row['url'] = '';
- $row['book_id'] = '';
- $row['client_activity_id'] = '';
- break;
- case "4":
- $row['url'] = '';
- $row['book_id'] = '';
- break;
- }
- $this->view->assign("row", $row);
- $activityName = '';
- if (!empty($row['client_activity_id'])) {
- $activity = model('activity')->get($row['client_activity_id']);
- $activityName = $activity->name;
- }
- $this->view->assign('page_id', $this->request->get('page_id'));
- $this->assignconfig('row', $row);
- $this->view->assign("activityName", $activityName);
- return $this->view->fetch();
- }
- }
|