BaseService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 $error = '1003';
  27. // 静态对象
  28. protected static $instance = null;
  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. * @return array
  51. * @since 2020/11/11
  52. * @author laravel开发员
  53. */
  54. public function getList()
  55. {
  56. // 初始化变量
  57. $map = [];
  58. $sort = [['id', 'desc']];
  59. $is_sql = 0;
  60. // 获取参数
  61. $argList = func_get_args();
  62. if (!empty($argList)) {
  63. // 查询条件
  64. $map = (isset($argList[0]) && !empty($argList[0])) ? $argList[0] : [];
  65. // 排序
  66. $sort = (isset($argList[1]) && !empty($argList[1])) ? $argList[1] : [['id', 'desc']];
  67. // 是否打印SQL
  68. $is_sql = isset($argList[2]) ? isset($argList[2]) : 0;
  69. }
  70. // 打印SQL
  71. if ($is_sql) {
  72. $this->model->getLastSql(1);
  73. }
  74. // 常规查询条件
  75. $param = request()->input();
  76. if ($param) {
  77. // 筛选名称
  78. if (isset($param['name']) && $param['name']) {
  79. $map[] = ['name', 'like', "%{$param['name']}%"];
  80. }
  81. // 筛选标题
  82. if (isset($param['title']) && $param['title']) {
  83. $map[] = ['title', 'like', "%{$param['title']}%"];
  84. }
  85. // 筛选类型
  86. if (isset($param['type']) && $param['type']) {
  87. $map[] = ['type', '=', $param['type']];
  88. }
  89. // 筛选状态
  90. if (isset($param['status']) && $param['status']) {
  91. $map[] = ['status', '=', $param['status']];
  92. }
  93. // 手机号码
  94. if (isset($param['mobile']) && $param['mobile']) {
  95. $map[] = ['mobile', '=', $param['mobile']];
  96. }
  97. }
  98. // 设置查询条件
  99. if (is_array($map)) {
  100. $map[] = ['mark', '=', 1];
  101. } elseif ($map) {
  102. $map .= " AND mark=1 ";
  103. } else {
  104. $map .= " mark=1 ";
  105. }
  106. // 排序(支持多重排序)
  107. $query = $this->model->where($map)->when($sort, function ($query, $sort) {
  108. foreach ($sort as $v) {
  109. $query->orderBy($v[0], $v[1]);
  110. }
  111. });
  112. // 分页条件
  113. $offset = (PAGE - 1) * PERPAGE;
  114. $result = $query->offset($offset)->limit(PERPAGE)->select('id')->get();
  115. $result = $result ? $result->toArray() : [];
  116. $list = [];
  117. if (is_array($result)) {
  118. foreach ($result as $val) {
  119. $info = $this->model->getInfo($val['id']);
  120. $list[] = $info;
  121. }
  122. }
  123. //获取数据总数
  124. $count = $this->model->where($map)->count();
  125. //返回结果
  126. $message = array(
  127. "msg" => '操作成功',
  128. "code" => 0,
  129. "data" => $list,
  130. "count" => $count,
  131. );
  132. return $message;
  133. }
  134. /**
  135. * 获取记录详情
  136. * @return array
  137. * @since 2020/11/11
  138. * @author laravel开发员
  139. */
  140. public function info()
  141. {
  142. // 记录ID
  143. $id = request()->input("id", 0);
  144. $info = [];
  145. if ($id) {
  146. $info = $this->model->getInfo($id);
  147. }
  148. return message(MESSAGE_OK, true, $info);
  149. }
  150. /**
  151. * 添加或编辑记录
  152. * @return array
  153. * @since 2020/11/11
  154. * @author laravel开发员
  155. */
  156. public function edit()
  157. {
  158. // 获取参数
  159. $argList = func_get_args();
  160. // 查询条件
  161. $data = isset($argList[0]) ? $argList[0] : [];
  162. // 是否打印SQL
  163. $is_sql = isset($argList[1]) ? $argList[1] : false;
  164. if (!$data) {
  165. $data = request()->all();
  166. }
  167. $error = '';
  168. $rowId = $this->model->edit($data, $error, $is_sql);
  169. if ($rowId) {
  170. return message();
  171. }
  172. return message($error, false);
  173. }
  174. /**
  175. * 删除记录
  176. * @return array
  177. * @since 2020/11/12
  178. * @author laravel开发员
  179. */
  180. public function delete()
  181. {
  182. // 参数
  183. $param = request()->all();
  184. // 记录ID
  185. $ids = getter($param, "id");
  186. if (empty($ids)) {
  187. return message("记录ID不能为空", false);
  188. }
  189. if (is_array($ids)) {
  190. // 批量删除
  191. $result = $this->model->deleteAll($ids);
  192. if (!$result) {
  193. return message("删除失败", false);
  194. }
  195. return message("删除成功");
  196. } else {
  197. // 单个删除
  198. $info = $this->model->getInfo($ids);
  199. if ($info) {
  200. $result = $this->model->drop($ids);
  201. if ($result !== false) {
  202. return message();
  203. }
  204. }
  205. return message($this->model->getError(), false);
  206. }
  207. }
  208. /**
  209. * 设置记录状态
  210. * @return array
  211. * @since 2020/11/11
  212. * @author laravel开发员
  213. */
  214. public function status()
  215. {
  216. $data = request()->all();
  217. if (!$data['id']) {
  218. return message('记录ID不能为空', false);
  219. }
  220. if (!$data['status']) {
  221. return message('记录状态不能为空', false);
  222. }
  223. $error = '';
  224. $item = [
  225. 'id' => $data['id'],
  226. 'status' => $data['status']
  227. ];
  228. $rowId = $this->model->edit($item, $error);
  229. if (!$rowId) {
  230. return message($error, false);
  231. }
  232. return message();
  233. }
  234. /**
  235. * 验证数据是否已经存在
  236. * @param $field 字段名
  237. * @param $value 字段值
  238. * @param string $pk 键名
  239. * @return mixed
  240. */
  241. public function checkExists($field, $value, $pk='id',$status=1)
  242. {
  243. $where = [$field=> $value,'mark'=>1];
  244. if($status>0){
  245. $where['status'] = $status;
  246. }
  247. return $this->model->where($where)->value($pk);
  248. }
  249. }