Modules_Manage_Controllers_CoinsController.proxy.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Modules\Manage\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Model\CoinsLogModel;
  5. use App\Model\CoinsModel;
  6. use App\Repositories\CoinsRepository;
  7. use Carbon\Carbon;
  8. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  9. use Modules\Manage\Responses\CommonSuccessPkResponse;
  10. class CoinsController extends BaseController
  11. {
  12. use \Hyperf\Di\Aop\ProxyTrait;
  13. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  14. function __construct()
  15. {
  16. if (method_exists(parent::class, '__construct')) {
  17. parent::__construct(...func_get_args());
  18. }
  19. $this->__handlePropertyHandler(__CLASS__);
  20. }
  21. /**
  22. * 分页列表
  23. * @param ValidatorFactoryInterface $validatorFactory
  24. * @return mixed
  25. */
  26. public function lists(ValidatorFactoryInterface $validatorFactory)
  27. {
  28. $inputData = $this->getJsonData();
  29. $rules = ['page' => 'nullable|numeric', 'page_size' => 'nullable|numeric'];
  30. $validator = $validatorFactory->make($inputData, $rules);
  31. if ($validator->fails()) {
  32. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  33. return json_fail($validator->errors()->first());
  34. }
  35. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  36. if ($pageSize > 500) {
  37. $pageSize = CommonConstant::PAGE_SIZE;
  38. }
  39. $page = isset($inputData['page']) ? intval($inputData['page']) : 1;
  40. $page = format_page($page);
  41. $arrData = [];
  42. $lists = CoinsModel::query()->orderBy('sort', 'DESC')->paginate($pageSize, ['*'], 'page', $page);
  43. if ($lists->isNotEmpty()) {
  44. $arrData = $lists->toArray();
  45. }
  46. return json_success($arrData);
  47. }
  48. /**
  49. * 详情
  50. * @param ValidatorFactoryInterface $validatorFactory
  51. * @return mixed
  52. */
  53. public function detail(ValidatorFactoryInterface $validatorFactory)
  54. {
  55. $inputData = $this->getJsonData();
  56. $rules = ['id' => 'required'];
  57. $validator = $validatorFactory->make($inputData, $rules);
  58. if ($validator->fails()) {
  59. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  60. return json_fail($validator->errors()->first());
  61. }
  62. $id = data_get($inputData, 'id', '');
  63. $arrData = [];
  64. $item = CoinsModel::query()->findOrFail($id);
  65. if (!empty($item)) {
  66. $arrData = $item->toArray();
  67. }
  68. return json_success($arrData);
  69. }
  70. /**
  71. * 创建或者更新.
  72. * @param ValidatorFactoryInterface $validatorFactory
  73. * @return mixed
  74. */
  75. public function update(ValidatorFactoryInterface $validatorFactory)
  76. {
  77. $inputData = $this->getJsonData();
  78. $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'];
  79. $validator = $validatorFactory->make($inputData, $rules);
  80. if ($validator->fails()) {
  81. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  82. return json_fail($validator->errors()->first());
  83. }
  84. $id = data_get($inputData, 'id', '');
  85. $fields = $rules;
  86. unset($fields['id']);
  87. foreach ($fields as $fieldName => $rule) {
  88. if (in_array($fieldName, ['coins', 'send_type', 'send_num', 'sort', 'type', 'recommend_desc'])) {
  89. $setarr[$fieldName] = intval(data_get($inputData, $fieldName, 0));
  90. } else {
  91. $setarr[$fieldName] = data_get($inputData, $fieldName, '');
  92. }
  93. }
  94. unset($setarr['status']);
  95. if (isset($inputData['status']) && !empty($inputData['status'])) {
  96. $setarr['status'] = intval($inputData['status']);
  97. }
  98. if ($id) {
  99. $item = CoinsModel::query()->findOrFail($id);
  100. $result = $item->update($setarr);
  101. } else {
  102. $result = CoinsModel::query()->create($setarr);
  103. if ($result) {
  104. $id = $result->id;
  105. }
  106. }
  107. if (empty($result)) {
  108. return json_fail('保存失败');
  109. } else {
  110. make(CoinsRepository::class)->delUserCoinsCache();
  111. return json_success(['result' => 'success', 'id' => $id]);
  112. }
  113. }
  114. /**
  115. * 删除
  116. * @param ValidatorFactoryInterface $validatorFactory
  117. * @return mixed|CommonSuccessPkResponse
  118. * @throws \Exception
  119. */
  120. public function delete(ValidatorFactoryInterface $validatorFactory)
  121. {
  122. $inputData = $this->getJsonData();
  123. $rules = ['id' => 'required|int'];
  124. $validator = $validatorFactory->make($inputData, $rules);
  125. if ($validator->fails()) {
  126. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  127. return json_fail($validator->errors()->first());
  128. }
  129. $item = CoinsModel::query()->findOrFail($inputData['id']);
  130. $result = $item->delete();
  131. if ($result) {
  132. make(CoinsRepository::class)->delUserCoinsCache();
  133. return new CommonSuccessPkResponse($inputData['id']);
  134. } else {
  135. return json_fail('保存失败');
  136. }
  137. }
  138. /**
  139. * 修改状态
  140. * @param ValidatorFactoryInterface $validatorFactory
  141. * @return mixed|CommonSuccessPkResponse
  142. */
  143. public function switchStatus(ValidatorFactoryInterface $validatorFactory)
  144. {
  145. $inputData = $this->getJsonData();
  146. $rules = ['id' => 'required|int', 'status' => 'required|int'];
  147. $validator = $validatorFactory->make($inputData, $rules);
  148. if ($validator->fails()) {
  149. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  150. return json_fail($validator->errors()->first());
  151. }
  152. $operatorName = 'opened';
  153. $item = CoinsModel::query()->findOrFail($inputData['id']);
  154. if ($item->status == $inputData['status']) {
  155. return new CommonSuccessPkResponse($item->id);
  156. }
  157. if ($inputData['status'] == 2) {
  158. $operatorName = 'closed';
  159. }
  160. $item->status = $inputData['status'];
  161. $result = $item->save();
  162. if ($result) {
  163. make(CoinsRepository::class)->delUserCoinsCache();
  164. return json_success(['result' => 'success', 'id' => $inputData['id'], 'operator_name' => $operatorName]);
  165. } else {
  166. return json_fail('保存失败');
  167. }
  168. }
  169. /**
  170. * 金币明细
  171. */
  172. public function coinsLogs(ValidatorFactoryInterface $validatorFactory)
  173. {
  174. $inputData = $this->getJsonData();
  175. $rules = ['page' => 'required|int', 'page_size' => 'required|int', 'user_code' => 'nullable|string', 'coin_type' => 'nullable|int', 'stime' => 'nullable|date', 'otime' => 'nullable|date'];
  176. $validator = $validatorFactory->make($inputData, $rules);
  177. if ($validator->fails()) {
  178. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  179. return json_fail($validator->errors()->first());
  180. }
  181. $userCode = $inputData['user_code'];
  182. $coinType = $inputData['coin_type'];
  183. $stime = $inputData['stime'];
  184. $otime = $inputData['otime'];
  185. $result = CoinsLogModel::query()->where(function ($q) use($userCode, $coinType, $stime, $otime) {
  186. if ($userCode) {
  187. $q->where('user_code', $userCode);
  188. }
  189. if ($coinType) {
  190. $q->where('coin_type', $coinType);
  191. }
  192. if ($stime && $otime) {
  193. $q->whereBetween('created_at', [Carbon::parse($stime)->toDateString(), Carbon::parse($otime)->toDateString()]);
  194. }
  195. })->orderByDesc('id')->paginate((int) $inputData['page_size'], ['*'], 'page', (int) $inputData['page']);
  196. return json_success($result);
  197. }
  198. }