BaseService.php 9.0 KB

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