BaseService.php 7.1 KB

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