AllianceWechatUploadController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\utils\ShareQrcode;
  10. use app\models\WechatConfig;
  11. use app\models\Option;
  12. class AllianceWechatUploadController extends BaseController
  13. {
  14. private $url = 'http://shangmeng.we10.cn/';
  15. /**
  16. * 获取小程序码
  17. *
  18. * @return void
  19. */
  20. public function actionQrcode()
  21. {
  22. try {
  23. $res = ShareQrcode::wxQrcodeByPlatform('pages/home/home', '0');
  24. if ($res['code'] == 1) {
  25. return $this->asJson([
  26. 'code' => 1,
  27. 'msg' => $res['response']['errmsg'],
  28. ]);
  29. }
  30. return $this->asJson([
  31. 'code' => 0,
  32. 'msg' => 'success',
  33. 'data' => $res['url_path'] . "?time=" . time(),//解决切换小程序appid后,前端图片显示的是旧的图片
  34. ]);
  35. } catch (\Exception $e) {
  36. return $this->asJson([
  37. 'code' => 1,
  38. 'msg' => $e->getMessage(),
  39. 'file' => $e->getFile(),
  40. 'line' => $e->getLine(),
  41. ]);
  42. }
  43. }
  44. /**
  45. * 登录
  46. *
  47. * @return void
  48. */
  49. public function actionWxLogin()
  50. {
  51. set_time_limit(0);
  52. $appid = Option::get('platform_appid', 0, 'saas')['value'] ?? '';
  53. if (empty($appid)) {
  54. return $this->asJson([
  55. 'code' => 1,
  56. 'msg' => '请先配置联盟小程序appid',
  57. ]);
  58. }
  59. $url = $this->url . 'login.php';
  60. $host = str_replace(['http://', 'https://'], ['', ''], \Yii::$app->request->hostInfo);
  61. $ws_url = isset(\Yii::$app->params['ws_url']) ? \Yii::$app->params['ws_url'] : 'ws.cyyvip.com';
  62. $res = http_get($url, [
  63. 'query' => [
  64. 'type' => 'shangmeng',
  65. 'appid' => $appid,
  66. 'http_url' => $host,
  67. 'version' => cyy_version(),
  68. 'ws_url' => $ws_url,
  69. ]
  70. ]);
  71. if ($res->getStatusCode() != 200) {
  72. return $this->asJson([
  73. 'code' => 1,
  74. 'msg' => '请求出错!',
  75. ]);
  76. }
  77. $content = json_decode((string)$res->getBody());
  78. return $this->asJson($content);
  79. }
  80. /**
  81. * 获取预览码
  82. *
  83. * @return void
  84. */
  85. public function actionPreview($num = 1)
  86. {
  87. set_time_limit(0);
  88. try {
  89. $appid = Option::get('platform_appid', 0, 'saas')['value'] ?? '';
  90. if (empty($appid)) {
  91. return $this->asJson([
  92. 'code' => 1,
  93. 'msg' => '请先配置联盟小程序appid',
  94. ]);
  95. }
  96. $url = $this->url . 'preview.php';
  97. $host = str_replace(['http://', 'https://'], ['', ''], \Yii::$app->request->hostInfo);
  98. $res = http_get($url, [
  99. 'query' => [
  100. 'type' => 'shangmeng',
  101. 'appid' => $appid,
  102. 'http_url' => $host,
  103. 'version' => cyy_version(),
  104. ]
  105. ]);
  106. if ($res->getStatusCode() != 200) {
  107. if ($num < 4) {
  108. return $this->actionPreview($num + 1);
  109. } else {
  110. return $this->asJson([
  111. 'code' => 1,
  112. 'msg' => '请求出错!',
  113. ]);
  114. }
  115. }
  116. $content = json_decode((string)$res->getBody());
  117. return $this->asJson($content);
  118. } catch (\Exception $e) {
  119. if ($num < 4) {
  120. return $this->actionPreview($num + 1);
  121. } else {
  122. return $this->asJson([
  123. 'code' => 1,
  124. 'msg' => '请求出错!',
  125. ]);
  126. }
  127. }
  128. }
  129. /**
  130. * 上传小程序
  131. *
  132. * @return void
  133. */
  134. public function actionUpload()
  135. {
  136. set_time_limit(0);
  137. $appid = Option::get('platform_appid', 0, 'saas')['value'] ?? '';
  138. if (empty($appid)) {
  139. return $this->asJson([
  140. 'code' => 1,
  141. 'msg' => '请先配置联盟小程序appid',
  142. ]);
  143. }
  144. $url = $this->url . 'upload.php';
  145. $host = str_replace(['http://', 'https://'], ['', ''], \Yii::$app->request->hostInfo);
  146. $res = http_get($url, [
  147. 'query' => [
  148. 'type' => 'shangmeng',
  149. 'appid' => $appid,
  150. 'http_url' => $host,
  151. 'version' => cyy_version(),
  152. ]
  153. ]);
  154. if ($res->getStatusCode() != 200) {
  155. return $this->asJson([
  156. 'code' => 1,
  157. 'msg' => '请求出错!',
  158. ]);
  159. }
  160. $content = json_decode((string)$res->getBody());
  161. return $this->asJson($content);
  162. }
  163. }