AppVersionServices.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\system;
  13. use app\dao\system\AppVersionDao;
  14. use app\services\BaseServices;
  15. use crmeb\services\FormBuilder as Form;
  16. use think\facade\Route as Url;
  17. /**
  18. * Class AppVersionServices
  19. * @package app\services\system
  20. */
  21. class AppVersionServices extends BaseServices
  22. {
  23. /**
  24. * DiyServices constructor.
  25. * @param AppVersionDao $dao
  26. */
  27. public function __construct(AppVersionDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 版本列表
  33. * @param $platform
  34. * @return array
  35. */
  36. public function versionList($platform)
  37. {
  38. [$page, $limit] = $this->getPageValue();
  39. $list = $this->dao->versionList($platform, $page, $limit);
  40. foreach ($list as &$item) {
  41. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  42. }
  43. $count = $this->dao->count(['platform' => $platform]);
  44. return compact('list', 'count');
  45. }
  46. /**
  47. * 添加版本表单
  48. * @return array
  49. * @throws \FormBuilder\Exception\FormBuilderException
  50. */
  51. public function createForm($id = 0)
  52. {
  53. if ($id) {
  54. $info = $this->dao->get($id);
  55. }
  56. $field[] = Form::hidden('id', $info['id'] ?? 0);
  57. $field[] = Form::input('version', '版本号', $info['version'] ?? '')->col(24);
  58. $field[] = Form::radio('platform', '平台类型', $info['platform'] ?? 1)->options([['label' => 'Android', 'value' => 1], ['label' => 'IOS', 'value' => 2]]);
  59. $field[] = Form::input('info', '版本介绍', $info['info'] ?? '')->type('textarea');
  60. $field[] = Form::input('url', '下载链接', $info['url'] ?? '');
  61. $field[] = Form::radio('is_force', '强制', $info['is_force'] ?? 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  62. $field[] = Form::radio('is_new', '是否最新', $info['is_new'] ?? 1)->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]]);
  63. return create_form('添加版本信息', $field, Url::buildUrl('/system/version_save'), 'POST');
  64. }
  65. /**
  66. * 保存数据
  67. * @param $id
  68. * @param $data
  69. * @return mixed
  70. */
  71. public function versionSave($id, $data)
  72. {
  73. if ($id) {
  74. return $this->transaction(function () use ($data, $id) {
  75. if ($data['is_new']) {
  76. $this->dao->update(['platform' => $data['platform']], ['is_new' => 0]);
  77. }
  78. return $this->dao->update($id, $data);
  79. });
  80. } else {
  81. $data['is_del'] = 0;
  82. $data['add_time'] = time();
  83. return $this->transaction(function () use ($data) {
  84. $this->dao->update(['platform' => $data['platform']], ['is_new' => 0]);
  85. return $this->dao->save($data);
  86. });
  87. }
  88. }
  89. /**
  90. * 获取系统下最新的版本信息
  91. * @param $platform
  92. * @return array
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function getNewInfo($platform)
  98. {
  99. $res = $this->dao->get(['platform' => $platform, 'is_new' => 1]);
  100. if ($res) {
  101. $res = $res->toArray();
  102. $res['time'] = date('Y-m-d H:i:s', $res['add_time']);
  103. return $res;
  104. } else {
  105. return [];
  106. }
  107. }
  108. }