Agent.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\admin\controller\greenroom;
  3. use app\admin\model\Admin;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 用户代理记录
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Agent extends Backend
  14. {
  15. /**
  16. * Agent模型对象
  17. * @var \app\admin\model\greenroom\Agent
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\greenroom\Agent;
  24. $this->rechargeModel= new \app\admin\model\greenroom\Recharge;
  25. $this->usercodeModel= new \app\admin\model\greenroom\Usercode;
  26. $this->dataList = $this->rechargeModel->select();
  27. foreach ($this->dataList as $k => &$v) {
  28. $data[$v['id']] = $v['level'];
  29. }
  30. $this->view->assign('level', $data);
  31. $this->adminmodel = new \app\admin\model\Admin;
  32. $admindata = Admin::field('id,nickname')->select();
  33. $temp=[];
  34. foreach ($admindata as $key=>$val){
  35. $temp[$val['id']]=$val['nickname'];
  36. }
  37. $this->admindata=$temp;
  38. $statusList=[0=>'审核中',1=>'已通过',2=>'未通过'];
  39. $this->view->assign('statusList', $statusList);
  40. $this->view->assign('userlist', $this->admindata);
  41. }
  42. /**
  43. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  44. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  45. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  46. */
  47. /**
  48. * 查看
  49. */
  50. public function index()
  51. {
  52. //当前是否为关联查询
  53. $this->relationSearch = true;
  54. //设置过滤方法
  55. $this->request->filter(['strip_tags', 'trim']);
  56. if ($this->request->isAjax()) {
  57. //如果发送的来源是Selectpage,则转发到Selectpage
  58. if ($this->request->request('keyField')) {
  59. return $this->selectpage();
  60. }
  61. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  62. $list = $this->model
  63. ->with(['admin','recharge'])
  64. ->where($where)
  65. ->order($sort, $order)
  66. ->paginate($limit);
  67. foreach ($list as $row) {
  68. $row->getRelation('admin')->visible(['username','appname']);
  69. $row->getRelation('recharge')->visible(['level']);
  70. }
  71. $result = array("total" => $list->total(), "rows" => $list->items());
  72. return json($result);
  73. }
  74. return $this->view->fetch();
  75. }
  76. public function edit($ids = null)
  77. {
  78. $row = $this->model->get($ids);
  79. if (!$row) {
  80. $this->error(__('No Results were found'));
  81. }
  82. $adminIds = $this->getDataLimitAdminIds();
  83. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  84. $this->error(__('You have no permission'));
  85. }
  86. if (false === $this->request->isPost()) {
  87. $this->view->assign('row', $row);
  88. return $this->view->fetch();
  89. }
  90. $params = $this->request->post('row/a');
  91. if (empty($params)) {
  92. $this->error(__('Parameter %s can not be empty', ''));
  93. }
  94. $params = $this->preExcludeFields($params);
  95. $result = false;
  96. Db::startTrans();
  97. try {
  98. //是否采用模型验证
  99. if ($this->modelValidate) {
  100. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  101. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  102. $row->validateFailException()->validate($validate);
  103. }
  104. if($params['status']==1&&$row->is_code===0)
  105. {
  106. $recharge = $this->rechargeModel->find($params['r_id']);
  107. if($recharge){
  108. $codes = build_code($recharge->codenum);
  109. $nowtime= time();
  110. $uscodes=[];
  111. foreach ($codes as $key=>$val){
  112. $uscodes[]=['code'=>$val,
  113. 'paydays'=>30,
  114. 'agent_id'=>$row->uid,
  115. 'addtime'=>$nowtime,
  116. 'status'=>1 ];
  117. }
  118. $this->usercodeModel->saveAll($uscodes,true);
  119. $params['is_code']=1;
  120. }
  121. }
  122. $result = $row->allowField(true)->save($params);
  123. Db::commit();
  124. } catch (ValidateException|PDOException|Exception $e) {
  125. Db::rollback();
  126. $this->error($e->getMessage());
  127. }
  128. if (false === $result) {
  129. $this->error(__('No rows were updated'));
  130. }
  131. $this->success();
  132. }
  133. }