Field.php 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. $fieldModel = model('Field');
  104. $param = $this->param;
  105. $types = $param['types'];
  106. $types_id = $param['types_id'] ? : 0;
  107. // $data['types'] = $param['types'];
  108. $data = $param['data'];
  109. $saveParam = []; # 新增数据
  110. $updateParam = []; # 编辑数据
  111. $delParam = []; # 删除数据
  112. $fieldIds = []; # 删除数据(兼容前端11.*.*版本)
  113. $i = 0;
  114. foreach ($data AS $k => $v) {
  115. $i++;
  116. if ($v['field_id']) {
  117. if (isset($v['is_deleted']) && $v['is_deleted'] == '1') {
  118. # 删除
  119. $delParam[] = $v['field_id']; //删除
  120. } else {
  121. # 编辑
  122. $updateParam[$k] = $v;
  123. $updateParam[$k]['order_id'] = $i;
  124. # 用来删除自定义字段(兼容前端11.*.*版本):记录存在的自定义字段ID,取出差集,就是要删的数。
  125. $fieldIds[] = $v['field_id'];
  126. }
  127. } else {
  128. # 新增
  129. $saveParam[$k] = $v;
  130. $saveParam[$k]['order_id'] = $i;
  131. $saveParam[$k]['types_id'] = $types_id;
  132. }
  133. }
  134. # 错误数据
  135. $errorMessage = [];
  136. # 兼容前端11.*.*版本的删除条件处理,通过比较差异,来确定谁被前端给删除了 todo 这段代码需要写在新增上面,不然会把新增的给删除掉
  137. $oldFieldIds = Db::name('admin_field')->where('types', $types)->column('field_id');
  138. $deleteIds = array_diff($oldFieldIds, $fieldIds);
  139. foreach ($deleteIds AS $key => $value) {
  140. if (!in_array($value, $delParam)) $delParam[] = $value;
  141. }
  142. # 新增
  143. if (!empty($saveParam)) {
  144. if (!$data = $fieldModel->createData($types, $saveParam)) {
  145. $errorMessage[] = $fieldModel->getError();
  146. }
  147. }
  148. # 编辑
  149. if (!empty($updateParam)) {
  150. if (!$data = $fieldModel->updateDataById($updateParam)) {
  151. $errorMessage[] = $fieldModel->getError();
  152. }
  153. }
  154. # 删除
  155. if (!empty($delParam)) {
  156. if (!$data = $fieldModel->delDataById($delParam)) {
  157. $errorMessage[] = $fieldModel->getError();
  158. }
  159. }
  160. # 自定义字段变更后,同步更新字段授权表
  161. (new FieldGrantLogic())->fieldGrantDiyHandle($types);
  162. if ($errorMessage) {
  163. return resultArray(['error' => $errorMessage]);
  164. } else {
  165. return resultArray(['data' => '修改成功']);
  166. }
  167. }
  168. /**
  169. * 自定义字段数据获取
  170. *
  171. * @return \think\response\Json
  172. * @throws \think\db\exception\DataNotFoundException
  173. * @throws \think\db\exception\ModelNotFoundException
  174. * @throws \think\exception\DbException
  175. */
  176. public function getField()
  177. {
  178. $fieldModel = model('Field');
  179. $userModel = model('User');
  180. $param = $this->param;
  181. $module = trim($param['module']);
  182. $controller = trim($param['controller']);
  183. $action = trim($param['action']);
  184. $system = !empty($param['system']) ? $param['system'] : 0;
  185. unset($param['system']);
  186. if (!$module || !$controller || !$action) {
  187. return resultArray(['error' => '参数错误']);
  188. }
  189. //判断权限
  190. $userInfo = $this->userInfo;
  191. $user_id = $userInfo['id'];
  192. $types = $param['types'];
  193. $types_id = $param['types_id'] ? : '';
  194. $dataInfo = [];
  195. if ($action == 'read' || $action == 'update') {
  196. //获取详情数据
  197. if (($param['action'] == 'update' || $param['action'] == 'read') && $param['action_id']) {
  198. switch ($param['types']) {
  199. case 'crm_customer' :
  200. $customerModel = new \app\crm\model\Customer();
  201. $dataInfo = $customerModel->getDataById(intval($param['action_id']));
  202. //判断权限
  203. $auth_user_ids = $userModel->getUserByPer('crm', 'customer', $param['action']);
  204. //读写权限
  205. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  206. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  207. //判断是否客户池数据
  208. $wherePool = $customerModel->getWhereByPool();
  209. $resPool = db('crm_customer')->alias('customer')->where(['customer_id' => $param['action_id']])->where($wherePool)->find();
  210. if (!$resPool && !in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  211. header('Content-Type:application/json; charset=utf-8');
  212. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  213. }
  214. break;
  215. case 'crm_leads' :
  216. $leadsModel = new \app\crm\model\Leads();
  217. $dataInfo = $leadsModel->getDataById(intval($param['action_id']));
  218. //判断权限
  219. $auth_user_ids = $userModel->getUserByPer('crm', 'leads', $param['action']);
  220. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  221. header('Content-Type:application/json; charset=utf-8');
  222. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  223. }
  224. break;
  225. case 'crm_contacts' :
  226. $contactsModel = new \app\crm\model\Contacts();
  227. $dataInfo = $contactsModel->getDataById(intval($param['action_id']));
  228. //判断权限
  229. $auth_user_ids = $userModel->getUserByPer('crm', 'contacts', $param['action']);
  230. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  231. header('Content-Type:application/json; charset=utf-8');
  232. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  233. }
  234. break;
  235. case 'crm_business' :
  236. $businessModel = new \app\crm\model\Business();
  237. $dataInfo = $businessModel->getDataById(intval($param['action_id']));
  238. //判断权限
  239. $auth_user_ids = $userModel->getUserByPer('crm', 'business', $param['action']);
  240. //读写权限
  241. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  242. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  243. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  244. header('Content-Type:application/json; charset=utf-8');
  245. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  246. }
  247. break;
  248. case 'crm_contract' :
  249. $contractModel = new \app\crm\model\Contract();
  250. $dataInfo = $contractModel->getDataById(intval($param['action_id']));
  251. //判断权限
  252. $auth_user_ids = $userModel->getUserByPer('crm', 'contract', $param['action']);
  253. //读写权限
  254. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  255. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  256. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  257. header('Content-Type:application/json; charset=utf-8');
  258. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  259. }
  260. break;
  261. case 'crm_product' :
  262. $productModel = new \app\crm\model\Product();
  263. $dataInfo = $productModel->getDataById(intval($param['action_id']));
  264. break;
  265. case 'crm_receivables' :
  266. $receivablesModel = new \app\crm\model\Receivables();
  267. $dataInfo = $receivablesModel->getDataById(intval($param['action_id']));
  268. //判断权限
  269. $auth_user_ids = $userModel->getUserByPer('crm', 'receivables', $param['action']);
  270. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids)) {
  271. header('Content-Type:application/json; charset=utf-8');
  272. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  273. }
  274. break;
  275. case 'crm_receivables_plan' :
  276. $receivablesPlanModel = new \app\crm\model\ReceivablesPlan();
  277. $dataInfo = $receivablesPlanModel->getDataById(intval($param['action_id']));
  278. break;
  279. case 'oa_examine' :
  280. $examineModel = new \app\oa\model\Examine();
  281. $examineFlowModel = new \app\admin\model\ExamineFlow();
  282. $dataInfo = $examineModel->getDataById(intval($param['action_id']));
  283. # 前端没有传types_id,这里需要指定一下types_id
  284. if (!empty($dataInfo['category_id'])) $param['types_id'] = $dataInfo['category_id'];
  285. $adminIds = $userModel->getAdminId(); //管理员
  286. $checkUserIds = $examineFlowModel->getUserByFlow($dataInfo['flow_id'], $dataInfo['create_user_id'], $dataInfo['check_user_id']);
  287. if (((int)$dataInfo['create_user_id'] != $user_id && !in_array($user_id,$adminIds) && !in_array($user_id,$checkUserIds))) {
  288. header('Content-Type:application/json; charset=utf-8');
  289. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  290. }
  291. break;
  292. case 'crm_visit' :
  293. $visit = new \app\crm\model\Visit();
  294. $dataInfo = $visit->getDataById(intval($param['action_id']));
  295. //判断权限
  296. $auth_user_ids = $userModel->getUserByPer('crm', 'visit', $param['action']);
  297. //读写权限
  298. $roPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'read');
  299. $rwPre = $userModel->rwPre($user_id, $dataInfo['ro_user_id'], $dataInfo['rw_user_id'], 'update');
  300. if (!in_array($dataInfo['owner_user_id'],$auth_user_ids) && !$roPre && !$rwPre) {
  301. header('Content-Type:application/json; charset=utf-8');
  302. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  303. }
  304. break;
  305. }
  306. }
  307. }
  308. $param['user_id'] = $user_id;
  309. $action_id = $param['action_id'] ? : '';
  310. $data = $fieldModel->field($param, $dataInfo) ? : [];
  311. # 去掉客户模块下的成交信息
  312. if ($param['types'] == 'crm_customer' && $param['action'] == 'read') {
  313. foreach ($data AS $key => $value) {
  314. if ($value['field'] == 'deal_status') {
  315. unset($data[(int)$key]);
  316. break;
  317. }
  318. }
  319. }
  320. if ($param['types'] == 'crm_customer' && $param['action'] == 'index') {
  321. $data[] = [
  322. 'field' => 'pool_day',
  323. 'name' => '距进入公海天数',
  324. 'form_type' => 'text',
  325. 'writeStatus' => 0,
  326. 'fieldName' => 'pool_day'
  327. ];
  328. }
  329. # 客户锁定状态
  330. if ($param['types'] == 'crm_customer' && $param['action'] == 'index') {
  331. $data[] = [
  332. 'field' => "is_lock",
  333. 'fieldName' => "is_lock",
  334. 'form_type' => "text",
  335. 'name' => "锁定状态",
  336. 'width' => ""
  337. ];
  338. }
  339. # 合同自动编号设置
  340. if ($param['types'] == 'crm_contract') {
  341. foreach ($data AS $key => $value) {
  342. if ($value['field'] == 'num') {
  343. if ($this->getAutoNumberStatus(1)) {
  344. $data[$key]['is_null'] = 0;
  345. $data[$key]['is_unique'] = 0;
  346. }
  347. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(1) ? 1 : 0;
  348. }
  349. }
  350. }
  351. # 回款自动编号设置
  352. if ($param['types'] == 'crm_receivables') {
  353. foreach ($data AS $key => $value) {
  354. if ($value['field'] == 'number') {
  355. if ($this->getAutoNumberStatus(2)) {
  356. $data[$key]['is_null'] = 0;
  357. $data[$key]['is_unique'] = 0;
  358. }
  359. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(2) ? 1 : 0;
  360. }
  361. }
  362. }
  363. # 回访自动编号设置
  364. if ($param['types'] == 'crm_visit') {
  365. foreach ($data AS $key => $value) {
  366. if ($value['field'] == 'number') {
  367. if ($this->getAutoNumberStatus(3)) {
  368. $data[$key]['is_null'] = 0;
  369. $data[$key]['is_unique'] = 0;
  370. }
  371. $data[$key]['autoGeneNumber'] = $this->getAutoNumberStatus(3) ? 1 : 0;
  372. }
  373. }
  374. }
  375. # 隐藏回款计划中的附件
  376. if ($param['types'] == 'crm_receivables_plan') {
  377. foreach ($data AS $key => $value) {
  378. if ($value['field'] == 'file') {
  379. unset($data[(int)$key]);
  380. }
  381. }
  382. }
  383. if (!empty($system) && $system == 1) {
  384. # 商机和合同排除产品字段
  385. if (in_array($param['types'], ['crm_business', 'crm_contract'])) {
  386. foreach ($data AS $key => $value) {
  387. if ($value['field'] == 'product' && $value['name'] == '产品') {
  388. unset($data[(int)$key]);
  389. break;
  390. }
  391. }
  392. }
  393. $data = array_values($data);
  394. # 系统信息
  395. switch ($types) {
  396. case 'crm_leads' :
  397. $leadsModel = new Leads();
  398. $leadsData = $leadsModel->getSystemInfo($action_id);
  399. $leadsArray = ['create_user_name' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'follow_time' => '最后跟进时间'];
  400. foreach ($leadsData AS $key => $value) {
  401. if (empty($leadsArray[$key])) continue;
  402. $data[] = [
  403. 'field' => $key,
  404. 'name' => $leadsArray[$key],
  405. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  406. 'value' => $value,
  407. 'system' => 1
  408. ];
  409. }
  410. break;
  411. case 'crm_customer' :
  412. $customerModel = new Customer();
  413. $customerData = $customerModel->getSystemInfo($action_id);
  414. $customerArray = ['obtain_time' => '负责人获取客户时间', 'create_time' => '创建时间', 'update_time' => '更新时间', 'follow_time' => '最后跟进时间', 'follow_record' => '最后跟进记录', 'deal_status' => '成交状态'];
  415. foreach ($customerData AS $key => $value) {
  416. if (empty($customerArray[$key])) continue;
  417. $data[] = [
  418. 'field' => $key,
  419. 'name' => $customerArray[$key],
  420. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  421. 'value' => $value,
  422. 'system' => 1
  423. ];
  424. }
  425. break;
  426. case 'crm_contacts' :
  427. $contactsModel = new Contacts();
  428. $contactsData = $contactsModel->getSystemInfo($action_id);
  429. $contactsArray = ['create_user_name' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'follow_time' => '最后跟进时间'];
  430. foreach ($contactsData AS $key => $value) {
  431. if (empty($contactsArray[$key])) continue;
  432. $data[] = [
  433. 'field' => $key,
  434. 'name' => $contactsArray[$key],
  435. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  436. 'value' => $value,
  437. 'system' => 1
  438. ];
  439. }
  440. break;
  441. case 'crm_business' :
  442. $businessModel = new Business();
  443. $businessData = $businessModel->getSystemInfo($action_id);
  444. $businessArray = ['create_user_name' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'follow_time' => '最后跟进时间'];
  445. foreach ($businessData AS $key => $value) {
  446. if (empty($businessArray[$key])) continue;
  447. $data[] = [
  448. 'field' => $key,
  449. 'name' => $businessArray[$key],
  450. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  451. 'value' => $value,
  452. 'system' => 1
  453. ];
  454. }
  455. break;
  456. case 'crm_contract' :
  457. $contractModel = new Contract();
  458. $contractData = $contractModel->getSystemInfo($action_id);
  459. $contractArray = ['create_user_name' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间', 'follow_time' => '最后跟进时间', 'done_money' => '已收款金额', 'un_money' => '未收款金额'];
  460. foreach ($contractData AS $key => $value) {
  461. if (empty($contractArray[$key])) continue;
  462. $data[] = [
  463. 'field' => $key,
  464. 'name' => $contractArray[$key],
  465. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  466. 'value' => $value,
  467. 'system' => 1
  468. ];
  469. }
  470. break;
  471. case 'crm_receivables' :
  472. $receivablesModel = new Receivables();
  473. $receivablesData = $receivablesModel->getSystemInfo($action_id);
  474. $receivablesArray = ['create_user_name' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  475. foreach ($receivablesData AS $key => $value) {
  476. if (empty($receivablesArray[$key])) continue;
  477. $data[] = [
  478. 'field' => $key,
  479. 'name' => $receivablesArray[$key],
  480. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  481. 'value' => $value,
  482. 'system' => 1
  483. ];
  484. }
  485. break;
  486. case 'crm_product' :
  487. $productModel = new Product();
  488. $productData = $productModel->getSystemInfo($action_id);
  489. $productArray = ['create_user_name' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  490. foreach ($productData AS $key => $value) {
  491. if (empty($productArray[$key])) continue;
  492. $data[] = [
  493. 'field' => $key,
  494. 'name' => $productArray[$key],
  495. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  496. 'value' => $value,
  497. 'system' => 1
  498. ];
  499. }
  500. break;
  501. case 'crm_visit' :
  502. $visitLogic = new VisitLogic();
  503. $visitData = $visitLogic->getSystemInfo($action_id);
  504. $visitArray = ['create_user_name' => '创建人', 'create_time' => '创建时间', 'update_time' => '更新时间'];
  505. foreach ($visitData AS $key => $value) {
  506. if (empty($visitArray[$key])) continue;
  507. $data[] = [
  508. 'field' => $key,
  509. 'name' => $visitArray[$key],
  510. 'form_type' => strpos($key, 'time') ? 'datetime' : 'text',
  511. 'value' => $value,
  512. 'system' => 1
  513. ];
  514. }
  515. break;
  516. }
  517. }
  518. $data = $fieldModel->resetField($param['types'], $data);
  519. return resultArray(['data' => array_values($data)]);
  520. }
  521. /**
  522. * 自定义字段数据验重
  523. *
  524. * @return \think\response\Json
  525. */
  526. public function validates()
  527. {
  528. $param = $this->param;
  529. $fieldModel = model('Field');
  530. if (is_array($param['val'])) {
  531. //多选类型暂不验证
  532. return resultArray(['data' => '验证通过']);
  533. }
  534. $res = $fieldModel->getValidate(trim($param['field']), trim($param['val']), intval($param['id']), trim($param['types']));
  535. if (!$res) {
  536. return resultArray(['error' => $fieldModel->getError()]);
  537. }
  538. return resultArray(['data' => '验证通过']);
  539. }
  540. /**
  541. * 自定义字段列表设置(排序、展示、列宽度)
  542. * @param types 分类
  543. * @param value 值
  544. */
  545. public function config()
  546. {
  547. $param = $this->param;
  548. $userInfo = $this->userInfo;
  549. $param['user_id'] = $userInfo['id'];
  550. $userFieldModel = model('UserField');
  551. $res = $userFieldModel->updateConfig($param['types'], $param);
  552. if (!$res) {
  553. return resultArray(['error' => $userFieldModel->getError()]);
  554. }
  555. return resultArray(['data' => '设置成功']);
  556. }
  557. /**
  558. * 自定义字段列宽度设置
  559. * @param types 分类
  560. * @param field 字段名
  561. * @param width 列宽度
  562. */
  563. public function columnWidth()
  564. {
  565. $param = $this->param;
  566. $userInfo = $this->userInfo;
  567. $userFieldModel = model('UserField');
  568. $width = $param['width'] > 10 ? $param['width'] : '';
  569. $unField = array('pool_day','owner_user_name','is_lock','create_user_name');
  570. if (!in_array($param['field'],$unField)) {
  571. $res = $userFieldModel->setColumnWidth($param['types'], $param['field'], $width, $userInfo['id']);
  572. if (!$res) {
  573. return resultArray(['error' => $userFieldModel->getError()]);
  574. }
  575. }
  576. return resultArray(['data' => '设置成功']);
  577. }
  578. /**
  579. * 自定义字段列表设置数据
  580. * @param types 分类
  581. * @param value 值
  582. */
  583. public function configIndex()
  584. {
  585. $param = $this->param;
  586. $userInfo = $this->userInfo;
  587. $userFieldModel = model('UserField');
  588. $res = $userFieldModel->getDataList($param['types'], $userInfo['id']);
  589. if (!$res) {
  590. return resultArray(['error' => $userFieldModel->getError()]);
  591. }
  592. return resultArray(['data' => $res]);
  593. }
  594. /**
  595. * 自定义验重字段
  596. * @param types 分类
  597. * @param
  598. */
  599. public function uniqueField()
  600. {
  601. $param = $this->param;
  602. if ($param['types'] == 'crm_user') {
  603. $list = array_filter(UserModel::$import_field_list, function ($val) {
  604. return $val['is_unique'] == 1;
  605. });
  606. $list = array_column($list, 'name');
  607. } else {
  608. $list = db('admin_field')->where(['types' => $param['types'],'is_unique' => 1])->column('name');
  609. }
  610. $list = $list ? implode(',',$list) : '无';
  611. return resultArray(['data' => $list]);
  612. }
  613. /**
  614. * 获取自动编号状态
  615. *
  616. * @param $type
  617. * @return int|mixed|string|null
  618. */
  619. private function getAutoNumberStatus($type)
  620. {
  621. return Db::name('crm_number_sequence')->where('number_type', $type)->where('status', 0)->value('number_sequence_id');
  622. }
  623. }