title = '渠道卡密管理'; $query = $this->_query($this->table); $this->card = input('card'); $this->chongzhi = 2; $where = array(); if ($this->card) { $where['card'] = $this->card; } if ($where) { $log = Log::instance()->db()->where($where)->select()->toArray(); $ids = array(); if ($log) { foreach ($log as $k => $v) { $ids[$v['card_id']] = $v['card_id']; } $ids = implode(',', $ids); $query->whereRaw(' id in ('.$ids.')'); } } $use = input('use'); if ($use) { if ($use > 0) { $query->where(array('use' => $use)); } else { $query->where(array('use' => 1, 'use_num' => 0)); } } $query->equal('cnum,cpwd,status,value,use_mid,use_num,group_id')->dateBetween('create_at'); $query->order('use asc, id desc')->page(); } /** * 数据列表处理 * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ protected function _page_filter(&$data) { $where['is_deleted'] = 0; $where['status'] = 1; $this->merchant = MerchantService::instance()->db()->whereRaw('id in('.CardService::instance()->getMerchant().')')->where($where)->select()->toArray(); //$this->merchant = array(); foreach ($data as $k => $v) { $data[$k]['log'] = Log::instance()->getData($v['id']); $data[$k]['log_error'] = Log::instance()->getNum($v['id'], 7); $data[$k]['merchant'] = array(); if (isset($v['use_mid']) && $v['use_mid'] > 0) { $data[$k]['merchant'] = MerchantService::instance()->get($v['use_mid']); } $data[$k]['group'] = CardGroupService::instance()->db()->where(array('id' => $v['group_id']))->find(); } if ($this->chongzhi == 1) { $this->total = CardService::instance()->total(false); } else { $this->total = CardService::instance()->total(true); } } /** * 充值卡管理 * @auth true * @menu true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function chongzhi() { $this->title = '充值卡管理'; $query = $this->_query($this->table); $this->card = input('card'); $this->chongzhi = 1; $where = array(); if ($this->card) { $where['card'] = $this->card; } if ($where) { $log = Log::instance()->db()->where($where)->select()->toArray(); $ids = array(); if ($log) { foreach ($log as $k => $v) { $ids[$v['card_id']] = $v['card_id']; } $ids = implode(',', $ids); $query->whereRaw(' id in ('.$ids.')'); } } $use = input('use'); if ($use) { if ($use > 0) { $query->where(array('use' => $use)); } else { $query->where(array('use' => 1, 'use_num' => 0)); } } $query->equal('cnum,cpwd,status,value,use_mid,use_num')->dateBetween('create_at'); $query->order('chongzhi_use asc, id desc')->page(); } /** * 表单数据处理 * @param array $data * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ protected function _form_filter(&$data) { //$this->merchant = MerchantService::instance()->getAll(); $this->merchant = array(); $this->group = CardGroupService::instance()->db()->where(array('status' => 1))->select()->toArray(); if ($this->request->isPost()) { if (empty($data['cnum'])) $this->error('卡号不能为空'); if (empty($data['cpwd'])) $this->error('密码不能为空'); } } /** * 添加卡密 * @auth true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function add() { $this->_form($this->table, 'form'); } /** * 编辑卡密 * @auth true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit() { $this->_form($this->table, 'form'); } /** * 修改卡密状态 * @auth true * @throws \think\db\exception\DbException */ public function state() { $this->_save($this->table, $this->_vali([ 'status.in:0,1' => '状态值范围异常!', 'status.require' => '状态值不能为空!', ])); } /** * 修改卡密可用状态 * @auth true * @throws \think\db\exception\DbException */ public function setUse() { $this->_save($this->table, $this->_vali([ 'use.in:0,1' => '状态值范围异常!', 'use.require' => '状态值不能为空!', ])); } /** * 删除卡密 * @auth true * @throws \think\db\exception\DbException */ public function remove() { $this->_delete($this->table); } /** * 分配卡密 * @auth true * @menu true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function set() { if ($this->request->isGet()) { $this->_form($this->table, 'set'); } else { $mid = input('mid'); $num = input('num'); // 面值 $value = input('value'); $data = CardService::instance()->getData($value, -1, $num, false); if ($data) { if (isset($data[0]) && $data[0]) { $index = count($data); foreach ($data as $k => $v) { CardService::instance()->setMid($k, $v, $mid); } } else { $index = 1; CardService::instance()->setMid(0, $data, $mid); } $this->success('操作成功,共分配' . $index . '个卡密', ''); } else { $this->error('操作失败,没有可用的卡密'); } } } /** * 导出卡密 * @auth true * @menu true * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function out() { if (input('output') === 'json') { $mid = input('mid'); $num = input('num'); $page = input('page', 1); // 面值 $value = input('value'); $type = input('type', 1); if ($type == 1) { $state = 100; } else { $state = false; } $data = CardService::instance()->getData($value, $mid, $num, $state, $page); if ($data) { $result['list'] = $data['data']; $result['show_value'] = $value > 0 ? true : false; $result['page'] = array ( 'current' => $data['current_page'], 'pages' => $data['last_page'], 'limit' => $data['per_page'], 'total' => $data['total'], 'current' => 1, 'pages' => 1, 'limit' => $data['per_page'], 'total' => $data['total'], ); $this->success('获取数据列表成功', $result); } else { $this->error('操作失败,没有可用的卡密'); } } else { $this->_form($this->table, 'out'); } } # 上传 public function upfile() { if ($this->request->isGet()) { $this->_form($this->table, 'upfile'); } else { $this->upload(); } } # 上传卡密 public function upload() { if ($_FILES) { $data = $_FILES; } else { $this->error('请上传文件'); } $mid = input('mid', -1); $group_id = input('group_id', 1); $value = input('value', 1000); if ($group_id == -1) { $group_id = CardGroupService::instance()->up(date('Ymd'), 1); } else { CardGroupService::instance()->db()->where(array('id' => $group_id))->update(array('status' => '1')); } if (strstr($data['file']['name'], '.xls')) { $file = 'card/card.xlsx'; } else { $file = 'card/card.txt'; } if (is_file($file)) { @unlink($file); } copy($data['file']['tmp_name'], $file); if (is_file($file)) { $data = $this->read($file); $yes = 0; $no = 0; if ($data) { foreach ($data as $k => $v) { if ($k >= 0) { $state = $this->up($k, $v['A'], $v['B'], (isset($v['C']) ? $v['C'] : 0), $value, $mid, $group_id); if ($state && $state > 0) { $yes++; } elseif (!$state) { $no++; } } } $this->success('已上传,其中' . $yes . '条上传成功,' . $no . '条上传失败', ''); } else { $this->error('上传失败'); } } else { $this->error('上传失败'); } } private function up($k, $cnum, $cpwd, $v, $value, $mid, $group_id) { if (is_numeric($cnum) && is_numeric($cpwd) && $cnum && $cpwd) { if (!$v) { $v = $value; } return CardService::instance()->up($k, $cnum, $cpwd, $v, $mid, 1, 1, $group_id); } else { return -1; } } private function read($file) { if (!strstr($file, '.xls')) { $content = file_get_contents($file); $data = explode("\r\n", $content); $result = array(); if (preg_match("/\s/", $data[0])) { $i = 0; foreach ($data as $k => $v) { if ($v) { $v = preg_replace('/\s(?=\S)/', ' ', $v); $temp = explode(' ', $v); $result[$i]['A'] = $temp[0]; $result[$i]['B'] = $temp[1]; if (isset($temp[2])) { $result[$i]['C'] = $temp[2]; } else { $result[$i]['C'] = ''; } $i++; } } } else { if (isset($data[2]) && strlen($data[2]) <= 5) { # 面值 $value = true; $total = count($data)/3; } else { $value = false; $total = count($data)/2; } for ($i = 0; $i< $total; $i++) { $index = $i; if ($i > 0) { $index += 1; } $result[$i]['A'] = $data[$index]; $result[$i]['B'] = $data[$index + 1] ?? 0; if ($value) { $result[$i]['C'] = $data[$index + 2] ?? ''; } else { $result[$i]['C'] = ''; } } } return $result; } // include('../excel/PHPExcel.php'); // // $objRead = new \PHPExcel_Reader_Excel2007(); // if (!$objRead->canRead($file)) { // $objRead = new \PHPExcel_Reader_Excel5(); // if (!$objRead->canRead($file)) { // Export::alert('file not exists!'); // } // } // $objRead = new IOFactory(); $sheet = 0; $offset = 0; //TODO 待解决 $obj = IOFactory::load($file); $currSheet = $obj->getActiveSheet(); $columnH = $currSheet->getHighestRow(); $columnCnt = array_search($columnH, $this->cell); $rowCnt = $currSheet->getHighestRow(); $data = array(); for ($_row = 1; $_row <= $rowCnt; $_row++) { for ($_column = 0; $_column <= $columnCnt; $_column++) { $cellId = $this->cell[$_column].$_row; //$cellValue = $currSheet->getCell($cellId)->getValue(); $cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); if ($cellValue instanceof \PHPExcel_RichText) { $cellValue = $cellValue->__toString(); } $data[$_row][$this->cell[$_column]] = $cellValue; } } return $data; } }