RoutineTemplate.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. namespace app\adminapi\controller\v1\application\routine;
  12. use app\jobs\notice\SyncMessageJob;
  13. use app\services\message\SystemNotificationServices;
  14. use app\services\other\QrcodeServices;
  15. use app\services\system\attachment\SystemAttachmentServices;
  16. use crmeb\exceptions\AdminException;
  17. use think\exception\ValidateException;
  18. use think\facade\App;
  19. use app\adminapi\controller\AuthController;
  20. use crmeb\services\FileService;
  21. use app\services\other\UploadService;
  22. use crmeb\services\app\MiniProgramService;
  23. /**
  24. * Class RoutineTemplate
  25. * @package app\adminapi\controller\v1\application\routine
  26. */
  27. class RoutineTemplate extends AuthController
  28. {
  29. protected $cacheTag = '_system_wechat';
  30. /**
  31. * 构造方法
  32. * WechatTemplate constructor.
  33. * @param App $app
  34. * @param SystemNotificationServices $services
  35. */
  36. public function __construct(App $app, SystemNotificationServices $services)
  37. {
  38. parent::__construct($app);
  39. $this->services = $services;
  40. }
  41. /**
  42. * 同步订阅消息
  43. * @return mixed
  44. * @throws \think\db\exception\DataNotFoundException
  45. * @throws \think\db\exception\DbException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. */
  48. public function syncSubscribe()
  49. {
  50. if (!sys_config('routine_appId') || !sys_config('routine_appsecret')) {
  51. throw new AdminException(400236);
  52. }
  53. $list = MiniProgramService::getSubscribeTemplateList();
  54. foreach ($list->data as $v) {
  55. MiniProgramService::delSubscribeTemplate($v['priTmplId']);
  56. }
  57. $tempKeys = $this->services->getTempKey('routine');
  58. foreach ($tempKeys as $key => $content) {
  59. SyncMessageJob::dispatch('SyncSubscribe', [$key, $content]);
  60. }
  61. return app('json')->success(100038);
  62. }
  63. /**
  64. * 下载小程序
  65. * @return mixed
  66. */
  67. public function downloadTemp()
  68. {
  69. [$name, $is_live] = $this->request->postMore([
  70. ['name', ''],
  71. ['is_live', 0]
  72. ], true);
  73. if (sys_config('routine_appId', '') == '') throw new AdminException(400236);
  74. try {
  75. @unlink(public_path() . 'statics/download/routine.zip');
  76. //拷贝源文件
  77. /** @var FileService $fileService */
  78. $fileService = app(FileService::class);
  79. $fileService->copyDir(public_path() . 'statics/mp_view', public_path() . 'statics/download');
  80. //替换appid和名称
  81. $this->updateConfigJson(sys_config('routine_appId'), $name != '' ? $name : sys_config('routine_name'));
  82. //是否开启直播
  83. if ($is_live == 0) $this->updateAppJson();
  84. //替换url
  85. $this->updateUrl('https://' . $_SERVER['HTTP_HOST']);
  86. //压缩文件
  87. $fileService->addZip(public_path() . 'statics/download', public_path() . 'statics/download/routine.zip', public_path() . 'statics/download');
  88. $data['url'] = sys_config('site_url') . '/statics/download/routine.zip';
  89. return app('json')->success($data);
  90. } catch (\Throwable $e) {
  91. throw new AdminException($e->getMessage());
  92. }
  93. }
  94. /**
  95. * 替换url
  96. * @param $url
  97. */
  98. public function updateUrl($url)
  99. {
  100. $fileUrl = app()->getRootPath() . "public/statics/download/common/vendor.js";
  101. $string = file_get_contents($fileUrl); //加载配置文件
  102. $string = str_replace('https://demo.crmeb.com', $url, $string); // 正则查找然后替换
  103. $newFileUrl = app()->getRootPath() . "public/statics/download/common/vendor.js";
  104. @file_put_contents($newFileUrl, $string); // 写入配置文件
  105. }
  106. /**
  107. * 判断是否开启直播(弃用)
  108. * @param int $iszhibo
  109. */
  110. public function updateAppJson()
  111. {
  112. $fileUrl = app()->getRootPath() . "public/statics/download/app.json";
  113. $string = file_get_contents($fileUrl); //加载配置文件
  114. $pats = '/,
  115. "plugins": \{
  116. "live-player-plugin": \{
  117. "version": "(.*?)",
  118. "provider": "(.*?)"
  119. }
  120. }/';
  121. $string = preg_replace($pats, '', $string); // 正则查找然后替换
  122. $newFileUrl = app()->getRootPath() . "public/statics/download/app.json";
  123. @file_put_contents($newFileUrl, $string); // 写入配置文件
  124. }
  125. /**
  126. * 替换appid
  127. * @param string $appid
  128. * @param string $projectanme
  129. */
  130. public function updateConfigJson($appId = '', $projectName = '')
  131. {
  132. $fileUrl = app()->getRootPath() . "public/statics/download/project.config.json";
  133. $string = file_get_contents($fileUrl); //加载配置文件
  134. // 替换appid
  135. $appIdOld = '/"appid"(.*?),/';
  136. $appIdNew = '"appid"' . ': ' . '"' . $appId . '",';
  137. $string = preg_replace($appIdOld, $appIdNew, $string); // 正则查找然后替换
  138. // 替换小程序名称
  139. $projectNameOld = '/"projectname"(.*?),/';
  140. $projectNameNew = '"projectname"' . ': ' . '"' . $projectName . '",';
  141. $string = preg_replace($projectNameOld, $projectNameNew, $string); // 正则查找然后替换
  142. $newFileUrl = app()->getRootPath() . "public/statics/download/project.config.json";
  143. @file_put_contents($newFileUrl, $string); // 写入配置文件
  144. }
  145. /**
  146. * 获取小程序码
  147. * @return string
  148. * @throws \think\Exception
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. */
  153. public function getDownloadInfo()
  154. {
  155. $data['routine_name'] = sys_config('routine_name', '');
  156. if (sys_config('routine_appId') == '') {
  157. $data['code'] = '';
  158. } else {
  159. $name = $data['routine_name'] . '.jpg';
  160. /** @var SystemAttachmentServices $systemAttachmentModel */
  161. $systemAttachmentModel = app()->make(SystemAttachmentServices::class);
  162. $imageInfo = $systemAttachmentModel->getInfo(['name' => $name]);
  163. if (!$imageInfo) {
  164. /** @var QrcodeServices $qrcode */
  165. $qrcode = app()->make(QrcodeServices::class);
  166. $resForever = $qrcode->qrCodeForever(0, 'code');
  167. if ($resForever) {
  168. $resCode = MiniProgramService::appCodeUnlimitService($resForever->id, '', 280);
  169. $res = ['res' => $resCode, 'id' => $resForever->id];
  170. } else {
  171. $res = false;
  172. }
  173. if (!$res) throw new ValidateException(400237);
  174. $upload = UploadService::init(1);
  175. if ($upload->to('routine/code')->setAuthThumb(false)->stream((string)$res['res'], $name) === false) {
  176. return $upload->getError();
  177. }
  178. $imageInfo = $upload->getUploadInfo();
  179. $imageInfo['image_type'] = 1;
  180. $systemAttachmentModel->attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  181. $qrcode->update($res['id'], ['status' => 1, 'time' => time(), 'qrcode_url' => $imageInfo['dir']]);
  182. $data['code'] = sys_config('site_url') . $imageInfo['dir'];
  183. } else $data['code'] = sys_config('site_url') . $imageInfo['att_dir'];
  184. }
  185. $data['appId'] = sys_config('routine_appId');
  186. $data['help'] = 'https://doc.crmeb.com/web/single/crmeb_v4/978';
  187. return app('json')->success($data);
  188. }
  189. }