BaseService.php 9.0 KB

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