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