Modules_Manage_Controllers_VersionController.proxy.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * APP版本升级管理
  4. * @author xiabian
  5. */
  6. declare (strict_types=1);
  7. namespace Modules\Manage\Controllers;
  8. use App\Model\VersionModel as Model;
  9. use Hyperf\Validation\Contract\ValidatorFactoryInterface;
  10. use Psr\SimpleCache\CacheInterface;
  11. class VersionController extends BaseController
  12. {
  13. use \Hyperf\Di\Aop\ProxyTrait;
  14. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  15. function __construct()
  16. {
  17. if (method_exists(parent::class, '__construct')) {
  18. parent::__construct(...func_get_args());
  19. }
  20. $this->__handlePropertyHandler(__CLASS__);
  21. }
  22. /**
  23. * 列表
  24. * @param ValidatorFactoryInterface $validatorFactory
  25. * @return mixed
  26. * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"title":"","device_type":"I"}}}
  27. */
  28. public function lists(ValidatorFactoryInterface $validatorFactory)
  29. {
  30. $inputData = $this->getJsonData();
  31. $page = isset($inputData['page']) ? (int) $inputData['page'] : 1;
  32. $page_size = isset($inputData['page_size']) ? (int) $inputData['page_size'] : 20;
  33. $select = ['*'];
  34. $model = Model::query();
  35. $search = isset($inputData['search']) ? $inputData['search'] : array();
  36. if ($search) {
  37. foreach ($search as $k => $v) {
  38. if (!strlen((string) $v)) {
  39. continue;
  40. }
  41. switch ($k) {
  42. case "title":
  43. // 标题
  44. break;
  45. default:
  46. $model->where($k, $v);
  47. }
  48. }
  49. }
  50. $result = $model->select($select)->orderBy('id', 'desc')->paginate($page_size, ['*'], 'page', $page);
  51. return json_success($result);
  52. }
  53. /**
  54. * 新增
  55. * @param ValidatorFactoryInterface $validatorFactory
  56. * @return mixed
  57. * @example param_json_data string:
  58. * {"handshake":"","data":{"title":"测试","remark":"","device_type":"A","force":0,"version_code":"1.0.2","version_type":"free","package_path":"http://www.baidu.com","status":0}}
  59. */
  60. public function add(ValidatorFactoryInterface $validatorFactory)
  61. {
  62. $inputData = $this->getJsonData();
  63. $rules = ['title' => 'required|string', 'remark' => 'nullable|string', 'device_type' => 'required|in:I,A', 'force' => 'required|in:0,1', 'version_code' => 'required|string', 'version_type' => 'required', 'package_path' => 'required|string', 'zip_url' => 'nullable|string', 'status' => 'required|in:0,1'];
  64. $validator = $validatorFactory->make($inputData, $rules);
  65. if ($validator->fails()) {
  66. return json_fail($validator->errors()->first());
  67. }
  68. $data = ['title' => $inputData['title'], 'remark' => $inputData['remark'] ?? "", 'device_type' => $inputData['device_type'], 'force' => $inputData['force'], 'version_code' => $inputData['version_code'], 'version_type' => $inputData['version_type'], 'package_path' => $inputData['package_path'], 'zip_url' => $inputData['zip_url'], 'status' => $inputData['status']];
  69. $result = Model::query()->create($data);
  70. $this->clearCache($inputData['version_code']);
  71. // 创建管理员日志
  72. $newitem = clone $result;
  73. $logSubject = "新增App版本";
  74. $logSvalue = $newitem->id;
  75. $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => []];
  76. $this->log($logSubject, $logSvalue, $logDbData);
  77. return json_success($result);
  78. }
  79. /**
  80. * 编辑
  81. * @param ValidatorFactoryInterface $validatorFactory
  82. * @return mixed
  83. * @example param_json_data string: {"handshake":"","data":{"id":7,"title":"测试编辑","remark":"","device_type":"","force":0,"version_code":"","version_type":"","package_path":"","status":0}}
  84. */
  85. public function update(ValidatorFactoryInterface $validatorFactory)
  86. {
  87. $inputData = $this->getJsonData();
  88. $rules = ['id' => 'required|int', 'title' => 'nullable|string', 'remark' => 'nullable|string', 'device_type' => 'nullable|in:I,A', 'force' => 'nullable|in:0,1', 'version_code' => 'nullable|string', 'version_type' => 'nullable', 'package_path' => 'nullable|string', 'zip_url' => 'nullable|string', 'status' => 'nullable|in:0,1'];
  89. $validator = $validatorFactory->make($inputData, $rules);
  90. if ($validator->fails()) {
  91. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  92. return json_fail($validator->errors()->first());
  93. }
  94. $info = Model::query()->where('id', $inputData['id'])->first();
  95. if (!$info) {
  96. return json_fail('APP版本信息不存在');
  97. }
  98. $versionCode = $inputData['version_code'] ?? "";
  99. $data = ['title' => $inputData['title'] ?? "", 'remark' => $inputData['remark'] ?? "", 'device_type' => $inputData['device_type'] ?? "", 'force' => $inputData['force'] ?? "", 'version_code' => $versionCode, 'version_type' => $inputData['version_type'] ?? "", 'package_path' => $inputData['package_path'] ?? "", 'zip_url' => $inputData['zip_url'] ?? "", 'status' => $inputData['status'] ?? ""];
  100. $data = action_data($data);
  101. $result = Model::query()->where('id', $inputData['id'])->update($data);
  102. $this->clearCache($versionCode);
  103. // 创建管理员日志
  104. $olditem = clone $info;
  105. $newitem = Model::query()->where('id', $inputData['id'])->first();
  106. $logSubject = "修改APP版本信息";
  107. $logSvalue = $olditem->id;
  108. $logDbData = ['after_items' => [$newitem->toArray()], 'before_items' => [$olditem->toArray()]];
  109. $this->log($logSubject, $logSvalue, $logDbData);
  110. return json_success($result);
  111. }
  112. /**
  113. * 删除 (真实删除数据)
  114. * @param ValidatorFactoryInterface $validatorFactory
  115. * @return mixed
  116. * @example param_json_data string: {"handshake":"","data":{"id":7}}
  117. */
  118. public function delete(ValidatorFactoryInterface $validatorFactory)
  119. {
  120. $inputData = $this->getJsonData();
  121. $rules = ['id' => 'required|int'];
  122. $validator = $validatorFactory->make($inputData, $rules);
  123. if ($validator->fails()) {
  124. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  125. return json_fail($validator->errors()->first());
  126. }
  127. $info = Model::query()->where('id', $inputData['id'])->first();
  128. if (!$info) {
  129. return json_fail('APP版本信息不存在');
  130. }
  131. $result = Model::query()->where('id', $inputData['id'])->delete();
  132. if (!$result) {
  133. return json_fail('删除失败');
  134. }
  135. $this->clearCache($info->version_code);
  136. // 创建管理员日志
  137. $olditem = clone $info;
  138. $logSubject = "删除APP版本信息";
  139. $logSvalue = $olditem->id;
  140. $logDbData = ['after_items' => [], 'before_items' => [$olditem->toArray()]];
  141. $this->log($logSubject, $logSvalue, $logDbData);
  142. return json_success($result);
  143. }
  144. public function clearCache($versionCode = '')
  145. {
  146. $arrKeys = ['version:I', 'version:A', 'version:H', 'force_version:I', 'force_version:A', 'force_version:H'];
  147. if (!empty($versionCode)) {
  148. $key1 = 'version:I:v' . $versionCode;
  149. $key2 = 'version:A:v' . $versionCode;
  150. $key3 = 'version:H:v' . $versionCode;
  151. $arrKeys = array_merge($arrKeys, [$key1, $key2, $key3]);
  152. }
  153. $r = make(CacheInterface::class)->deleteMultiple($arrKeys);
  154. return $r;
  155. }
  156. }