DefaultService.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace Home\Service;
  3. /**
  4. * DefaultService
  5. */
  6. class DefaultService extends CommonService {
  7. /**
  8. * 填充时机
  9. * @var array
  10. */
  11. private $createTime = array('insert', 'update');
  12. /**
  13. * 创建数据
  14. * @param array $data 提交的数据
  15. * @param array $fields 对应模型的数据
  16. * @param string $ctrlName 控制器名
  17. * @param string $time 填充时机
  18. * @return array
  19. */
  20. public function create(array $data, array $fields, $ctrlName, $time) {
  21. $once = false;
  22. $uploadInfo = null;
  23. $uploadDir = C('UPLOAD_ROOT') . $ctrlName . '/';
  24. $data = array_map(trim_value, $data);
  25. $time = in_array(strtolower($time), $this->createTime) ?
  26. strtolower($time) : $this->createTime[0];
  27. $inputService = D('Input', 'Service');
  28. foreach ($fields as $field) {
  29. $fn = $field['name'];
  30. $fm = $field['comment'];
  31. // 是否文件类型的表单域
  32. if ($inputService->isFileInput($field['input']['type'])) {
  33. if (!$once) {
  34. // 只执行一次上传
  35. $uploadInfo = upload($uploadDir);
  36. if (false === $uploadInfo['status']
  37. && !empty($uploadInfo['info'])) {
  38. // 上传失败
  39. return $this->errorResultReturn($uploadInfo['info']);
  40. }
  41. $once = true;
  42. }
  43. if (true === $uploadInfo['status']
  44. && !$this->isEmpty($_FILES[$fn]['tmp_name'])
  45. && is_array($uploadInfo['info'][0])) {
  46. // 处理真正上传过的file表单域
  47. $size = $uploadInfo['info'][0]['size'];
  48. if (convMb2B($field['input']['width']) < $size) {
  49. // 删除已上传的文件
  50. foreach ($uploadInfo['info'] as $upload) {
  51. // 删除文件
  52. unlink(WEB_ROOT . $upload['path']);
  53. }
  54. // 超过限制大小
  55. $msg ="{$fm}文件大小不能超过{$field['input']['width']}M!";
  56. return $this->errorResultReturn($msg);
  57. }
  58. $data[$fn] = $uploadInfo['info'][0]['path'];
  59. array_shift($uploadInfo['info']);
  60. }
  61. }
  62. // checkbox类型需要合并值
  63. if ($inputService->isCheckbox($field['input']['type'])) {
  64. if (isset($data[$fn]) && !empty($data[$fn])) {
  65. $data[$fn] = $data[$fn] = implode(',', $data[$fn]);
  66. } else {
  67. $data[$fn] = '';
  68. }
  69. }
  70. //时间戳格式
  71. if ('date_utime' == $field['input']['type'] && !empty($data[$fn])) {
  72. $data[$fn] = strtotime($data[$fn]);
  73. }
  74. //时间戳格式
  75. if ('date_microtime' == $field['input']['type'] && !empty($data[$fn])) {
  76. $data[$fn] = strtotime($data[$fn]) * 1000;
  77. }
  78. // 检查field[type]约束
  79. if (!empty($data[$fn])) {
  80. $result = $this->checkTypeContraint($field['type'],$data[$fn]);
  81. if (!$result['status'] && !empty($result['data'])) {
  82. $msg = "{$fm}为{$field['type']}类型";
  83. if ('int' == $result['data']) {
  84. $msg .= ",值只能为整数!";
  85. } else if ('double' == $result['data']) {
  86. $msg .= ",值只能为浮点数!";
  87. }
  88. return $this->errorResultReturn($msg);
  89. }
  90. }
  91. // 日期型格式
  92. if ('date' == $field['input']['type'] && !empty($data[$fn])) {
  93. $result = $this->checkTypeContraint($field['input']['type'],
  94. $data[$fn]);
  95. if (!$result['status']) {
  96. $msg = "{$fm}日期格式不正确!";
  97. return $this->errorResultReturn($msg);
  98. }
  99. }
  100. // 字符长度
  101. if (('CHAR' == $field['type'] || 'VARCHAR' == $field['type'])
  102. && !empty($data[$fn])
  103. && strlen($data[$fn]) > $field['length']) {
  104. $msg = "{$fm}长度只能小于{$field['length']}个字符!";
  105. return $this->errorResultReturn($msg);
  106. }
  107. // 字段必填
  108. if (1 != $field['is_system']
  109. && 1 == $field['is_require']
  110. && empty($field['auto_fill'])
  111. && (!isset($data[$fn]) || empty($data[$fn]))) {
  112. return $this->errorResultReturn("{$fm}必需填写!");
  113. }
  114. // 字段唯一
  115. if (1 != $field['is_system']
  116. && 1 == $field['is_unique']
  117. && !empty($data[$fn])) {
  118. $isUnique = $this->isRowUnique($ctrlName,
  119. $field['name'],
  120. $data[$fn],
  121. $data['id']);
  122. if (!$isUnique) {
  123. return $this->errorResultReturn("{$fm}已经存在!");
  124. }
  125. }
  126. // 自定义字段 auto_filter 自动过滤
  127. if (!empty($field['auto_filter'])) {
  128. if (!function_exists($field['auto_filter'])) {
  129. $msg ="过滤函数{$field['auto_filter']}不存在,请先进行注册函数!";
  130. return $this->errorResultReturn($msg);
  131. }
  132. $data[$fn] = $field['auto_filter']($data[$fn]);
  133. }
  134. // 系统字段 auto_fill 自动填充
  135. if ($field['is_system']
  136. && !empty($field['auto_fill'])
  137. && ('both' == $field['fill_time']
  138. || $time == $field['fill_time'])) {
  139. if (!function_exists($field['auto_fill'])) {
  140. $msg = "填充函数{$field['auto_fill']}不存在,请先进行注册函数!";
  141. return $this->errorResultReturn($msg);
  142. }
  143. $data[$fn] = $field['auto_fill']();
  144. }
  145. // 自定义字段 auto_fill 自动填充
  146. if (!empty($field['auto_fill'])
  147. && empty($data[$fn])
  148. && ('both' == $field['fill_time']
  149. || $time == $field['fill_time'])) {
  150. if (!function_exists($field['auto_fill'])) {
  151. $msg = "填充函数{$field['auto_fill']}不存在,请先进行注册函数!";
  152. return $this->errorResultReturn($msg);
  153. }
  154. $data[$fn] = $field['auto_fill']();
  155. }
  156. }
  157. return $this->resultReturn(true, $data);
  158. }
  159. /**
  160. * 添加数据
  161. * @param array $data 需要添加的数据
  162. * @param string $ctrlName 添加数据的模型
  163. * @return array
  164. */
  165. public function add(array $data, $ctrlName) {
  166. $m = $this->getModel($ctrlName);
  167. if (false === $m->add($data)) {
  168. echo $m->getLastSql();
  169. return $this->resultReturn(false);
  170. }
  171. $data['id'] = $m->getLastInsId();
  172. $tblName = D('Model', 'Service')->getTblName($ctrlName);
  173. $model = M('Model')->getByTblName($tblName);
  174. // 更新被关联的表单域
  175. $inputService = D('Input', 'Service');
  176. $inputService->updateRalationInput($data, $model['id']);
  177. return $this->resultReturn(true);
  178. }
  179. /**
  180. * 更新数据
  181. * @param array $data 需要更新的数据
  182. * @param string $ctrlName 更新数据的模型
  183. * @return array
  184. */
  185. public function update(array $data, $ctrlName) {
  186. if (false === M($ctrlName)->save($data)) {
  187. return $this->resultReturn(false);
  188. }
  189. $tblName = D('Model', 'Service')->getTblName($ctrlName);
  190. $model = M('Model')->getByTblName($tblName);
  191. // 更新被关联的表单域
  192. $inputService = D('Input', 'Service');
  193. $inputService->updateRalationInput($data, $model['id']);
  194. return $this->resultReturn(true);
  195. }
  196. /**
  197. * 删除模型数据
  198. * @param int $id 删除数据的id
  199. * @param string $ctrlName 删除数据的模型
  200. * @return array
  201. */
  202. public function delete($id, $ctrlName) {
  203. $old = M($ctrlName)->getById($id);
  204. if (is_null($old) || false === M($ctrlName)->delete($id)) {
  205. return $this->resultReturn(false);
  206. }
  207. $tblName = D('Model', 'Service')->getTblName($ctrlName);
  208. $model = M('Model')->getByTblName($tblName);
  209. // 级联删除
  210. $this->cascadeDel($model['id'], $old);
  211. // 更新被关联的表单域
  212. $inputService = D('Input', 'Service');
  213. $inputService->updateRalationInput($old, $model['id']);
  214. return $this->resultReturn(true);
  215. }
  216. /**
  217. * 检查类型约束
  218. * @param string $type 需要约束的类型
  219. * @param string $value 需要约束的值
  220. * @return mixed
  221. */
  222. public function checkTypeContraint($type, $value) {
  223. switch ($type) {
  224. case 'TINYINT':
  225. case 'SMALLINT':
  226. case 'INT':
  227. case 'BIGINT':
  228. if (!isint($value)) {
  229. return $this->resultReturn(false, 'int');
  230. }
  231. break;
  232. case 'FLOAT':
  233. case 'DOUBLE':
  234. if (!isdouble($value)) {
  235. return $this->resultReturn(false, 'double');
  236. }
  237. break;
  238. case 'date':
  239. if (!is_valid_date($value)) {
  240. return $this->resultReturn(false, 'date');
  241. }
  242. break;
  243. case 'date_time':
  244. if (!is_valid_datetime($value)) {
  245. return $this->resultReturn(false, 'date');
  246. }
  247. break;
  248. }
  249. return $this->resultReturn(true);
  250. }
  251. /**
  252. * 检查字段值唯一
  253. * @param string $mn 模型名称
  254. * @param string $fn 字段名称
  255. * @param string $val 字段值
  256. * @return boolean
  257. */
  258. public function isRowUnique($mn, $fn, $val, $id = null) {
  259. $where = array($fn => $val);
  260. if (isset($id)) {
  261. $where['id'] = array('neq', $id);
  262. }
  263. if (M($mn)->where($where)->count() > 0) {
  264. return false;
  265. }
  266. return true;
  267. }
  268. /**
  269. * 级联删除数据
  270. * @param int $modelId 模型id
  271. * @param array $data 被删除的关联数据
  272. * @return
  273. */
  274. public function cascadeDel($modelId, $data) {
  275. // 得到关联到模型的字段
  276. $fields = D('Field', 'Service')->getByRelationModel($modelId);
  277. // 级联删除关联的数据
  278. foreach ($fields as $field) {
  279. $rf = $field['relation_field'];
  280. $where = array($field['name'] => $data[$rf]);
  281. // 得到该字段所在的模型
  282. $model = M('Model')->getById($field['model_id']);
  283. M(D('Model', 'Service')->getCtrlName($model['tbl_name']))
  284. ->where($where)
  285. ->delete();
  286. }
  287. return ;
  288. }
  289. /**
  290. * 判断是否为空
  291. * @param mixed $mixed 需要检查的值
  292. * @return boolean
  293. */
  294. private function isEmpty($mixed) {
  295. if (is_array($mixed)) {
  296. $mixed = array_filter($mixed);
  297. return empty($mixed);
  298. } else {
  299. return empty($mixed);
  300. }
  301. }
  302. protected function getModel($ctrName) {
  303. if(strpos($ctrName, '.') !== false) {
  304. return M($ctrName, null);
  305. } else {
  306. return M($ctrName);
  307. }
  308. }
  309. /**
  310. * 删除文件
  311. * @param array $files 需要删除的文件路径
  312. * @return
  313. */
  314. private function unlinkFiles($files) {
  315. foreach ($files as $file) {
  316. unlink($file);
  317. }
  318. }
  319. protected function isRelation() {
  320. return false;
  321. }
  322. protected function getModelName() {
  323. return $this->getCtrName();
  324. }
  325. }