| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- /**
- * APP版本升级管理
- * @author xiabian
- */
- declare(strict_types=1);
- namespace Modules\Manage\Controllers;
- use App\Model\VersionModel as Model;
- use Hyperf\Validation\Contract\ValidatorFactoryInterface;
- use Psr\SimpleCache\CacheInterface;
- class VersionController extends BaseController
- {
- /**
- * 列表
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"page":1,"page_size":5,"search":{"title":"","device_type":"I"}}}
- */
- public function lists(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $page = isset($inputData['page']) ? (int)$inputData['page'] : 1;
- $page_size = isset($inputData['page_size']) ? (int)$inputData['page_size'] : 20;
- $select = ['*'];
- $model = Model::query();
- $search = isset($inputData['search']) ? $inputData['search'] : array();
- if ($search) {
- foreach ($search as $k => $v) {
- if (!strlen((string)$v)) {
- continue;
- }
- switch ($k) {
- case "title": // 标题
- break;
- default:
- $model->where($k, $v);
- }
- }
- }
- $result = $model->select($select)
- ->orderBy('id', 'desc')
- ->paginate($page_size, ['*'], 'page', $page);
- return json_success($result);
- }
- /**
- * 新增
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string:
- * {"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}}
- */
- public function add(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $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',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- return json_fail($validator->errors()->first());
- }
- $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'],
- ];
- $result = Model::query()
- ->create($data);
- $this->clearCache($inputData['version_code']);
- // 创建管理员日志
- $newitem = clone $result;
- $logSubject = "新增App版本";
- $logSvalue = $newitem->id;
- $logDbData = [
- 'after_items' => [
- $newitem->toArray(),
- ],
- 'before_items' => [],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- /**
- * 编辑
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":7,"title":"测试编辑","remark":"","device_type":"","force":0,"version_code":"","version_type":"","package_path":"","status":0}}
- */
- public function update(ValidatorFactoryInterface $validatorFactory)
- {
- $inputData = $this->getJsonData();
- $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',
- ];
- $validator = $validatorFactory->make($inputData, $rules);
- if ($validator->fails()) {
- // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
- return json_fail($validator->errors()->first());
- }
- $info = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- if (!$info) {
- return json_fail('APP版本信息不存在');
- }
- $versionCode = $inputData['version_code'] ?? "";
- $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'] ?? "",
- ];
- $data = action_data($data);
- $result = Model::query()
- ->where('id', $inputData['id'])
- ->update($data);
- $this->clearCache($versionCode);
- // 创建管理员日志
- $olditem = clone $info;
- $newitem = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- $logSubject = "修改APP版本信息";
- $logSvalue = $olditem->id;
- $logDbData = [
- 'after_items' => [
- $newitem->toArray(),
- ],
- 'before_items' => [
- $olditem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- /**
- * 删除 (真实删除数据)
- * @param ValidatorFactoryInterface $validatorFactory
- * @return mixed
- * @example param_json_data string: {"handshake":"","data":{"id":7}}
- */
- 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());
- }
- $info = Model::query()
- ->where('id', $inputData['id'])
- ->first();
- if (!$info) {
- return json_fail('APP版本信息不存在');
- }
- $result = Model::query()
- ->where('id', $inputData['id'])
- ->delete();
- if (!$result) {
- return json_fail('删除失败');
- }
- $this->clearCache($info->version_code);
- // 创建管理员日志
- $olditem = clone $info;
- $logSubject = "删除APP版本信息";
- $logSvalue = $olditem->id;
- $logDbData = [
- 'after_items' => [
- ],
- 'before_items' => [
- $olditem->toArray(),
- ],
- ];
- $this->log($logSubject, $logSvalue, $logDbData);
- return json_success($result);
- }
- public function clearCache($versionCode = '')
- {
- $arrKeys = ['version:I','version:A','version:H', 'force_version:I', 'force_version:A', 'force_version:H'];
- if (!empty($versionCode)) {
- $key1 = 'version:I:v'. $versionCode;
- $key2 = 'version:A:v'. $versionCode;
- $key3 = 'version:H:v'. $versionCode;
- $arrKeys = array_merge($arrKeys, [$key1, $key2, $key3]);
- }
- $r = make(CacheInterface::class)
- ->deleteMultiple($arrKeys);
- return $r;
- }
- }
|