$instance
$instance : object
Reference to the CI singleton
机构管理列表
This class object is the super class that every library in CodeIgniter will be assigned to.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* 机构管理列表
* @author lxn
*/
class Outfit extends CI_Controller
{
private static $data = array();
public function __construct()
{
parent::__construct();
$this->load->model('User_model', 'user');
$this->load->model('Public_model', 'public');
$this->load->model('Admin_model', 'admin');
$this->load->model('Webcommon_model', 'common');//积分流水表
$this->load->model('Agency_model', 'agency');//机构表
$this->load->model('Vendercode_model', 'vender');//机构表
self::$data['header'] = $this->public->header('');
self::$data['left'] = $this->public->leftmenu('');
self::$data['footer'] = $this->public->footer();
$this->load->library('Session');
$this->public->authentication();
}
/**
* 机构列表
*/
public function index()
{
if ($_GET) {
//获取Datatables发送的参数 必要
$draw = $_GET['draw'];//这个值作者会直接返回给前台
//排序
$order_column = $_GET['order']['0']['column'];//那一列排序,从0开始
$order_dir = $_GET['order']['0']['dir'];//ase desc 升序或者降序
//拼接排序sql
$orderSql = "";
if (isset($order_column)) {
$i = intval($order_column);
switch ($i) {
case 0;
$orderSql = " order by id " . $order_dir;
break; //ID
// case 2;$orderSql = " order by id ".$order_dir;break; //发布时间
case 3;
$orderSql = " order by anonymous_report " . $order_dir;
break; //分类
case 4;
$orderSql = " order by sales " . $order_dir;
break;
case 5;
$orderSql = " order by create_time " . $order_dir;
break;
case 6;
$orderSql = " order by update_time " . $order_dir;
break; //发布时间
case 7;
$orderSql = " order by reportNum " . $order_dir;
break;
case 9;
$orderSql = " order by a.vendor_id " . $order_dir;
break;
default;
$orderSql = '';
}
}
//搜索
$search = $_GET['search']['value'];//获取前台传过来的过滤条件
//分页
$start = $_GET['start'];//从多少开始
$length = $_GET['length'];//数据长度
$limitSql = '';
$limitFlag = isset($_GET['start']) && $length != -1;
if ($limitFlag) {
$limitSql = " LIMIT " . intval($start) . ", " . intval($length);
}
$where = "";
//状态查询
if (isset($_GET['stime']) && $_GET['stime'] != 'all') {
$stime = $_GET['stime'];
$where .= " AND a.create_time >=" . $stime;
}
//状态查询
if (isset($_GET['etime']) && $_GET['etime'] != 'all') {
$etime = $_GET['etime'];
$where .= " AND a.create_time <=" . $etime;
}
//定义查询数据总记录数sql
$sumSql = "SELECT count(*) as sum FROM agency as a where id>0 " . $where;
//定义过滤条件查询过滤后的记录数sql
$sumSqlWhere = ' and (a.name LIKE "%' . $search . '%" or a.sales LIKE "%' . $search . '%" or a.login_name LIKE "%' . $search . '%")';
//条件过滤后记录数 必要
$recordsFiltered = 0;
//表的总记录数 必要
$recordsTotal = 0;
//查询总数
$total = $this->common->getsqldata($sumSql);
$recordsTotal = $total[0]['sum'];
$totalResultSql = "SELECT a.*,v.code,ifnull(r.reportNum,0) reportNum from agency as a
left join vendor v on v.id = a.vendor_id
left join (
select agency_id,count(id) as reportNum from report group by agency_id
) as r on a.id=r.agency_id
where a.id>0 " . $where;
if (strlen($search) > 0) {
//记录总数
$total = $this->common->getsqldata($sumSql . $sumSqlWhere);
$recordsFiltered = $total[0]['sum'];
$list = $this->common->getsqldata($totalResultSql . $sumSqlWhere . $orderSql . $limitSql);
} else {
//记录总数
$recordsFiltered = $recordsTotal;
$list = $this->common->getsqldata($totalResultSql . $orderSql . $limitSql);
}
header("Content-Type:text/html;charset=utf-8");
foreach ($list as $key => $value) {
}
exit(json_encode(array(
"draw" => intval($draw),
"recordsTotal" => intval($recordsTotal),
"recordsFiltered" => intval($recordsFiltered),
"data" => $list
)));
} else {
$this->load->view('admincp/outfit/agencylist', self::$data);
}
}
/**
* 查看详情
* */
public function detail()
{
$userId = $this->input->get_post('userId');
self::$data['userId'] = $userId;
$user = $this->user->getUserRow($userId);
if (empty($user)) show_404();
//查询userInfo
$userInfo = $this->userinfo->getUserInfoRow($userId, 2);
$userInfo['articleNum'] = $this->article->myPublicArticleCount($userId);
// echo $this->db->last_query();exit;
$param['userId'] = $userId;
$param['examineStatus'] = 1;
$recommendList = $this->article->getAppArticleAllList($param, 10);
//获得总积分和剩余积分
$usercount = $this->usercount->getOneInfo($userId, 'jifenNum,jifenConsumptionNum');
$jifenNum = empty($usercount['jifenNum']) ? 0 : $usercount['jifenNum'];
$jifenConsumptionNum = empty($usercount['jifenConsumptionNum']) ? 0 : $usercount['jifenConsumptionNum'];
$jifen = intval($jifenNum) - intval($jifenConsumptionNum);
//获取本月获得的积分
$where['status'] = 1;
$time = date('y-m-01 00:00:00', time());
$time = strtotime($time);
$monthjifen = $this->userpoints->getUsermoonsumPoints($where, $userId, $time);
$monthjifen = empty($monthjifen) ? '0' : $monthjifen['operandNum'];
//计算出可提现金额
$allowcashed = $jifen - $monthjifen;
//编辑列表
$where['roleId'] = 9;
$adminarr = $this->admin->getAdminList($where);
//查询详情
$userInfo['headUrl'] = empty($userInfo['headUrl']) ? $userInfo['thirdheadUrl'] : $this->config->item('hwclouds') . $userInfo['headUrl'];
$userInfo['headUrl'] = empty($userInfo['headUrl']) ? $this->config->item('base_url') . '/public/web/images/use_tx_03.png' : $userInfo['headUrl'];
self::$data['user'] = $user;
self::$data['jifenNum'] = $jifenNum;
self::$data['jifenConsumptionNum'] = $jifen;
self::$data['allowcashed'] = ($allowcashed > 0) ? $allowcashed : '0';
self::$data['recommendList'] = $recommendList;
self::$data['userInfo'] = $userInfo;
self::$data['adminarr'] = $adminarr;
$this->load->view('/admincp/appuser/userinfo', self::$data);
}
/**
* 添加用户
*/
public function addagency()
{
$id = $this->input->get_post('id');
if ($id) {
$agentInfo = $this->agency->getCateRow($id);
$vender = $this->vender->getvenderRowByid($agentInfo['vendor_id']);
$agentInfo['vendor_name'] = $vender['code'];
self::$data['agentInfo'] = $agentInfo;
}
$this->load->view('/admincp/outfit/addagency', self::$data);
}
/**
* 上传用户
*/
public function lead()
{
$this->load->view('/admincp/appuser/lead', self::$data);
}
public function dolead($exts = 'xls')
{
// var_dump($_FILES);
$filename = $_SERVER['DOCUMENT_ROOT'] . $this->uploadimg();
//导入数据方法
//导入PHPExcel类库,PHPExcel没有用命名空间
// exit;
$this->load->library('PHPExcel/PHPExcel'); //注意路径
// exit;
//创建PHPExcel对象
$PHPExcel = new PHPExcel(); //如果excel文件后缀名为.xls,导入这个类
$this->load->library('PHPExcel/PHPExcel/Reader/PHPExcel_Reader_Excel2007');
$PHPReader = new PHPExcel_Reader_Excel2007();
// echo 1;die;;
//p($filename);die; //载入文件
$PHPExcel = $PHPReader->load($filename); //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
$currentSheet = $PHPExcel->getSheet(0); //获取总列数
$allColumn = $currentSheet->getHighestColumn(); //获取总行数
$allRow = $currentSheet->getHighestRow();
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) { //从哪列开始,A表示第一列
for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
//数据坐标
$address = $currentColumn . $currentRow; //读取到的数据,保存到数组$arr中
$data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
}
}
$this->save_import($data);
exit;
}
public function save_import($data)
{
$i = 0;
$ii = 0;
foreach ($data as $k => $v) {
if (!empty($v['B'])) {
$res = $this->user->checkUnique($v['B']);
// echo $this->db->last_query();
// exit;
if ($res) {
$i = $i + 1;
} else {
$userData = array(
'name' => $v['A'],
'cellphone' => $v['B'],
'gender' => $v['C'],
'birthday' => $v['D'] . '-' . $v['E'] . '-' . $v['F'],
'create_time' => date('Y-m-d H:i:s')
);
$bool = $this->user->insertData($userData);
$ii = $ii + 1;
}
}
}
$referer = $this->config->item('base_url') . '/admincp/appuser/';
$msg = '导入成功,成功导入' . $ii . '条,已经存在的用户' . $i . '条。';
$isok = 0;
echo $this->public->message($msg, $referer, $isok, array(), array('keyword' => 'appuser'));
exit;
}
/**
* 城市
* */
public function citylist()
{
$provinceId = $this->input->get_post('provinceId');
$citylist = array();
if (!empty($provinceId)) {
$citylist = $this->province->getCity($provinceId);
}
// return json_decode($citylist);exit;
self::$data['citylist'] = $citylist;
$this->load->view('/admincp/user/citylist', self::$data);
}
//上传文件
public function uploadimg()
{
//其实我们在上传文件时,点击上传后,数据由http协议先发送到apache服务器那边,这里apache服务器已经将上传的文件存放到了服务器下的C:\windows\Temp目录下了。这时我们只需转存到我们需要存放的目录即可。
//php中自身对上传的文件大小存在限制默认为2M
//获取文件的大小
$file_size = $_FILES['photoimg']['size'];
if ($file_size > 2 * 1024 * 1024) {
$referer = $this->config->item('base_url') . '/admincp/appuser/lead';
$msg = '文件过大,不能上传大于2M的文件';
$isok = 1;
echo $this->public->message($msg, $referer, $isok, array(), array('keyword' => 'appuser'));
exit;
}
$file_type = $_FILES['photoimg']['type'];
// echo $file_type;exit;
// if($file_type!="application/wps-office.xlsx") {
// $referer = $this->config->item('base_url') . '/admincp/appuser/lead';
// $msg = '文件类型只能为xlsx格式';
// $isok = 1;
// echo $this ->public-> message( $msg, $referer, $isok, array(), array('keyword'=>'appuser'));exit;
//
// }
//判断是否上传成功(是否使用post方式上传)
if (is_uploaded_file($_FILES['photoimg']['tmp_name'])) {
//把文件转存到你希望的目录(不要使用copy函数)
$uploaded_file = $_FILES['photoimg']['tmp_name'];
//我们给每个用户动态的创建一个文件夹
$user_path = $_SERVER['DOCUMENT_ROOT'] . "/attachment/" . date('Ymd') . "/";
$file_true_name = $_FILES['photoimg']['name'];
$imagename = time() . rand(1, 1000) . substr($file_true_name, strrpos($file_true_name, "."));
//判断该用户文件夹是否已经有这个文件夹
if (!file_exists($user_path)) {
mkdir($user_path, 0777, true);
}
// echo $user_path;exit;
//$move_to_file=$user_path."/".$_FILES['myfile']['name'];
$move_to_file = $user_path . $imagename;
//echo "$uploaded_file $move_to_file";
if (move_uploaded_file($uploaded_file, iconv("utf-8", "gb2312", $move_to_file))) {
return '/attachment/' . date('Ymd') . '/' . $imagename;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
/**
* 删除机构
*/
public function delete()
{
$res = array(
'err' => 1,
'msg' => '系统错误'
);
$Id = trim($this->input->get_post('Id'));
$agentInfo = $this->agency->getCateRow($Id);
if (empty($agentInfo)) {
$res['msg'] = '参数错误';
echo json_encode($res);
exit();
}
$bool = $this->agency->delete($Id);
$res['err'] = $bool ? 0 : 1;
$res['msg'] = $bool ? '删除成功' : '删除失败';
echo json_encode($res);
exit();
}
public function updateData()
{
$referer = $this->config->item('base_url') . '/admincp/Outfit/index';
$key = $this->input->get_post('key');
$password = $this->input->get_post('password');
$name = $this->input->get_post('name');
$sales = $this->input->get_post('sales');
$anonymous_report = $this->input->get_post('anonymous_report');
$valid_begin_data = $this->input->get_post('valid_begin_date');
$valid_end_data = $this->input->get_post('valid_end_date');
$vendor_name = $this->input->get_post('vendor_name');
$vendor_id = $this->vender->getvenderRow($vendor_name);
if (empty($vendor_id)) {
$msg = '添加失败!厂家码不存在';
$isok = 1;
echo $this->public->message($msg, $referer, $isok, array(), array('keyword' => 'appuser'));
exit;
}
$id = $this->input->get_post('id');
$agentData = array(
'key' => $key,
'name' => $name,
'login_name' => $key,
'vendor_id' => $vendor_id['id'],
'anonymous_report' => $anonymous_report,
'sales' => $sales,
'update_time' => date('Y-m-d H:i:s'),
'valid_begin_date' => date('Y-m-d H:i:s', strtotime($valid_begin_data)),
'valid_end_date' => date('Y-m-d H:i:s', strtotime($valid_end_data))
);
if (empty($id)) {
$agencyInfo = $this->agency->getagencybykey($key);
if ($agencyInfo) {
$msg = '添加失败!机构名已存在';
$isok = 1;
echo $this->public->message($msg, $referer, $isok, array(), array('keyword' => 'appuser'));
exit;
}
$agentData['create_time'] = date('Y-m-d H:i:s');
$agentData['password'] = password_hash($password, PASSWORD_BCRYPT);
try {
$bool = $this->agency->addagency($agentData);
} catch (Exception $e) {
$bool = false;
}
$msg = $bool ? '添加成功!' : '添加失败!';
$isok = $bool ? 0 : 1;
echo $this->public->message($msg, $referer, $isok, array(), array('keyword' => 'appuser'));
exit;
} else {
if (!empty($password)) {
$agentData['password'] = password_hash($password, PASSWORD_BCRYPT);
}
$bool = $this->agency->editagency($agentData, $id);
$msg = $bool ? '编辑成功!' : '编辑失败!';
$isok = $bool ? 0 : 1;
echo $this->public->message($msg, $referer, $isok, array(), array('keyword' => 'appuser'));
exit;
}
}
}