Customer.php 47KB

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