You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wkcrm/application/crm/controller/Activity.php

421 lines
14 KiB

4 years ago
<?php
/**
* 活动控制器
*
* @author qifan
* @date 2020-12-09
*/
3 years ago
4 years ago
namespace app\crm\controller;
use app\admin\controller\ApiCommon;
use app\crm\logic\ActivityLogic;
use think\Hook;
use think\Request;
class Activity extends ApiCommon
{
/**
* 用于判断权限
* @permission 无限制
* @allow 登录用户可访问
* @other 其他根据系统设置
**/
public function _initialize()
{
$action = [
3 years ago
'permission' => [],
'allow' => ['index', 'save', 'read', 'update', 'delete', 'getphrase', 'setphrase', 'getrecordauth', 'excelimport', 'excelexport', 'exceldownload']
4 years ago
];
3 years ago
Hook::listen('check_auth', $action);
4 years ago
$request = Request::instance();
$a = strtolower($request->action());
if (!in_array($a, $action['permission'])) {
parent::_initialize();
}
}
3 years ago
4 years ago
/**
* 活动列表
*
* @param ActivityLogic $activityLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function index(ActivityLogic $activityLogic)
{
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
3 years ago
4 years ago
$data = $activityLogic->index($param);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 创建活动【跟进记录】
*
* @param ActivityLogic $activityLogic
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function save(ActivityLogic $activityLogic)
{
if (!checkPerByAction('crm', 'activity', 'save')) {
return resultArray(['error' => '你没有创建跟进记录的权限!']);
}
3 years ago
if (empty($this->param['activity_type'])) return resultArray(['error' => '缺少模块类型!']);
4 years ago
if (empty($this->param['activity_type_id'])) return resultArray(['error' => '缺少活动类型ID']);
3 years ago
if (empty($this->param['content'])) return resultArray(['error' => '请填写跟进内容!']);
4 years ago
if (!empty($this->param['next_time']) && strtotime($this->param['next_time']) < time()) {
return resultArray(['error' => '下次联系时间不能在当前时间之前!']);
}
3 years ago
4 years ago
$param = $this->param;
$param['user_id'] = $this->userInfo['id'];
3 years ago
4 years ago
if (!$activityLogic->save($param)) return resultArray(['error' => '操作失败!']);
return resultArray(['data' => '操作成功!']);
}
3 years ago
4 years ago
/**
* 活动详情
*
* @param ActivityLogic $activityLogic
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function read(ActivityLogic $activityLogic)
{
if (!checkPerByAction('crm', 'activity', 'read')) {
return resultArray(['error' => '你没有查看跟进记录的权限!']);
}
if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
3 years ago
4 years ago
$data = $activityLogic->read($this->param['activity_id']);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 编辑活动【跟进记录】
*
* @param ActivityLogic $activityLogic
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function update(ActivityLogic $activityLogic)
{
if (!checkPerByAction('crm', 'activity', 'update')) {
return resultArray(['error' => '你没有编辑跟进记录的权限!']);
}
3 years ago
if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
if (empty($this->param['activity_type'])) return resultArray(['error' => '缺少活动类型!']);
4 years ago
if (empty($this->param['activity_type_id'])) return resultArray(['error' => '缺少活动类型ID']);
3 years ago
if (empty($this->param['content'])) return resultArray(['error' => '请填写跟进内容!']);
$param = $this->param;
4 years ago
$userId = $this->userInfo['id'];
3 years ago
4 years ago
if (!$activityLogic->update($param)) return resultArray(['error' => '操作失败!']);
3 years ago
4 years ago
$data = $activityLogic->getFollowData($param['activity_id'], $userId);
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 删除活动【跟进记录】
*
* @param ActivityLogic $activityLogic
* @return \think\response\Json
*/
public function delete(ActivityLogic $activityLogic)
{
3 years ago
$userInfo = $this->userInfo;
4 years ago
if (!checkPerByAction('crm', 'activity', 'delete')) {
return resultArray(['error' => '你没有删除跟进记录的权限!']);
}
if (empty($this->param['activity_id'])) return resultArray(['error' => '请选择跟进记录!']);
3 years ago
if (!$activityLogic->delete($this->param['activity_id'], $userInfo['id'])) return resultArray(['error' => '操作失败!']);
4 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
4 years ago
/**
* 获取常用语
*
* @param ActivityLogic $activityLogic
* @return \think\response\Json
*/
public function getPhrase(ActivityLogic $activityLogic)
{
$data = $activityLogic->getPhrase();
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 设置常用语
*
* @param ActivityLogic $activityLogic
* @return \think\response\Json
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function setPhrase(ActivityLogic $activityLogic)
{
3 years ago
if (empty($this->param['phrase'])) return resultArray(['error' => '缺少常用语数据!']);
4 years ago
if (!is_array($this->param['phrase'])) return resultArray(['error' => '参数格式错误!']);
3 years ago
4 years ago
if (!$activityLogic->setPhrase($this->param['phrase'])) return resultArray(['error' => '操作失败!']);
3 years ago
4 years ago
return resultArray(['data' => '操作成功!']);
}
3 years ago
4 years ago
/**
* 跟进记录权限
*
* @return \think\response\Json
*/
public function getRecordAuth()
{
$data = [
3 years ago
'index' => checkPerByAction('crm', 'activity', 'index'),
'read' => checkPerByAction('crm', 'activity', 'read'),
'save' => checkPerByAction('crm', 'activity', 'save'),
4 years ago
'update' => checkPerByAction('crm', 'activity', 'update'),
'delete' => checkPerByAction('crm', 'activity', 'delete'),
];
3 years ago
4 years ago
return resultArray(['data' => $data]);
}
3 years ago
4 years ago
/**
* 导入模板下载
* @author alvin guogaobo
* @version 1.0 版本号
* @since 2021/4/10 0010 16:01
*/
3 years ago
public function excelDownload($save_path = '')
{
4 years ago
$param = $this->param;
$excelModel = new \app\admin\model\Excel();
3 years ago
$field_list = $this->importData($param);
$types = 'crm_activity';
$excelModel->excelImportDownload($field_list, $types, $save_path);
4 years ago
}
/**
* 导入导出模板标题
* @param $param
*
* @author alvin guogaobo
* @version 1.0 版本号
* @since 2021/4/13 0013 11:15
*/
3 years ago
public function importData($param)
{
$field_list = [];
switch ($param['crmType']) {
4 years ago
case 1 :
$field = [
'2' => [
'name' => '所属线索',
'field' => 'activity_type_id',
3 years ago
'form_type' => 'leads_id',
4 years ago
'is_null' => 1,
]
];
3 years ago
$name = '所属线索';
4 years ago
break;
case 3:
$field = [
'2' => [
'name' => '所属联系人',
'field' => 'activity_type_id',
3 years ago
'form_type' => 'contacts_id',
4 years ago
'is_null' => 1,
],
];
3 years ago
$name = '所属联系人';
4 years ago
break;
case 5:
$field = [
'2' => [
'name' => '所属商机',
'field' => 'activity_type_id',
3 years ago
'form_type' => 'business_id',
4 years ago
'is_null' => 1,
3 years ago
],
4 years ago
];
3 years ago
$name = '所属商机';
4 years ago
break;
case 6:
$field = [
'2' => [
'name' => '所属合同',
'field' => 'activity_type_id',
3 years ago
'form_type' => 'contract_id',
4 years ago
'is_null' => 1,
],
];
3 years ago
$name = '所属合同';
4 years ago
break;
case 2:
3 years ago
$field = [
4 years ago
'2' => [
'name' => '所属客户',
'field' => 'activity_type_id',
3 years ago
'form_type' => 'customer_id',
4 years ago
'is_null' => 1,
],
];
3 years ago
$name = '所属客户';
4 years ago
break;
}
$record_type = db('crm_config')->where(['name' => 'record_type'])->find();
if ($record_type) {
$arr = json_decode($record_type['value']);
}
4 years ago
$fields = [
'0' => [
'name' => '跟进内容',
'field' => 'content',
'form_type' => 'text',
'is_null' => 1,
],
'1' => [
'name' => '创建人',
'field' => 'create_user_id',
'form_type' => 'user',
'is_null' => 1,
],
'2' => [
'name' => '跟进时间-例:2020-2-1',
'field' => 'next_time',
'form_type' => 'datetime',
],
3 years ago
'3' => [
4 years ago
'name' => '跟进方式',
'field' => 'category',
3 years ago
'form_type' => 'select',
'setting' =>$arr
4 years ago
],
];
// 导入的字段列表
3 years ago
if ($param['is_excel']) {
$field_lists = [
'0' => ['name' => $name, 'field' => 'activity_type_name'],
4 years ago
'1' => ['name' => '跟进内容', 'field' => 'content'],
'2' => ['name' => '创建人', 'field' => 'create_user_name'],
'3' => ['name' => '跟进时间', 'field' => 'create_time'],
3 years ago
'4' => ['name' => '跟进方式', 'field' => 'category'],
'5' => ['name' => '下次联系时间', 'field' => 'next_time']
4 years ago
];
3 years ago
if ($param['crmType'] == 2) {
$fields = [
'6' => ['name' => '相关联系人', 'field' => 'contacts_ids'],
'7' => ['name' => '相关商机', 'field' => 'business_ids'],];
$field_list = array_merge($field_lists, $fields);
} else {
$field_list =$field_lists;
}
} else {
$first_array = array_splice($fields, 2, 0, $field);
$field_list = $fields;
if ($param['crmType'] == 2) {
$field = [
'5' => [
'name' => '相关联系人',
'field' => 'contacts_ids',
'form_type' => 'contacts_id',
],
'6' => [
'name' => '相关商机',
'field' => 'business_ids',
'form_type' => 'business_id',
]
];
$field_list = array_merge($field_list, $field);
4 years ago
}
}
return $field_list;
}
3 years ago
4 years ago
/**
* 导入数据
*
* @author alvin guogaobo
* @version 1.0 版本号
* @since 2021/4/10 0010 16:27
*/
3 years ago
public function excelImport()
{
4 years ago
$param = $this->param;
3 years ago
$userInfo = $this->userInfo;
$param['user_id'] = $userInfo['id'];
$field_list = $this->importData($param);
4 years ago
$excelModel = new \app\admin\model\Excel();
$file = request()->file('file');
3 years ago
switch ($param['crmType']) {
4 years ago
case 1 :
3 years ago
$param['types'] = 'crm_leads';
$param['activity_type'] = 1;
4 years ago
break;
case 3:
3 years ago
$param['types'] = 'crm_contacts';
$param['activity_type'] = 3;
4 years ago
break;
case 5:
3 years ago
$param['types'] = 'crm_business';
$param['activity_type'] = 5;
4 years ago
break;
case 6:
3 years ago
$param['types'] = 'crm_contract';
$param['activity_type'] = 6;
4 years ago
break;
case 2:
3 years ago
$param['types'] = 'crm_customer';
$param['activity_type'] = 2;
4 years ago
break;
}
3 years ago
$res = $excelModel->ActivityImport($file, $field_list, $param, $this);
4 years ago
if (!$res) {
return resultArray(['error' => $excelModel->getError()]);
}
return resultArray(['data' => $excelModel->getError()]);
}
/**
* 导出跟进记录
* action 列表分辨是否导出
* label 导出类型 合同 客户 联系人
*
* @author alvin guogaobo
* @version 1.0 版本号
* @since 2021/4/13 0013 11:32
*/
3 years ago
public function excelExport()
{
$activityLogic = new ActivityLogic();
$indexLogic = new \app\crm\logic\IndexLogic();
$userInfo = $this->userInfo;
4 years ago
$param = $this->param;
3 years ago
$param['action'] = 'crm_activity';
$param['is_excel'] = 1;
$param['id'] = $userInfo['id'];
$list = $indexLogic->activityList($param);
$field_list = $this->importData($param);
$data = $activityLogic->excelExport($field_list, $list);
4 years ago
return $data;
}
4 years ago
}