| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- namespace Modules\ManageApi\Controllers;
- use App\Constants\CommonConstant;
- use App\Constants\RedisKeyConstant;
- use App\Models\AdminModel;
- use App\Models\ConfigModel;
- use App\Models\MerchantModel;
- use Illuminate\Support\Facades\Cache;
- class ManagerController extends BaseController
- {
- public function lists()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'page' => 'required|integer',
- 'page_size' => 'required|integer',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
- if ($pageSize > 500) {
- $pageSize = CommonConstant::PAGE_SIZE;
- }
- $page = format_page($inputData['page'] ?? 0);
- $res = AdminModel::query()->orderByDesc('created_at')->paginate($pageSize,
- ['id', 'account', 'created_at', 'updated_at', 'login_time', 'status'], 'page', $page);
- return json_success($res);
- }
- /**
- * 添加管理员
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function add()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'account' => 'required|max:120',
- 'pass' => 'required|max:64',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $admin = AdminModel::query()->where('account', $inputData['account'])->first();
- if ($admin) {
- return json_fail('用户名重复,请重新创建');
- }
- $salt = random(6);
- $data = [
- 'salt' => $salt,
- 'account' => $inputData['account'],
- 'password' => generatePassword($inputData['pass'], $salt),
- ];
- $res = AdminModel::query()->create($data);
- if ($res) {
- return json_success([], '创建成功');
- } else {
- return json_fail('创建失败');
- }
- }
- /**
- * 修改管理员
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function edit()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|integer',
- 'pass' => 'nullable|string',
- 'status' => 'required|in:1,2',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $admin = AdminModel::query()->where('id', $inputData['id'])->first();
- if (!$admin) {
- return json_fail('该用户不存在');
- }
- if (!empty($inputData['pass'])) {
- $admin->password = generatePassword($inputData['pass'], $admin->salt);
- }
- if (!empty($inputData['status'])) {
- $admin->status = $inputData['status'];
- }
- $res = $admin->save();
- if ($res) {
- return json_success([], '修改成功');
- } else {
- return json_fail('修改失败');
- }
- }
- /**
- * 设置列表
- *
- * @return mixed
- */
- public function configLists()
- {
- $res = ConfigModel::query()->get(['name', 'key', 'value', 'id']);
- return json_success($res);
- }
- /**
- * 添加设置
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function configAdd()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'name' => 'required|string',
- 'key' => 'required|string',
- 'value' => 'required|string',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $data = [
- 'key' => $inputData['key'],
- 'value' => $inputData['value'],
- 'name' => $inputData['name']
- ];
- $res = ConfigModel::query()->create($data);
- if ($res) {
- Cache::forget(RedisKeyConstant::CONFIG);
- return json_success([], '添加成功');
- } else {
- return json_fail('添加失败');
- }
- }
- /**
- * 修改设置
- *
- * @return \Illuminate\Http\JsonResponse|mixed
- * @throws \Illuminate\Contracts\Container\BindingResolutionException
- */
- public function configEdit()
- {
- $inputData = $this->getJsonData();
- $rules = [
- 'id' => 'required|integer',
- 'name' => 'nullable|string',
- 'key' => 'nullable|string',
- 'value' => 'nullable|string',
- ];
- $validator = app('validator')->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = ConfigModel::query()->where('id', $inputData['id'])->first();
- if (!$info) {
- return json_fail('修改的设置不存在');
- }
- if (!empty($inputData['key'])) {
- $info->key = $inputData['key'];
- }
- if (!empty($inputData['value'])) {
- $info->value = $inputData['value'];
- }
- if (!empty($inputData['name'])) {
- $info->name = $inputData['name'];
- }
- if ($info->save()) {
- Cache::forget(RedisKeyConstant::CONFIG);
- return json_success([], '修改成功');
- } else {
- return json_fail('修改失败');
- }
- }
- }
|