BaseService.php 9.5 KB

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