GoodsDetailForm.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\wxlive\models\goods;
  8. use app\utils\CurlHelper;
  9. use yii\base\Model;
  10. use app\plugins\wxlive\models\Wechat;
  11. class GoodsDetailForm extends Model
  12. {
  13. public $store_id;
  14. public $goods_id;
  15. public $page = 1;
  16. public $limit = 10;
  17. public $status = -1;
  18. public $data;
  19. public function getGoodsList()
  20. {
  21. $start = ($this->page - 1) * $this->limit;
  22. $wechat = Wechat::getWechat();
  23. $postData = [
  24. 'offset' => $start,
  25. 'limit' => $this->limit,
  26. 'status' => $this->status ,
  27. ];
  28. return $wechat->broadcast->getApproved($postData);
  29. }
  30. public function submitGoods() {
  31. $wechat = Wechat::getWechat();
  32. $coverImgUrl = trim($this->data['coverImgUrl']);
  33. $coverImgPath = $this->saveTempImage($coverImgUrl);
  34. $coverImgMedia = $wechat->media->upload('image', $coverImgPath);
  35. $postData = [
  36. 'name' => trim($this->data['name']),
  37. 'coverImgUrl' => $coverImgMedia['media_id'],
  38. 'priceType' => intval($this->data['priceType']),
  39. 'price' => floor($this->data['price']),
  40. 'price2' => $this->data['price2'] ? floor($this->data['price2']) : '',
  41. 'url' => 'goods/goods/goods?id='.$this->data['id'],
  42. ];
  43. $wechat = Wechat::getWechat();
  44. return $wechat->broadcast->create($postData);
  45. }
  46. public function delGoods()
  47. {
  48. $wechat = Wechat::getWechat();
  49. return $wechat->broadcast->delete((int)$this->goods_id);
  50. }
  51. /**
  52. * 获取请求地址
  53. * @return string
  54. */
  55. protected function getRequestUrl()
  56. {
  57. $wechat = $this->getWechat();
  58. $accessToen = $wechat->getAccessToken();
  59. return $accessToen;
  60. }
  61. /**
  62. * 发送请求
  63. * @param $url
  64. * @param array $data
  65. * @return \Curl\Curl
  66. */
  67. protected function sendPost($url, $data = [])
  68. {
  69. $postJson = json_encode($data);
  70. $curl = $this->getWechat()->curl;
  71. $curl->setHeader('Content-Type', 'application/json; charset=utf-8');
  72. $curl->setHeader('Content-Length', strlen($postJson));
  73. $curl->setOpt(CURLOPT_POST, true);
  74. $curl->setOpt(CURLOPT_URL, $url);
  75. $curl->setOpt(CURLOPT_POSTFIELDS, $postJson);
  76. $curl->_exec();
  77. return $curl;
  78. }
  79. /**
  80. * 发送请求
  81. * @param $url
  82. * @param array $data
  83. * @return \Curl\Curl
  84. */
  85. protected function sendGet($url, $data = [])
  86. {
  87. $this->getWechat()->curl->get($url, $data);
  88. return $this->getWechat()->curl->response;
  89. }
  90. public function upload($url, $filedata)
  91. {
  92. if (class_exists('\CURLFile')) {
  93. $filedata = ['media' => new \CURLFile(realpath($filedata))];
  94. } else {
  95. $filedata = ['media' => '@' . realpath($filedata)];
  96. }
  97. $curl = curl_init ();
  98. if (class_exists ( '\CURLFile' )){
  99. curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, true );
  100. }else{
  101. if(defined('CURLOPT_SAFE_UPLOAD')){ curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false );
  102. }
  103. }
  104. curl_setopt ( $curl, CURLOPT_URL, $url );
  105. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
  106. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
  107. if (! empty ( $filedata )) {
  108. curl_setopt ( $curl, CURLOPT_POST, 1 );
  109. curl_setopt ( $curl, CURLOPT_POSTFIELDS, $filedata );
  110. }
  111. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
  112. $output = curl_exec ( $curl );
  113. curl_close ( $curl );
  114. return $output;
  115. }
  116. //获取网络图片到临时目录
  117. private function saveTempImage($url)
  118. {
  119. if (strpos($url,'http') === false) {
  120. $url = 'http:'. trim($url);
  121. }
  122. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  123. mkdir(\Yii::$app->runtimePath . '/image');
  124. }
  125. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  126. CurlHelper::download($url, $save_path);
  127. return $save_path;
  128. }
  129. }