VersionController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /**
  14. * 列表
  15. * @param ValidatorFactoryInterface $validatorFactory
  16. * @return mixed
  17. * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"title":"","device_type":"I"}}}
  18. */
  19. public function lists(ValidatorFactoryInterface $validatorFactory)
  20. {
  21. $inputData = $this->getJsonData();
  22. $page = isset($inputData['page']) ? (int)$inputData['page'] : 1;
  23. $page_size = isset($inputData['page_size']) ? (int)$inputData['page_size'] : 20;
  24. $select = ['*'];
  25. $model = Model::query();
  26. $search = isset($inputData['search']) ? $inputData['search'] : array();
  27. if ($search) {
  28. foreach ($search as $k => $v) {
  29. if (!strlen((string)$v)) {
  30. continue;
  31. }
  32. switch ($k) {
  33. case "title": // 标题
  34. break;
  35. default:
  36. $model->where($k, $v);
  37. }
  38. }
  39. }
  40. $result = $model->select($select)
  41. ->orderBy('id', 'desc')
  42. ->paginate($page_size, ['*'], 'page', $page);
  43. return json_success($result);
  44. }
  45. /**
  46. * 新增
  47. * @param ValidatorFactoryInterface $validatorFactory
  48. * @return mixed
  49. * @example param_json_data string:
  50. * {"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}}
  51. */
  52. public function add(ValidatorFactoryInterface $validatorFactory)
  53. {
  54. $inputData = $this->getJsonData();
  55. $rules = [
  56. 'title' => 'required|string',
  57. 'remark' => 'nullable|string',
  58. 'device_type' => 'required|in:I,A',
  59. 'force' => 'required|in:0,1',
  60. 'version_code' => 'required|string',
  61. 'version_type' => 'required',
  62. 'package_path' => 'required|string',
  63. 'zip_url' => 'nullable|string',
  64. 'status' => 'required|in:0,1',
  65. ];
  66. $validator = $validatorFactory->make($inputData, $rules);
  67. if ($validator->fails()) {
  68. return json_fail($validator->errors()->first());
  69. }
  70. $data = [
  71. 'title' => $inputData['title'],
  72. 'remark' => $inputData['remark'] ?? "",
  73. 'device_type' => $inputData['device_type'],
  74. 'force' => $inputData['force'],
  75. 'version_code' => $inputData['version_code'],
  76. 'version_type' => $inputData['version_type'],
  77. 'package_path' => $inputData['package_path'],
  78. 'zip_url' => $inputData['zip_url'],
  79. 'status' => $inputData['status'],
  80. ];
  81. $result = Model::query()
  82. ->create($data);
  83. $this->clearCache($inputData['version_code']);
  84. // 创建管理员日志
  85. $newitem = clone $result;
  86. $logSubject = "新增App版本";
  87. $logSvalue = $newitem->id;
  88. $logDbData = [
  89. 'after_items' => [
  90. $newitem->toArray(),
  91. ],
  92. 'before_items' => [],
  93. ];
  94. $this->log($logSubject, $logSvalue, $logDbData);
  95. return json_success($result);
  96. }
  97. /**
  98. * 编辑
  99. * @param ValidatorFactoryInterface $validatorFactory
  100. * @return mixed
  101. * @example param_json_data string: {"handshake":"","data":{"id":7,"title":"测试编辑","remark":"","device_type":"","force":0,"version_code":"","version_type":"","package_path":"","status":0}}
  102. */
  103. public function update(ValidatorFactoryInterface $validatorFactory)
  104. {
  105. $inputData = $this->getJsonData();
  106. $rules = [
  107. 'id' => 'required|int',
  108. 'title' => 'nullable|string',
  109. 'remark' => 'nullable|string',
  110. 'device_type' => 'nullable|in:I,A',
  111. 'force' => 'nullable|in:0,1',
  112. 'version_code' => 'nullable|string',
  113. 'version_type' => 'nullable',
  114. 'package_path' => 'nullable|string',
  115. 'zip_url' => 'nullable|string',
  116. 'status' => 'nullable|in:0,1',
  117. ];
  118. $validator = $validatorFactory->make($inputData, $rules);
  119. if ($validator->fails()) {
  120. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  121. return json_fail($validator->errors()->first());
  122. }
  123. $info = Model::query()
  124. ->where('id', $inputData['id'])
  125. ->first();
  126. if (!$info) {
  127. return json_fail('APP版本信息不存在');
  128. }
  129. $versionCode = $inputData['version_code'] ?? "";
  130. $data = [
  131. 'title' => $inputData['title'] ?? "",
  132. 'remark' => $inputData['remark'] ?? "",
  133. 'device_type' => $inputData['device_type'] ?? "",
  134. 'force' => $inputData['force'] ?? "",
  135. 'version_code' => $versionCode,
  136. 'version_type' => $inputData['version_type'] ?? "",
  137. 'package_path' => $inputData['package_path'] ?? "",
  138. 'zip_url' => $inputData['zip_url'] ?? "",
  139. 'status' => $inputData['status'] ?? "",
  140. ];
  141. $data = action_data($data);
  142. $result = Model::query()
  143. ->where('id', $inputData['id'])
  144. ->update($data);
  145. $this->clearCache($versionCode);
  146. // 创建管理员日志
  147. $olditem = clone $info;
  148. $newitem = Model::query()
  149. ->where('id', $inputData['id'])
  150. ->first();
  151. $logSubject = "修改APP版本信息";
  152. $logSvalue = $olditem->id;
  153. $logDbData = [
  154. 'after_items' => [
  155. $newitem->toArray(),
  156. ],
  157. 'before_items' => [
  158. $olditem->toArray(),
  159. ],
  160. ];
  161. $this->log($logSubject, $logSvalue, $logDbData);
  162. return json_success($result);
  163. }
  164. /**
  165. * 删除 (真实删除数据)
  166. * @param ValidatorFactoryInterface $validatorFactory
  167. * @return mixed
  168. * @example param_json_data string: {"handshake":"","data":{"id":7}}
  169. */
  170. public function delete(ValidatorFactoryInterface $validatorFactory)
  171. {
  172. $inputData = $this->getJsonData();
  173. $rules = [
  174. 'id' => 'required|int',
  175. ];
  176. $validator = $validatorFactory->make($inputData, $rules);
  177. if ($validator->fails()) {
  178. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  179. return json_fail($validator->errors()->first());
  180. }
  181. $info = Model::query()
  182. ->where('id', $inputData['id'])
  183. ->first();
  184. if (!$info) {
  185. return json_fail('APP版本信息不存在');
  186. }
  187. $result = Model::query()
  188. ->where('id', $inputData['id'])
  189. ->delete();
  190. if (!$result) {
  191. return json_fail('删除失败');
  192. }
  193. $this->clearCache($info->version_code);
  194. // 创建管理员日志
  195. $olditem = clone $info;
  196. $logSubject = "删除APP版本信息";
  197. $logSvalue = $olditem->id;
  198. $logDbData = [
  199. 'after_items' => [
  200. ],
  201. 'before_items' => [
  202. $olditem->toArray(),
  203. ],
  204. ];
  205. $this->log($logSubject, $logSvalue, $logDbData);
  206. return json_success($result);
  207. }
  208. public function clearCache($versionCode = '')
  209. {
  210. $arrKeys = ['version:I','version:A','version:H', 'force_version:I', 'force_version:A', 'force_version:H'];
  211. if (!empty($versionCode)) {
  212. $key1 = 'version:I:v'. $versionCode;
  213. $key2 = 'version:A:v'. $versionCode;
  214. $key3 = 'version:H:v'. $versionCode;
  215. $arrKeys = array_merge($arrKeys, [$key1, $key2, $key3]);
  216. }
  217. $r = make(CacheInterface::class)
  218. ->deleteMultiple($arrKeys);
  219. return $r;
  220. }
  221. }