Contacts.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. $dataInfo = $contactsModel->getDataById($v);
  167. if ($delIds) {
  168. $data = $contactsModel->delDatas($delIds);
  169. if (!$data) {
  170. return resultArray(['error' => $contactsModel->getError()]);
  171. }
  172. // 删除联系人扩展数据
  173. db('crm_contacts_data')->whereIn('contacts_id', $delIds)->delete();
  174. //删除跟进记录
  175. $recordModel->delDataByTypes(3,$delIds);
  176. //删除关联附件
  177. $fileModel->delRFileByModule('crm_contacts',$delIds);
  178. //删除关联操作记录
  179. $actionRecordModel->delDataById(['types'=>'crm_contacts','action_id'=>$delIds]);
  180. $userInfo = $this->userInfo;
  181. foreach ($contacts_id as $k => $v) {
  182. RecordActionLog($userInfo['id'], 'crm_contacts', 'delete', $dataInfo['name'], '', '', '删除了联系人:' . $dataInfo['name']);
  183. }
  184. }
  185. if ($errorMessage) {
  186. return resultArray(['error' => $errorMessage]);
  187. } else {
  188. return resultArray(['data' => '删除成功']);
  189. }
  190. }
  191. /**
  192. * 联系人转移
  193. * @author Michael_xu
  194. * @param owner_user_id 变更负责人
  195. * @param is_remove 1移出,2转为团队成员
  196. * @param type 权限 1只读2读写
  197. * @return
  198. */
  199. public function transfer()
  200. {
  201. $param = $this->param;
  202. $userInfo = $this->userInfo;
  203. $contactsModel = model('Contacts');
  204. $settingModel = model('Setting');
  205. $userModel = new \app\admin\model\User();
  206. $authIds = $userModel->getUserByPer(); //权限范围的user_id
  207. if (!$param['owner_user_id']) {
  208. return resultArray(['error' => '变更负责人不能为空']);
  209. }
  210. $owner_user_info = $userModel->getUserById($param['owner_user_id']);
  211. if (!$param['contacts_id'] || !is_array($param['contacts_id'])) {
  212. return resultArray(['error' => '请选择需要转移的联系人']);
  213. }
  214. $is_remove = $param['is_remove'] == 2 ? : 1;
  215. $type = $param['type'] == 2 ? : 1;
  216. $data = [];
  217. $data['owner_user_id'] = $param['owner_user_id'];
  218. $data['update_time'] = time();
  219. $errorMessage = [];
  220. foreach ($param['contacts_id'] as $contacts_id) {
  221. $contactsInfo = $contactsModel->getDataById($contacts_id);
  222. // 转移至当前负责人的直接跳过
  223. if ($param['owner_user_id'] == $contactsInfo['owner_user_id']) continue;
  224. if (!$contactsInfo) {
  225. $errorMessage[] = '名称:为《'.$contactsInfo['name'].'》的联系人转移失败,错误原因:数据不存在;';
  226. continue;
  227. }
  228. //权限判断
  229. if (!in_array($contactsInfo['owner_user_id'],$authIds)) {
  230. $errorMessage[] = $contactsInfo['name'].'"转移失败,错误原因:无权限;';
  231. continue;
  232. }
  233. $resContacts = db('crm_contacts')->where(['contacts_id' => $contacts_id])->update($data);
  234. if (!$resContacts) {
  235. $errorMessage[] = $contactsInfo['name'].'"转移失败,错误原因:数据出错;';
  236. continue;
  237. }
  238. updateActionLog($userInfo['id'], 'crm_contacts', $contacts_id, '', '', '将联系人转移给:' . $owner_user_info['realname']);
  239. RecordActionLog($userInfo['id'], 'crm_contacts', 'transfer',$contactsInfo['name'], '','','将联系人:'.$contactsInfo['name'].'转移给:' . $owner_user_info['realname']);
  240. }
  241. if (!$errorMessage) {
  242. return resultArray(['data' => '转移成功']);
  243. } else {
  244. return resultArray(['error' => $errorMessage]);
  245. }
  246. }
  247. /**
  248. * 联系人导入模板
  249. * @author Michael_xu
  250. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::importExcel()调用
  251. * @return
  252. */
  253. public function excelDownload($save_path = '')
  254. {
  255. # 下次升级
  256. $param = $this->param;
  257. $userInfo = $this->userInfo;
  258. $excelModel = new \app\admin\model\Excel();
  259. // 导出的字段列表
  260. $fieldModel = new \app\admin\model\Field();
  261. $fieldParam['types'] = 'crm_contacts';
  262. $fieldParam['action'] = 'excel';
  263. $field_list = $fieldModel->field($fieldParam);
  264. $array=[];
  265. $field=[1=>[
  266. 'field'=>'owner_user_id',
  267. 'types'=>'crm_contacts',
  268. 'name'=>'负责人',
  269. 'form_type'=>'user',
  270. 'default_value'=>'',
  271. 'is_null' => 1,
  272. 'input_tips' =>'',
  273. 'setting' => Array(),
  274. 'is_hidden'=>0,
  275. 'writeStatus' => 1,
  276. 'value' => '']
  277. ];
  278. $first_array = array_splice($field_list, 2,0, $field);
  279. $excelModel->excelImportDownload($field_list, 'crm_contacts', $save_path);
  280. }
  281. /**
  282. * 联系人导出
  283. * @author Michael_xu
  284. * @param
  285. * @return
  286. */
  287. public function excelExport()
  288. {
  289. $param = $this->param;
  290. $userInfo = $this->userInfo;
  291. $param['user_id'] = $userInfo['id'];
  292. $action_name='导出全部';
  293. if ($param['contacts_id']) {
  294. $param['contacts_id'] = ['condition' => 'in','value' => $param['contacts_id'],'form_type' => 'text','name' => ''];
  295. $param['is_excel'] = 1;
  296. $action_name='导出选中';
  297. }
  298. $excelModel = new \app\admin\model\Excel();
  299. // 导出的字段列表
  300. $fieldModel = new \app\admin\model\Field();
  301. $field_list = $fieldModel->getIndexFieldConfig('crm_contacts', $userInfo['id'],'','excel');
  302. // 文件名
  303. $file_name = '5kcrm_contacts_'.date('Ymd');
  304. $model = model('Contacts');
  305. $temp_file = $param['temp_file'];
  306. unset($param['temp_file']);
  307. $page = $param['page'] ?: 1;
  308. unset($param['page']);
  309. unset($param['export_queue_index']);
  310. RecordActionLog($userInfo['id'],'crm_contracts','excelexport',$action_name,'','','导出联系人');
  311. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function($page, $limit) use ($model, $param, $field_list) {
  312. $param['page'] = $page;
  313. $param['limit'] = $limit;
  314. $data = $model->getDataList($param);
  315. $data['list'] = $model->exportHandle($data['list'], $field_list, 'contacts');
  316. return $data;
  317. });
  318. }
  319. /**
  320. * 联系人数据导入
  321. * @author Michael_xu
  322. * @param
  323. * @return
  324. */
  325. public function excelImport()
  326. {
  327. $param = $this->param;
  328. $userInfo = $this->userInfo;
  329. $excelModel = new \app\admin\model\Excel();
  330. $param['types'] = 'crm_contacts';
  331. $param['create_user_id'] = $userInfo['id'];
  332. $param['owner_user_id'] = $param['owner_user_id'] ? : $userInfo['id'];
  333. $file = request()->file('file');
  334. // $res = $excelModel->importExcel($file, $param, $this);
  335. $res = $excelModel->batchImportData($file, $param, $this);
  336. // if (!$res) {
  337. // return resultArray(['error'=>$excelModel->getError()]);
  338. // }
  339. RecordActionLog($userInfo['id'],'crm_contacts','excel','导入联系人','','','导入联系人');
  340. return resultArray(['data' => $excelModel->getError()]);
  341. }
  342. /**
  343. * 联系人 关联/取消关联 商机
  344. * @return [type] [description]
  345. */
  346. public function relation()
  347. {
  348. $param = $this->param;
  349. if (!$param['contacts_id'] || !$param['contacts_id']) {
  350. return resultArray(['error' => '参数错误!']);
  351. }
  352. $res = 1;
  353. if ($param['is_relation'] == 1) {//关联
  354. $data = [];
  355. if (is_array($param['contacts_id'])) {//商机关联联系人
  356. foreach ($param['contacts_id'] as $key => $value) {
  357. $data['contacts_id'] = $value;
  358. $data['business_id'] = $param['business_id'];
  359. $ret = Db::name('crm_contacts_business')->where(['contacts_id' => $value,'business_id' => $param['business_id']])->find();
  360. if (!$ret) {
  361. if (!Db::name('crm_contacts_business')->insert($data)) {
  362. $res = 0;
  363. }
  364. }
  365. }
  366. } else {//联系人关联商机
  367. foreach ($param['business_id'] as $key => $value) {
  368. $data['business_id'] = $value;
  369. $data['contacts_id'] = $param['contacts_id'];
  370. $ret = Db::name('crm_contacts_business')->where(['contacts_id' => $param['contacts_id'],'business_id' => $value])->find();
  371. if (!$ret) {
  372. if (!Db::name('crm_contacts_business')->insert($data)) {
  373. $res = 0;
  374. }
  375. }
  376. }
  377. }
  378. } else {//取消关联
  379. $where = array();
  380. if (is_array($param['contacts_id'])) {
  381. $where['contacts_id'] = array('in',$param['contacts_id']);
  382. $where['business_id'] = array('eq',$param['business_id']);
  383. } else {
  384. $where['business_id'] = array('in',$param['business_id']);
  385. $where['contacts_id'] = array('eq',$param['contacts_id']);
  386. }
  387. Db::name('crm_contacts_business')->where($where)->delete();
  388. # 商机首要联系人处理
  389. if (is_array($param['contacts_id'])) {
  390. foreach ($param['contacts_id'] AS $key => $value) {
  391. $contactsId = Db::name('crm_business')->where('business_id', $param['business_id'])->value('contacts_id');
  392. if ($contactsId == $value) {
  393. Db::name('crm_business')->where('business_id', $param['business_id'])->update(['contacts_id' => 0]);
  394. }
  395. }
  396. }
  397. }
  398. if ($res == 1) {
  399. return resultArray(['data' => '操作成功!']);
  400. } else {
  401. return resultArray(['error' => '操作失败,请重试!']);
  402. }
  403. }
  404. /**
  405. * 设置关注
  406. *
  407. * @return \think\response\Json
  408. * @throws \think\Exception
  409. * @throws \think\exception\PDOException
  410. */
  411. public function star()
  412. {
  413. $userId = $this->userInfo['id'];
  414. $targetId = $this->param['target_id'];
  415. $type = $this->param['type'];
  416. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  417. if (!$this->setStar($type, $userId, $targetId)) {
  418. return resultArray(['error' => '设置关注失败!']);
  419. }
  420. return resultArray(['data' => '设置关注成功!']);
  421. }
  422. /**
  423. * 设置首要联系人
  424. *
  425. * @return \think\response\Json
  426. * @throws \think\Exception
  427. * @throws \think\exception\PDOException
  428. */
  429. public function setPrimary()
  430. {
  431. $customerId = $this->param['customer_id'];
  432. $contactsId = $this->param['contacts_id'];
  433. if (empty($customerId)) return resultArray(['error' => '缺少客户ID!']);
  434. if (empty($customerId)) return resultArray(['error' => '缺少联系人ID!']);
  435. $contactsModel = new \app\crm\model\Contacts();
  436. $contactsModel->setPrimary($customerId, $contactsId);
  437. return resultArray(['data' => '操作成功!']);
  438. }
  439. /**
  440. * 获取联系人
  441. *
  442. * @return \think\response\Json
  443. * @throws \think\db\exception\DataNotFoundException
  444. * @throws \think\db\exception\ModelNotFoundException
  445. * @throws \think\exception\DbException
  446. */
  447. public function getContactsList()
  448. {
  449. if (empty($this->param['customer_id'])) return resultArray(['error' => '缺少客户ID!']);
  450. $contactsModel = new \app\crm\model\Contacts();
  451. $data = $contactsModel->getContactsList($this->param['customer_id']);
  452. return resultArray(['data' => $data]);
  453. }
  454. /**
  455. * 系统信息
  456. *
  457. * @return \think\response\Json
  458. * @throws \think\db\exception\DataNotFoundException
  459. * @throws \think\db\exception\ModelNotFoundException
  460. * @throws \think\exception\DbException
  461. */
  462. public function system()
  463. {
  464. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  465. $contactsModel = new \app\crm\model\Contacts();
  466. $data = $contactsModel->getSystemInfo($this->param['id']);
  467. return resultArray(['data' => $data]);
  468. }
  469. /**
  470. * table标签栏数量
  471. *
  472. * @return \think\response\Json
  473. * @throws \think\Exception
  474. */
  475. public function count()
  476. {
  477. if (empty($this->param['contacts_id'])) return resultArray(['error' => '参数错误!']);
  478. $contactsId = $this->param['contacts_id'];
  479. $userInfo = $this->userInfo;
  480. # 团队成员
  481. $contacts = Db::name('crm_contacts')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('contacts_id', $contactsId)->find();
  482. $contacts['ro_user_id'] = stringToArray($contacts['ro_user_id']);
  483. $contacts['rw_user_id'] = stringToArray($contacts['rw_user_id']);
  484. $contacts['owner_user_id'] = [$contacts['owner_user_id']];
  485. $teamCount = array_filter(array_unique(array_merge($contacts['ro_user_id'], $contacts['rw_user_id'], $contacts['owner_user_id'])));
  486. # 查询联系人和商机的关联表
  487. $businessIds = Db::name('crm_contacts_business')->where('contacts_id', $contactsId)->column('business_id');
  488. # 商机权限条件
  489. $businessAuth = $this->getBusinessSearchWhere($userInfo['id']);
  490. # 商机
  491. $businessCount = Db::name('crm_business')->whereIn('business_id', $businessIds)->where($businessAuth)->count();
  492. # 附件
  493. $fileCount = Db::name('crm_contacts_file')->alias('contacts')->join('__ADMIN_FILE__ file', 'file.file_id = contacts.file_id', 'LEFT')->where('contacts_id', $contactsId)->count();
  494. $data = [
  495. 'businessCount' => $businessCount,
  496. 'fileCount' => $fileCount,
  497. 'memberCount' => count($teamCount),
  498. ];
  499. return resultArray(['data' => $data]);
  500. }
  501. }