FieldVerificationTrait.php 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * 字段验证(线索、客户、联系人、商机、合同、回款、回访、产品、办公审批)
  4. */
  5. namespace app\admin\traits;
  6. trait FieldVerificationTrait
  7. {
  8. /**
  9. * 数据验证
  10. *
  11. * @param array $param 要验证的数据
  12. * @param int $userId 用户ID
  13. * @param string $types 自定义表栏目类型:crm_leads、crm_customer ...
  14. * @param int $dataId 编辑时,相应的模块数据的ID
  15. * @param int $typesId 自定义表栏目类型ID
  16. * @author fanqi
  17. * @since 2021-05-18
  18. * @return string
  19. */
  20. public function fieldDataValidate($param, $types, $userId, $dataId = 0, $typesId = 0)
  21. {
  22. $error = '';
  23. $grantData = getFieldGrantData($userId); # 字段授权
  24. $userLevel = isSuperAdministrators($userId); # 用户级别
  25. # 查询自定义字段表数据
  26. $fieldList = $this->getFieldList($types, $typesId);
  27. # 验证
  28. foreach ($fieldList AS $key => $value) {
  29. # 字段授权,没有读写权限,跳过验证。
  30. if (!$userLevel && !empty($grantData[$types])) {
  31. $status = getFieldGrantStatus($value['field'], $grantData[$types]);
  32. if (empty($status['read']) || (empty($dataId) && empty($status['write']))) continue;
  33. }
  34. # 验证非明细表格字段数据
  35. if (!empty($value['is_null']) && !in_array($value['form_type'], ['detail_table', 'boolean_value']) && (isset($param[$value['field']]) && empty($param[$value['field']]))) {
  36. $error = $value['name'] . '字段不能为空!';
  37. break;
  38. }
  39. # 验证字段长度
  40. if (!empty($value['max_length']) && $value['form_type'] != 'detail_table' && strlen($param[$value['field']]) > $value['max_length']) {
  41. $error = $value['name'] . '字段超过设定长度!';
  42. break;
  43. }
  44. # 验证百分数字段长度
  45. if ($value['form_type'] == 'percent' && strlen($param[$value['field']]) > 11) {
  46. $error = $value['name'] . "字段长度不能大于10位!";
  47. break;
  48. }
  49. # 验证数字字段长度
  50. if ($value['form_type'] == 'number' && strlen($param[$value['field']]) > 16) {
  51. $error = $value['name'] . "字段长度不能大于16位!";
  52. break;
  53. }
  54. # 验证明细表格不能为空
  55. if (!empty($value['is_null']) && $value['form_type'] == 'detail_table' && isset($param[$value['field']]) && empty($param[$value['field']])) {
  56. $error = $value['name'] . '数据不能为空!';
  57. }
  58. # 验证明细表格可以为空,明细表格里的字段不能为空的情况。
  59. if ($value['form_type'] == 'detail_table') {
  60. foreach ($param[$value['field']] AS $val) {
  61. foreach ($val AS $v) {
  62. if (!empty($v['is_null']) && empty($v['is_hidden']) && isset($v['value']) && empty($v['value'])) {
  63. $error = $value['name'] . '中的' . $v['name'] . '字段不能为空!';
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. if (empty($value['is_unique'])) continue;
  70. // 人员、部门、文件、手写签名、描述文字、多选、明细表格跳过验证
  71. if (in_array($value['form_type'], ['user', 'structure', 'file', 'handwriting_sign', 'desc_text', 'checkbox', 'detail_table'])) continue;
  72. $uniqueStatus = false;
  73. # 验证唯一性
  74. if ($value['form_type'] == 'date_interval' && !empty($param[$value['field']])) {
  75. // 日期区间
  76. $uniqueStatus = $this->checkDataUniqueForDateInterval($types, $value['field'], $param[$value['field']], $dataId);
  77. } elseif ($value['form_type'] == 'position' && !empty($param[$value['field']])) {
  78. // 地址
  79. $uniqueStatus = $this->checkDataUniqueForPosition($types, $value['field'], $param[$value['field']], $dataId);
  80. } elseif ($value['form_type'] == 'location' && !empty($param[$value['field']])) {
  81. // 定位
  82. $uniqueStatus = $this->checkDataUniqueForLocation($types, $value['field'], $param[$value['field']], $dataId);
  83. } else {
  84. if (!empty($param[$value['field']])) $uniqueStatus = $this->checkDataUniqueForCommon($types, $value['field'], $param[$value['field']], $dataId);
  85. }
  86. if (!empty($uniqueStatus)) {
  87. $error = $types == 'crm_customer' ? '(客户/公海)中的' . $value['name'] . "字段值重复!" : $value['name'] . "字段值重复!";
  88. break;
  89. }
  90. }
  91. return $error;
  92. }
  93. /**
  94. * 验证唯一性(通用)
  95. * 单行文本、多行文本、网址、布尔值、单选、数字
  96. * 手机、邮箱、日期、日期时间、货币、百分数
  97. *
  98. * @param string $types 栏目类型
  99. * @param string $field 字段名称
  100. * @param string $value 字段值
  101. * @param int $dataId 更新时,传来的数据ID
  102. * @author fanqi
  103. * @since 2021-05-18
  104. * @return float|mixed|string
  105. */
  106. private function checkDataUniqueForCommon($types, $field, $value, $dataId = 0)
  107. {
  108. if (empty($value)) return false;
  109. # 主键
  110. $primaryKey = getPrimaryKeyName($types);
  111. # 查询条件
  112. $where[$field] = $value;
  113. if (!empty($dataId)) $where[$primaryKey] = ['neq', $dataId];
  114. return db($types)->where($where)->value($primaryKey);
  115. }
  116. /**
  117. * 验证唯一性(日期区间)
  118. *
  119. * @param string $types 栏目类型
  120. * @param string $field 字段名称
  121. * @param array $value 字段值
  122. * @param int $dataId 更新时,传来的数据ID
  123. * @author fanqi
  124. * @since 2021-05-18
  125. * @return float|mixed|string
  126. */
  127. private function checkDataUniqueForDateInterval($types, $field, $value, $dataId = 0)
  128. {
  129. if (empty($value)) return false;
  130. # 主键
  131. $primaryKey = getPrimaryKeyName($types);
  132. # 查询条件
  133. $where[$field] = implode('_', $value);
  134. if (!empty($dataId)) $where[$primaryKey] = ['neq', $dataId];
  135. return db($types)->where($where)->value($primaryKey);
  136. }
  137. /**
  138. * 验证唯一性(地址)
  139. *
  140. * @param string $types 栏目类型
  141. * @param string $field 字段名称
  142. * @param array $value 字段值
  143. * @param int $dataId 更新时,传来的数据ID
  144. * @author fanqi
  145. * @since 2021-05-18
  146. * @return float|mixed|string
  147. */
  148. private function checkDataUniqueForPosition($types, $field, $value, $dataId = 0)
  149. {
  150. if (empty($value)) return false;
  151. # 主键
  152. $primaryKey = getPrimaryKeyName($types);
  153. # 查询条件
  154. $where[$field] = implode(',', array_column($value, 'name'));
  155. if (!empty($dataId)) $where[$primaryKey] = ['neq', $dataId];
  156. return db($types)->where($where)->value($primaryKey);
  157. }
  158. /**
  159. * 验证唯一性(定位)
  160. *
  161. * @param string $types 栏目类型
  162. * @param string $field 字段名称
  163. * @param array $value 字段值
  164. * @param int $dataId 更新时,传来的数据ID
  165. * @author fanqi
  166. * @since 2021-05-18
  167. * @return float|mixed|string
  168. */
  169. private function checkDataUniqueForLocation($types, $field, $value, $dataId = 0)
  170. {
  171. if (empty($value['address'])) return false;
  172. # 主键
  173. $primaryKey = getPrimaryKeyName($types);
  174. # 查询条件
  175. $where[$field] = $value['address'];
  176. if (!empty($dataId)) $where[$primaryKey] = ['neq', $dataId];
  177. return db($types)->where($where)->value($primaryKey);
  178. }
  179. // /**
  180. // * 验证唯一性(部门)
  181. // *
  182. // * @param string $types 栏目类型
  183. // * @param string $field 字段名称
  184. // * @param string $value 字段值
  185. // * @param int $dataId 更新时,传来的数据ID
  186. // * @author fanqi
  187. // * @since 2021-05-18
  188. // * @return float|mixed|string
  189. // */
  190. // private function checkDataUniqueForStructure($types, $field, $value, $dataId = 0)
  191. // {
  192. // if (empty($value)) return false;
  193. //
  194. // # 主键
  195. // $primaryKey = getPrimaryKeyName($types);
  196. //
  197. // # 查询条件
  198. // $where[$field] = ',' . $value . ',';
  199. // if (!empty($dataId)) $where[$primaryKey] = ['neq', $dataId];
  200. //
  201. // return db($types)->where($where)->value($primaryKey);
  202. // }
  203. /**
  204. * 自定义字段列表
  205. *
  206. * @param string $types 自定义表栏目类型:crm_leads、crm_customer ...
  207. * @param int $typesId 自定义表栏目类型ID
  208. * @author fanqi
  209. * @since 2021-05-18
  210. * @return bool|\PDOStatement|string|\think\Collection
  211. */
  212. private function getFieldList($types, $typesId)
  213. {
  214. # 查询条件
  215. $where = [
  216. 'types' => $types,
  217. 'types_id' => $typesId,
  218. 'is_hidden' => 0,
  219. 'form_type' => ['neq', 'desc_text']
  220. ];
  221. # 查询字段
  222. $fields = ['field', 'name', 'form_type', 'is_unique', 'is_null', 'max_length'];
  223. return db('admin_field')->field($fields)->where($where)->select();
  224. }
  225. }