WechatTemplateUploadController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers\setting;
  8. use app\modules\admin\controllers\BaseController;
  9. use app\models\Option;
  10. use app\modules\admin\models\WechatThirdForm;
  11. class WechatTemplateUploadController extends BaseController
  12. {
  13. private $url = 'http://chidian.we10.cn/';
  14. /**
  15. * 登录
  16. *
  17. * @return void
  18. */
  19. public function actionWxLogin()
  20. {
  21. set_time_limit(0);
  22. $appid = Option::get('platform_wechat_third_appid', 0, 'saas')['value'] ?? '';
  23. if (empty($appid)) {
  24. return $this->asJson([
  25. 'code' => 1,
  26. 'msg' => '请先配置三方平台小程序appid',
  27. ]);
  28. }
  29. $url = $this->url . 'template/login.php';
  30. $host = str_replace(['http://', 'https://'], ['', ''], \Yii::$app->request->hostInfo);
  31. $ws_url = isset(\Yii::$app->params['ws_url']) ? \Yii::$app->params['ws_url'] : 'ws.cyyvip.com';
  32. $res = http_get($url, [
  33. 'query' => [
  34. 'appid' => $appid,
  35. 'http_url' => $host,
  36. 'version' => cyy_version(),
  37. 'ws_url' => $ws_url,
  38. ]
  39. ]);
  40. if ($res->getStatusCode() != 200) {
  41. return $this->asJson([
  42. 'code' => 1,
  43. 'msg' => '请求出错!',
  44. ]);
  45. }
  46. $content = json_decode((string)$res->getBody());
  47. return $this->asJson($content);
  48. }
  49. /**
  50. * 上传小程序
  51. *
  52. * @return void
  53. */
  54. public function actionUpload()
  55. {
  56. set_time_limit(0);
  57. $appid = Option::get('platform_wechat_third_appid', 0, 'saas')['value'] ?? '';
  58. if (empty($appid)) {
  59. return $this->asJson([
  60. 'code' => 1,
  61. 'msg' => '请先配置三方平台小程序appid',
  62. ]);
  63. }
  64. $url = $this->url . 'template/upload.php';
  65. $host = str_replace(['http://', 'https://'], ['', ''], \Yii::$app->request->hostInfo);
  66. $res = http_get($url, [
  67. 'query' => [
  68. 'appid' => $appid,
  69. 'http_url' => $host,
  70. 'version' => cyy_version(),
  71. ]
  72. ]);
  73. if ($res->getStatusCode() != 200) {
  74. return $this->asJson([
  75. 'code' => 1,
  76. 'msg' => '请求出错!',
  77. ]);
  78. }
  79. $content = json_decode((string)$res->getBody());
  80. return $this->asJson($content);
  81. }
  82. public function actionAddTemplate()
  83. {
  84. try {
  85. $form = new WechatThirdForm;
  86. $res = $form->openPlatform->code_template->getDrafts();
  87. if ($res['errcode'] != 0) {
  88. throw new \Exception('添加到模版库失败');
  89. }
  90. $draft_list = $res['draft_list'];
  91. foreach ($draft_list as &$item) {
  92. $item['draft_id'] = (string)$item['draft_id'];
  93. }
  94. $last_names = array_column($draft_list, 'create_time');
  95. array_multisort($last_names, SORT_DESC, $draft_list);
  96. $id = $draft_list[0]['draft_id'];
  97. $res = $form->openPlatform->code_template->createFromDraft($id);
  98. if ($res['errcode'] != 0) {
  99. throw new \Exception('添加到模版库失败');
  100. }
  101. return $this->asJson([
  102. 'code' => 0,
  103. 'msg' => '添加成功',
  104. ]);
  105. } catch(\Throwable $e) {
  106. return $this->asJson([
  107. 'code' => 1,
  108. 'msg' => $e->getMessage(),
  109. ]);
  110. }
  111. }
  112. }