Suber.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\admin\model\Area;
  4. use app\admin\model\UserGroup;
  5. use app\common\controller\Backend;
  6. use app\common\library\Auth;
  7. use app\common\model\UserInfo;
  8. use think\Db;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 会员管理
  13. *
  14. * @icon fa fa-user
  15. */
  16. class Suber extends Backend
  17. {
  18. protected $relationSearch = true;
  19. protected $searchFields = 'id,username,nickname';
  20. protected $noNeedLogin=['levels'];
  21. /**
  22. * @var \app\admin\model\User
  23. */
  24. protected $model = null;
  25. public function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->assign('levels',\app\common\model\User::$levels);
  29. $this->model = new \app\admin\model\User;
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if ($this->request->isAjax()) {
  39. //如果发送的来源是Selectpage,则转发到Selectpage
  40. if ($this->request->request('keyField')) {
  41. return $this->selectpage();
  42. }
  43. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  44. $list = $this->model
  45. ->with('userinfo')
  46. ->where('user.type',1)
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $k => $v) {
  51. $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
  52. $v->hidden(['password', 'salt']);
  53. $v->appendArea();
  54. }
  55. $right_per=$this->model
  56. ->where('user.level','>',\app\common\model\User::LEVEL_0)
  57. ->with('userinfo')
  58. ->where($where)
  59. ->avg('userinfo.right_per');
  60. $result = array("total" => $list->total(), "rows" => $list->items(),'right_per'=>$right_per);
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 添加
  67. */
  68. public function add()
  69. {
  70. if ($this->request->isPost()) {
  71. $this->token();
  72. }
  73. return parent::add();
  74. }
  75. /**
  76. * 编辑
  77. */
  78. public function edit($ids = null)
  79. {
  80. if ($this->request->isPost()) {
  81. $this->token();
  82. }
  83. $row = $this->model->get($ids);
  84. $this->modelValidate = true;
  85. if (!$row) {
  86. $this->error(__('No Results were found'));
  87. }
  88. $this->view->assign('groupList', build_select('row[group_id]', UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
  89. if ($this->request->isPost()) {
  90. $params = $this->request->post("row/a");
  91. if ($params) {
  92. $params = $this->preExcludeFields($params);
  93. if(!empty($params['level_close_at'])){
  94. $params['level_close_at']=strtotime($params['level_close_at']);
  95. }
  96. $result = false;
  97. Db::startTrans();
  98. try {
  99. //是否采用模型验证
  100. if ($this->modelValidate) {
  101. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  102. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  103. $row->validateFailException(true)->validate($validate);
  104. }
  105. $result = $row->allowField(true)->save($params);
  106. Db::commit();
  107. } catch (ValidateException $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. } catch (PDOException $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. } catch (\Exception $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. }
  117. if ($result !== false) {
  118. $this->success();
  119. } else {
  120. $this->error(__('No rows were updated'));
  121. }
  122. }
  123. $this->error(__('Parameter %s can not be empty', ''));
  124. }else{
  125. $this->view->assign("row", $row);
  126. return view();
  127. }
  128. }
  129. /**
  130. * 删除
  131. */
  132. public function del($ids = "")
  133. {
  134. if (!$this->request->isPost()) {
  135. $this->error(__("Invalid parameters"));
  136. }
  137. $ids = $ids ? $ids : $this->request->post("ids");
  138. $row = $this->model->get($ids);
  139. $this->modelValidate = true;
  140. if (!$row) {
  141. $this->error(__('No Results were found'));
  142. }
  143. Auth::instance()->delete($row['id']);
  144. $this->success();
  145. }
  146. public function levels(){
  147. return \app\common\model\User::$levels;
  148. }
  149. public function rate($ids){
  150. $user=$this->model->findOrFail($ids);
  151. $inputDate=input('time');
  152. if(!$inputDate){
  153. $inputDate=date('Y-m-01 00:00:00').' - '.date('Y-m-01 00:00:00',strtotime('+1month',strtotime(date('Y-m-01 00:00:00'))));
  154. }
  155. $this->assign('date',$inputDate);
  156. list($start,$end)=explode(' - ',$inputDate);
  157. $x=[];
  158. $temp=[];
  159. $values=[];
  160. $start=strtotime($start);
  161. $end=strtotime($end);
  162. for ($i=0;$i<($end-$start)/86400;$i++){
  163. $temp[]=$a=strtotime("+{$i}days",$start);
  164. $x[]=date('Y/m/d',$a);
  165. }
  166. $data=$user->crate()->whereBetween('day',[$start,$end])->column('right_per','day');
  167. $idx=0;
  168. $prev_per=0;
  169. foreach ($temp as $day){
  170. if(isset($data[$day])){
  171. $per=$data[$day];
  172. }else{
  173. if($idx==0){
  174. $per=UserInfo::where('user_id',$ids)->value('right_per',0);
  175. }else{
  176. $per=$prev_per;
  177. }
  178. }
  179. $values[]=$per;
  180. $idx++;
  181. $prev_per=$per;
  182. }
  183. $this->assign('x',$x);
  184. $this->assign('value',$values);
  185. return view();
  186. }
  187. }