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