WeappVersionService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\weapp;
  12. use app\dict\sys\CloudDict;
  13. use app\service\core\site\CoreSiteService;
  14. use app\service\core\weapp\CoreWeappCloudService;
  15. use app\service\core\weapp\CoreWeappConfigService;
  16. use app\service\core\weapp\CoreWeappService;
  17. use core\base\BaseAdminService;
  18. use app\model\weapp\WeappVersion;
  19. use core\exception\CommonException;
  20. /**
  21. * 小程序包版本发布
  22. */
  23. class WeappVersionService extends BaseAdminService
  24. {
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->model = new WeappVersion();
  29. }
  30. /**
  31. * 添加小程序版本
  32. * @param array $data
  33. */
  34. public function add(array $data)
  35. {
  36. $uploading = $this->model->where([ ['site_id', '=', $this->site_id], ['status', '=', 0] ])->field('id')->findOrEmpty();
  37. if (!$uploading->isEmpty()) throw new CommonException('WEAPP_UPLOADING');
  38. $version_no = $this->model->where([ ['site_id', '=', $this->site_id] ])->order('version_no desc')->field('version_no')->findOrEmpty()->toArray()['version_no'] ?? 0;
  39. $version_no += 1;
  40. $version = "1.0.{$version_no}";
  41. $upload_res = (new CoreWeappCloudService())->setConfig(function () {
  42. $config = (new CoreWeappConfigService())->getWeappConfig($this->site_id);
  43. return [
  44. 'app_id' => $config['app_id'],
  45. 'upload_private_key' => $config['upload_private_key'],
  46. 'addon' => (new CoreSiteService())->getAddonKeysBySiteId($this->site_id)
  47. ];
  48. })->uploadWeapp([
  49. 'site_id' => $this->site_id,
  50. 'version' => $version,
  51. 'desc' => $data['desc'] ?? ''
  52. ]);
  53. $res = $this->model->create([
  54. 'site_id' => $this->site_id,
  55. 'version' => $version,
  56. 'version_no' => $version_no,
  57. 'desc' => $data['desc'] ?? '',
  58. 'create_time' => time(),
  59. 'task_key' => $upload_res['key']
  60. ]);
  61. return $res->id;
  62. }
  63. public function getPreviewImage() {
  64. try {
  65. $version = $this->model->where([ ['site_id', '=', $this->site_id] ])->order('id desc')->findOrEmpty();
  66. if (!$version->isEmpty() || in_array($version['status'], [CloudDict::APPLET_UPLOAD_SUCCESS, CloudDict::APPLET_AUDITING])) {
  67. if ($version['from_type'] == 'cloud_build') {
  68. return (new CoreWeappCloudService())->getWeappPreviewImage();
  69. } else {
  70. return image_to_base64((new CoreWeappService())->getWeappPreviewImage($this->site_id), true);
  71. }
  72. }
  73. } catch (\Exception $e) {
  74. return '';
  75. }
  76. }
  77. /**
  78. * 获取小程序版本列表
  79. * @param array $where
  80. * @return array
  81. */
  82. public function getPage(array $where = [])
  83. {
  84. $field = 'id, version, version_no, desc, create_time, status, fail_reason, task_key';
  85. $order = 'create_time desc';
  86. $where[] = ['site_id', '=', $this->site_id];
  87. $search_model = $this->model->where($where)->field($field)->order($order)->append(['status_name']);
  88. return $this->pageQuery($search_model);
  89. }
  90. /**
  91. * 编辑
  92. * @param int $id
  93. * @param array $data
  94. * @return true
  95. */
  96. public function edit(int $id, array $data)
  97. {
  98. $data['status'] = 0;
  99. $data['update_time'] = time();
  100. $this->model->where([['id', '=', $id], ['site_id', '=', $this->site_id] ])->create($data);
  101. return true;
  102. }
  103. /**
  104. * 删除
  105. * @param int $id
  106. * @return true
  107. */
  108. public function del(int $id){
  109. $this->model->where([['id', '=', $id], ['site_id', '=', $this->site_id]])->delete();
  110. return true;
  111. }
  112. /**
  113. * 获取小程序上传日志
  114. * @param string $key
  115. * @return null
  116. */
  117. public function getUploadLog(string $key) {
  118. $build_log = (new CoreWeappCloudService())->getWeappCompileLog($key);
  119. if (isset($build_log['data']) && isset($build_log['data'][0]) && is_array($build_log['data'][0])) {
  120. $last = end($build_log['data'][0]);
  121. if ($last['code'] == 0) {
  122. (new WeappVersion())->update(['status' => CloudDict::APPLET_UPLOAD_FAIL, 'fail_reason' => $last['msg'] ?? '', 'update_time' => time() ], ['task_key' => $key]);
  123. return $build_log;
  124. }
  125. if ($last['percent'] == 100) {
  126. (new WeappVersion())->update(['status' => CloudDict::APPLET_UPLOAD_SUCCESS, 'update_time' => time() ], ['task_key' => $key]);
  127. }
  128. }
  129. return $build_log;
  130. }
  131. }