2025-08-01 14:20:12 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\crmAdmin\controller;
|
|
|
|
|
|
|
|
|
|
|
|
use app\common\model\Corp;
|
2025-08-13 09:16:02 +08:00
|
|
|
|
use app\crm\model\Customer;
|
2025-08-21 17:25:37 +08:00
|
|
|
|
use app\crm\utils\Util;
|
2025-08-01 14:20:12 +08:00
|
|
|
|
use app\crmAdmin\service\CustomerService;
|
|
|
|
|
|
use support\Request;
|
|
|
|
|
|
|
|
|
|
|
|
class CustomerController extends BaseController
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 不需要头信息的方法
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected $noNeedHeader = [];
|
2025-08-05 15:16:05 +08:00
|
|
|
|
protected $noNeedHeaderData = ['export'];
|
2025-08-01 14:20:12 +08:00
|
|
|
|
|
2025-08-05 15:16:05 +08:00
|
|
|
|
protected $noNeedLogin = ['export'];
|
2025-08-01 14:20:12 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 客户列表
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function list(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
|
|
|
|
|
$corp = Corp::where(['abbr' => $request->basic_data['corp_abbr']])->first();
|
2025-08-25 18:13:16 +08:00
|
|
|
|
$param['corp_id'] = $corp->id;
|
2025-08-01 14:20:12 +08:00
|
|
|
|
// $param['corp_abbr'] = $request->basic_data['corp_abbr'];
|
|
|
|
|
|
$result = (new CustomerService())->list($param);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->success($this->page($result));
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
|
return $this->error($e->getMessage().$e->getFile().$e->getLine().$e->getTraceAsString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 客户导出
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function export(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
2025-08-01 17:18:08 +08:00
|
|
|
|
// $corp = Corp::where(['abbr' => $request->basic_data['corp_abbr']])->first();
|
2025-08-25 18:13:16 +08:00
|
|
|
|
// $param['corp_id'] = $corp->id;
|
2025-08-05 15:16:05 +08:00
|
|
|
|
|
2025-08-05 15:37:26 +08:00
|
|
|
|
$result = (new CustomerService())->export($param);
|
|
|
|
|
|
$excelContent = file_get_contents($result['filepath']);
|
2025-08-05 15:16:05 +08:00
|
|
|
|
// 生成文件名
|
|
|
|
|
|
$filename = '客户数据_' . date('Y-m-d_H-i-s') . '.xlsx';
|
|
|
|
|
|
return response()
|
|
|
|
|
|
->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
|
|
|
|
|
->withHeader('Content-Disposition', 'attachment; filename="'.$filename.'"')
|
|
|
|
|
|
->withBody($excelContent);
|
|
|
|
|
|
|
|
|
|
|
|
/*$result = (new CustomerService())->export($param);
|
2025-08-01 17:18:08 +08:00
|
|
|
|
// 检查文件是否存在
|
|
|
|
|
|
if (!file_exists($result['filepath'])) {
|
|
|
|
|
|
return $this->error('导出文件生成失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取文件信息
|
|
|
|
|
|
$fileSize = filesize($result['filepath']);
|
|
|
|
|
|
$filename = $result['filename'];
|
2025-08-01 14:20:12 +08:00
|
|
|
|
|
2025-08-01 17:18:08 +08:00
|
|
|
|
// 设置响应头
|
|
|
|
|
|
$response = response()->file($result['filepath']);
|
|
|
|
|
|
$response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
|
|
|
|
$response->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
|
|
|
|
|
$response->header('Content-Length', $fileSize);
|
|
|
|
|
|
$response->header('Cache-Control', 'no-cache, must-revalidate');
|
|
|
|
|
|
$response->header('Pragma', 'no-cache');
|
2025-08-05 15:16:05 +08:00
|
|
|
|
|
|
|
|
|
|
return $response;*/
|
|
|
|
|
|
|
2025-08-01 14:20:12 +08:00
|
|
|
|
} catch (\Throwable $e) {
|
2025-08-01 17:18:08 +08:00
|
|
|
|
return $this->error($e->getMessage());
|
2025-08-01 14:20:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-14 20:59:20 +08:00
|
|
|
|
* 审核客户
|
2025-08-01 14:20:12 +08:00
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function audit(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
|
|
|
|
|
if (empty($param['id'])) {
|
|
|
|
|
|
return $this->error('ID'.trans('not_null'));
|
|
|
|
|
|
}
|
2025-08-01 17:18:08 +08:00
|
|
|
|
|
|
|
|
|
|
if (!in_array($param['scene'],['delete','report'])) {
|
2025-08-01 14:20:12 +08:00
|
|
|
|
return $this->error('scene 不合法');
|
|
|
|
|
|
}
|
|
|
|
|
|
$result = (new CustomerService())->audit($param);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->success($result);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
|
return $this->error($e->getMessage().$e->getFile().$e->getLine().$e->getTraceAsString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-04 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 批量转让
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function transfer(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
|
|
|
|
|
if (empty($param['customer_id'])) {
|
|
|
|
|
|
return $this->error('customer_id'.trans('not_null'));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (empty($param['user_id'])) {
|
|
|
|
|
|
return $this->error('user_id'.trans('not_null'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = (new CustomerService())->transfer($param);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->success($result);
|
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
|
return $this->error($th->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-13 09:16:02 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 移除
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function delete(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
2025-11-17 10:05:29 +08:00
|
|
|
|
$scene = $param['scene'] ?? '';
|
|
|
|
|
|
|
|
|
|
|
|
if($scene != 'force' && empty($param['status_desc'])){
|
|
|
|
|
|
return $this->error( '移除原因不能为空');
|
|
|
|
|
|
}
|
2025-08-13 09:16:02 +08:00
|
|
|
|
if (empty($param['customer_id'])) {
|
|
|
|
|
|
return $this->error('customer_id'.trans('not_null'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-19 16:36:36 +08:00
|
|
|
|
$customer_id = explode(',', $param['customer_id']);
|
2025-08-21 17:25:37 +08:00
|
|
|
|
$query = Customer::whereIn('id',$customer_id)->withTrashed();
|
|
|
|
|
|
if(!$query->exists()){
|
2025-08-13 09:16:02 +08:00
|
|
|
|
return $this->error( trans('fail'));
|
|
|
|
|
|
}
|
2025-08-21 17:25:37 +08:00
|
|
|
|
if ($scene === 'list') { //管理端客户列表移除
|
|
|
|
|
|
$res = $query->update(['status' => 4, 'deleted_at' => time()]);
|
|
|
|
|
|
} elseif ($scene === 'force') { //强删除
|
|
|
|
|
|
$res = $query->forceDelete();
|
|
|
|
|
|
} else { //公海软删除
|
|
|
|
|
|
$res = $query->update(['status' => 3, 'deleted_at' => time()]);
|
2025-08-19 16:36:36 +08:00
|
|
|
|
}
|
2025-08-13 09:16:02 +08:00
|
|
|
|
|
|
|
|
|
|
return $this->success($res);
|
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
|
return $this->error($th->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-21 17:25:37 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 客户恢复
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function restore(Request $request)
|
|
|
|
|
|
{
|
2025-08-21 17:50:42 +08:00
|
|
|
|
try {
|
|
|
|
|
|
$id = $request->input('id', '');
|
|
|
|
|
|
if (Util::isEmpty($id)) {
|
|
|
|
|
|
return $this->error(trans('id_empty'));
|
|
|
|
|
|
}
|
|
|
|
|
|
$ids = explode(',', $id);
|
|
|
|
|
|
$query = Customer::withTrashed()->whereIn('id', $ids);
|
|
|
|
|
|
if (!$query->exists()) {
|
|
|
|
|
|
return $this->error(trans('fail'));
|
|
|
|
|
|
}
|
|
|
|
|
|
// 恢复软删除(清空 deleted_at)
|
|
|
|
|
|
$query->restore();
|
|
|
|
|
|
// 恢复为在库状态,放入公海
|
|
|
|
|
|
$res = Customer::whereIn('id', $ids)->update(['status' => 1, 'user_id' => 0]);
|
2025-08-21 17:25:37 +08:00
|
|
|
|
return $this->success($res, trans('success'));
|
2025-08-21 17:50:42 +08:00
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
|
return $this->error($th->getMessage());
|
2025-08-21 17:25:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-04 11:17:38 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 客户详情
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function detail(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
|
|
|
|
|
if (empty($param['id'])) {
|
|
|
|
|
|
return $this->error('id'.trans('not_null'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = (new CustomerService())->detail($param);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->success($result);
|
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
|
return $this->error($th->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-13 09:16:02 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 客户详情
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function follow(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
|
|
|
|
|
if (empty($param['id'])) {
|
|
|
|
|
|
return $this->error('id'.trans('not_null'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = (new CustomerService())->follow($param);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->success($result);
|
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
|
return $this->error($th->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-16 16:12:06 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 离职员工客户分配
|
|
|
|
|
|
* @param Request $request
|
|
|
|
|
|
* @return \support\Response
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function allocation(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$param = $request->all();
|
|
|
|
|
|
if (empty($param['corp_id'])) {
|
|
|
|
|
|
return $this->error('corp_id'.trans('not_null'));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (empty($param['user_id'])) {
|
|
|
|
|
|
return $this->error('user_id'.trans('not_null'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$result = (new CustomerService())->allocation($param);
|
|
|
|
|
|
|
2025-08-18 16:54:24 +08:00
|
|
|
|
return $this->success($result,'成功分配'.$result['userCount'].'位离职用户的'.$result['customerCount'].'位客户');
|
2025-08-16 16:12:06 +08:00
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
|
return $this->error($th->getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-01 14:20:12 +08:00
|
|
|
|
}
|