Customer.php 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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\logic\CustomerLogic;
  10. use app\crm\traits\SearchConditionTrait;
  11. use app\crm\traits\StarTrait;
  12. use phpDocumentor\Reflection\Types\False_;
  13. use think\Cache;
  14. use think\Hook;
  15. use think\Request;
  16. use think\Db;
  17. class Customer extends ApiCommon
  18. {
  19. use StarTrait, SearchConditionTrait;
  20. /**
  21. * 用于判断权限
  22. * @permission 无限制
  23. * @allow 登录用户可访问
  24. * @other 其他根据系统设置
  25. **/
  26. public function _initialize()
  27. {
  28. $action = [
  29. 'permission' => ['exceldownload', 'setfollow', 'delete'],
  30. 'allow' => ['read', 'system', 'count', 'poolauthority', 'level']
  31. ];
  32. Hook::listen('check_auth', $action);
  33. $request = Request::instance();
  34. $a = strtolower($request->action());
  35. if (!in_array($a, $action['permission'])) {
  36. parent::_initialize();
  37. } else {
  38. $param = Request::instance()->param();
  39. $this->param = $param;
  40. }
  41. }
  42. /**
  43. * 客户列表
  44. * @return
  45. * @author Michael_xu
  46. */
  47. public function index()
  48. {
  49. $customerModel = model('Customer');
  50. $param = $this->param;
  51. $userInfo = $this->userInfo;
  52. $param['user_id'] = $userInfo['id'];
  53. $data = $customerModel->getDataList($param);
  54. return resultArray(['data' => $data]);
  55. }
  56. /**
  57. * 客户公海(没有负责人或已经到期)
  58. * @return
  59. * @author Michael_xu
  60. */
  61. public function pool()
  62. {
  63. $param = $this->param;
  64. $param['action'] = 'pool';
  65. unset($param['poolId']); # todo uniApp传来的参数,临时删除掉 fanqi。
  66. $data = model('Customer')->getDataList($param);
  67. return resultArray(['data' => $data]);
  68. }
  69. /**
  70. * 添加客户
  71. * @param
  72. * @return
  73. * @author Michael_xu
  74. */
  75. public function save()
  76. {
  77. $customerModel = model('Customer');
  78. $param = $this->param;
  79. $userInfo = $this->userInfo;
  80. $param['user_id'] = $userInfo['id'];
  81. $param['create_user_id'] = $userInfo['id'];
  82. $param['owner_user_id'] = $userInfo['id'];
  83. if ($res = $customerModel->createData($param)) {
  84. return resultArray(['data' => $res]);
  85. } else {
  86. return resultArray(['error' => $customerModel->getError()]);
  87. }
  88. }
  89. /**
  90. * 客户详情
  91. * @param
  92. * @return
  93. * @author Michael_xu
  94. */
  95. public function read()
  96. {
  97. $customerModel = model('Customer');
  98. $cutomerLogic = new CustomerLogic();
  99. $param = $this->param;
  100. $userInfo = $this->userInfo;
  101. $data = $customerModel->getDataById($param['id'], $userInfo['id']);
  102. if (!$data) {
  103. return resultArray(['error' => $customerModel->getError()]);
  104. }
  105. //数据权限判断
  106. $userModel = new \app\admin\model\User();
  107. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'read');
  108. //读权限
  109. $roPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'read');
  110. $rwPre = $userModel->rwPre($userInfo['id'], $data['ro_user_id'], $data['rw_user_id'], 'update');
  111. //判断是否客户池数据
  112. $wherePool = $customerModel->getWhereByPool();
  113. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['id']])->where($wherePool)->find();
  114. if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && !$roPre && !$rwPre) {
  115. $authData['dataAuth'] = (int)0;
  116. return resultArray(['data' => $authData]);
  117. }
  118. return resultArray(['data' => $data]);
  119. }
  120. /**
  121. * 编辑客户
  122. * @param
  123. * @return
  124. * @author Michael_xu
  125. */
  126. public function update()
  127. {
  128. $customerModel = model('Customer');
  129. $param = $this->param;
  130. $userInfo = $this->userInfo;
  131. //数据详情
  132. $data = $customerModel->getDataById($param['id']);
  133. if (!$data) {
  134. return resultArray(['error' => $customerModel->getError()]);
  135. }
  136. $param['user_id'] = $userInfo['id'];
  137. if ($customerModel->updateDataById($param, $param['id'])) {
  138. return resultArray(['data' => '编辑成功']);
  139. } else {
  140. return resultArray(['error' => $customerModel->getError()]);
  141. }
  142. }
  143. /**
  144. * 删除客户
  145. * @param
  146. * @return
  147. * @author Michael_xu
  148. */
  149. public function delete()
  150. {
  151. $param = $this->param;
  152. $user = new ApiCommon();
  153. $userInfo = $user->userInfo;
  154. // 是否客户池
  155. if ($param['isSeas'] == 1) {
  156. $permission = checkPerByAction('crm', 'customer', 'poolDelete');
  157. } else {
  158. $permission = checkPerByAction('crm', 'customer', 'delete');
  159. }
  160. if ($permission == false) {
  161. return resultArray(['error' => '无权操作']);
  162. }
  163. $customerModel = model('Customer');
  164. $userModel = new \app\admin\model\User();
  165. $recordModel = new \app\admin\model\Record();
  166. $fileModel = new \app\admin\model\File();
  167. $actionRecordModel = new \app\admin\model\ActionRecord();
  168. if (!is_array($param['id'])) {
  169. $customer_id[] = $param['id'];
  170. } else {
  171. $customer_id = $param['id'];
  172. }
  173. $delIds = [];
  174. $errorMessage = [];
  175. //数据权限判断
  176. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', 'delete');
  177. //判断是否客户池数据(客户池数据只有管理员可以删)
  178. $adminId = $userModel->getAdminId();
  179. $wherePool = $customerModel->getWhereByPool();
  180. foreach ($customer_id as $k => $v) {
  181. $isDel = true;
  182. //数据详情
  183. $data = db('crm_customer')->where(['customer_id' => $v])->find();
  184. if (!$data) {
  185. $isDel = false;
  186. $errorMessage[] = 'id为' . $v . '的客户删除失败,错误原因:' . $customerModel->getError();
  187. }
  188. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  189. if (!$resPool && !in_array($data['owner_user_id'], $auth_user_ids) && $isDel) {
  190. $isDel = false;
  191. $errorMessage[] = '无权操作';
  192. }
  193. // 公海 (原逻辑,公海仅允许管理员删除,修改为授权,不再限制)
  194. // if ($resPool && !in_array($data['owner_user_id'],$adminId)) {
  195. // $isDel = false;
  196. // $errorMessage[] = '名称为'.$data['name'].'的客户删除失败,错误原因:无权操作';
  197. // }
  198. //有商机、合同、联系人则不能删除
  199. if ($isDel) {
  200. $resBusiness = db('crm_business')->where(['customer_id' => $v])->find();
  201. if ($resBusiness) {
  202. $isDel = false;
  203. $errorMessage[] = '客户下存在商机,不能删除';
  204. }
  205. }
  206. if ($isDel) {
  207. $resContacts = db('crm_contacts')->where(['customer_id' => $v])->find();
  208. if ($resContacts) {
  209. $isDel = false;
  210. // $errorMessage[] = '名称为' . $data['name'] . '的客户删除失败,错误原因:客户下存在联系人,不能删除';
  211. $errorMessage[] = '客户下存在联系人,不能删除';
  212. }
  213. }
  214. if ($isDel) {
  215. $resContract = db('crm_contract')->where(['customer_id' => $v])->find();
  216. if ($resContract) {
  217. $isDel = false;
  218. $errorMessage[] = '客户下存在合同,不能删除';
  219. }
  220. }
  221. if ($isDel) {
  222. $delIds[] = $v;
  223. }
  224. }
  225. $dataInfo = $customerModel->where('customer_id', ['in', $delIds])->select();
  226. if ($delIds) {
  227. $delRes = $customerModel->delDatas($delIds);
  228. if (!$delRes) {
  229. return resultArray(['error' => $customerModel->getError()]);
  230. }
  231. // 删除客户扩展数据
  232. db('crm_customer_data')->whereIn('customer_id', $delIds)->delete();
  233. // 删除跟进记录
  234. $recordModel->delDataByTypes(2, $delIds);
  235. // 删除关联附件
  236. $fileModel->delRFileByModule('crm_customer', $delIds);
  237. // 删除关联操作记录
  238. $actionRecordModel->delDataById(['types' => 'crm_customer', 'action_id' => $delIds]);
  239. foreach ($dataInfo as $k => $v) {
  240. RecordActionLog($userInfo['id'], 'crm_customer', 'delete', $v['name'], '', '', '删除了客户:' . $v['name']);
  241. }
  242. }
  243. if ($errorMessage) {
  244. return resultArray(['error' => $errorMessage]);
  245. } else {
  246. return resultArray(['data' => '删除成功']);
  247. }
  248. }
  249. /**
  250. * 客户转移
  251. * @param owner_user_id 变更负责人
  252. * @param is_remove 1移出,2转为团队成员
  253. * @param types business,contract 相关模块
  254. * @param type 权限 1只读2读写
  255. * @return
  256. * @author Michael_xu
  257. */
  258. public function transfer()
  259. {
  260. $param = $this->param;
  261. $userInfo = $this->userInfo;
  262. $customerModel = model('Customer');
  263. $businessModel = model('Business');
  264. $contractModel = model('Contract');
  265. $contactsModel = model('Contacts');
  266. $settingModel = model('Setting');
  267. $customerConfigModel = model('CustomerConfig');
  268. $userModel = new \app\admin\model\User();
  269. if (!$param['owner_user_id']) {
  270. return resultArray(['error' => '变更负责人不能为空']);
  271. }
  272. if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  273. return resultArray(['error' => '请选择需要转移的客户']);
  274. }
  275. $is_remove = ($param['is_remove'] == 2) ? 2 : 1;
  276. $type = $param['type'] == 2 ?: 1;
  277. $types = $param['types'] ?: [];
  278. $data = [];
  279. $data['owner_user_id'] = $param['owner_user_id'];
  280. $data['update_time'] = time();
  281. $data['follow'] = '待跟进';
  282. # 获取客户的时间
  283. $data['obtain_time'] = time();
  284. $ownerUserName = $userModel->getUserNameById($param['owner_user_id']);
  285. $errorMessage = [];
  286. foreach ($param['customer_id'] as $customer_id) {
  287. $customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
  288. if (!$customerInfo) {
  289. $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户转移失败,错误原因:数据不存在;';
  290. continue;
  291. }
  292. $resCustomer = true;
  293. //权限判断
  294. if (!$customerModel->checkData($customer_id)) {
  295. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:无权限;';
  296. continue;
  297. }
  298. //拥有客户数上限检测
  299. if (!$customerConfigModel->checkData($param['owner_user_id'], 1)) {
  300. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:' . $customerConfigModel->getError();
  301. continue;
  302. }
  303. //团队成员
  304. $teamData = [];
  305. $teamData['type'] = $type; //权限 1只读2读写
  306. $teamData['user_id'] = [$customerInfo['owner_user_id']]; //协作人
  307. $teamData['types'] = 'crm_customer'; //类型
  308. $teamData['types_id'] = $customer_id; //类型ID
  309. $teamData['is_del'] = ($is_remove == 1) ? 1 : '';
  310. $res = $settingModel->createTeamData($teamData);
  311. # 处理分配标识,待办事项专用
  312. $data['is_allocation'] = 1;
  313. $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  314. if (!$resCustomer) {
  315. $errorMessage[] = $customerInfo['name'] . '转移失败,错误原因:数据出错;';
  316. continue;
  317. } else {
  318. # 处理转移时,负责人出现在只读和读写成员列表中
  319. $customerArray = [];
  320. $teamCustomer = db('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customer_id)->find();
  321. if (!empty($teamCustomer['ro_user_id'])) {
  322. $customerRo = arrayToString(array_diff(stringToArray($teamCustomer['ro_user_id']), [$teamCustomer['owner_user_id']]));
  323. $customerArray['ro_user_id'] = $customerRo;
  324. }
  325. if (!empty($teamCustomer['rw_user_id'])) {
  326. $customerRo = arrayToString(array_diff(stringToArray($teamCustomer['rw_user_id']), [$teamCustomer['owner_user_id']]));
  327. $customerArray['rw_user_id'] = $customerRo;
  328. }
  329. db('crm_customer')->where('customer_id', $customer_id)->update($customerArray);
  330. }
  331. if (in_array('crm_contacts', $types)) {
  332. $contactsIds = [];
  333. $contactsIds = db('crm_contacts')->where(['customer_id' => $customer_id])->column('contacts_id');
  334. if ($contactsIds) {
  335. $resContacts = $contactsModel->transferDataById($contactsIds, $param['owner_user_id'], $type, $is_remove);
  336. if ($resContacts !== true) {
  337. $errorMessage[] = $resContacts;
  338. continue;
  339. }
  340. }
  341. }
  342. //商机、合同转移
  343. if (in_array('crm_business', $types)) {
  344. $businessIds = [];
  345. $businessIds = db('crm_business')->where(['customer_id' => $customer_id])->column('business_id');
  346. if ($businessIds) {
  347. $resBusiness = $businessModel->transferDataById($businessIds, $param['owner_user_id'], $type, $is_remove);
  348. if ($resBusiness !== true) {
  349. $errorMessage = $errorMessage ? array_merge($errorMessage, $resBusiness) : $resBusiness;
  350. continue;
  351. }
  352. }
  353. }
  354. if (in_array('crm_contract', $types)) {
  355. $contractIds = [];
  356. $contractIds = db('crm_contract')->where(['customer_id' => $customer_id])->column('contract_id');
  357. if ($contractIds) {
  358. $resContract = $contractModel->transferDataById($contractIds, $param['owner_user_id'], $type, $is_remove);
  359. if ($resContract !== true) {
  360. $errorMessage = $errorMessage ? array_merge($errorMessage, $resContract) : $resContract;
  361. continue;
  362. }
  363. }
  364. }
  365. //修改记录
  366. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户转移给:' . $ownerUserName);
  367. RecordActionLog($userInfo['id'], 'crm_customer', 'transfer', $customerInfo['name'], '', '', '将客户:' . $customerInfo['name'] . '转移给:' . $ownerUserName);
  368. }
  369. if (!$errorMessage) {
  370. return resultArray(['data' => '转移成功']);
  371. } else {
  372. return resultArray(['error' => $errorMessage]);
  373. }
  374. }
  375. /**
  376. * 客户放入公海(负责人置为0)
  377. * @param
  378. * @return
  379. * @author Michael_xu
  380. */
  381. public function putInPool()
  382. {
  383. if (empty($this->param['customer_id'])) return resultArray(['error' => '请选择要放入公海的客户!']);
  384. if (!is_array($this->param['customer_id'])) return resultArray(['error' => '客户ID格式不正确!']);
  385. if (empty($this->param['pool_id'])) return resultArray(['error' => '请选择公海!']);
  386. $userInfo = $this->userInfo;
  387. $userId = $userInfo['id'];
  388. $customerIds = $this->param['customer_id'];
  389. $poolId = $this->param['pool_id'];
  390. $customerModel = new \app\crm\model\Customer();
  391. # 消息数据
  392. $message = [];
  393. # 获取客户数据
  394. $customerData = [];
  395. $customerList = db('crm_customer')->field(['customer_id', 'owner_user_id', 'name'])->whereIn('customer_id', $customerIds)->select();
  396. foreach ($customerList as $key => $value) {
  397. $customerData[$value['customer_id']] = $value;
  398. }
  399. # 整理数据
  400. $ip = request()->ip();
  401. $poolRelationData = [];
  402. $poolRecordData = [];
  403. $fieldRecordData = [];
  404. $operationLogData = [];
  405. foreach ($customerIds as $key => $value) {
  406. if (empty($customerData[$value])) {
  407. $message[] = '将客户放入公海失败,错误原因:数据不存在!';
  408. unset($customerIds[(int)$key]);
  409. continue;
  410. }
  411. if (!$customerModel->checkData($value)) {
  412. $message[] = '"' . $customerData[$value]['name'] . '"放入公海失败,错误原因:无权限';
  413. continue;
  414. }
  415. if (isset($customerData[$value]['owner_user_id']) && empty($customerData[$value]['owner_user_id'])) {
  416. $message[] = '将客户《' . $customerData[$value]['name'] . '》放入公海失败,错误原因:已经处于公海!';
  417. unset($customerIds[(int)$key]);
  418. continue;
  419. }
  420. # 公海关联数据
  421. $poolRelationData[] = [
  422. 'pool_id' => $poolId,
  423. 'customer_id' => $value
  424. ];
  425. # 公海操作记录数据
  426. $poolRecordData[] = [
  427. 'customer_id' => $value,
  428. 'user_id' => $userId,
  429. 'pool_id' => $poolId,
  430. 'type' => 2,
  431. 'create_time' => time()
  432. ];
  433. # 字段操作记录数据
  434. $fieldRecordData[] = [
  435. 'user_id' => $userId,
  436. 'types' => 'crm_customer',
  437. 'action_id' => $value,
  438. 'content' => '将客户放入公海',
  439. 'create_time' => time()
  440. ];
  441. # 数据操作日志数据
  442. $operationLogData[] = [
  443. 'user_id' => $userId,
  444. 'client_ip' => $ip,
  445. 'module' => 'crm_customer',
  446. 'action_id' => $value,
  447. 'content' => '将客户放入公海',
  448. 'create_time' => time(),
  449. 'action_name' => 'update',
  450. 'target_name' => !empty($customerData[$value]['name']) ? $customerData[$value]['name'] : ''
  451. ];
  452. }
  453. if (empty($customerIds)) return resultArray(['error' => $message]);
  454. Db::startTrans();
  455. try {
  456. # 修改客户数据
  457. Db::name('crm_customer')->whereIn('customer_id', $customerIds)->exp('before_owner_user_id', 'owner_user_id')->update([
  458. 'ro_user_id' => '',
  459. 'rw_user_id' => '',
  460. 'owner_user_id' => 0,
  461. 'into_pool_time' => time()
  462. ]);
  463. # 删除联系人的负责人
  464. Db::name('crm_contacts')->whereIn('customer_id', $customerIds)->update(['owner_user_id' => 0]);
  465. # 将客户放入公海
  466. Db::name('crm_customer_pool_relation')->insertAll($poolRelationData);
  467. # 公海操作记录
  468. Db::name('crm_customer_pool_record')->insertAll($poolRecordData);
  469. # 字段操作记录
  470. Db::name('admin_action_record')->insertAll($fieldRecordData);
  471. # 数据操作日志
  472. Db::name('admin_operation_log')->insertAll($operationLogData);
  473. Db::commit();
  474. } catch (\Exception $e) {
  475. Db::rollback();
  476. $message = ['操作失败!'];
  477. }
  478. return resultArray(!empty($message) ? ['error' => $message] : ['data' => '操作成功!']);
  479. // $param = $this->param;
  480. // $userInfo = $this->userInfo;
  481. // $customerModel = model('Customer');
  482. // $settingModel = new \app\crm\model\Setting();
  483. // if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  484. // return resultArray(['error' => '请选择需要放入公海的客户']);
  485. // }
  486. // $data = [];
  487. // $data['owner_user_id'] = 0;
  488. // $data['is_lock'] = 0;
  489. // $data['update_time'] = time();
  490. // $errorMessage = [];
  491. // foreach ($param['customer_id'] as $customer_id) {
  492. // $customerInfo = [];
  493. // $customerInfo = db('crm_customer')->where(['customer_id' => $customer_id])->find();
  494. // if (!$customerInfo) {
  495. // $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户放入公海失败,错误原因:数据不存在;';
  496. // continue;
  497. // }
  498. // //权限判断
  499. // if (!$customerModel->checkData($customer_id)) {
  500. // $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:无权限';
  501. // continue;
  502. // }
  503. // //将团队成员全部清除
  504. // $data['ro_user_id'] = '';
  505. // $data['rw_user_id'] = '';
  506. // $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  507. // if (!$resCustomer) {
  508. // $errorMessage[] = '"' . $customerInfo['name'] . '"放入公海失败,错误原因:数据出错;';
  509. // continue;
  510. // }
  511. // //联系人负责人清除
  512. // db('crm_contacts')->where(['customer_id' => $customer_id])->update(['owner_user_id' => 0]);
  513. // //修改记录
  514. // updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户放入公海');
  515. // RecordActionLog($userInfo['id'],'crm_pool','pool',$customerInfo['name'],'','','将客户'.$customerInfo['name'].'放入公海');
  516. // }
  517. // if (!$errorMessage) {
  518. // return resultArray(['data' => '操作成功']);
  519. // } else {
  520. // return resultArray(['error' => $errorMessage]);
  521. // }
  522. }
  523. /**
  524. * 客户锁定,解锁
  525. * @param is_lock 1锁定,2解锁
  526. * @return
  527. * @author Michael_xu
  528. */
  529. public function lock()
  530. {
  531. $param = $this->param;
  532. $userInfo = $this->userInfo;
  533. $customerModel = model('Customer');
  534. $customerConfigModel = model('CustomerConfig');
  535. $is_lock = ((int)$param['is_lock'] == 2) ? (int)$param['is_lock'] : 1;
  536. $lock_name = ($is_lock == 2) ? '解锁' : '锁定';
  537. if (!$param['customer_id'] || !is_array($param['customer_id'])) {
  538. return resultArray(['error' => '请选择需要' . $lock_name . '的客户']);
  539. }
  540. $data = [];
  541. $data['is_lock'] = ($is_lock == 1) ? $is_lock : 0;
  542. $data['update_time'] = time();
  543. $errorMessage = [];
  544. foreach ($param['customer_id'] as $customer_id) {
  545. $customerInfo = [];
  546. $customerInfo = $customerModel->getDataById($customer_id);
  547. if (!$customerInfo) {
  548. $errorMessage[] = '名称:为《' . $customerInfo['name'] . '》的客户' . $lock_name . '失败,错误原因:数据不存在;';
  549. continue;
  550. }
  551. //权限判断
  552. if (!$customerModel->checkData($customer_id)) {
  553. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:无权限';
  554. continue;
  555. }
  556. //锁定上限检测
  557. if ($is_lock == 1 && !$customerConfigModel->checkData($customerInfo['owner_user_id'], 2)) {
  558. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:' . $customerConfigModel->getError();
  559. continue;
  560. }
  561. //已成交客户,锁定,提示无需锁定
  562. // if ($customerInfo['deal_status'] == '已成交' && $is_lock == 1) {
  563. // $errorMessage[] = $customerInfo['name'].$lock_name.'失败,错误原因:已成交状态,无需锁定';
  564. // continue;
  565. // }
  566. $resCustomer = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  567. if (!$resCustomer) {
  568. $errorMessage[] = $customerInfo['name'] . $lock_name . '失败,错误原因:数据出错;';
  569. }
  570. //修改记录
  571. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, '', '', '将客户' . $lock_name);
  572. if ($is_lock == 2) {
  573. RecordActionLog($userInfo['id'], 'crm_customer', 'islock', $customerInfo['name'], '', '', '将客户' . $customerInfo['name'] . $lock_name);
  574. } else {
  575. RecordActionLog($userInfo['id'], 'crm_customer', 'lock', $customerInfo['name'], '', '', '将客户' . $customerInfo['name'] . $lock_name);
  576. }
  577. }
  578. if (!$errorMessage) {
  579. return resultArray(['data' => '操作成功']);
  580. } else {
  581. return resultArray(['error' => $errorMessage]);
  582. }
  583. }
  584. /**
  585. * 客户领取
  586. * @param
  587. * @return
  588. * @author Michael_xu
  589. */
  590. public function receive()
  591. {
  592. $param = $this->param;
  593. $userInfo = $this->userInfo;
  594. $customerModel = model('Customer');
  595. $customerConfigModel = model('CustomerConfig');
  596. $customer_ids = $param['customer_id'] ?: $userInfo['id'];
  597. if (!$customer_ids || !is_array($customer_ids)) {
  598. return resultArray(['error' => '请选择需要领取的客户']);
  599. }
  600. $errorMessage = [];
  601. $wherePool = $customerModel->getWhereByPool();
  602. foreach ($customer_ids as $k => $v) {
  603. $dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
  604. //判断是否是客户池数据
  605. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  606. if (!$resPool) {
  607. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:非公海数据无权操作;';
  608. continue;
  609. }
  610. //拥有客户数上限检测
  611. if (!$customerConfigModel->checkData($userInfo['id'], 1)) {
  612. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:' . $customerConfigModel->getError();
  613. continue;
  614. }
  615. $data = [];
  616. $data['owner_user_id'] = $userInfo['id'];
  617. $data['update_time'] = time();
  618. $data['deal_time'] = time();
  619. $data['follow'] = '待跟进';
  620. //将团队成员全部清除
  621. $data['ro_user_id'] = '';
  622. $data['rw_user_id'] = '';
  623. # 获取客户的时间
  624. $data['obtain_time'] = time();
  625. $resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
  626. if (!$resCustomer) {
  627. $errorMessage[] = '客户《' . $dataName . '》领取失败,错误原因:数据出错;';
  628. continue;
  629. }
  630. //联系人领取
  631. db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $userInfo['id']]);
  632. //修改记录
  633. updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '领取了客户');
  634. RecordActionLog($userInfo['id'], 'crm_customer', 'update', $dataName, '', '', '领取了客户:' . $dataName);
  635. }
  636. if (!$errorMessage) {
  637. return resultArray(['data' => '领取成功']);
  638. } else {
  639. return resultArray(['error' => $errorMessage]);
  640. }
  641. }
  642. /**
  643. * 客户分配
  644. * @param
  645. * @return
  646. * @author Michael_xu
  647. */
  648. public function distribute()
  649. {
  650. $param = $this->param;
  651. $userInfo = $this->userInfo;
  652. $customerModel = model('Customer');
  653. $userModel = new \app\admin\model\User();
  654. $customerConfigModel = model('CustomerConfig');
  655. $customer_ids = $param['customer_id'];
  656. $owner_user_id = $param['owner_user_id'];
  657. if (!$customer_ids || !is_array($customer_ids)) {
  658. return resultArray(['error' => '请选择需要分配的客户']);
  659. }
  660. if (!$owner_user_id) {
  661. return resultArray(['error' => '请选择分配人']);
  662. }
  663. $ownerUserName = $userModel->getUserNameById($owner_user_id);
  664. $errorMessage = [];
  665. $wherePool = $customerModel->getWhereByPool();
  666. foreach ($customer_ids as $k => $v) {
  667. $dataName = db('crm_customer')->where(['customer_id' => $v])->value('name');
  668. //判断是否是客户池数据
  669. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $v])->where($wherePool)->find();
  670. if (!$resPool) {
  671. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:非公海数据无权操作;';
  672. continue;
  673. }
  674. //拥有客户数上限检测
  675. if (!$customerConfigModel->checkData($owner_user_id, 1)) {
  676. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:' . $customerConfigModel->getError();
  677. continue;
  678. }
  679. $data = [];
  680. $data['owner_user_id'] = $owner_user_id;
  681. $data['update_time'] = time();
  682. $data['deal_time'] = time();
  683. $data['follow'] = '待跟进';
  684. //将团队成员全部清除
  685. $data['ro_user_id'] = '';
  686. $data['rw_user_id'] = '';
  687. # 处理分配标识,待办事项专用
  688. $data['is_allocation'] = 1;
  689. # 获取客户的时间
  690. $data['obtain_time'] = time();
  691. $resCustomer = db('crm_customer')->where(['customer_id' => $v])->update($data);
  692. if (!$resCustomer) {
  693. $errorMessage[] = '客户《' . $dataName . '》分配失败,错误原因:数据出错;';
  694. }
  695. db('crm_contacts')->where(['customer_id' => $v])->update(['owner_user_id' => $owner_user_id]);
  696. //修改记录
  697. updateActionLog($userInfo['id'], 'crm_customer', $v, '', '', '将客户分配给:' . $ownerUserName);
  698. RecordActionLog($userInfo['id'], 'crm_customer', 'distribute', $dataName, '', '', '将客户' . $dataName . '分配给:' . $ownerUserName);
  699. //站内信
  700. $send_user_id[] = $owner_user_id;
  701. $sendContent = $userInfo['realname'] . '将客户《' . $dataName . '》,分配给您';
  702. if ($send_user_id) {
  703. sendMessage($send_user_id, $sendContent, $v, 1);
  704. }
  705. }
  706. if (!$errorMessage) {
  707. return resultArray(['data' => '分配成功']);
  708. } else {
  709. return resultArray(['error' => $errorMessage]);
  710. }
  711. }
  712. /**
  713. * 客户导出
  714. * @param
  715. * @return
  716. * @author Michael_xu
  717. */
  718. public function excelExport()
  719. {
  720. $param = $this->param;
  721. $userInfo = $this->userInfo;
  722. $param['user_id'] = $userInfo['id'];
  723. $action_name = '导出全部';
  724. if ($param['customer_id']) {
  725. $param['customer_id'] = ['condition' => 'in', 'value' => $param['customer_id'], 'form_type' => 'text', 'name' => ''];
  726. $action_name = '导出选中';
  727. }
  728. $param['is_excel'] = 1;
  729. $excelModel = new \app\admin\model\Excel();
  730. // 导出的字段列表
  731. $fieldModel = new \app\admin\model\Field();
  732. $field_list = $fieldModel->getIndexFieldConfig('crm_customer', $userInfo['id'], '', 'excel');
  733. // 文件名
  734. $file_name = '5kcrm_customer_' . date('Ymd');
  735. $model = model('Customer');
  736. $temp_file = $param['temp_file'];
  737. unset($param['temp_file']);
  738. $page = $param['page'] ?: 1;
  739. unset($param['page']);
  740. unset($param['export_queue_index']);
  741. RecordActionLog($userInfo['id'], 'crm_customer', 'excelexport', $action_name, '', '', '导出客户');
  742. return $excelModel->batchExportCsv($file_name, $temp_file, $field_list, $page, function ($page, $limit) use ($model, $param, $field_list) {
  743. $param['page'] = $page;
  744. $param['limit'] = $limit;
  745. $data = $model->getDataList($param);
  746. $data['list'] = $model->exportHandle($data['list'], $field_list, 'customer');
  747. return $data;
  748. });
  749. }
  750. /**
  751. * 客户导入模板下载
  752. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  753. * @return
  754. * @author Michael_xu
  755. */
  756. public function excelDownload($save_path = '')
  757. {
  758. # 下次升级
  759. $param = $this->param;
  760. $userInfo = $this->userInfo;
  761. $excelModel = new \app\admin\model\Excel();
  762. // 导入的字段列表
  763. $fieldModel = new \app\admin\model\Field();
  764. $fieldParam['types'] = 'crm_customer';
  765. $fieldParam['action'] = 'excel';
  766. $field_list = $fieldModel->field($fieldParam);
  767. $field = [1 => [
  768. 'field' => 'owner_user_id',
  769. 'types' => 'crm_customer',
  770. 'name' => '负责人',
  771. 'form_type' => 'user',
  772. 'default_value' => '',
  773. 'is_null' => 1,
  774. 'input_tips' => '',
  775. 'setting' => array(),
  776. 'is_hidden' => 0,
  777. 'writeStatus' => 1,
  778. 'value' => '']
  779. ];
  780. $first_array = array_splice($field_list, 2, 0, $field);
  781. $excelModel->excelImportDownload($field_list, 'crm_customer', $save_path);
  782. }
  783. /**
  784. * 客户数据导入
  785. * @param
  786. * @return
  787. * @author Michael_xu
  788. */
  789. public function excelImport()
  790. {
  791. $param = $this->param;
  792. $userInfo = $this->userInfo;
  793. $excelModel = new \app\admin\model\Excel();
  794. $param['create_user_id'] = $userInfo['id'];
  795. $param['owner_user_id'] = $param['owner_user_id'] ?: 0;
  796. $param['deal_time'] = time();
  797. $param['deal_status'] = '未成交';
  798. $param['types'] = 'crm_customer';
  799. $file = request()->file('file');
  800. // $res = $excelModel->importExcel($file, $param, $this);
  801. $res = $excelModel->batchImportData($file, $param, $this);
  802. RecordActionLog($userInfo['id'], 'crm_customer', 'excel', '导入客户', '', '', '导入客户');
  803. return resultArray(['data' => $excelModel->getError()]);
  804. }
  805. /**
  806. * 客户标记为已跟进
  807. * @param
  808. * @return
  809. * @author Michael_xu
  810. */
  811. public function setFollow()
  812. {
  813. $param = $this->param;
  814. $customerIds = $param['id'] ?: [];
  815. if (!$customerIds || !is_array($customerIds)) {
  816. return resultArray(['error' => '参数错误']);
  817. }
  818. $data['follow'] = '已跟进';
  819. $data['update_time'] = time();
  820. $res = db('crm_customer')->where(['customer_id' => ['in', $customerIds]])->update($data);
  821. if (!$res) {
  822. return resultArray(['error' => '操作失败,请重试']);
  823. }
  824. return resultArray(['data' => '跟进成功']);
  825. }
  826. /**
  827. * 置顶 / 取消置顶
  828. * @return [type] [description]
  829. */
  830. public function top()
  831. {
  832. $customerModel=model('Customer');
  833. $param = $this->param;
  834. $userInfo = $this->userInfo;
  835. $param['create_role_id'] = $userInfo['id'];
  836. $param['top_time'] = time();
  837. $top_id = Db::name('crm_top')->where(['module' => ['eq', $param['module']], 'create_role_id' => ['eq', $userInfo['id']], 'module_id' => ['eq', $param['module_id']]])->column('top_id');
  838. if ($top_id) {
  839. if ($res = Db::name('crm_top')->where('top_id', $top_id[0])->update($param)) {
  840. return resultArray(['data' => $res]);
  841. } else {
  842. return resultArray(['error' => Db::name('crm_top')->getError()]);
  843. }
  844. } else {
  845. if ($res = Db::name('crm_top')->data($param)->insert()) {
  846. return resultArray(['data' => $res]);
  847. } else {
  848. return resultArray(['error' => $customerModel->getError()]);
  849. }
  850. }
  851. }
  852. /**
  853. * 客户成交状态
  854. * @param status 1已成交,2未成交
  855. * @return
  856. * @author Michael_xu
  857. */
  858. public function deal_status()
  859. {
  860. $param = $this->param;
  861. $userInfo = $this->userInfo;
  862. $statusArr = ['1' => '已成交', '2' => '未成交'];
  863. $statusList = ['1', '2'];
  864. if (!$param['customer_id'] || !in_array($param['status'], $statusList)) {
  865. return resultArray(['error' => '参数错误']);
  866. }
  867. $customerModel = model('Customer');
  868. $customerConfigModel = model('CustomerConfig');
  869. $userModel = new \app\admin\model\User();
  870. $customer_ids = $param['customer_id'];
  871. if (!is_array($customer_ids) || !$customer_ids) {
  872. $customer_ids[] = $customer_ids;
  873. }
  874. $data = [];
  875. $data['update_time'] = time();
  876. $data['deal_time'] = time();
  877. $data['deal_status'] = $statusArr[$param['status']];
  878. $errorMessage = [];
  879. foreach ($customer_ids as $customer_id) {
  880. $dataInfo = [];
  881. $dataInfo = db('crm_customer')->where(['customer_id' => $customer_id])->field('owner_user_id,deal_status,name')->find();
  882. //权限判断
  883. if (!$customerModel->checkData($customer_id, 1)) {
  884. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerModel->getError();
  885. continue;
  886. }
  887. $owner_user_id = $dataInfo['owner_user_id'];;
  888. if (!$owner_user_id) {
  889. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:公海数据无权操作';
  890. continue;
  891. }
  892. //拥有客户数上限检测
  893. if ($statusArr[$param['status']] == '未成交' && $dataInfo['deal_status'] == '已成交') {
  894. if (!$customerConfigModel->checkData($owner_user_id, 1, 1)) {
  895. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:' . $customerConfigModel->getError();
  896. continue;
  897. }
  898. }
  899. if ($statusArr[$param['status']] == '已成交') {
  900. $data['is_lock'] = 0;
  901. }
  902. $res = db('crm_customer')->where(['customer_id' => $customer_id])->update($data);
  903. if (!$res) {
  904. $errorMessage[] = '名称:为《' . $dataInfo['name'] . '》的客户更改失败,错误原因:操作失败,请重试!';
  905. continue;
  906. }
  907. //修改记录
  908. updateActionLog($userInfo['id'], 'crm_customer', $customer_id, ['deal_status' => $dataInfo['deal_status']], ['deal_status' => $data['deal_status']]);
  909. if ($param['status'] == 1) {
  910. RecordActionLog($userInfo['id'], 'crm_customer', 'status', $dataInfo['name'], '', '', '修改客户:' . $dataInfo['name'] . '成交状态:' . $statusArr[$param['status']]);
  911. } else {
  912. RecordActionLog($userInfo['id'], 'crm_customer', 'status', $dataInfo['name'], '', '', '修改客户:' . $dataInfo['name'] . '成交状态:' . $statusArr[$param['status']]);
  913. }
  914. }
  915. if (!$errorMessage) {
  916. return resultArray(['data' => '操作成功']);
  917. } else {
  918. return resultArray(['error' => $errorMessage]);
  919. }
  920. }
  921. /**
  922. * 设置关注
  923. *
  924. * @return \think\response\Json
  925. * @throws \think\Exception
  926. * @throws \think\exception\PDOException
  927. */
  928. public function star()
  929. {
  930. $userId = $this->userInfo['id'];
  931. $targetId = $this->param['target_id'];
  932. $type = $this->param['type'];
  933. if (empty($userId) || empty($targetId) || empty($type)) return resultArray(['error' => '缺少必要参数!']);
  934. if (!$this->setStar($type, $userId, $targetId)) {
  935. return resultArray(['error' => '设置关注失败!']);
  936. }
  937. return resultArray(['data' => '设置关注成功!']);
  938. }
  939. /**
  940. * 附近客户
  941. *
  942. * @return \think\response\Json
  943. */
  944. public function nearby()
  945. {
  946. if (empty($this->param['lng'])) return resultArray(['error' => '缺少经度参数!']);
  947. if (empty($this->param['lat'])) return resultArray(['error' => '缺少纬度参数!']);
  948. if (empty($this->param['distance'])) return resultArray(['error' => '请选择距离!']);
  949. $customerModel = model('Customer');
  950. $data = $customerModel->getNearbyList($this->param);
  951. return resultArray(['data' => $data]);
  952. }
  953. /**
  954. * 系统信息
  955. *
  956. * @return \think\response\Json
  957. * @throws \think\db\exception\DataNotFoundException
  958. * @throws \think\db\exception\ModelNotFoundException
  959. * @throws \think\exception\DbException
  960. */
  961. public function system()
  962. {
  963. if (empty($this->param['id'])) return resultArray(['error' => '参数错误!']);
  964. $customerModel = new \app\crm\model\Customer();
  965. $data = $customerModel->getSystemInfo($this->param['id']);
  966. return resultArray(['data' => $data]);
  967. }
  968. /**
  969. * table标签栏数量
  970. *
  971. * @return \think\response\Json
  972. * @throws \think\db\exception\DataNotFoundException
  973. * @throws \think\db\exception\ModelNotFoundException
  974. * @throws \think\exception\DbException
  975. */
  976. public function count()
  977. {
  978. if (empty($this->param['customer_id'])) return resultArray(['error' => '参数错误!']);
  979. $userInfo = $this->userInfo;
  980. $customerId = $this->param['customer_id'];
  981. # 联系人
  982. $contactsAuth = $this->getContactsSearchWhere($userInfo['id']);
  983. $contactsCount = Db::name('crm_contacts')->where('customer_id', $customerId)->where($contactsAuth)->count();
  984. # 团队成员
  985. $customer = Db::name('crm_customer')->field(['owner_user_id', 'ro_user_id', 'rw_user_id'])->where('customer_id', $customerId)->find();
  986. $customer['ro_user_id'] = explode(',', trim($customer['ro_user_id'], ','));
  987. $customer['rw_user_id'] = explode(',', trim($customer['rw_user_id'], ','));
  988. $customer['owner_user_id'] = [$customer['owner_user_id']];
  989. $teamCount = array_filter(array_unique(array_merge($customer['ro_user_id'], $customer['rw_user_id'], $customer['owner_user_id'])));
  990. # 商机
  991. $businessAuth = $this->getBusinessSearchWhere($userInfo['id']);
  992. $businessCount = Db::name('crm_business')->where('customer_id', $customerId)->where($businessAuth)->count();
  993. # 合同
  994. $contractAuth = $this->getContractSearchWhere($userInfo['id']);
  995. $contractCount = Db::name('crm_contract')->where('customer_id', $customerId)->where($contractAuth)->count();
  996. # 回款
  997. $receivablesAuth = $this->getReceivablesSearchWhere();
  998. $receivablesCount = Db::name('crm_receivables')->where('customer_id', $customerId)->whereIn('owner_user_id', $receivablesAuth)->count();
  999. # 回访
  1000. $visitAuth = $this->getVisitSearchWhere($userInfo['id']);
  1001. $visitCount = Db::name('crm_visit')->where(['customer_id' => $customerId, 'deleted_state' => 0])->where($visitAuth)->count();
  1002. # 发票
  1003. $invoiceAuth = $this->getInvoiceSearchWhere();
  1004. $invoiceCount = Db::name('crm_invoice')->where('customer_id', $customerId)->whereIn('owner_user_id', $invoiceAuth)->count();
  1005. # 附件
  1006. $fileCount = Db::name('crm_customer_file')->alias('customer')->join('__ADMIN_FILE__ file', 'file.file_id = customer.file_id')->where('customer_id', $customerId)->count();
  1007. $data = [
  1008. 'businessCount' => $businessCount,
  1009. 'contactCount' => $contactsCount,
  1010. 'contractCount' => $contractCount,
  1011. 'fileCount' => $fileCount,
  1012. 'invoiceCount' => $invoiceCount,
  1013. 'memberCount' => count($teamCount),
  1014. 'receivablesCount' => $receivablesCount,
  1015. 'returnVisitCount' => $visitCount
  1016. ];
  1017. return resultArray(['data' => $data]);
  1018. }
  1019. /**
  1020. * 公海权限
  1021. *
  1022. * @param CustomerLogic $customerLogic
  1023. * @return \think\response\Json
  1024. * @throws \think\db\exception\DataNotFoundException
  1025. * @throws \think\db\exception\ModelNotFoundException
  1026. * @throws \think\exception\DbException
  1027. */
  1028. public function poolAuthority(CustomerLogic $customerLogic)
  1029. {
  1030. $authority = [
  1031. 'delete' => false, # 删除
  1032. 'distribute' => false, # 分配
  1033. 'excelexport' => false, # 导出
  1034. 'index' => false, # 列表
  1035. 'receive' => false, # 领取
  1036. ];
  1037. $userId = $this->userInfo['id'];
  1038. if (empty($userId)) return resultArray(['data' => $authority]);
  1039. # 员工角色数据
  1040. $groupIds = $customerLogic->getEmployeeGroups($userId);
  1041. # 员工角色下的规则数据
  1042. $ruleIds = $customerLogic->getEmployeeRules($groupIds);
  1043. # 公海规则数据
  1044. $poolRules = $customerLogic->getPoolRules();
  1045. # 整理员工规则数据
  1046. $rules = [];
  1047. $ruleIds = implode(',', $ruleIds);
  1048. $rules = array_filter(array_unique(explode(',', $ruleIds)));
  1049. # 整理公海规则数据
  1050. $deleteId = $distributeId = $exportId = $indexId = $receiveId = 0;
  1051. foreach ($poolRules as $key => $value) {
  1052. if ($value['name'] == 'pool') $indexId = $value['id'];
  1053. if ($value['name'] == 'distribute') $distributeId = $value['id'];
  1054. if ($value['name'] == 'receive') $receiveId = $value['id'];
  1055. if ($value['name'] == 'poolExcelExport') $exportId = $value['id'];
  1056. if ($value['name'] == 'poolDelete') $deleteId = $value['id'];
  1057. }
  1058. # 权限判断
  1059. $authority['delete'] = $userId == 1 || in_array(1, $groupIds) || in_array($deleteId, $rules) ? true : false;
  1060. $authority['distribute'] = $userId == 1 || in_array(1, $groupIds) || in_array($distributeId, $rules) ? true : false;
  1061. $authority['excelexport'] = $userId == 1 || in_array(1, $groupIds) || in_array($exportId, $rules) ? true : false;
  1062. $authority['index'] = $userId == 1 || in_array(1, $groupIds) || in_array($indexId, $rules) ? true : false;
  1063. $authority['receive'] = $userId == 1 || in_array(1, $groupIds) || in_array($receiveId, $rules) ? true : false;
  1064. return resultArray(['data' => $authority]);
  1065. }
  1066. /**
  1067. * 客户级别列表
  1068. *
  1069. * @return \think\response\Json
  1070. * @since 2021-03-29
  1071. * @author fanqi
  1072. */
  1073. public function level()
  1074. {
  1075. $data = db('admin_field')->where(['types' => 'crm_customer', 'field' => 'level'])->value('setting');
  1076. $data = explode(chr(10), $data);
  1077. return resultArray(['data' => $data]);
  1078. }
  1079. }