UserInvite.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 用户邀请管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class UserInvite extends Backend
  11. {
  12. /**
  13. * UserInvite模型对象
  14. * @var \app\admin\model\UserInvite
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\UserInvite;
  21. }
  22. public function import()
  23. {
  24. parent::import();
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->with(['user','inviteuser'])
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->getRelation('user')->visible(['nickname']);
  53. $row->getRelation('inviteuser')->visible(['nickname']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $params = $this->request->post("row/a");
  67. $params = $this->preExcludeFields($params);
  68. if (!$params) {
  69. $this->error(__('Parameter %s can not be empty', ''));
  70. }
  71. Db::startTrans();
  72. try {
  73. //是否采用模型验证
  74. if ($this->modelValidate) {
  75. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  76. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  77. $this->model->validateFailException(true)->validate($validate);
  78. }
  79. if ($params['invite_user_id'] == $params['user_id']) {
  80. throw new Exception('不能邀请自己');
  81. }
  82. if (!empty($params['invite_user_id'])) {
  83. $userWhere['id'] = $params['invite_user_id'];
  84. $user = Db::name('user')->where($userWhere)->find();
  85. if ($user['pre_userid'] != 0) {
  86. throw new Exception('该用户已被邀请');
  87. }
  88. $userRes = Db::name('user_id')->where($userWhere)->update(['pre_userid'=>$params['user_id']]);
  89. if (!$userRes) {
  90. throw new Exception('更新用户邀请信息失败');
  91. }
  92. }
  93. $result = $this->model->allowField(true)->save($params);
  94. if ($result == false) {
  95. throw new Exception(__('No rows were inserted'));
  96. }
  97. Db::commit();
  98. $this->success();
  99. } catch (ValidateException|PDOException|Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. }
  104. return $this->view->fetch();
  105. }
  106. /**
  107. * 编辑
  108. */
  109. public function edit($ids = null)
  110. {
  111. $row = $this->model->get($ids);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. $adminIds = $this->getDataLimitAdminIds();
  116. if (is_array($adminIds)) {
  117. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  118. $this->error(__('You have no permission'));
  119. }
  120. }
  121. if ($this->request->isPost()) {
  122. $params = $this->request->post("row/a");
  123. if (!$params) {
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. $params = $this->preExcludeFields($params);
  127. Db::startTrans();
  128. try {
  129. //是否采用模型验证
  130. if ($this->modelValidate) {
  131. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  132. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  133. $row->validateFailException(true)->validate($validate);
  134. }
  135. if ($params['invite_user_id'] == $params['user_id']) {
  136. throw new Exception('不能邀请自己');
  137. }
  138. if (!empty($params['invite_user_id'])) {
  139. $userWhere['id'] = $params['invite_user_id'];
  140. $user = Db::name('user')->where($userWhere)->find();
  141. if ($user['pre_userid'] != $params['user_id']) {
  142. $userRes = Db::name('user_id')->where($userWhere)->update(['pre_userid'=>$params['user_id']]);
  143. if (!$userRes) {
  144. throw new Exception('更新用户邀请信息失败');
  145. }
  146. }
  147. }
  148. $result = $row->allowField(true)->save($params);
  149. if ($result == false) {
  150. throw new Exception(__('No rows were inserted'));
  151. }
  152. Db::commit();
  153. $this->success();
  154. } catch (ValidateException|PDOException|Exception $e) {
  155. Db::rollback();
  156. $this->error($e->getMessage());
  157. }
  158. }
  159. $this->view->assign("row", $row);
  160. return $this->view->fetch();
  161. }
  162. }