Leads.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 线索
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\controller;
  8. use app\admin\controller\ApiCommon;
  9. use app\crm\traits\StarTrait;
  10. use think\Db;
  11. use think\Hook;
  12. use think\Request;
  13. class Leads extends ApiCommon
  14. {
  15. use StarTrait;
  16. /**
  17. * 用于判断权限
  18. * @permission 无限制
  19. * @allow 登录用户可访问
  20. * @other 其他根据系统设置
  21. **/
  22. public function _initialize()
  23. {
  24. $action = [
  25. 'permission' => ['exceldownload', 'setfollow'],
  26. 'allow' => ['system', 'count']
  27. ];
  28. Hook::listen('check_auth', $action);
  29. $request = Request::instance();
  30. $a = strtolower($request->action());
  31. if (!in_array($a, $action['permission'])) {
  32. parent::_initialize();
  33. }
  34. }
  35. /**
  36. * 线索列表
  37. * @return
  38. * @author Michael_xu
  39. */
  40. public function index()
  41. {
  42. $leadsModel = model('Leads');
  43. $param = $this->param;
  44. $userInfo = $this->userInfo;
  45. $param['user_id'] = $userInfo['id'];
  46. $data = $leadsModel->getDataList($param);
  47. return resultArray(['data' => $data]);
  48. }
  49. /**
  50. * 线索公海
  51. * @return
  52. * @author Michael_xu
  53. */
  54. public function pool()
  55. {
  56. $leadsModel = model('Leads');
  57. $param = $this->param;
  58. //线索公海条件(没有负责人或已经到期)
  59. $end_time = '';
  60. $param['update_time'] = array();
  61. $data = $leadsModel->getDataList($param);
  62. return resultArray(['data' => $data]);
  63. }
  64. /**
  65. * 添加线索
  66. * @param \think\Request $request
  67. * @return
  68. * @author Michael_xu
  69. */
  70. public function save()
  71. {
  72. $leadsModel = model('Leads');
  73. $param = $this->param;
  74. $userInfo = $this->userInfo;
  75. $param['create_user_id'] = $userInfo['id'];
  76. $param['owner_user_id'] = $userInfo['id'];
  77. if ($leadsModel->createData($param)) {
  78. return resultArray(['data' => '添加成功']);
  79. } else {
  80. return resultArray(['error' => $leadsModel->getError()]);
  81. }
  82. }
  83. /**
  84. * 线索详情
  85. * @param
  86. * @return
  87. * @author Michael_xu
  88. */
  89. public function read()
  90. {
  91. $leadsModel = model('Leads');
  92. $userModel = new \app\admin\model\User();
  93. $param = $this->param;
  94. $userInfo = $this->userInfo;
  95. $data = $leadsModel->getDataById($param['id'], $userInfo['id']);
  96. //判断权限
  97. $auth_user_ids = $userModel->getUserByPer('crm', 'leads', 'read');
  98. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  99. //无权限
  100. $authData['dataAuth'] = (int)0;
  101. return resultArray(['data' => $authData]);
  102. }
  103. if (!$data) {
  104. return resultArray(['error' => $leadsModel->getError()]);
  105. }
  106. return resultArray(['data' => $data]);
  107. }
  108. /**
  109. * 编辑线索
  110. * @param
  111. * @return
  112. * @author Michael_xu
  113. */
  114. public function update()
  115. {
  116. $leadsModel = model('Leads');
  117. $param = $this->param;
  118. $userInfo = $this->userInfo;
  119. $param['user_id'] = $userInfo['id'];
  120. if ($leadsModel->updateDataById($param, $param['id'])) {
  121. return resultArray(['data' => '编辑成功']);
  122. } else {
  123. return resultArray(['error' => $leadsModel->getError()]);
  124. }
  125. }
  126. /**
  127. * 删除线索
  128. * @param
  129. * @return
  130. * @author Michael_xu
  131. */
  132. public function delete()
  133. {
  134. $param = $this->param;
  135. $leadsModel = model('Leads');
  136. $recordModel = new \app\admin\model\Record();
  137. $fileModel = new \app\admin\model\File();
  138. $actionRecordModel = new \app\admin\model\ActionRecord();
  139. if (!is_array($param['id'])) {
  140. $leads_id[] = $param['id'];
  141. } else {
  142. $leads_id = $param['id'];
  143. }
  144. $delIds = [];
  145. $errorMessage = [];
  146. //数据权限判断
  147. $userModel = new \app\admin\model\User();
  148. $auth_user_ids = $userModel->getUserByPer('crm', 'leads', 'delete');
  149. foreach ($leads_id as $k => $v) {
  150. $isDel = true;
  151. //数据详情
  152. $data = $leadsModel->getDataById($v);
  153. if (!$data) {
  154. $isDel = false;
  155. $errorMessage[] = 'id为' . $v . '的线索删除失败,错误原因:' . $leadsModel->getError();
  156. }
  157. if (!in_array($data['owner_user_id'], $auth_user_ids)) {
  158. $isDel = false;
  159. $errorMessage[] = '名称为' . $data['name'] . '的线索删除失败,错误原因:无权操作';
  160. }
  161. if ($isDel) {
  162. $delIds[] = $v;
  163. }
  164. }
  165. $dataInfo = $leadsModel->where('leads_id',['in',$delIds])->select();
  166. if ($delIds) {
  167. $data = $leadsModel->delDatas($delIds);
  168. if (!$data) {
  169. return resultArray(['error' => $leadsModel->getError()]);
  170. }
  171. //删除跟进记录
  172. $recordModel->delDataByTypes(1, $delIds);
  173. //删除关联附件
  174. $fileModel->delRFileByModule('crm_leads', $delIds);
  175. //删除关联操作记录
  176. $actionRecordModel->delDataById(['types' => 'crm_leads', 'action_id' => $delIds]);
  177. $userInfo = $this->userInfo;
  178. foreach ($dataInfo as $k => $v) {
  179. RecordActionLog($userInfo['id'], 'crm_leads', 'delete', $v['name'], '', '', '删除了线索:' . $v['name']);
  180. }
  181. }
  182. if ($errorMessage) {
  183. return resultArray(['error' => $errorMessage]);
  184. } else {
  185. return resultArray(['data' => '删除成功']);
  186. }
  187. }
  188. /**
  189. * 线索转化为客户
  190. * @param
  191. * @return
  192. * @author Michael_xu
  193. */
  194. public function transform()
  195. {
  196. $leadsModel = model('Leads');
  197. $customerModel = model('Customer');
  198. $fieldModel = new \app\admin\model\Field();
  199. $param = $this->param;
  200. $userInfo = $this->userInfo;
  201. $userModel = new \app\admin\model\User();
  202. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  203. if (!$param['leads_id'] || !is_array($param['leads_id'])) {
  204. return resultArray(['error' => '请选择需要转化的线索']);
  205. }
  206. $errorMessage = [];
  207. foreach ($param['leads_id'] as $leads_id) {
  208. $data = [];
  209. $leadsInfo = db('crm_leads')->where(['leads_id' => $leads_id])->find();
  210. //字段对照关系处理
  211. $data = $fieldModel->getRelevantData('crm_leads', $leadsInfo) ?: [];
  212. $data['create_user_id'] = $userInfo['id'];
  213. $data['owner_user_id'] = $userInfo['id'];
  214. $data['deal_status'] = '未成交';
  215. $data['deal_time'] = time();
  216. $data['create_time'] = time();
  217. $data['update_time'] = time();
  218. $data['next_time'] = $leadsInfo['next_time'];
  219. if (empty($data['telephone'])) $data['telephone'] = 0;
  220. # 获取客户的时间
  221. $data['obtain_time'] = time();
  222. //权限判断
  223. if (!$leadsInfo) {
  224. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:数据不存在;';
  225. continue;
  226. }
  227. if (!in_array($leadsInfo['owner_user_id'], $authIds)) {
  228. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:无权限;';
  229. continue;
  230. }
  231. $resCustomer = $customerModel->createData($data);
  232. if (!$resCustomer) {
  233. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:' . $customerModel->getError();
  234. continue;
  235. }
  236. $leadsData = [];
  237. $leadsData['is_transform'] = 1; //标记为已转化
  238. $leadsData['customer_id'] = $resCustomer['customer_id'];
  239. db('crm_leads')->where(['leads_id' => $leads_id])->update($leadsData);
  240. //修改记录
  241. updateActionLog($userInfo['id'], 'crm_customer', $resCustomer['customer_id'], '', '', '将线索"' . $leadsInfo['name'] . '转化为客户');
  242. RecordActionLog($userInfo['id'],'crm_leads','save',$leadsInfo['name'],'','','将线索'.$leadsInfo['name'].'转化为客户');
  243. # 将线索下的跟进记录同步至客户下
  244. $record = db('crm_activity')->field('activity_id', true)->where(['type' => 1, 'activity_type' => 1, 'activity_type_id' => $leads_id])->select();
  245. foreach ($record as $key1 => $value1) {
  246. $record[$key1]['activity_type'] = 2;
  247. $record[$key1]['activity_type_id'] = $resCustomer['customer_id'];
  248. $record[$key1]['create_time'] = time() + 1;
  249. $record[$key1]['update_time'] = time() + 1;
  250. }
  251. if (!empty($record)) db('crm_activity')->insertAll($record);
  252. }
  253. if (!$errorMessage) {
  254. return resultArray(['data' => '转化成功']);
  255. } else {
  256. return resultArray(['error' => $errorMessage]);
  257. }
  258. }
  259. /**
  260. * 线索转移
  261. * @param owner_user_id 变更负责人
  262. * @param is_remove 1移出,2转为团队成员
  263. * @param type 权限 1只读2读写
  264. * @return
  265. * @author Michael_xu
  266. */
  267. public function transfer()
  268. {
  269. $param = $this->param;
  270. $userInfo = $this->userInfo;
  271. $leadsModel = model('Leads');
  272. $settingModel = model('Setting');
  273. $userModel = new \app\admin\model\User();
  274. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  275. if (!$param['owner_user_id']) {
  276. return resultArray(['error' => '变更负责人不能为空']);
  277. }
  278. if (!$param['leads_id'] || !is_array($param['leads_id'])) {
  279. return resultArray(['error' => '请选择需要转移的线索']);
  280. }
  281. $is_remove = $param['is_remove'] == 2 ?: 1;
  282. $type = $param['type'] == 2 ?: 1;
  283. $data = [];
  284. $data['owner_user_id'] = $param['owner_user_id'];
  285. $data['update_time'] = time();
  286. $data['follow'] = '待跟进';
  287. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  288. $errorMessage = [];
  289. foreach ($param['leads_id'] as $leads_id) {
  290. $leadsInfo = $leadsModel->getDataById($leads_id);
  291. if (!$leadsInfo) {
  292. $errorMessage[] = '名称:为《' . $leadsInfo['name'] . '》的线索转移失败,错误原因:数据不存在;';
  293. continue;
  294. }
  295. //权限判断
  296. if (!in_array($leadsInfo['owner_user_id'], $authIds)) {
  297. $errorMessage[] = '"' . $leadsInfo['name'] . '"转移失败,错误原因:无权限;';
  298. continue;
  299. }
  300. # 处理分配标识,待办事项专用
  301. $data['is_allocation'] = 1;
  302. $resLeads = db('crm_leads')->where(['leads_id' => $leads_id])->update($data);
  303. if (!$resLeads) {
  304. $errorMessage[] = '"' . $leadsInfo['name'] . '"转移失败,错误原因:数据出错;';
  305. continue;
  306. }
  307. //修改记录
  308. updateActionLog($userInfo['id'], 'crm_leads', $leads_id, '', '', '将线索转移给:' . $ownerUserName);
  309. RecordActionLog($userInfo['id'], 'crm_leads', 'transfer',$leadsInfo['name'], '','','将线索:'.$leadsInfo['name'].'转移给:' . $ownerUserName);
  310. }
  311. if (!$errorMessage) {
  312. return resultArray(['data' => '转移成功']);
  313. } else {
  314. return resultArray(['error' => $errorMessage]);
  315. }
  316. }
  317. /**
  318. * 线索导入模板
  319. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::importExcel()调用
  320. * @return
  321. * @author Michael_xu
  322. */
  323. public function excelDownload($save_path = '')
  324. {
  325. $param = $this->param;
  326. $userInfo = $this->userInfo;
  327. $excelModel = new \app\admin\model\Excel();
  328. // 导出的字段列表
  329. $fieldModel = new \app\admin\model\Field();
  330. $fieldParam['types'] = 'crm_leads';
  331. $fieldParam['action'] = 'excel';
  332. $field_list = $fieldModel->field($fieldParam);
  333. // $field_list = $fieldModel->getIndexFieldList('crm_leads', $userInfo['id']);
  334. $data = $excelModel->excelImportDownload($field_list, 'crm_leads', $save_path);
  335. # 下次升级使用
  336. // $param = $this->param;
  337. // $userInfo = $this->userInfo;
  338. // $excelModel = new \app\admin\model\Excel();
  339. // // 导出的字段列表
  340. // $fieldModel = new \app\admin\model\Field();
  341. // $fieldParam['types'] = 'crm_leads';
  342. // $fieldParam['action'] = 'excel';
  343. // $field_list = $fieldModel->field($fieldParam);
  344. // $field=[1=>[
  345. // 'field'=>'owner_user_id',
  346. // 'types'=>'crm_leads',
  347. // 'name'=>'负责人',
  348. // 'form_type'=>'user',
  349. // 'default_value'=>'',
  350. // 'is_unique' => 1,
  351. // 'is_null' => 1,
  352. // 'input_tips' =>'',
  353. // 'setting' => Array(),
  354. // 'is_hidden'=>0,
  355. // 'writeStatus' => 1,
  356. // 'value' => '']
  357. // ];
  358. // $first_array = array_splice($field_list, 0, 2);
  359. // $array = array_merge($first_array, $field, $field_list);
  360. // $data = $excelModel->excelImportDownload($array, 'crm_leads', $save_path);
  361. return resultArray(['data' => $data]);
  362. }
  363. /**
  364. * 线索导出
  365. * @param
  366. * @return
  367. * @author Michael_xu
  368. */
  369. public function excelExport()
  370. {
  371. $param = $this->param;
  372. $userInfo = $this->userInfo;
  373. $param['user_id'] = $userInfo['id'];
  374. $action_name='导出全部';
  375. if ($param['leads_id']) {
  376. $param['leads_id'] = ['condition' => 'in', 'value' => $param['leads_id'], 'form_type' => 'text', 'name' => ''];
  377. $param['is_excel'] = 1;
  378. $action_name='导出选中';
  379. }
  380. $excelModel = new \app\admin\model\Excel();
  381. // 导出的字段列表
  382. $fieldModel = new \app\admin\model\Field();
  383. $field_list = $fieldModel->getIndexFieldConfig('crm_leads', $userInfo['id']);
  384. // 文件名
  385. $file_name = '5kcrm_leads_' . date('Ymd');
  386. $model = model('Leads');
  387. $temp_file = $param['temp_file'];
  388. unset($param['temp_file']);
  389. $page = $param['page'] ?: 1;
  390. unset($param['page']);
  391. unset($param['export_queue_index']);
  392. RecordActionLog($userInfo['id'],'crm_leads','excelexport',$action_name,'','','导出线索');
  393. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $field_list, $param) {
  394. $param['page'] = $page;
  395. $param['limit'] = $limit;
  396. $data = $model->getDataList($param);
  397. $data['list'] = $model->exportHandle($data['list'], $field_list, 'leads');
  398. return $data;
  399. });
  400. }
  401. /**
  402. * 线索数据导入
  403. * @param
  404. * @return
  405. * @author Michael_xu
  406. */
  407. public function excelImport()
  408. {
  409. $param = $this->param;
  410. $userInfo = $this->userInfo;
  411. $excelModel = new \app\admin\model\Excel();
  412. $param['types'] = 'crm_leads';
  413. $param['create_user_id'] = $userInfo['id'];
  414. $file = request()->file('file');
  415. // $res = $excelModel->importExcel($file, $param);
  416. $res = $excelModel->batchImportData($file, $param, $this);
  417. if (!$res) {
  418. return resultArray(['error' => $excelModel->getError()]);
  419. }
  420. RecordActionLog($userInfo['id'],'crm_leads','excel','导入线索','','','导入线索');
  421. return resultArray(['data' => $excelModel->getError()]);
  422. }
  423. /**
  424. * 线索标记为已跟进
  425. * @param
  426. * @return
  427. * @author Michael_xu
  428. */
  429. public function setFollow()
  430. {
  431. $param = $this->param;
  432. $leadsIds = input('post.id/a') ?: [];
  433. if (!$leadsIds || !is_array($leadsIds)) {
  434. return resultArray(['error' => '参数错误']);
  435. }
  436. $data['follow'] = '已跟进';
  437. $data['update_time'] = time();
  438. $res = db('crm_leads')->where(['leads_id' => ['in', $leadsIds]])->update($data);
  439. if (!$res) {
  440. return resultArray(['error' => '操作失败,请重试']);
  441. }
  442. return resultArray(['data' => '跟进成功']);
  443. }
  444. /**
  445. * 设置关注
  446. *
  447. * @return \think\response\Json
  448. * @throws \think\Exception
  449. * @throws \think\exception\PDOException
  450. */
  451. public function star()
  452. {
  453. $userId = $this->userInfo['id'];
  454. $targetId = $this->param['target_id'];
  455. $type = $this->param['type'];
  456. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  457. if (!$this->setStar($type, $userId, $targetId)) {
  458. return resultArray(['error' => '设置关注失败!']);
  459. }
  460. return resultArray(['data' => '设置关注成功!']);
  461. }
  462. /**
  463. * 系统信息
  464. *
  465. * @return \think\response\Json
  466. * @throws \think\db\exception\DataNotFoundException
  467. * @throws \think\db\exception\ModelNotFoundException
  468. * @throws \think\exception\DbException
  469. */
  470. public function system()
  471. {
  472. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  473. $leadsModel = new \app\crm\model\Leads();
  474. $data = $leadsModel->getSystemInfo($this->param['id']);
  475. return resultArray(['data' => $data]);
  476. }
  477. /**
  478. * table标签栏数量
  479. *
  480. * @return \think\response\Json
  481. * @throws \think\Exception
  482. */
  483. public function count()
  484. {
  485. if (empty($this->param['leads_id'])) return resultArray(['error' => '参数错误!']);
  486. # 附件
  487. $fileCount = Db::name('crm_leads_file')->alias('leads')->join('__ADMIN_FILE__ file', 'file.file_id = leads.file_id')->where('leads_id', $this->param['leads_id'])->count();
  488. return resultArray(['data' => ['fileCount' => $fileCount]]);
  489. }
  490. }