admin update for receiver clues import and add

This commit is contained in:
xxb
2021-07-07 14:45:07 +08:00
parent f5ab51d3c3
commit 71d5dfb439
4 changed files with 316 additions and 4 deletions
+157
View File
@@ -4,8 +4,13 @@ defined('BASEPATH') OR exit('No direct script access allowed');
class Clues extends HD_Controller{
private $searchTpAry = array('mobile' => '客户手机号', 'name' => '客户姓名');
protected $log_dir;
public function __construct(){
parent::__construct();
$this->log_dir = 'receiver_clues';
$this->load->model('receiver/receiver_clues_model','clues_model');
$this->load->model('receiver/receiver_clues_cfrom_model','clues_cfrom_model');
$this->load->model('receiver/receiver_status_model','status_model');
@@ -70,10 +75,107 @@ class Clues extends HD_Controller{
}
public function get(){
$id = $this->input->get('id');
if($id){
$row = $this->clues_model->get(array('id' => $id));
if(!$row){
return $this->show_json(SYS_CODE_FAIL, '记录不存在');
}
$info = array(
'name' => $row['name'],
'mobile' => $row['mobile'],
'cf_id' => $row['cf_id'],
);
$title = "编辑线索";
$action = "edit";
} else {
$info = array(
'name' => '',
'mobile' => '',
'cf_id' => '',
);
$title = "新增线索";
$action = "add";
}
$where = array('status' => 1);
$select = 'id, title';
$map_cfrom = $this->clues_cfrom_model->map('id', 'title', $where, '', 0, 0, $select);
$this->data['info'] = $info;
$this->data['cfrom_ary'] = $map_cfrom;
$this->data['action'] = $action;
$this->data['_title'] = $title;
return $this->show_view('receiver/clues/get');
}
//添加单条数据
public function add(){
$info = $this->input->post('info');
if(!$info['name']){
return $this->show_json(SYS_CODE_FAIL, '请填写姓名');
}
if(! mobile_valid($info['mobile'])){
return $this->show_json(SYS_CODE_FAIL, '手机号码不准确');
}
$add = array(
'name' => $info['name'],
'mobile' => $info['mobile'],
'cf_id' => $info['cf_id'] ? $info['cf_id'] : 0,
'c_time' => time(),
);
$ret = $this->clues_model->add($add);
if(!$ret){
debug_log("[error]# add fail; " . $this->clues_model->db->last_query(), __FUNCTION__, $this->log_dir);
return $this->show_json(SYS_CODE_FAIL, '添加失败');
}
return $this->show_json(SYS_CODE_SUCCESS, '添加成功');
}
function add_excel(){
require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php';
$res = $this->upload();
if (!$res['code']) {
return $this->show_json(0, $res['message']);
}
$file = $res['path'];
if ($res['file_ext'] == '.xls') {
$reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档
} elseif ($res['file_ext'] == '.xlsx') {
$reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档
} else {
return $this->show_json(SYS_CODE_FAIL, '文件无法识别');
}
$PHPExcel = $reader->load($file); // 文档名称
$objWorksheet = $PHPExcel->getActiveSheet();
$rowCnt = $objWorksheet->getHighestRow(); //获取总行数
if ($rowCnt > 800) {
@unlink($file);
return $this->show_json(0, '数据大于800请拆分多个表格导入');
}
$data = array();
for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容
if ($objWorksheet->getCell('A' . $_row)->getValue()) {
$data[] = array(
'name' => $objWorksheet->getCell('A' . $_row)->getValue(),
'mobile' => $objWorksheet->getCell('B' . $_row)->getValue(),
'cfrom' => $objWorksheet->getCell('C' . $_row)->getValue(),
);
}
}
$data && $data = array_unique($data); //去除重复数据
$done = $this->add_batch($data);
@unlink($file);
$this->data['load_num'] = count($data);
$this->data['done'] = $done;
return $this->show_json(SYS_CODE_SUCCESS, "成功导入{$done}");
}
//编辑单条数据
@@ -160,4 +262,59 @@ class Clues extends HD_Controller{
return $this->show_json(SYS_CODE_SUCCESS, '分配成功!');
}
private function upload()
{
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/temp/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 5120;
$config['file_name'] = 'receiver_clues' . time() . rand(1, 99999);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
return array('code' => SYS_CODE_FAIL, 'message' => $this->upload->display_errors('', ''));
} else {
$data = $this->upload->data();
return array('code' => SYS_CODE_SUCCESS, 'path' => $data['full_path'], 'file_ext' => $data['file_ext']);
}
}
/**
* 批量新增
* @param $lists
* @return int
*/
private function add_batch($lists){
$done = 0;
$adds = array();
$where = array('status' => 1);
$select = 'id, title';
$map_cfrom = $this->clues_cfrom_model->map('title', 'id', $where, '', 0, 0, $select);
foreach($lists as $v){
if(!$v['name']){
continue;
}
if(!mobile_valid($v['mobile'])){
continue;
}
$cf_id = $map_cfrom[$v['cfrom']];
$adds[] = array(
'name' => $v['name'],
'mobile' => $v['mobile'],
'cf_id' => $cf_id ? $cf_id : 0,
'c_time' => time(),
);
$done++;
}
if($adds){
$ret = $this->clues_model->add_batch($adds);
if(!$ret){
debug_log("[error] add_batch fail; " . $this->clues_model->db->last_query(), __FUNCTION__, $this->log_dir);
$done = 0;
}
}
return $done;
}
}
+88
View File
@@ -0,0 +1,88 @@
<form class="am-form am-form-horizontal ptb20 pr20 mt10" data-auto="true" method="post" style="padding-top: 10px;padding-bottom: 10px;"
action="" id="edit-form">
<div class="am-form-group">
<label class="am-para-label">姓名:</label>
<div class="am-para-input w300">
<input type="text" placeholder="输入姓名" v-model="info.name" name="name"/>
</div>
</div>
<div class="am-form-group">
<label class="am-para-label">手机号:</label>
<div class="am-para-input w300">
<input type="text" placeholder="输入手机号" v-model="info.mobile" name="mobile"/>
</div>
</div>
<div class="am-form-group">
<label class="am-para-label">来源:</label>
<div class="am-para-input w300">
<select name="cf_id" v-model="info.cf_id">
<option value="">选择来源</option>
<option :value="i" v-for="(v,i) in cfrom_ary">{{v}}</option>
</select>
</div>
</div>
<div class="am-form-group">
<div class="am-para-input">
<button class="am-btn am-btn-secondary" type="button" id="add-btn" @click="saveEdit">保存</button>
</div>
</div>
</form>
<script>
var loading = false;
var vue_obj = false;
$(function(){
vue_obj = new Vue({
el: '#edit-form',
data: {
info:{},
cfrom_ary:[],
action:""
},
mounted:function() {
var vm = this;
vm.info = <?=json_encode($info, JSON_UNESCAPED_UNICODE)?>;
vm.cfrom_ary = <?=json_encode($cfrom_ary, JSON_UNESCAPED_UNICODE)?>;
vm.action = "<?=$action?>";
},
methods:{
saveEdit:function(){
var vm = this;
if (loading) {
return;
}
loading = 1;
$.ajax({
url: "receiver/clues/" + vm.action,
type: 'post',
dataType: 'json',
data: {
info: vm.info
},
beforeSend: function () {
layer.load(1, {
shade: [0.1, '#fff'] //0.1透明度的白色背景
});
},
success: function (data) {
if (1 == data['code']) {
layer.msg(data.msg, {
icon: 1,
time: 2000
}, function () {
window.location.reload();
});
} else {
layer.msg(data.msg, {icon: 2});
}
},
complete: function () {
loading = false;
layer.closeAll('loading');
}
});
}
}
});
});
</script>
+71 -4
View File
@@ -59,14 +59,12 @@
</div>
<div class="am-form-group fl ml10">
<button type="button" id="export" class="am-btn am-btn-success am-btn-sm w100">导入</button>
<button type="button" id="import" class="am-btn am-btn-success am-btn-sm w100">导入</button>
</div>
<div class="am-form-group fl ml10">
<button type="button" class="am-btn am-btn-success am-btn-sm w100"
data-modal="/app/jdb/customers/get_customers?rid=<?= $params['rid'] ?>"
class="am-btn am-btn-primary am-btn-xs" data-title="新增客户">新增
</button>
data-modal="/receiver/clues/get" data-title="新增线索">新增</button>
</div>
</div>
</div>
@@ -114,6 +112,25 @@
<?php page_view($pager) ?>
</div>
</div>
<div id="cule-modal" style="display: none;padding: 10px">
<div class="modal-body">
<div class="text-center">
<h2><i class="fa fa-info-circle"></i> 请上传Excel文件</h2>
<p>Excel文件格式必须与模板保持一致,否则无法导入</p>
<div class="form-group-action">
<a href="/temp/clues.xlsx" target="_blank" class="btn btn-default">查看模板</a>
<label href="javascript:" class="btn btn-primary" style="margin-left: 10px;">
<form id="import-form">
<input type="file" id="clue-file" name="file" accept=".xls,.xlsx"
style="left:-9999px;position:absolute;" onchange="load_clue()">
</form>
<span>上传Excel文件</span>
</label>
</div>
</div>
</div>
</div>
</div>
<script>
require(['laydate', 'autocomplete'], function (laydate) {
@@ -155,4 +172,54 @@
return $.form.modal("/receiver/clues/get_adviser?ids=" + ids, 'open_type=modal', "分配顾问");
}
function load_clue(){
var file = $("#clue-file");
if (file.val() == '') {
layer.msg('文件是空的');
return 0;
}
var loading = layer.load(1, {
shade: [0.1,'#fff'] //0.1透明度的白色背景
});
var options = {
url: "/receiver/clues/add_excel",
type: "post",
dataType: "json",
data: {},
success: function (res) {
if(1 == res.code){
layer.msg(res.msg, {
icon: 1,
time: 2000
}, function () {
$.form.reload();
});
}
},
complete: function () {
file.val('');
layer.close(loading);
}
};
$("#import-form").ajaxSubmit(options);
}
$(function(){
$("#import").click(function(){
$("#clue-file").val('');
layer.open({
type: 1,
area: ['40%'], //宽高
content: $('#cule-modal'),
title: '导入线索',
shade: false,
btn: ['完成'],
yes: function (index) {
layer.close(index);
}
});
});
});
</script>
Binary file not shown.