| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace Modules\Manage\Controllers;
- use App\Constants\CommonConstant;
- use App\Model\CoinsLogModel;
- use App\Model\CoinsModel;
- use App\Repositories\CoinsRepository;
- use Carbon\Carbon;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Modules\Manage\Responses\CommonSuccessPkResponse;
- class CoinsController extends BaseController
- {
- use \Hyperf\Di\Aop\ProxyTrait;
- use \Hyperf\Di\Aop\PropertyHandlerTrait;
- function __construct()
- {
- if (method_exists(parent::class, '__construct')) {
- parent::__construct(...func_get_args());
- }
- $this->__handlePropertyHandler(__CLASS__);
- }
- /**
- * 分页列表
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'nullable|numeric', 'page_size' => 'nullable|numeric'];
- $validator = $validatorFactory->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 = isset($inputData['page']) ? intval($inputData['page']) : 1;
- $page = format_page($page);
- $arrData = [];
- $lists = CoinsModel::query()->orderBy('sort', 'DESC')->paginate($pageSize, ['*'], 'page', $page);
- if ($lists->isNotEmpty()) {
- $arrData = $lists->toArray();
- }
- return json_success($arrData);
- }
- /**
- * 详情
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function detail(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $id = data_get($inputData, 'id', '');
- $arrData = [];
- $item = CoinsModel::query()->findOrFail($id);
- if (!empty($item)) {
- $arrData = $item->toArray();
- }
- return json_success($arrData);
- }
- /**
- * 创建或者更新.
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- */
- public function update(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'nullable|int', 'coins' => 'required|int', 'send_type' => 'required|int', 'send_num' => 'required|int', 'title' => 'required|string|max:255', 'price' => 'required|numeric', 'real_price' => 'required|numeric', 'title_desc' => 'nullable|string|max:255', 'price_desc' => 'nullable|string|max:255', 'sort' => 'required|int', 'type' => 'required|int', 'recommend_desc' => 'nullable|string|max:255', 'status' => 'nullable|int'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $id = data_get($inputData, 'id', '');
- $fields = $rules;
- unset($fields['id']);
- foreach ($fields as $fieldName => $rule) {
- if (in_array($fieldName, ['coins', 'send_type', 'send_num', 'sort', 'type', 'recommend_desc'])) {
- $setarr[$fieldName] = intval(data_get($inputData, $fieldName, 0));
- } else {
- $setarr[$fieldName] = data_get($inputData, $fieldName, '');
- }
- }
- unset($setarr['status']);
- if (isset($inputData['status']) && !empty($inputData['status'])) {
- $setarr['status'] = intval($inputData['status']);
- }
- if ($id) {
- $item = CoinsModel::query()->findOrFail($id);
- $result = $item->update($setarr);
- } else {
- $result = CoinsModel::query()->create($setarr);
- if ($result) {
- $id = $result->id;
- }
- }
- if (empty($result)) {
- return json_fail('保存失败');
- } else {
- make(CoinsRepository::class)->delUserCoinsCache();
- return json_success(['result' => 'success', 'id' => $id]);
- }
- }
- /**
- * 删除
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed|CommonSuccessPkResponse
- * @throws \Exception
- */
- public function delete(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required|int'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $item = CoinsModel::query()->findOrFail($inputData['id']);
- $result = $item->delete();
- if ($result) {
- make(CoinsRepository::class)->delUserCoinsCache();
- return new CommonSuccessPkResponse($inputData['id']);
- } else {
- return json_fail('保存失败');
- }
- }
- /**
- * 修改状态
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed|CommonSuccessPkResponse
- */
- public function switchStatus(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['id' => 'required|int', 'status' => 'required|int'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $operatorName = 'opened';
- $item = CoinsModel::query()->findOrFail($inputData['id']);
- if ($item->status == $inputData['status']) {
- return new CommonSuccessPkResponse($item->id);
- }
- if ($inputData['status'] == 2) {
- $operatorName = 'closed';
- }
- $item->status = $inputData['status'];
- $result = $item->save();
- if ($result) {
- make(CoinsRepository::class)->delUserCoinsCache();
- return json_success(['result' => 'success', 'id' => $inputData['id'], 'operator_name' => $operatorName]);
- } else {
- return json_fail('保存失败');
- }
- }
- /**
- * 金币明细
- */
- public function coinsLogs(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'user_code' => 'nullable|string', 'coin_type' => 'nullable|int', 'stime' => 'nullable|date', 'otime' => 'nullable|date'];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $userCode = $inputData['user_code'];
- $coinType = $inputData['coin_type'];
- $stime = $inputData['stime'];
- $otime = $inputData['otime'];
- $result = CoinsLogModel::query()->where(function ($q) use($userCode, $coinType, $stime, $otime) {
- if ($userCode) {
- $q->where('user_code', $userCode);
- }
- if ($coinType) {
- $q->where('coin_type', $coinType);
- }
- if ($stime && $otime) {
- $q->whereBetween('created_at', [Carbon::parse($stime)->toDateString(), Carbon::parse($otime)->toDateString()]);
- }
- })->orderByDesc('id')->paginate((int) $inputData['page_size'], ['*'], 'page', (int) $inputData['page']);
- return json_success($result);
- }
- }
|