123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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'] = 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. if ($delIds) {
  166. $data = $leadsModel->delDatas($delIds);
  167. if (!$data) {
  168. return resultArray(['error' => $leadsModel->getError()]);
  169. }
  170. //删除跟进记录
  171. $recordModel->delDataByTypes(1, $delIds);
  172. //删除关联附件
  173. $fileModel->delRFileByModule('crm_leads', $delIds);
  174. //删除关联操作记录
  175. $actionRecordModel->delDataById(['types' => 'crm_leads', 'action_id' => $delIds]);
  176. actionLog($delIds, '', '', '');
  177. }
  178. if ($errorMessage) {
  179. return resultArray(['error' => $errorMessage]);
  180. } else {
  181. return resultArray(['data' => '删除成功']);
  182. }
  183. }
  184. /**
  185. * 线索转化为客户
  186. * @param
  187. * @return
  188. * @author Michael_xu
  189. */
  190. public function transform()
  191. {
  192. $leadsModel = model('Leads');
  193. $customerModel = model('Customer');
  194. $fieldModel = new \app\admin\model\Field();
  195. $param = $this->param;
  196. $userInfo = $this->userInfo;
  197. $userModel = new \app\admin\model\User();
  198. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  199. if (!$param['leads_id'] || !is_array($param['leads_id'])) {
  200. return resultArray(['error' => '请选择需要转化的线索']);
  201. }
  202. $errorMessage = [];
  203. foreach ($param['leads_id'] as $leads_id) {
  204. $data = [];
  205. $leadsInfo = db('crm_leads')->where(['leads_id' => $leads_id])->find();
  206. //字段对照关系处理
  207. $data = $fieldModel->getRelevantData('crm_leads', $leadsInfo) ?: [];
  208. $data['create_user_id'] = $userInfo['id'];
  209. $data['owner_user_id'] = $userInfo['id'];
  210. $data['deal_status'] = '未成交';
  211. $data['deal_time'] = time();
  212. $data['create_time'] = time();
  213. $data['update_time'] = time();
  214. $data['next_time'] = $leadsInfo['next_time'];
  215. if (empty($data['telephone'])) $data['telephone'] = 0;
  216. # 获取客户的时间
  217. $data['obtain_time'] = time();
  218. //权限判断
  219. if (!$leadsInfo) {
  220. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:数据不存在;';
  221. continue;
  222. }
  223. if (!in_array($leadsInfo['owner_user_id'], $authIds)) {
  224. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:无权限;';
  225. continue;
  226. }
  227. $resCustomer = $customerModel->createData($data);
  228. if (!$resCustomer) {
  229. $errorMessage[] = '线索《' . $leadsInfo['name'] . '》转化失败,错误原因:' . $customerModel->getError();
  230. continue;
  231. }
  232. $leadsData = [];
  233. $leadsData['is_transform'] = 1; //标记为已转化
  234. $leadsData['customer_id'] = $resCustomer['customer_id'];
  235. db('crm_leads')->where(['leads_id' => $leads_id])->update($leadsData);
  236. //修改记录
  237. updateActionLog($userInfo['id'], 'crm_customer', $resCustomer['customer_id'], '', '', '将线索"' . $leadsInfo['name'] . '转化为客户');
  238. # 将线索下的跟进记录同步至客户下
  239. $record = db('crm_activity')->field('activity_id', true)->where(['type' => 1, 'activity_type' => 1, 'activity_type_id' => $leads_id])->select();
  240. foreach ($record as $key1 => $value1) {
  241. $record[$key1]['activity_type'] = 2;
  242. $record[$key1]['activity_type_id'] = $resCustomer['customer_id'];
  243. $record[$key1]['create_time'] = time() + 1;
  244. $record[$key1]['update_time'] = time() + 1;
  245. }
  246. if (!empty($record)) db('crm_activity')->insertAll($record);
  247. }
  248. if (!$errorMessage) {
  249. return resultArray(['data' => '转化成功']);
  250. } else {
  251. return resultArray(['error' => $errorMessage]);
  252. }
  253. }
  254. /**
  255. * 线索转移
  256. * @param owner_user_id 变更负责人
  257. * @param is_remove 1移出,2转为团队成员
  258. * @param type 权限 1只读2读写
  259. * @return
  260. * @author Michael_xu
  261. */
  262. public function transfer()
  263. {
  264. $param = $this->param;
  265. $userInfo = $this->userInfo;
  266. $leadsModel = model('Leads');
  267. $settingModel = model('Setting');
  268. $userModel = new \app\admin\model\User();
  269. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  270. if (!$param['owner_user_id']) {
  271. return resultArray(['error' => '变更负责人不能为空']);
  272. }
  273. if (!$param['leads_id'] || !is_array($param['leads_id'])) {
  274. return resultArray(['error' => '请选择需要转移的线索']);
  275. }
  276. $is_remove = $param['is_remove'] == 2 ?: 1;
  277. $type = $param['type'] == 2 ?: 1;
  278. $data = [];
  279. $data['owner_user_id'] = $param['owner_user_id'];
  280. $data['update_time'] = time();
  281. $data['follow'] = '待跟进';
  282. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  283. $errorMessage = [];
  284. foreach ($param['leads_id'] as $leads_id) {
  285. $leadsInfo = $leadsModel->getDataById($leads_id);
  286. if (!$leadsInfo) {
  287. $errorMessage[] = '名称:为《' . $leadsInfo['name'] . '》的线索转移失败,错误原因:数据不存在;';
  288. continue;
  289. }
  290. //权限判断
  291. if (!in_array($leadsInfo['owner_user_id'], $authIds)) {
  292. $errorMessage[] = '"' . $leadsInfo['name'] . '"转移失败,错误原因:无权限;';
  293. continue;
  294. }
  295. $resLeads = db('crm_leads')->where(['leads_id' => $leads_id])->update($data);
  296. if (!$resLeads) {
  297. $errorMessage[] = '"' . $leadsInfo['name'] . '"转移失败,错误原因:数据出错;';
  298. continue;
  299. }
  300. //修改记录
  301. updateActionLog($userInfo['id'], 'crm_leads', $leads_id, '', '', '将线索转移给:' . $ownerUserName);
  302. }
  303. if (!$errorMessage) {
  304. return resultArray(['data' => '转移成功']);
  305. } else {
  306. return resultArray(['error' => $errorMessage]);
  307. }
  308. }
  309. /**
  310. * 线索导入模板
  311. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::importExcel()调用
  312. * @return
  313. * @author Michael_xu
  314. */
  315. public function excelDownload($save_path = '')
  316. {
  317. $param = $this->param;
  318. $userInfo = $this->userInfo;
  319. $excelModel = new \app\admin\model\Excel();
  320. // 导出的字段列表
  321. $fieldModel = new \app\admin\model\Field();
  322. $fieldParam['types'] = 'crm_leads';
  323. $fieldParam['action'] = 'excel';
  324. $field_list = $fieldModel->field($fieldParam);
  325. // $field_list = $fieldModel->getIndexFieldList('crm_leads', $userInfo['id']);
  326. $data = $excelModel->excelImportDownload($field_list, 'crm_leads', $save_path);
  327. return resultArray(['data' => $data]);
  328. }
  329. /**
  330. * 线索导出
  331. * @param
  332. * @return
  333. * @author Michael_xu
  334. */
  335. public function excelExport()
  336. {
  337. $param = $this->param;
  338. $userInfo = $this->userInfo;
  339. $param['user_id'] = $userInfo['id'];
  340. if ($param['leads_id']) {
  341. $param['leads_id'] = ['condition' => 'in', 'value' => $param['leads_id'], 'form_type' => 'text', 'name' => ''];
  342. $param['is_excel'] = 1;
  343. }
  344. $excelModel = new \app\admin\model\Excel();
  345. // 导出的字段列表
  346. $fieldModel = new \app\admin\model\Field();
  347. $field_list = $fieldModel->getIndexFieldConfig('crm_leads', $userInfo['id']);
  348. // 文件名
  349. $file_name = '5kcrm_leads_' . date('Ymd');
  350. $model = model('Leads');
  351. $temp_file = $param['temp_file'];
  352. unset($param['temp_file']);
  353. $page = $param['page'] ?: 1;
  354. unset($param['page']);
  355. unset($param['export_queue_index']);
  356. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $field_list, $param) {
  357. $param['page'] = $page;
  358. $param['limit'] = $limit;
  359. $data = $model->getDataList($param);
  360. $data['list'] = $model->exportHandle($data['list'], $field_list, 'leads');
  361. return $data;
  362. });
  363. }
  364. /**
  365. * 线索数据导入
  366. * @param
  367. * @return
  368. * @author Michael_xu
  369. */
  370. public function excelImport()
  371. {
  372. $param = $this->param;
  373. $userInfo = $this->userInfo;
  374. $excelModel = new \app\admin\model\Excel();
  375. $param['types'] = 'crm_leads';
  376. $param['create_user_id'] = $userInfo['id'];
  377. $param['owner_user_id'] = $param['owner_user_id'] ?: $userInfo['id'];
  378. $file = request()->file('file');
  379. // $res = $excelModel->importExcel($file, $param);
  380. $res = $excelModel->batchImportData($file, $param, $this);
  381. if (!$res) {
  382. return resultArray(['error' => $excelModel->getError()]);
  383. }
  384. return resultArray(['data' => $excelModel->getError()]);
  385. }
  386. /**
  387. * 线索标记为已跟进
  388. * @param
  389. * @return
  390. * @author Michael_xu
  391. */
  392. public function setFollow()
  393. {
  394. $param = $this->param;
  395. $leadsIds = input('post.id/a') ?: [];
  396. if (!$leadsIds || !is_array($leadsIds)) {
  397. return resultArray(['error' => '参数错误']);
  398. }
  399. $data['follow'] = '已跟进';
  400. $data['update_time'] = time();
  401. $res = db('crm_leads')->where(['leads_id' => ['in', $leadsIds]])->update($data);
  402. if (!$res) {
  403. return resultArray(['error' => '操作失败,请重试']);
  404. }
  405. return resultArray(['data' => '跟进成功']);
  406. }
  407. /**
  408. * 设置关注
  409. *
  410. * @return \think\response\Json
  411. * @throws \think\Exception
  412. * @throws \think\exception\PDOException
  413. */
  414. public function star()
  415. {
  416. $userId = $this->userInfo['id'];
  417. $targetId = $this->param['target_id'];
  418. $type = $this->param['type'];
  419. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  420. if (!$this->setStar($type, $userId, $targetId)) {
  421. return resultArray(['error' => '设置关注失败!']);
  422. }
  423. return resultArray(['data' => '设置关注成功!']);
  424. }
  425. /**
  426. * 系统信息
  427. *
  428. * @return \think\response\Json
  429. * @throws \think\db\exception\DataNotFoundException
  430. * @throws \think\db\exception\ModelNotFoundException
  431. * @throws \think\exception\DbException
  432. */
  433. public function system()
  434. {
  435. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  436. $leadsModel = new \app\crm\model\Leads();
  437. $data = $leadsModel->getSystemInfo($this->param['id']);
  438. return resultArray(['data' => $data]);
  439. }
  440. /**
  441. * table标签栏数量
  442. *
  443. * @return \think\response\Json
  444. * @throws \think\Exception
  445. */
  446. public function count()
  447. {
  448. if (empty($this->param['leads_id'])) return resultArray(['error' => '参数错误!']);
  449. # 附件
  450. $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();
  451. return resultArray(['data' => ['fileCount' => $fileCount]]);
  452. }
  453. }