BaseService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. /**
  13. * 服务基类
  14. * @author laravel开发员
  15. * @since 2020/11/10
  16. * Class BaseService
  17. * @package App\Services
  18. */
  19. class BaseService
  20. {
  21. // 模型
  22. protected $model;
  23. // 验证类
  24. protected $validate;
  25. // 静态对象
  26. protected static $instance = null;
  27. // 错错误码
  28. protected $error = '1003';
  29. /**
  30. * 静态入口
  31. * @return static|null
  32. */
  33. public static function make()
  34. {
  35. if(!self::$instance){
  36. self::$instance = (new static());
  37. }
  38. return self::$instance;
  39. }
  40. /**
  41. * 获得错误信息
  42. * @return string
  43. */
  44. public function getError()
  45. {
  46. return $this->error;
  47. }
  48. /**
  49. * 验证数据是否已经存在
  50. * @param $field 字段名
  51. * @param $value 字段值
  52. * @param string $pk 键名
  53. * @return mixed
  54. */
  55. public function checkExists($field, $value, $pk='id',$status=1)
  56. {
  57. $where = [$field=> $value,'mark'=>1];
  58. return $this->model->where($where)->where(function($query) use($status){
  59. if($status){
  60. $status = is_array($status)? $status : [$status];
  61. $query->whereIn('status', $status);
  62. }
  63. })->value($pk);
  64. }
  65. /**
  66. * 获取数据列表
  67. * @return array
  68. * @since 2020/11/11
  69. * @author laravel开发员
  70. */
  71. public function getList()
  72. {
  73. // 初始化变量
  74. $map = [];
  75. $sort = [['id', 'desc']];
  76. $is_sql = 0;
  77. // 获取参数
  78. $argList = func_get_args();
  79. if (!empty($argList)) {
  80. // 查询条件
  81. $map = (isset($argList[0]) && !empty($argList[0])) ? $argList[0] : [];
  82. // 排序
  83. $sort = (isset($argList[1]) && !empty($argList[1])) ? $argList[1] : [['id', 'desc']];
  84. // 是否打印SQL
  85. $is_sql = isset($argList[2]) ? isset($argList[2]) : 0;
  86. }
  87. // 打印SQL
  88. if ($is_sql) {
  89. $this->model->getLastSql(1);
  90. }
  91. // 常规查询条件
  92. $param = request()->input();
  93. if ($param) {
  94. // 筛选名称
  95. if (isset($param['name']) && $param['name']) {
  96. $map[] = ['name', 'like', "%{$param['name']}%"];
  97. }
  98. // 筛选标题
  99. if (isset($param['title']) && $param['title']) {
  100. $map[] = ['title', 'like', "%{$param['title']}%"];
  101. }
  102. // 筛选类型
  103. if (isset($param['type']) && $param['type']) {
  104. $map[] = ['type', '=', $param['type']];
  105. }
  106. // 筛选状态
  107. if (isset($param['status']) && $param['status']) {
  108. $map[] = ['status', '=', $param['status']];
  109. }
  110. // 手机号码
  111. if (isset($param['mobile']) && $param['mobile']) {
  112. $map[] = ['mobile', '=', $param['mobile']];
  113. }
  114. }
  115. // 设置查询条件
  116. if (is_array($map)) {
  117. $map[] = ['mark', '=', 1];
  118. } elseif ($map) {
  119. $map .= " AND mark=1 ";
  120. } else {
  121. $map .= " mark=1 ";
  122. }
  123. // 排序(支持多重排序)
  124. $query = $this->model->where($map)->when($sort, function ($query, $sort) {
  125. foreach ($sort as $v) {
  126. $query->orderBy($v[0], $v[1]);
  127. }
  128. });
  129. // 分页条件
  130. $offset = (PAGE - 1) * PERPAGE;
  131. $result = $query->offset($offset)->limit(PERPAGE)->select('id')->get();
  132. $result = $result ? $result->toArray() : [];
  133. $list = [];
  134. if (is_array($result)) {
  135. foreach ($result as $val) {
  136. $info = $this->model->getInfo($val['id']);
  137. $list[] = $info;
  138. }
  139. }
  140. //获取数据总数
  141. $count = $this->model->where($map)->count();
  142. //返回结果
  143. $message = array(
  144. "msg" => '操作成功',
  145. "code" => 0,
  146. "data" => $list,
  147. "count" => $count,
  148. );
  149. return $message;
  150. }
  151. /**
  152. * 获取记录详情
  153. * @return array
  154. * @since 2020/11/11
  155. * @author laravel开发员
  156. */
  157. public function info()
  158. {
  159. // 记录ID
  160. $id = request()->input("id", 0);
  161. $info = [];
  162. if ($id) {
  163. $info = $this->model->getInfo($id);
  164. }
  165. return message(MESSAGE_OK, true, $info);
  166. }
  167. /**
  168. * 添加或编辑记录
  169. * @return array
  170. * @since 2020/11/11
  171. * @author laravel开发员
  172. */
  173. public function edit()
  174. {
  175. // 获取参数
  176. $argList = func_get_args();
  177. // 查询条件
  178. $data = isset($argList[0]) ? $argList[0] : [];
  179. // 是否打印SQL
  180. $is_sql = isset($argList[1]) ? $argList[1] : false;
  181. if (!$data) {
  182. $data = request()->all();
  183. }
  184. $error = '';
  185. $rowId = $this->model->edit($data, $error, $is_sql);
  186. if ($rowId) {
  187. return message();
  188. }
  189. return message($error, false);
  190. }
  191. /**
  192. * 删除记录
  193. * @return array
  194. * @since 2020/11/12
  195. * @author laravel开发员
  196. */
  197. public function delete()
  198. {
  199. // 参数
  200. $param = request()->all();
  201. // 记录ID
  202. $ids = getter($param, "id");
  203. if (empty($ids)) {
  204. return message("记录ID不能为空", false);
  205. }
  206. if (is_array($ids)) {
  207. // 批量删除
  208. $result = $this->model->deleteAll($ids);
  209. if (!$result) {
  210. return message("删除失败", false);
  211. }
  212. return message("删除成功");
  213. } else {
  214. // 单个删除
  215. $info = $this->model->getInfo($ids);
  216. if ($info) {
  217. $result = $this->model->drop($ids);
  218. if ($result !== false) {
  219. return message();
  220. }
  221. }
  222. return message($this->model->getError(), false);
  223. }
  224. }
  225. /**
  226. * 设置记录状态
  227. * @return array
  228. * @since 2020/11/11
  229. * @author laravel开发员
  230. */
  231. public function status()
  232. {
  233. $data = request()->all();
  234. if (!$data['id']) {
  235. return message('记录ID不能为空', false);
  236. }
  237. if (!$data['status']) {
  238. return message('记录状态不能为空', false);
  239. }
  240. $error = '';
  241. $item = [
  242. 'id' => $data['id'],
  243. 'status' => $data['status']
  244. ];
  245. $rowId = $this->model->edit($item, $error);
  246. if (!$rowId) {
  247. return message($error, false);
  248. }
  249. return message();
  250. }
  251. }