Contacts.php 18KB

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