model = new \app\admin\model\user\User; $this->view->assign("apyPayList", $this->model->getApyPayList()); $this->view->assign("lhActList", $this->model->getLhActList()); $this->view->assign("authList", $this->model->getAuthList()); $this->view->assign("earlyAuthList", $this->model->getEarlyAuthList()); } public function import() { parent::import(); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } // list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $offset = $this->request->get("offset", 0); $limit = $this->request->get("limit", 0); $params = json_decode(input('filter'), true); $op = json_decode(input('op'), true); if (count($params) > 0) { $new_params = $new_op = []; foreach ($params as $key => $value) { if ($key == 'nickname') { $new_params['a.nickname'] = $value; $new_op['a.nickname'] = $op[$key]; } elseif ($key == 'grade_name') { $new_params['b.name'] = $value; $new_op['b.name'] = $op[$key]; }elseif ($key == 'level_name') { $new_params['c.name'] = $value; $new_op['c.name'] = $op[$key]; }elseif ($key == 'daili_name') { $new_params['e.nickname'] = $value; $new_op['e.nickname'] = $op[$key]; } else { $new_params['a.' . $key] = $value; $new_op['a.' . $key] = $op[$key]; } } $w = $this->rewriteQuery($new_params, $new_op); } else { $w['a.id'] = array('>', 0); } $admin_id = $this->auth->id; if($admin_id > 1){ $user = Db::name("user")->where("admin_id",$admin_id)->find(); if($user){ $w['a.path'] = array("like",$user['path']."%"); } } $total = Db::name('user')->alias('a') ->field('a.id,e.nickname as daili_name') ->join('app_ai_grade b', 'a.grade_id = b.id' , 'left') ->join('app_level c', 'a.level_id = c.id' , 'left') ->join('admin e', 'a.p_admin_id = e.id' , 'left') ->where($w) ->count(); $list = Db::name('user')->alias('a') ->field('a.*,b.name as grade_name,c.name as level_name,d.nickname as admin_name,e.nickname as daili_name') ->join('app_ai_grade b', 'a.grade_id = b.id' , 'left') ->join('app_level c', 'a.level_id = c.id' , 'left') ->join('admin d', 'a.admin_id = d.id' , 'left') ->join('admin e', 'a.p_admin_id = e.id' , 'left') ->where($w) ->order('a.id', 'desc') ->limit($offset, $limit) ->select(); foreach ($list as $key => $value) { $auth = Db::name("app_auth")->where("user_id",$value['id'])->where("status",1)->find(); if($auth && $auth['status'] == 1 && $value['early_auth'] == 0){ Db::name("user")->where("id",$value['id'])->update(['early_auth'=>1]); $value['early_auth'] = 1; } // $high_auth = Db::name("app_auth_high")->where("user_id",$value['id'])->where("status",1)->find(); // if($high_auth['status'] == 1 && $value['high_auth'] == 0){ // Db::name("user")->where("id",$value['id'])->update(['high_auth'=>1]); // $value['high_auth'] = 1; // } $curr = Db::name("app_currency_user a")->field("a.id,a.curr_id,a.num,a.num_fb,a.num_lh,a.num_dj,a.num_fb_dj,a.num_lh_dj,b.name,b.full_name,b.exchange,b.thumb_image,b.is_reg,b.is_wid,b.is_tran,b.is_otc,a.tron_address") ->join("app_currency b","a.curr_id=b.id","left") ->where("a.user_id",$value['id']) ->order("a.curr_id asc") ->select(); $count_xh_usdt = 0;//现货总U foreach ($curr as $ke => $val) { if($val['curr_id'] == 2){ $usdt_num = 0; }else{ $usdt_num = sprintf("%.6f",($val['num']+$val['num_dj'])*$val['exchange']); } $count_xh_usdt = $count_xh_usdt+$usdt_num; } $value['count_usdt'] = sprintf("%.2f",$count_xh_usdt); $list[$key] = $value; } $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $user = Db::name("user")->where("username",$params['username'])->find(); if($user){ $this->error("用户名已存在"); } $user2 = Db::name("user")->where("email",$params['email'])->find(); if($user2){ $this->error("邮箱已存在"); } $parent = Db::name("user")->where("id",$params['pid'])->find(); if(empty($parent)){ $this->error("上级用户不存在"); } Db::startTrans(); try { // $params['is_robot'] = 0; $params['salt'] = $this->build(); $params['password'] = strtoupper(md5(strtoupper(md5($params['password'].'skund')))); $params['status'] = "normal"; if($params['pay_pwd']){ $params['pay_pwd'] = strtoupper(md5(strtoupper(md5($params['pay_pwd'].'skund')))); }else{ $params['pay_pwd'] = strtoupper(md5(strtoupper(md5("123456".'skund')))); } $params['avatar'] = Config::get("site.default_avatar"); $params['referral_code'] = $this->create_invite_code(); $user_id = Db::name("user")->insertGetId($params); //对接公链 $currency = Db::name('app_currency')->select(); foreach ($currency as $key => $value) { $wallet[] = [ 'user_id' => $user_id, 'curr_id' => $value['id'], ]; } if(!empty($wallet)) { Db::name('app_currency_user')->insertAll($wallet); } //查询所属代理 $ids = str_ireplace("|", ",", $parent['path']); $p_user = Db::name("user")->where("id","in",$ids)->order("id desc")->select(); $admin_id = 0; foreach ($p_user as $key => $value) { if($value['admin_id'] > 0){ $admin_id = $value['admin_id']; break; } } Db::name('user')->where('id',$user_id) ->update(['path'=>$parent['path'].$user_id.'|','p_admin_id'=>$admin_id]); Db::commit(); $this->success("添加成功"); } catch (Exception $e) { Db::rollback(); $this->error("添加失败"); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { if($params['password']){ $params['salt'] = $this->build(); $params['password'] = strtoupper(md5(strtoupper(md5($params['password'].'skund')))); }else{ unset($params['password']); } // $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 $result = $row->allowField(true)->save($params); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 能用的随机数生成 * @param string $type 类型 alpha/alnum/numeric/nozero/unique/md5/encrypt/sha1 * @param int $len 长度 * @return string */ public function build($type = 'alnum', $len = 6) { switch ($type) { case 'alpha': case 'alnum': case 'numeric': case 'nozero': switch ($type) { case 'alpha': $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 'alnum': $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; break; case 'numeric': $pool = '0123456789'; break; case 'nozero': $pool = '123456789'; break; } return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len); case 'unique': case 'md5': return md5(uniqid(mt_rand())); case 'encrypt': case 'sha1': return sha1(uniqid(mt_rand(), true)); } } /** * 生成个人唯一邀请码 */ public function create_invite_code() { $d = strtoupper( $this->build('alnum',9)); $w['referral_code'] = array('eq', $d); $user_info = Db::name('user')->field("id")->where($w)->find(); if ($user_info) { $d = $this->create_invite_code(); } return $d; } /** * 查询下级 */ public function get_children_view( $ids ) { $w = []; $w['a.user_id'] = array( "eq" , $ids ); $params = $this->request->post(); if(isset($params['go'])){ $user = Db::name("user")->field("id,pid,email,nickname")->where("id",$ids)->find(); $all_user = Db::name("user a") ->field("a.id,a.pid,a.email,a.nickname") ->where("a.id",">",0) ->select(); $team_arr = $this->getTree($all_user,$ids ,0,false,$clear=true ); // echo "
";var_dump(  $ids , $team_arr  );exit;
            
            $data[] = array(
                'key'=>$ids,
                'name'=>"ID:".$ids."\r\n" . "邮箱:" . $user['email']."\r\n" . "昵称:" . $user['nickname'],
                'gender'=>'M',
            );
            if($team_arr){
                foreach ($team_arr as $key => $value) {
                    $data[] = array(
                        "key" => $value['id'],
                        'name' => "ID:".$value['id']."\r\n" . "邮箱:" . $value['email']."\r\n" . "昵称:" . $value['nickname'],
                        'parent' => $value['pid'],
                        'gender' => "M",
                    );
                }
            }
            
            $this->success("ok",'',$data);
        }
        
        return $this->view->fetch("get_children");
        
    }
    
    /**
     * 获取所有下级    
     * $data          用户数组          
     * $pid           用户user_id
     * $level         递归层次            
     * $bool_level    判断是否分级(不分级则获取所有的下级)
     * $clear         是否销毁静态变量
     */
    public function getTree($data, $pid = 0, $level = 0, $bool_level = true ,$clear=false ) {

        if ($pid < 1) {
            return false;
        }
        
        static $arr = [];
        if($clear){
            $arr = null;
            static $arr = [];
        }
        
        if ($bool_level) {
            $level = $level - 1;
        } else {
            $level = 1;
        }

        if ($level >= 0) {
            foreach ($data as $key => $val) {
                if ($val['pid'] == $pid) {
                    $arr[] = $val;
                    $this->getTree($data, $val['id'], $level, $bool_level ,false);
                }
            }
        }
        
        $arr1 = $arr;
        static $arr = [];
        return $arr1;
        
    }
    
    public function get_ctrl($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $result = Db::name('user')
                    ->where('id',$row['id'])->update(['tradectrl_json'=>$params['tradectrl_json'],'updatetime'=>time()]);
                if($result)
                {
                    $this->success('ok');
                }else{
                    $this->error('系统繁忙');
                }
            }
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}