CommonModel.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: pl125 <xskjs888@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace huomp\model\common;
  10. use think\Loader;
  11. use think\Model;
  12. class CommonModel extends Model {
  13. // 关联模型过滤
  14. protected $relationFilter = [];
  15. public static function className() {
  16. return get_called_class();
  17. }
  18. /**
  19. * @param array $params
  20. *
  21. * @return array|false|\PDOStatement|string|\think\Collection|Model
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public function getDatas($params = []) {
  27. if (empty($params)) {
  28. return $this->select();
  29. }
  30. $this->setCondition($params);
  31. if (!empty($params['id'])) {
  32. $datas = $this->find();
  33. } else {
  34. $datas = $this->select();
  35. }
  36. if (!empty($params['relation'])) {
  37. $allowedRelations = $this->allowedRelations($params['relation']);
  38. if (!empty($allowedRelations)) {
  39. if (!empty($params['id'])) {
  40. if (!empty($datas)) {
  41. $datas->append($allowedRelations);
  42. }
  43. } else {
  44. if (count($datas) > 0) {
  45. $datas->load($allowedRelations);
  46. $datas->append($allowedRelations);
  47. }
  48. }
  49. }
  50. }
  51. return $datas;
  52. }
  53. /**
  54. * @access public
  55. *
  56. * @param array $params 过滤参数
  57. *
  58. * @return $this
  59. */
  60. public function setCondition($params) {
  61. if (empty($params)) {
  62. return $this;
  63. }
  64. if (!empty($params['relation'])) {
  65. $allowedRelations = $this->allowedRelations($params['relation']);
  66. if (!empty($allowedRelations)) {
  67. if (!empty($params['id']) && count($allowedRelations) == 1) {
  68. $this->paramsFilter($params);
  69. } else {
  70. $this->paramsFilter($params);//->with($allowedRelations);
  71. }
  72. }
  73. } else {
  74. $this->paramsFilter($params);
  75. }
  76. return $this;
  77. }
  78. public function orderFilter($order = []) {
  79. if (empty($order)) {
  80. return '';
  81. }
  82. $orderWhere = [];
  83. $_order = $this->strToArr($order);
  84. foreach ($_order as $key => $value) {
  85. $upDwn = substr($value, 0, 1);
  86. $orderType = $upDwn == '-' ? 'desc' : 'asc';
  87. $orderField = substr($value, 1);
  88. $orderWhere[$orderField] = $orderType;
  89. }
  90. return $orderWhere;
  91. }
  92. /**
  93. * @access public
  94. *
  95. * @param array $params 过滤参数
  96. * @param model $model 关联模型
  97. *
  98. * @return model|array $this|链式查询条件数组
  99. */
  100. public function paramsFilter($params, $model = null) {
  101. if (!empty($model)) {
  102. $_this = $model;
  103. } else {
  104. $_this = $this;
  105. }
  106. if (isset($_this->visible)) {
  107. $whiteParams = $_this->visible;
  108. }
  109. // 设置field字段过滤
  110. if (!empty($params['field'])) {
  111. $filterParams = $this->strToArr($params['field']);
  112. if (!empty($whiteParams)) {
  113. $mixedField = array_intersect($filterParams, $whiteParams);
  114. } else {
  115. $mixedField = $filterParams;
  116. }
  117. if (!empty($mixedField)) {
  118. $_this->field($mixedField);
  119. }
  120. }
  121. // 设置id,ids
  122. if (!empty($params['ids'])) {
  123. $ids = $this->strToArr($params['ids']);
  124. foreach ($ids as $key => $value) {
  125. $ids[$key] = intval($value);
  126. }
  127. }
  128. if (!empty($params['id'])) {
  129. $id = intval($params['id']);
  130. if (!empty($id)) {
  131. return $_this->where('id', $id);
  132. }
  133. } elseif (!empty($ids)) {
  134. $_this->where('id', 'in', $ids);
  135. }
  136. if (!empty($params['where'])) {
  137. if (empty($model)) {
  138. $_this->where($params['where']);
  139. }
  140. }
  141. // 设置分页
  142. if (!empty($params['page'])) {
  143. $pageArr = $this->strToArr($params['page']);
  144. $page = [];
  145. foreach ($pageArr as $value) {
  146. $page[] = intval($value);
  147. }
  148. if (count($page) == 1) {
  149. $_this->page($page[0]);
  150. } elseif (count($page) == 2) {
  151. $_this->page($page[0], $page[1]);
  152. }
  153. } elseif (!empty($params['limit'])) { // 设置limit查询
  154. $limitArr = $this->strToArr($params['limit']);
  155. $limit = [];
  156. foreach ($limitArr as $value) {
  157. $limit[] = intval($value);
  158. }
  159. if (count($limit) == 1) {
  160. $_this->limit($limit[0]);
  161. } elseif (count($limit) == 2) {
  162. $_this->limit($limit[0], $limit[1]);
  163. }
  164. } else {
  165. $_this->limit(10);
  166. }
  167. //设置排序
  168. if (!empty($params['order'])) {
  169. $order = $this->strToArr($params['order']);
  170. foreach ($order as $key => $value) {
  171. $upDwn = substr($value, 0, 1);
  172. $orderType = $upDwn == '-' ? 'desc' : 'asc';
  173. $orderField = substr($value, 1);
  174. if (!empty($whiteParams)) {
  175. if (in_array($orderField, $whiteParams)) {
  176. $orderWhere[$orderField] = $orderType;
  177. }
  178. } else {
  179. $orderWhere[$orderField] = $orderType;
  180. }
  181. }
  182. if (!empty($orderWhere)) {
  183. $_this->order($orderWhere);
  184. }
  185. }
  186. return $_this;
  187. }
  188. /**
  189. * 设置链式查询
  190. *
  191. * @access public
  192. *
  193. * @param array $params 链式查询条件
  194. * @param model $model 模型
  195. *
  196. * @return $this
  197. */
  198. public function setParamsQuery($params, $model = null) {
  199. if (!empty($model)) {
  200. $_this = $model;
  201. } else {
  202. $_this = $this;
  203. }
  204. $_this->alias('articles');
  205. if (!empty($params['field'])) {
  206. $_this->field($params['field']);
  207. }
  208. if (!empty($params['ids'])) {
  209. $_this->where('articles.id', $params['ids'][1], $params['ids'][2]);
  210. }
  211. if (!empty($params['limit'])) {
  212. $_this->limit($params['limit']);
  213. }
  214. if (!empty($params['page'])) {
  215. $_this->page($params['page']);
  216. }
  217. if (!empty($params['order'])) {
  218. $_this->order($params['order']);
  219. }
  220. return $_this;
  221. }
  222. public function allowedRelations($relations) {
  223. if (is_string($relations)) {
  224. $relations = explode(',', $relations);
  225. }
  226. if (!is_array($relations)) {
  227. return false;
  228. }
  229. return array_intersect($this->relationFilter, $relations);
  230. }
  231. /**
  232. * 是否允许关联
  233. *
  234. * @access public
  235. *
  236. * @param string $relationName 模型关联方法名
  237. *
  238. * @return boolean
  239. */
  240. public function isWhite($relationName) {
  241. if (!is_string($relationName)) {
  242. return false;
  243. }
  244. $name = Loader::parseName($relationName, 1, false);
  245. if (in_array($name, $this->relationFilter)) {
  246. return true;
  247. } else {
  248. return false;
  249. }
  250. }
  251. /**
  252. * 懒人函数
  253. *
  254. * @access public
  255. *
  256. * @param string $string 字符串
  257. *
  258. * @return array
  259. */
  260. public function strToArr($string) {
  261. return is_string($string) ? explode(',', $string) : $string;
  262. }
  263. /**
  264. * 懒人函数
  265. *
  266. * @access public
  267. *
  268. * @param array $arr 数组
  269. *
  270. * @return string
  271. */
  272. public function arrToStr($arr) {
  273. return is_array($arr) ? implode(',', $arr) : $arr;
  274. }
  275. /**
  276. * @param array $data
  277. * @param string|int $key
  278. * @param string $default
  279. *
  280. * @return string
  281. */
  282. protected function getVal($data, $key, $default = '') {
  283. if (empty($key) || empty($data) || !isset($data[$key])) {
  284. return $default;
  285. }
  286. return $data[$key];
  287. }
  288. /**
  289. * 计算在哪张表
  290. *
  291. * @return \Think\Model
  292. */
  293. public function computeTable() {
  294. return $this;
  295. }
  296. /**
  297. * @param $data
  298. * @param bool $replace
  299. * @param bool $get_last_insert_id
  300. *
  301. * @return bool|int|string
  302. */
  303. public function insertLog($data, $replace = false, $get_last_insert_id = true) {
  304. if ($_id = $this->computeTable()->insert($data, $replace, $get_last_insert_id)) {
  305. return $_id;
  306. } else {
  307. return false;
  308. }
  309. }
  310. /**
  311. * 获取单条记录
  312. *
  313. * @param int $id
  314. *
  315. * @return array|false
  316. */
  317. public function getInfoById($id) {
  318. $_map['id'] = $id;
  319. $_info = $this->where($_map)->find();
  320. if (false === $_info) {
  321. return false;
  322. }
  323. if (is_object($_info)) {
  324. return $_info->toArray();
  325. } else {
  326. return $_info;
  327. }
  328. }
  329. /**当前模型名称(数据库表名,不带前缀)
  330. *
  331. * @return string
  332. */
  333. public function getName() {
  334. return $this->name;
  335. }
  336. }