WechatForm.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\setting;
  8. use app\models\WechatConfig;
  9. use yii\base\Model;
  10. class WechatForm extends Model
  11. {
  12. private $url = 'http://chidian.we10.cn/';
  13. public $store_id;
  14. public function preview($num = 1)
  15. {
  16. $wechat_config = WechatConfig::findOne(['store_id' => $this->store_id, 'type' => 1]);
  17. $url = $this->url . 'preview.php';
  18. $host = str_replace(['http://', 'https://'], ['', ''], \Yii::$app->request->hostInfo);
  19. try {
  20. $res = http_get($url, [
  21. 'query' => [
  22. 'type' => 'new_system',
  23. 'appid' => $wechat_config->app_id,
  24. 'http_url' => $host,
  25. 'store_id' => $this->store_id,
  26. 'version' => cyy_version(),
  27. ]
  28. ]);
  29. if ($res->getStatusCode() != 200) {
  30. if ($num < 4) {
  31. sleep(5);
  32. return $this->preview($num + 1);
  33. } else {
  34. return [
  35. 'code' => 1,
  36. 'msg' => '请求出错!',
  37. ];
  38. }
  39. }
  40. $content = json_decode((string)$res->getBody());
  41. return $content;
  42. } catch (\Throwable $e) {
  43. if ($num < 4) {
  44. return $this->preview($num + 1);
  45. } else {
  46. return [
  47. 'code' => 1,
  48. 'msg' => '请求出错!',
  49. ];
  50. }
  51. }
  52. }
  53. public function upload($num = 1)
  54. {
  55. $wechat_config = WechatConfig::findOne(['store_id' => $this->store_id, 'type' => 1]);
  56. $url = $this->url . 'upload.php';
  57. $host = str_replace(['http://', 'https://'], ['', ''], \Yii::$app->request->hostInfo);
  58. try {
  59. $res = http_get($url, [
  60. 'query' => [
  61. 'type' => 'new_system',
  62. 'appid' => $wechat_config->app_id,
  63. 'http_url' => $host,
  64. 'store_id' => $this->store_id,
  65. 'version' => cyy_version(),
  66. ]
  67. ]);
  68. if ($res->getStatusCode() != 200) {
  69. if ($num < 4) {
  70. sleep(5);
  71. return $this->upload($num + 1);
  72. } else {
  73. return [
  74. 'code' => 1,
  75. 'msg' => '请求出错!',
  76. ];
  77. }
  78. }
  79. $content = json_decode((string)$res->getBody());
  80. return $content;
  81. } catch (\Throwable $e) {
  82. if ($num < 4) {
  83. return $this->upload($num + 1);
  84. } else {
  85. return [
  86. 'code' => 1,
  87. 'msg' => '请求出错!',
  88. ];
  89. }
  90. }
  91. }
  92. }