123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Http\Requests\Admin\Custom as Request;
- use App\Models\City;
- use App\Models\Custom as Model;
- use App\Models\CustomAddr;
- use App\Models\CustomScore;
- use Illuminate\Support\Carbon;
- use App\Models\WeiBan\Follow as WeiBanFollow;
- use App\Models\FilesManager;
- use App\Models\WeiBan\External as WeiBanExternal;
- use PhpOffice\PhpSpreadsheet\Cell\DataType;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Style\Fill;
- /**
- * 客户管理
- *
- * @author 刘相欣
- *
- */
- class Custom extends Auth{
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','用户管理');
- $this->assign('breadcrumb2','客户管理');
- }
- /**
- * 列表页
- *
- * */
- public function index(Model $Model,CustomScore $CustomScore,WeiBanFollow $WeiBanFollow,City $City){
- // 接受参数
- $code = request('custom_code','');
- $phone = request('phone','');
- $username = request('username','');
- $weibanId = request('weiban_extid','');
- $cityId = request('city_id',0);
- $status = request('status');
- $startTime = request('start_time','');
- // 编码转ID
- $uid = $Model->codeToId($code);
- // 查询条件
- $map = [];
- // 编码ID
- if( $uid ) $map[] = ['uid','=',$uid];
- if( $phone ) $map[] = ['phone','=',$phone];
- if( $username ) $map[] = ['username','=',$username];
- if( $cityId ) $map[] = ['city_id','=',$cityId];
- if( $weibanId ) $map[] = ['weiban_extid','=',$weibanId];
- if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
- if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
- if( !is_null($status) ) $map[] = ['status','=',$status];
- // 查询数据
- $list = $Model->query()->where($map)->orderByDesc('uid')->paginate(config('page_num',10))->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 城市名
- $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
- // id转编号
- $value['custom_code'] = $Model->idToCode($value['uid']);
- // id转编号
- $value['custom_score'] = $CustomScore->getCustomScore($value['uid']);
- // 如果不存在微伴ID
- if( !$value['weiban_extid'] ) {
- // 通过手机号查询注册的账号
- $value['weiban_extid'] = (string) $WeiBanFollow->query()->where([['phone_number','=',$value['phone']]])->value('weiban_extid');
- // 如果存在的话,修正
- if( $value['weiban_extid'] ) $Model->edit($value['uid'],['weiban_extid'=>$value['weiban_extid']]);
- }
- // 重组
- $list[$key] = $value;
- }
- // 获取列表
- $cityList = $City->getCityList();
- // 分配数据
- $this->assign('cityList',$cityList);
- // 分配数据
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $this->assign('list',$list);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 添加
- *
- * */
- public function add(Request $request,Model $Model){
- if( request()->isMethod('post') ){
- // 验证参数
- $request->scene('add')->validate();
- // 接收数据
- // 接收数据
- $data['username'] = request('username','');
- $data['phone'] = request('phone','');
- // 写入数据表
- $uid = $Model->add($data);
- // 如果操作失败
- if( !$uid ) return json_send(['code'=>'error','msg'=>'新增失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,1,[],$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
- }
- // 分配数据
- $this->assign('crumbs','新增');
- // 加载模板
- return $this->fetch();
- }
- /**
- * 修改
- *
- * */
- public function edit(Request $request,Model $Model,City $City){
- // 接收参数
- $uid = request('uid',0);
- // 查询用户
- $oldData = $Model->where(['uid'=>$uid])->first();
- // 修改
- if(request()->isMethod('post')){
- // 验证参数
- $request->scene('edit')->validate();
- // 如果用户不存在
- if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
- // 转数组
- $oldData = $oldData->toArray();
- // 接收数据
- $data['username'] = request('username','');
- $data['phone'] = request('phone','');
- $data['city_id'] = request('city_id',0);
- $data['weiban_extid'] = request('weiban_extid','');
- // 写入数据表
- $result = $Model->edit($uid,$data);
- // 如果操作失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'新增失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,$data);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
- }
- // 错误告知
- if( !$oldData ) return $this->error('查无数据');
- // 获取列表
- $cityList = $City->getCityList();
- // 分配数据
- $this->assign('cityList',$cityList);
- $this->assign('oldData',$oldData);
- $this->assign('crumbs','修改');
- // 加载模板
- return $this->fetch();
- }
- /**
- * 修改状态
- *
- * */
- public function set_status(Request $request,Model $Model){
- // 验证参数
- $request->scene('set_status')->validate();
- // 设置状态
- $uid = request('uid',0);
- $status = request('status',0);
- // 查询用户
- $oldData = $Model->where(['uid'=>$uid])->first();
- // 如果用户不存在
- if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
- // 执行修改
- $result = $Model->edit($uid,['status'=>$status]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['status'=>$status]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
- /**
- * 修改状态
- *
- * */
- public function set_manager(Request $request,Model $Model){
- // 验证参数
- $request->scene('set_manager')->validate();
- // 设置状态
- $uid = request('uid',0);
- $isManager = request('is_manager',0);
- // 查询用户
- $oldData = $Model->where(['uid'=>$uid])->first();
- // 如果用户不存在
- if( !$oldData ) return json_send(['code'=>'error','msg'=>'用户不存在']);
- // 执行修改
- $result = $Model->edit($uid,['is_manager'=>$isManager]);
- // 提示新增失败
- if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
- // 记录行为
- $this->addAdminHistory(admin('uid'),$Model->getTable(),$uid,2,$oldData,['is_manager'=>$isManager]);
- // 告知结果
- return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
- }
- /**
- * 表格导入
- *
- * */
- public function import_execl( Request $request,Model $Model,FilesManager $FilesManager,City $City,CustomAddr $CustomAddr,WeiBanExternal $WeiBanExternal){
- // 验证参数
- $request->scene('import_execl')->validate();
- // 获取表格信息
- $file = request()->file('custom_file');
- // 返回结果
- $sheetList = $FilesManager->excelToCustom($file);
- // 如果不存在结果
- if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
- // 循环表格数据
- foreach ($sheetList as $value) {
- // 获取城市ID
- $value['city_id'] = 0;
- // 存在城市名称,查询城市ID
- if( $value['contact_city'] ) {
- // 获取城市ID
- $value['city_id'] = (int) $City->getIdByName($value['contact_city']);
- // 如果城市不存在的话
- if ( !$value['city_id'] ) return json_send(['code'=>'error','msg'=>$value['contact_city'].' => 未找到匹配的城市,请核对城市全称']);
- }
- // 获取手机号,查询是否用客户
- $custom = $Model->getOneByPhone($value['phone']);
- // 组装数据
- $data = ['weiban_extid'=>$value['weiban_extid'],'username'=>$value['username']];
- // 如果客户不存在
- if( !$custom ) $data['phone'] = $value['phone'];
- // 如果存在城市ID,才修改城市
- if( $value['city_id'] ) $data['city_id'] = $value['city_id'];
- // 如果存在手机号
- $uid = $custom ? $Model->edit($custom['uid'],$data) : $Model->add($data);
- // 如果客户存在
- if( !$uid ) return json_send(['code'=>'error','msg'=>$value['username'].'【'.$value['phone'].'】'.'用户创建或者更新失败']);
- // 存在详细地址,才创建地址库
- if( $value['contact_name'] && $value['contact_phone'] && $value['contact_province'] && $value['contact_city'] && $value['contact_area'] && $value['contact_addr'] ) {
- // 收件地址是否存在
- $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first();
- // 如果不存在地址
- if( !$oldAddr ) $CustomAddr->add(['custom_uid'=>$uid,'contact_name'=>$value['contact_name'],'contact_shop'=>$value['contact_shop'],'contact_phone'=>$value['contact_phone'],'contact_province'=>$value['contact_province'],'contact_city'=>$value['contact_city'],'contact_area'=>$value['contact_area'],'contact_addr'=>$value['contact_addr']]);
- }
- }
- // 提示成功
- return json_send(['code'=>'success','msg'=>'批量导入成功','path'=>'']);
- }
- /**
- * 导出表格
- *
- * */
- public function down_excel(Model $Model,City $City){
- // 接受参数
- $code = request('custom_code','');
- $phone = request('phone','');
- $username = request('username','');
- $weibanId = request('weiban_extid','');
- $cityId = request('city_id',0);
- $status = request('status');
- $startTime = request('start_time','');
- // 编码转ID
- $uid = $Model->codeToId($code);
- // 查询条件
- $map = [];
- // 编码ID
- if( $uid ) $map[] = ['uid','=',$uid];
- if( $phone ) $map[] = ['phone','=',$phone];
- if( $username ) $map[] = ['username','=',$username];
- if( $cityId ) $map[] = ['city_id','=',$cityId];
- if( $weibanId ) $map[] = ['weiban_extid','=',$weibanId];
- if( $startTime ) $map[] = ['insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
- if( $startTime ) $map[] = ['insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
- if( !is_null($status) ) $map[] = ['status','=',$status];
- // 查询数据
- $list = $Model->query()->where($map)->orderByDesc('uid')->get()->toArray();
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 城市名
- $value['city_name'] = $value['city_id'] ? $City->getOne($value['city_id'],'name') : '';
- // id转编号
- $value['custom_code'] = $Model->idToCode($value['uid']);
- // 重组
- $list[$key] = $value;
- }
- try {
- // 去下载
- $this->toDown($list);
- } catch (\Throwable $th) {
- echo $th->getMessage();
- }
- }
- /**
- * 去下载
- */
- private function toDown($data){
- $list = [];
- foreach ($data as $key=>$value) {
- // 单元格内容写入
- $list[$key]['custom_code'] = $value['custom_code'];
- $list[$key]['username'] = $value['username'];
- $list[$key]['phone'] = $value['phone'];
- $list[$key]['weiban_extid'] = $value['weiban_extid'];
- $list[$key]['city_name'] = $value['city_name'];
- $list[$key]['status'] = $value['status']?'禁用':'正常';
- $list[$key]['insert_time'] = date('Y-m-d H:i:s',$value['insert_time']);
- $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
- }
- try {
- $config = [
- 'path' =>public_path().'/uploads/' // xlsx文件保存路径
- ];
- $excel = new \Vtiful\Kernel\Excel($config);
- $header = [
- '客户编码',
- '客户昵称',
- '联系方式',
- '微伴ID',
- '客户城市',
- '客户状态',
- '创建时间',
- '更新时间',
- ];
- $filePath = $excel->fileName('user01.xlsx', 'sheet1')
- ->header($header)
- ->data($list)
- ->output();
- $filename = '客户列表.xlsx';
- header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- header('Content-Disposition: attachment;filename="' . $filename . '"');
- header('Content-Length: ' . filesize($filePath));
- header('Content-Transfer-Encoding: binary');
- header('Cache-Control: must-revalidate');
- header('Cache-Control: max-age=0');
- header('Pragma: public');
- ob_clean();
- flush();
- if (copy($filePath, 'php://output') === false) {
- return json_send(['code'=>'error','msg'=>'下载失败']);
- }
- @unlink($filePath);
- return json_send(['code'=>'success','msg'=>'下载成功','path'=>'']);
- }catch (\Exception $exception) {
- return json_send(['code'=>'error','msg'=>'下载失败']);
- }
- }
- /**
- * 设置表格样式
- *
- */
- private function setStyle(Spreadsheet $spreadsheet){
- // 选择当前活动的工作表
- $sheet = $spreadsheet->getActiveSheet();
- // 宽
- $sheet->getColumnDimension('A')->setWidth(15);
- $sheet->getColumnDimension('B')->setWidth(15);
- $sheet->getColumnDimension('C')->setWidth(15);
- $sheet->getColumnDimension('D')->setWidth(30);
- $sheet->getColumnDimension('E')->setWidth(15);
- $sheet->getColumnDimension('F')->setWidth(15);
- $sheet->getColumnDimension('G')->setWidth(20);
- $sheet->getColumnDimension('H')->setWidth(20);
- // 默认高度
- $sheet->getDefaultRowDimension()->setRowHeight(18);
- // 加粗第一行
- $sheet->getStyle('A:H')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
- $sheet->getStyle('A1:H1')->getFont()->setBold(true);
- $sheet->getStyle('A1:H1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
- // 设置表格标题
- $sheet
- ->setCellValue('A1', '客户编码')
- ->setCellValue('B1', '客户昵称')
- ->setCellValue('C1', '联系方式')
- ->setCellValue('D1', '微伴ID')
- ->setCellValue('E1', '客户城市')
- ->setCellValue('F1', '客户状态')
- ->setCellValue('G1', '创建时间')
- ->setCellValue('H1', '更新时间');
- // 返回结果
- return $sheet;
- }
- }
|