Userpledge.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\model\ProductLists;
  5. use think\exception\DbException;
  6. use think\response\Json;
  7. use Exception;
  8. use think\Db;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 用户存储列表 TeamRewards
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Userpledge extends Backend
  17. {
  18. /**
  19. * TeamRewards模型对象
  20. * @var \app\admin\model\TeamRewards
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = new \app\common\model\UserPledge;
  27. }
  28. /**
  29. * 查看
  30. *
  31. * @return string|Json
  32. * @throws \think\Exception
  33. * @throws DbException
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if (false === $this->request->isAjax()) {
  40. return $this->view->fetch();
  41. }
  42. //如果发送的来源是 Selectpage,则转发到 Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  47. $list = $this->model->with('users,pledges')
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. $result = ['total' => $list->total(), 'rows' => $list->items()];
  52. return json($result);
  53. }
  54. /**
  55. * 详情
  56. */
  57. public function detail($ids)
  58. {
  59. $row = $this->model->get(['id' => $ids]);
  60. if (!$row) {
  61. $this->error(__('No Results were found'));
  62. }
  63. if ($this->request->isAjax()) {
  64. $this->success("Ajax请求成功", null, ['id' => $ids]);
  65. }
  66. $details = json_decode($row->details, true);
  67. $this->view->assign("row", $details);
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 编辑
  72. *
  73. * @param $ids
  74. * @return string
  75. * @throws DbException
  76. * @throws \think\Exception
  77. */
  78. public function cancel($ids = null)
  79. {
  80. $row = $this->model->get($ids);
  81. if (!$row) {
  82. $this->error(__('No Results were found'));
  83. }
  84. $adminIds = $this->getDataLimitAdminIds();
  85. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  86. $this->error(__('You have no permission'));
  87. }
  88. if (false === $this->request->isPost()) {
  89. $this->view->assign('row', $row);
  90. return $this->view->fetch();
  91. }
  92. $params = $this->request->post('row/a');
  93. if (empty($params)) {
  94. $this->error(__('Parameter %s can not be empty', ''));
  95. }
  96. if ($params['type_id'] == '1' && strpos($params['product_id'] , ',') !== false) {
  97. $this->error(__('单品只能选择一个质押产品'));
  98. }
  99. $params = $this->preExcludeFields($params);
  100. $result = false;
  101. Db::startTrans();
  102. try {
  103. //是否采用模型验证
  104. if ($this->modelValidate) {
  105. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  106. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  107. $row->validateFailException()->validate($validate);
  108. }
  109. $result = $row->allowField(true)->save($params);
  110. Db::commit();
  111. } catch (ValidateException|PDOException|Exception $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. }
  115. if (false === $result) {
  116. $this->error(__('No rows were updated'));
  117. }
  118. $this->success();
  119. }
  120. }