Field.php 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 自定义字段
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\controller;
  8. use app\admin\logic\FieldGrantLogic;
  9. use app\crm\logic\VisitLogic;
  10. use app\crm\model\Business;
  11. use app\crm\model\Contacts;
  12. use app\crm\model\Contract;
  13. use app\crm\model\Customer;
  14. use app\crm\model\InvoiceInfoLogic;
  15. use app\crm\model\Leads;
  16. use app\crm\model\Product;
  17. use app\crm\model\Receivables;
  18. use think\Hook;
  19. use think\Request;
  20. use think\Db;
  21. use app\admin\model\User as UserModel;
  22. class Field extends ApiCommon
  23. {
  24. /**
  25. * 用于判断权限
  26. * @permission 无限制
  27. * @allow 登录用户可访问
  28. * @other 其他根据系统设置
  29. **/
  30. public function _initialize()
  31. {
  32. $action = [
  33. 'permission'=>[''],
  34. 'allow'=>['index','getfield','update','read','config','validates','configindex','columnwidth','uniquefield']
  35. ];
  36. Hook::listen('check_auth',$action);
  37. $request = Request::instance();
  38. $a = strtolower($request->action());
  39. if (!in_array($a, $action['permission'])) {
  40. parent::_initialize();
  41. }
  42. }
  43. /**
  44. * 自定义字段列表
  45. */
  46. public function index()
  47. {
  48. //权限判断
  49. if (!checkPerByAction('admin', 'crm', 'field')) {
  50. header('Content-Type:application/json; charset=utf-8');
  51. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  52. }
  53. $param = $this->param;
  54. $types_arr = [
  55. ['types' => 'crm_leads','name' => '线索管理'],
  56. ['types' => 'crm_customer','name' => '客户管理'],
  57. ['types' => 'crm_contacts','name' => '联系人管理'],
  58. ['types' => 'crm_product','name' => '产品管理'],
  59. ['types' => 'crm_business','name' => '商机管理'],
  60. ['types' => 'crm_contract','name' => '合同管理'],
  61. ['types' => 'crm_receivables','name' => '回款管理'],
  62. ['types' => 'crm_visit','name' => '客户回访管理'],
  63. ];
  64. $examine_types_arr = [];
  65. switch ($param['type']) {
  66. case 'crm' : $typesArr = $types_arr; break;
  67. case 'examine' : $typesArr = $examine_types_arr; break;
  68. default : $typesArr = $types_arr; break;
  69. }
  70. foreach ($typesArr as $k=>$v) {
  71. $updateTime = db('admin_field')->where(['types' => $v['types']])->max('update_time');
  72. $typesArr[$k]['update_time'] = !empty($updateTime) ? date('Y-m-d H:i:s', $updateTime) : '';
  73. }
  74. return resultArray(['data' => $typesArr]);
  75. }
  76. /**
  77. * 自定义字段数据
  78. */
  79. public function read()
  80. {
  81. $fieldModel = model('Field');
  82. $param = $this->param;
  83. $data = $fieldModel->getDataList($param);
  84. if ($data === false) {
  85. return resultArray(['error' => $fieldModel->getError()]);
  86. }
  87. return resultArray(['data' => $data]);
  88. }
  89. /**
  90. * 自定义字段创建
  91. */
  92. public function update()
  93. {
  94. # 权限判断 todo 允许有相应权限的普通员工操作,暂时注释代码,后期修改权限验证。
  95. // if (!checkPerByAction('admin', 'crm', 'field')) {
  96. // header('Content-Type:application/json; charset=utf-8');
  97. // exit(json_encode(['code'=>102,'error'=>'无权操作']));
  98. // }
  99. # 系统审批类型暂不支持编辑
  100. if ($this->param['types'] == 'oa_examine' && $this->param['types_id'] < 7) {
  101. return resultArray(['error' => '系统审批类型暂不支持编辑']);
  102. }
  103. $userInfo=$this->userInfo;
  104. $fieldModel = model('Field');
  105. $param = $this->param;
  106. $types = $param['types'];
  107. $types_id = $param['types_id'] ? : 0;
  108. // $data['types'] = $param['types'];
  109. $data = $param['data'];
  110. $saveParam = []; # 新增数据
  111. $updateParam = []; # 编辑数据
  112. $delParam = []; # 删除数据
  113. $fieldIds = []; # 删除数据(兼容前端11.*.*版本)
  114. $errorMessage = []; # 错误数据
  115. $i = 0;
  116. foreach ($data AS $k => $v) {
  117. $i++;
  118. # 必填的字段不可以隐藏
  119. if (!empty($v['is_null']) && !empty($v['is_hidden'])) {
  120. $errorMessage = '必填的字段不可以隐藏!';
  121. break;
  122. }
  123. if ($v['field_id']) {
  124. if (isset($v['is_deleted']) && $v['is_deleted'] == '1') {
  125. # 删除
  126. $delParam[] = $v['field_id']; //删除
  127. } else {
  128. # 编辑
  129. $updateParam[$k] = $v;
  130. $updateParam[$k]['order_id'] = $i;
  131. # 用来删除自定义字段(兼容前端11.*.*版本):记录存在的自定义字段ID,取出差集,就是要删的数。
  132. $fieldIds[] = $v['field_id'];
  133. }
  134. } else {
  135. # 新增
  136. $saveParam[$k] = $v;
  137. $saveParam[$k]['order_id'] = $i;
  138. $saveParam[$k]['types_id'] = $types_id;
  139. }
  140. }
  141. # 必填的字段不可以隐藏
  142. if ($errorMessage) return resultArray(['error' => $errorMessage]);
  143. # 兼容前端11.*.*版本的删除条件处理,通过比较差异,来确定谁被前端给删除了 todo 这段代码需要写在新增上面,不然会把新增的给删除掉
  144. $oldFieldIds = Db::name('admin_field')->where('types', $types)->column('field_id');
  145. $deleteIds = array_diff($oldFieldIds, $fieldIds);
  146. foreach ($deleteIds AS $key => $value) {
  147. if (!in_array($value, $delParam)) $delParam[] = $value;
  148. }
  149. $recordModules = [
  150. 'crm_leads' => '线索',
  151. 'crm_customer' => '客户',
  152. 'crm_pool' => '客户公海',
  153. 'crm_contacts' => '联系人',
  154. 'crm_product' => '产品',
  155. 'crm_business' => '商机',
  156. 'crm_contract' => '合同',
  157. 'crm_receivables' => '回款',
  158. 'crm_visit' => '回访',
  159. 'crm_invoice' => '回款',
  160. 'oa_log' => '办公日志',
  161. 'oa_examine' => '办公审批',
  162. ];
  163. # 新增
  164. if (!empty($saveParam)) {
  165. if (!$data = $fieldModel->createData($types, $saveParam)) {
  166. $errorMessage[] = $fieldModel->getError();
  167. }
  168. }
  169. # 编辑
  170. if (!empty($updateParam)) {
  171. if (!$data = $fieldModel->updateDataById($updateParam, $types)) {
  172. $errorMessage[] = $fieldModel->getError();
  173. }
  174. }
  175. # 删除
  176. if (!empty($delParam)) {
  177. if (!$data = $fieldModel->delDataById($delParam, $types)) {
  178. $errorMessage[] = $fieldModel->getError();
  179. }
  180. }
  181. # 自定义字段变更后,同步更新字段授权表
  182. (new FieldGrantLogic())->fieldGrantDiyHandle($types);
  183. if ($errorMessage) {
  184. return resultArray(['error' => $errorMessage]);
  185. } else {
  186. # 系统操作记录
  187. $recordModules = [
  188. 'crm_leads' => '线索',
  189. 'crm_customer' => '客户',
  190. 'crm_pool' => '客户公海',
  191. 'crm_contacts' => '联系人',
  192. 'crm_product' => '产品',
  193. 'crm_business' => '商机',
  194. 'crm_contract' => '合同',
  195. 'crm_receivables' => '回款',
  196. 'crm_visit' => '回访',
  197. 'crm_invoice' => '回款',
  198. 'oa_log' => '办公日志',
  199. 'oa_examine' => '办公审批',
  200. ];
  201. if($types !== 'oa_examine'){
  202. $systemModules='customer';
  203. }else{
  204. $systemModules='approval';
  205. }
  206. SystemActionLog($userInfo['id'], $types,$systemModules, 1, 'update', $recordModules[$types], '','','编辑了自定义字段:'.$recordModules[$types]);
  207. return resultArray(['data' => '修改成功']);
  208. }
  209. }
  210. /**
  211. * 自定义字段数据获取
  212. *
  213. * @return \think\response\Json
  214. * @throws \think\db\exception\DataNotFoundException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. * @throws \think\exception\DbException
  217. */
  218. public function getField()
  219. {
  220. $fieldModel = model('Field');
  221. $userModel = model('User');
  222. $param = $this->param;
  223. $module = trim($param['module']);
  224. $controller = trim($param['controller']);
  225. $action = trim($param['action']);
  226. $system = !empty($param['system']) ? $param['system'] : 0;
  227. unset($param['system']);
  228. if (!$module || !$controller || !$action) {
  229. return resultArray(['error' => '参数错误']);
  230. }
  231. //判断权限
  232. $userInfo = $this->userInfo;
  233. $user_id = $userInfo['id'];
  234. $types = $param['types'];
  235. $types_id = $param['types_id'] ? : '';
  236. $dataInfo = [];
  237. if ($action == 'read' || $action == 'update') {
  238. //获取详情数据
  239. if (($param['action'] == 'update' || $param['action'] == 'read') && $param['action_id']) {
  240. switch ($param['types']) {
  241. case 'crm_customer' :
  242. $customerModel = new \app\crm\model\Customer();
  243. $dataInfo = $customerModel->getDataById(intval($param['action_id']));
  244. // 公海
  245. if (!empty($param['pool_id'])) {
  246. $data = $fieldModel->getPoolFieldData($param['pool_id'], $dataInfo);
  247. return resultArray(['data' => $data]);
  248. }
  249. //判断权限
  250. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', $param['action']);
  251. //读写权限
  252. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  253. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  254. //判断是否客户池数据
  255. $wherePool = $customerModel->getWhereByPool();
  256. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['action_id']])->where($wherePool)->find();
  257. if (!$resPool && !in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  258. header('Content-Type:application/json; charset=utf-8');
  259. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  260. }
  261. break;
  262. case 'crm_leads' :
  263. $leadsModel = new \app\crm\model\Leads();
  264. $dataInfo = $leadsModel->getDataById(intval($param['action_id']));
  265. //判断权限
  266. $auth_user_ids = $userModel->getUserByPer('crm', 'leads', $param['action']);
  267. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  268. header('Content-Type:application/json; charset=utf-8');
  269. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  270. }
  271. break;
  272. case 'crm_contacts' :
  273. $contactsModel = new \app\crm\model\Contacts();
  274. $dataInfo = $contactsModel->getDataById(intval($param['action_id']));
  275. //判断权限
  276. $auth_user_ids = $userModel->getUserByPer('crm', 'contacts', $param['action']);
  277. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  278. header('Content-Type:application/json; charset=utf-8');
  279. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  280. }
  281. break;
  282. case 'crm_business' :
  283. $businessModel = new \app\crm\model\Business();
  284. $dataInfo = $businessModel->getDataById(intval($param['action_id']));
  285. //判断权限
  286. $auth_user_ids = $userModel->getUserByPer('crm', 'business', $param['action']);
  287. //读写权限
  288. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  289. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  290. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  291. header('Content-Type:application/json; charset=utf-8');
  292. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  293. }
  294. break;
  295. case 'crm_contract' :
  296. $contractModel = new \app\crm\model\Contract();
  297. $dataInfo = $contractModel->getDataById(intval($param['action_id']));
  298. //判断权限
  299. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', $param['action']);
  300. //读写权限
  301. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  302. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  303. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  304. header('Content-Type:application/json; charset=utf-8');
  305. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  306. }
  307. break;
  308. case 'crm_product' :
  309. $productModel = new \app\crm\model\Product();
  310. $dataInfo = $productModel->getDataById(intval($param['action_id']));
  311. break;
  312. case 'crm_receivables' :
  313. $receivablesModel = new \app\crm\model\Receivables();
  314. $dataInfo = $receivablesModel->getDataById(intval($param['action_id']));
  315. //判断权限
  316. $auth_user_ids = $userModel->getUserByPer('crm', 'receivables', $param['action']);
  317. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  318. header('Content-Type:application/json; charset=utf-8');
  319. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  320. }
  321. break;
  322. case 'crm_receivables_plan' :
  323. $receivablesPlanModel = new \app\crm\model\ReceivablesPlan();
  324. $dataInfo = $receivablesPlanModel->getDataById(intval($param['action_id']));
  325. break;
  326. case 'oa_examine' :
  327. $examineModel = new \app\oa\model\Examine();
  328. $examineFlowModel = new \app\admin\model\ExamineFlow();
  329. $dataInfo = $examineModel->getDataById(intval($param['action_id']));
  330. # 前端没有传types_id,这里需要指定一下types_id
  331. if (!empty($dataInfo['category_id'])) $param['types_id'] = $dataInfo['category_id'];
  332. $adminIds = $userModel->getAdminId(); //管理员
  333. $checkUserIds = $examineFlowModel->getUserByFlow($dataInfo['flow_id'], $dataInfo['create_user_id'], $dataInfo['check_user_id']);
  334. if (((int)$dataInfo['create_user_id'] != $user_id && !in_array($user_id,$adminIds) && !in_array($user_id,$checkUserIds))) {
  335. header('Content-Type:application/json; charset=utf-8');
  336. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  337. }
  338. break;
  339. case 'crm_visit' :
  340. $visit = new \app\crm\model\Visit();
  341. $dataInfo = $visit->getDataById(intval($param['action_id']));
  342. $fieldModel = new \app\admin\model\Field();
  343. $datetimeField = $fieldModel->getFieldByFormType('crm_visit', 'datetime'); //日期时间类型
  344. foreach ($datetimeField as $key => $val) {
  345. $dataInfo[$val] = !empty($dataInfo[$val]) ? date('Y-m-d H:i:s', $dataInfo[$val]) : null;
  346. }
  347. //判断权限
  348. $auth_user_ids = $userModel->getUserByPer('crm', 'visit', $param['action']);
  349. //读写权限
  350. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  351. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  352. if (!in_array($user_id, stringToArray($dataInfo['owner_user_id'])) && !in_array($user_id,$auth_user_ids) && !$roPre && !$rwPre) {
  353. header('Content-Type:application/json; charset=utf-8');
  354. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  355. }
  356. break;
  357. }
  358. }
  359. }
  360. $param['user_id'] = $user_id;
  361. $action_id = $param['action_id'] ? : '';
  362. $data = $fieldModel->field($param, $dataInfo) ? : [];
  363. # 回访模块下,负责人名称变更为回访人
  364. if ($param['types'] == 'crm_visit') {
  365. foreach ($data AS $key => $value) {
  366. if ($value['field'] == 'owner_user_id') {
  367. $data[$key]['name'] = '回访人';
  368. break;
  369. }
  370. }
  371. }
  372. # 去掉客户模块下的成交信息
  373. if ($param['types'] == 'crm_customer' && $param['action'] == 'read') {
  374. foreach ($data AS $key => $value) {
  375. if ($value['field'] == 'deal_status') {
  376. unset($data[(int)$key]);
  377. break;
  378. }
  379. }
  380. }
  381. # 合同回款 基本信息审核状态
  382. if(in_array($param['types'], ['crm_receivables', 'crm_contract']) && $param['action'] == 'read'){
  383. $check=['0'=>'待审核','1'=>'审核中','2'=>'审核通过','3'=>'审核未通过','4'=>'撤销','5'=>'草稿(未提交)','6'=>'作废'];
  384. $data[] = [
  385. 'field' => 'check_status',
  386. 'name' => '审核状态',
  387. 'form_type' => 'text',
  388. 'writeStatus' => 0,
  389. 'fieldName' => 'check_status',
  390. 'value' => $check[$dataInfo['check_status']],
  391. ];
  392. }
  393. # 合同自动编号设置
  394. if ($param['types'] == 'crm_contract') {
  395. foreach ($data AS $key => $value) {
  396. if ($value['field'] == 'num') {
  397. if ($this->getAutoNumberStatus(1)) {
  398. $data[$key]['is_null'] = 0;
  399. $data[$key]['is_unique'] = 0;
  400. }
  401. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(1) ? 1 : 0;
  402. }
  403. }
  404. }
  405. # 回款自动编号设置
  406. if ($param['types'] == 'crm_receivables') {
  407. foreach ($data AS $key => $value) {
  408. if ($value['field'] == 'number') {
  409. if ($this->getAutoNumberStatus(2)) {
  410. $data[$key]['is_null'] = 0;
  411. $data[$key]['is_unique'] = 0;
  412. }
  413. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(2) ? 1 : 0;
  414. }
  415. }
  416. }
  417. # 回访自动编号设置
  418. if ($param['types'] == 'crm_visit') {
  419. foreach ($data AS $key => $value) {
  420. if ($value['field'] == 'number') {
  421. if ($this->getAutoNumberStatus(3)) {
  422. $data[$key]['is_null'] = 0;
  423. $data[$key]['is_unique'] = 0;
  424. }
  425. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(3) ? 1 : 0;
  426. }
  427. }
  428. }
  429. # 隐藏回款计划中的附件
  430. if ($param['types'] == 'crm_receivables_plan') {
  431. foreach ($data AS $key => $value) {
  432. if ($value['field'] == 'file') {
  433. unset($data[(int)$key]);
  434. }
  435. }
  436. }
  437. if (!empty($system) && $system == 1) {
  438. # 商机和合同排除产品字段
  439. if (in_array($param['types'], ['crm_business', 'crm_contract'])) {
  440. foreach ($data AS $key => $value) {
  441. if ($value['field'] == 'product' && $value['name'] == '产品') {
  442. unset($data[(int)$key]);
  443. break;
  444. }
  445. }
  446. }
  447. $data = array_values($data);
  448. # 系统信息
  449. switch ($types) {
  450. case 'crm_leads' :
  451. $leadsModel = new Leads();
  452. $leadsData = $leadsModel->getSystemInfo($action_id);
  453. $leadsArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间'];
  454. foreach ($leadsData AS $key => $value) {
  455. if (empty($leadsArray[$key])) continue;
  456. $data[] = [
  457. 'field' => $key,
  458. 'name' => $leadsArray[$key],
  459. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  460. 'value' => $value,
  461. 'system' => 1
  462. ];
  463. }
  464. break;
  465. case 'crm_customer' :
  466. $customerModel = new Customer();
  467. $customerData = $customerModel->getSystemInfo($action_id);
  468. $customerArray = ['obtain_time' => '负责人获取客户时间', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间', 'last_record' => '最后跟进记录', 'deal_status' => '成交状态'];
  469. foreach ($customerData AS $key => $value) {
  470. if (empty($customerArray[$key])) continue;
  471. $data[] = [
  472. 'field' => $key,
  473. 'name' => $customerArray[$key],
  474. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  475. 'value' => $value,
  476. 'system' => 1
  477. ];
  478. }
  479. break;
  480. case 'crm_contacts' :
  481. $contactsModel = new Contacts();
  482. $contactsData = $contactsModel->getSystemInfo($action_id);
  483. $contactsArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间'];
  484. foreach ($contactsData AS $key => $value) {
  485. if (empty($contactsArray[$key])) continue;
  486. $data[] = [
  487. 'field' => $key,
  488. 'name' => $contactsArray[$key],
  489. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  490. 'value' => $value,
  491. 'system' => 1
  492. ];
  493. }
  494. break;
  495. case 'crm_business' :
  496. $businessModel = new Business();
  497. $businessData = $businessModel->getSystemInfo($action_id);
  498. $businessArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间'];
  499. foreach ($businessData AS $key => $value) {
  500. if (empty($businessArray[$key])) continue;
  501. $data[] = [
  502. 'field' => $key,
  503. 'name' => $businessArray[$key],
  504. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  505. 'value' => $value,
  506. 'system' => 1
  507. ];
  508. }
  509. break;
  510. case 'crm_contract' :
  511. $contractModel = new Contract();
  512. $contractData = $contractModel->getSystemInfo($action_id);
  513. $contractArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'last_time' => '最后跟进时间', 'done_money' => '已收款金额', 'un_money' => '未收款金额'];
  514. foreach ($contractData AS $key => $value) {
  515. if (empty($contractArray[$key])) continue;
  516. $data[] = [
  517. 'field' => $key,
  518. 'name' => $contractArray[$key],
  519. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  520. 'value' => $value,
  521. 'system' => 1
  522. ];
  523. }
  524. break;
  525. case 'crm_receivables' :
  526. $receivablesModel = new Receivables();
  527. $receivablesData = $receivablesModel->getSystemInfo($action_id);
  528. $receivablesArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  529. foreach ($receivablesData AS $key => $value) {
  530. if (empty($receivablesArray[$key])) continue;
  531. $data[] = [
  532. 'field' => $key,
  533. 'name' => $receivablesArray[$key],
  534. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  535. 'value' => $value,
  536. 'system' => 1
  537. ];
  538. }
  539. break;
  540. case 'crm_product' :
  541. $productModel = new Product();
  542. $productData = $productModel->getSystemInfo($action_id);
  543. $productArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  544. foreach ($productData AS $key => $value) {
  545. if (empty($productArray[$key])) continue;
  546. $data[] = [
  547. 'field' => $key,
  548. 'name' => $productArray[$key],
  549. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  550. 'value' => $value,
  551. 'system' => 1
  552. ];
  553. }
  554. break;
  555. case 'crm_visit' :
  556. $visitLogic = new VisitLogic();
  557. $visitData = $visitLogic->getSystemInfo($action_id);
  558. $visitArray = ['create_user_id' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  559. foreach ($visitData AS $key => $value) {
  560. if (empty($visitArray[$key])) continue;
  561. $data[] = [
  562. 'field' => $key,
  563. 'name' => $visitArray[$key],
  564. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  565. 'value' => $value,
  566. 'system' => 1
  567. ];
  568. }
  569. break;
  570. }
  571. }
  572. $data = $fieldModel->resetField($user_id, $param['types'], $param['action'], $data);
  573. return resultArray(['data' => array_values($data)]);
  574. }
  575. /**
  576. * 自定义字段数据验重
  577. *
  578. * @return \think\response\Json
  579. */
  580. public function validates()
  581. {
  582. $param = $this->param;
  583. $fieldModel = model('Field');
  584. if (is_array($param['val'])) {
  585. //多选类型暂不验证
  586. return resultArray(['data' => '验证通过']);
  587. }
  588. $res = $fieldModel->getValidate(trim($param['field']), trim($param['val']), intval($param['id']), trim($param['types']));
  589. if (!$res) {
  590. return resultArray(['error' => $fieldModel->getError()]);
  591. }
  592. return resultArray(['data' => '验证通过']);
  593. }
  594. /**
  595. * 自定义字段列表设置(排序、展示、列宽度)
  596. * @param types 分类
  597. * @param value 值
  598. */
  599. public function config()
  600. {
  601. $param = $this->param;
  602. $userInfo = $this->userInfo;
  603. $param['user_id'] = $userInfo['id'];
  604. $userFieldModel = model('UserField');
  605. $res = $userFieldModel->updateConfig($param['types'], $param);
  606. if (!$res) {
  607. return resultArray(['error' => $userFieldModel->getError()]);
  608. }
  609. return resultArray(['data' => '设置成功']);
  610. }
  611. /**
  612. * 自定义字段列宽度设置
  613. * @param types 分类
  614. * @param field 字段名
  615. * @param width 列宽度
  616. */
  617. public function columnWidth()
  618. {
  619. $param = $this->param;
  620. $userInfo = $this->userInfo;
  621. $userFieldModel = model('UserField');
  622. $width = $param['width'] > 10 ? $param['width'] : '';
  623. $unField = array('pool_day','owner_user_name','is_lock','create_user_name');
  624. switch ($param['field']) {
  625. case 'status_id_info' : $param['field'] = 'status_id';
  626. break;
  627. }
  628. if (!in_array($param['field'],$unField)) {
  629. $res = $userFieldModel->setColumnWidth($param['types'], $param['field'], $width, $userInfo['id']);
  630. if (!$res) {
  631. return resultArray(['error' => $userFieldModel->getError()]);
  632. }
  633. }
  634. return resultArray(['data' => '设置成功']);
  635. }
  636. /**
  637. * 自定义字段列表设置数据
  638. * @param types 分类
  639. * @param value 值
  640. */
  641. public function configIndex()
  642. {
  643. $param = $this->param;
  644. $userInfo = $this->userInfo;
  645. $userFieldModel = model('UserField');
  646. $res = $userFieldModel->getDataList($param['types'], $userInfo['id']);
  647. if (!$res) {
  648. return resultArray(['error' => $userFieldModel->getError()]);
  649. }
  650. return resultArray(['data' => $res]);
  651. }
  652. /**
  653. * 自定义验重字段
  654. * @param types 分类
  655. * @param
  656. */
  657. public function uniqueField()
  658. {
  659. $param = $this->param;
  660. if ($param['types'] == 'crm_user') {
  661. $list = array_filter(UserModel::$import_field_list, function ($val) {
  662. return $val['is_unique'] == 1;
  663. });
  664. $list = array_column($list, 'name');
  665. } else {
  666. $list = db('admin_field')->where(['types' => $param['types'],'is_unique' => 1])->column('name');
  667. }
  668. $list = $list ? implode(',',$list) : '无';
  669. return resultArray(['data' => $list]);
  670. }
  671. /**
  672. * 获取自动编号状态
  673. *
  674. * @param $type
  675. * @return int|mixed|string|null
  676. */
  677. private function getAutoNumberStatus($type)
  678. {
  679. return Db::name('crm_number_sequence')->where('number_type', $type)->where('status', 0)->value('number_sequence_id');
  680. }
  681. }