BaseService.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. protected $errorData = [];
  28. // 静态对象
  29. protected static $instance = null;
  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. $businessId = isset($param['bsid']) ? intval($param['bsid']) : 0;
  151. if ($businessId > 0) {
  152. $query->where(['business_id' => $businessId]);
  153. }
  154. $userType = isset($param['user_type']) ? $param['user_type'] : 0;
  155. if ($userType) {
  156. $userType = explode(',', $userType);
  157. $query->whereIn('user_type', $userType);
  158. }
  159. })->when($sort, function ($query, $sort) {
  160. foreach ($sort as $v) {
  161. $query->orderBy($v[0], $v[1]);
  162. }
  163. });
  164. // 分页条件
  165. $offset = (PAGE - 1) * PERPAGE;
  166. $result = $query->offset($offset)->limit(PERPAGE)->select('id')->get();
  167. $result = $result ? $result->toArray() : [];
  168. $list = [];
  169. if (is_array($result)) {
  170. foreach ($result as $val) {
  171. $info = $this->model->getInfo($val['id']);
  172. $list[] = $info;
  173. }
  174. }
  175. //获取数据总数
  176. $count = $this->model->where($map)->count();
  177. //返回结果
  178. $message = array(
  179. "msg" => '操作成功',
  180. "code" => 0,
  181. "data" => $list,
  182. "count" => $count,
  183. );
  184. return $message;
  185. }
  186. /**
  187. * 获取记录详情
  188. * @return array
  189. * @since 2020/11/11
  190. * @author laravel开发员
  191. */
  192. public function info()
  193. {
  194. // 记录ID
  195. $id = request()->input("id", 0);
  196. $info = [];
  197. if ($id) {
  198. $info = $this->model->getInfo($id);
  199. }
  200. return message(MESSAGE_OK, true, $info);
  201. }
  202. /**
  203. * 添加或编辑记录
  204. * @return array
  205. * @since 2020/11/11
  206. * @author laravel开发员
  207. */
  208. public function edit()
  209. {
  210. // 获取参数
  211. $argList = func_get_args();
  212. // 查询条件
  213. $data = isset($argList[0]) ? $argList[0] : [];
  214. // 是否打印SQL
  215. $is_sql = isset($argList[1]) ? $argList[1] : false;
  216. if (!$data) {
  217. $data = request()->all();
  218. }
  219. $error = '';
  220. $rowId = $this->model->edit($data, $error, $is_sql);
  221. if ($rowId) {
  222. return message(MESSAGE_OK, true, ['id' => $rowId]);
  223. }
  224. return message($error, false);
  225. }
  226. /**
  227. * 删除记录
  228. * @return array
  229. * @since 2020/11/12
  230. * @author laravel开发员
  231. */
  232. public function delete()
  233. {
  234. // 参数
  235. $param = request()->all();
  236. // 记录ID
  237. $ids = getter($param, "id");
  238. if (empty($ids)) {
  239. return message("记录ID不能为空", false);
  240. }
  241. $this->model->where(['mark' => 0])->where('update_time', '<=', time() - 3600)->delete();
  242. if (is_array($ids)) {
  243. // 批量删除
  244. $result = $this->model->deleteAll($ids);
  245. if (!$result) {
  246. return message("删除失败", false);
  247. }
  248. return message("删除成功");
  249. } else {
  250. // 单个删除
  251. $info = $this->model->getInfo($ids);
  252. if ($info) {
  253. $result = $this->model->drop($ids);
  254. if ($result !== false) {
  255. return message();
  256. }
  257. }
  258. return message($this->model->getError(), false);
  259. }
  260. }
  261. /**
  262. * 验证数据是否已经存在
  263. * @param $field 字段名
  264. * @param $value 字段值
  265. * @param string $pk 键名
  266. * @return mixed
  267. */
  268. public function checkExists($field, $value, $pk = 'id', $status = 1)
  269. {
  270. $where = [$field => $value, 'mark' => 1];
  271. if ($status > 0) {
  272. $where['status'] = $status;
  273. }
  274. return $this->model->where($where)->value($pk);
  275. }
  276. /**
  277. * 设置记录状态
  278. * @return array
  279. * @since 2020/11/11
  280. * @author laravel开发员
  281. */
  282. public function status()
  283. {
  284. $data = request()->all();
  285. if (!$data['id']) {
  286. return message('记录ID不能为空', false);
  287. }
  288. if (!$data['status']) {
  289. return message('记录状态不能为空', false);
  290. }
  291. $error = '';
  292. $item = [
  293. 'id' => $data['id'],
  294. 'status' => $data['status']
  295. ];
  296. $rowId = $this->model->edit($item, $error);
  297. if (!$rowId) {
  298. return message($error, false);
  299. }
  300. return message();
  301. }
  302. }