123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace app\common\service;
- use app\admin\library\Auth;
- use app\admin\model\Admin;
- use app\common\model\Mobile;
- use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
- use PhpOffice\PhpSpreadsheet\Reader\Csv;
- use PhpOffice\PhpSpreadsheet\Reader\Xls;
- use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
- use think\Db;
- use think\Exception;
- use think\exception\PDOException;
- class MobileImport{
- public static function import($file,$admin_id,$type=1){
- if (!$file) {
- throw_user(__('Parameter %s can not be empty', 'file'));
- }
- $file=parse_url($file,PHP_URL_PATH);
- $filePath = ROOT_PATH . DS . 'public' . DS . $file;
- if (!is_file($filePath)) {
- throw_user(__('No results were found'));
- }
- //实例化reader
- $ext = pathinfo($filePath, PATHINFO_EXTENSION);
- if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
- throw_user(__('Unknown data format'));
- }
- if ($ext === 'csv') {
- $file = fopen($filePath, 'r');
- $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
- $fp = fopen($filePath, "w");
- $n = 0;
- while ($line = fgets($file)) {
- $line = rtrim($line, "\n\r\0");
- $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
- if ($encoding != 'utf-8') {
- $line = mb_convert_encoding($line, 'utf-8', $encoding);
- }
- if ($n == 0 || preg_match('/^".*"$/', $line)) {
- fwrite($fp, $line . "\n");
- } else {
- fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
- }
- $n++;
- }
- fclose($file) || fclose($fp);
- $reader = new Csv();
- } elseif ($ext === 'xls') {
- $reader = new Xls();
- } else {
- $reader = new Xlsx();
- }
- //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
- $importHeadType = 'comment';
- /*$table = (new Mobile)->getTable();
- $database = \think\Config::get('database.database');*/
- //$fieldArr = [];
- /* $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
- foreach ($list as $k => $v) {
- if ($importHeadType == 'comment') {
- $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
- } else {
- $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
- }
- }*/
- $fieldArr=[
- "手机号"=>'no',
- "卡名称"=>'name',
- "省份"=>'province',
- "归属地"=>'city',
- "运营商"=>'network',
- "卡品牌"=>'brand',
- "原价"=>'amount_original',
- "底价"=>'amount_di',
- "售价"=>'amount_base',
- "预存话费"=>'amount_charge',
- "备注(仅我可见)"=>'remark',
- "号码状态"=>'status',
- "供应商"=>'proxy_id',
- ];
- //dd($fieldArr);
- $infoArr=[
- '免流APP'=>'free_app',
- '资费套餐'=>'describe',
- '注意事项'=>'content',
- '每年流量(G'=>'flow_year',
- '费用'=>'fee',
- '首月免月租'=>'first_month_free',
- '前多少名免单'=>'flow_free_limit',
- '简介'=>'describe',
- ];
- //加载文件
- $insert = [];
- try {
- if (!$PHPExcel = $reader->load($filePath)) {
- throw_user(__('Unknown data format'));
- }
- $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
- $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
- $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
- $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
- $fields = [];
- for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
- $fields[] = preg_replace("/\s+/",'',$val);
- }
- }
- for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
- $values = [];
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
- $values[] = is_null($val) ? '' : $val;
- }
- $row = [];
- $row_info = [];
- $temp = array_combine($fields, $values);
- foreach ($temp as $k => $v) {
- if (isset($fieldArr[$k]) && $k !== '') {
- $row[$fieldArr[$k]] = $v;
- }
- if(isset($infoArr[$k]) && $k!==''){
- $row_info[$infoArr[$k]]=$v;
- }
- }
- if(!$row['no'] || Mobile::withTrashed()->where('no',$row['no'])->find()){
- continue;
- }
- Db::startTrans();
- if($type==1) {
- $row['proxy_id'] = Admin::where('nickname', $row['proxy_id'])->value('id');
- if (!$row['proxy_id']) {
- Db::rollback();
- continue;
- }
- }
- $row['admin_id']=$admin_id;
- $row['type']=$type;
- $row['status']=0;
- $mobile=(new Mobile);
- $mobile->allowField(true)->save($row);
- if(!$mobile){
- Db::rollback();
- continue;
- }
- $info=$mobile->info()->save($row_info);
- if(!$info){
- Db::rollback();
- continue;
- }
- Db::commit();
- }
- } catch (Exception $exception) {
- throw $exception;
- throw_user($exception->getMessage());
- }
- }
- }
|