model = new \app\admin\model\User; $this->view->assign("statusList", $this->model->getStatusList()); } /** * 查看 */ public function index() { $this->selectpageFields = ['id','nickname']; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->where($where) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 关系转移 */ public function updateintro(){ if($this->request->isPost()){ $user_id = input('user_id'); $new_intro_uid = input('new_intro_uid'); //验证 if(empty($user_id)) $this->error("请输入需要转移的用户ID"); if(empty($new_intro_uid)) $this->error("请输入新的上级用户ID"); Db::startTrans(); $user = Db::name('user')->where('id',$user_id)->lock(true)->find(); if(empty($user)){ Db::rollback(); $this->error('不存在的用户'); } $new_intro = Db::name('user')->where('id',$new_intro_uid)->lock(true)->find(); if(empty($new_intro)){ Db::rollback(); $this->error('不存在的新上级用户'); } $commonuser = new \app\common\model\User; //我的多个下级,的推荐人,改成新人 $my_down = Db::name('user')->where('intro_uid',$user['id'])->select(); // dump($my_down); if(!empty($my_down)){ foreach($my_down as $key => $down_user){ $rs_down = $commonuser->updateIntro($down_user['id'],$new_intro_uid); if($rs_down !== true){ Db::rollback(); $this->error($rs_down); } } } //理论上已经用不到了,预防万一又被查到 //我被转移了,我的上级保持不变,下级都没了 $my_update = [ 'intro_num' => 0, 'intro_num_all' => 0, ]; $rs2 = Db::name('user')->where('id',$user['id'])->update($my_update); if($rs2 === false){ Db::rollback(); $this->error('更新被转移用户错误'); } //记录个日志 $log = [ 'info' => '用户['.$user['id'].']关系被转移,其名下直推有['.implode(',',array_column($my_down,'id')).'],都改到['.$new_intro_uid.']名下', 'createtime' => time(), ]; Db::name('intro_log')->insertGetId($log); //提交 Db::commit(); $this->success('转移完成'); } return $this->view->fetch(); } }