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