BaseService.php 8.4 KB

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