| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\wxlive\models\goods;
- use app\utils\CurlHelper;
- use yii\base\Model;
- use app\plugins\wxlive\models\Wechat;
- class GoodsDetailForm extends Model
- {
- public $store_id;
- public $goods_id;
- public $page = 1;
- public $limit = 10;
- public $status = -1;
- public $data;
- public function getGoodsList()
- {
- $start = ($this->page - 1) * $this->limit;
- $wechat = Wechat::getWechat();
- $postData = [
- 'offset' => $start,
- 'limit' => $this->limit,
- 'status' => $this->status ,
- ];
- return $wechat->broadcast->getApproved($postData);
- }
- public function submitGoods() {
- $wechat = Wechat::getWechat();
- $coverImgUrl = trim($this->data['coverImgUrl']);
- $coverImgPath = $this->saveTempImage($coverImgUrl);
- $coverImgMedia = $wechat->media->upload('image', $coverImgPath);
- $postData = [
- 'name' => trim($this->data['name']),
- 'coverImgUrl' => $coverImgMedia['media_id'],
- 'priceType' => intval($this->data['priceType']),
- 'price' => floor($this->data['price']),
- 'price2' => $this->data['price2'] ? floor($this->data['price2']) : '',
- 'url' => 'goods/goods/goods?id='.$this->data['id'],
- ];
- $wechat = Wechat::getWechat();
- return $wechat->broadcast->create($postData);
- }
- public function delGoods()
- {
- $wechat = Wechat::getWechat();
- return $wechat->broadcast->delete((int)$this->goods_id);
- }
- /**
- * 获取请求地址
- * @return string
- */
- protected function getRequestUrl()
- {
- $wechat = $this->getWechat();
- $accessToen = $wechat->getAccessToken();
- return $accessToen;
- }
- /**
- * 发送请求
- * @param $url
- * @param array $data
- * @return \Curl\Curl
- */
- protected function sendPost($url, $data = [])
- {
- $postJson = json_encode($data);
- $curl = $this->getWechat()->curl;
- $curl->setHeader('Content-Type', 'application/json; charset=utf-8');
- $curl->setHeader('Content-Length', strlen($postJson));
- $curl->setOpt(CURLOPT_POST, true);
- $curl->setOpt(CURLOPT_URL, $url);
- $curl->setOpt(CURLOPT_POSTFIELDS, $postJson);
- $curl->_exec();
- return $curl;
- }
- /**
- * 发送请求
- * @param $url
- * @param array $data
- * @return \Curl\Curl
- */
- protected function sendGet($url, $data = [])
- {
- $this->getWechat()->curl->get($url, $data);
- return $this->getWechat()->curl->response;
- }
- public function upload($url, $filedata)
- {
- if (class_exists('\CURLFile')) {
- $filedata = ['media' => new \CURLFile(realpath($filedata))];
- } else {
- $filedata = ['media' => '@' . realpath($filedata)];
- }
- $curl = curl_init ();
- if (class_exists ( '\CURLFile' )){
- curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, true );
- }else{
- if(defined('CURLOPT_SAFE_UPLOAD')){ curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false );
- }
- }
- curl_setopt ( $curl, CURLOPT_URL, $url );
- curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
- curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
- if (! empty ( $filedata )) {
- curl_setopt ( $curl, CURLOPT_POST, 1 );
- curl_setopt ( $curl, CURLOPT_POSTFIELDS, $filedata );
- }
- curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
- $output = curl_exec ( $curl );
- curl_close ( $curl );
- return $output;
- }
- //获取网络图片到临时目录
- private function saveTempImage($url)
- {
- if (strpos($url,'http') === false) {
- $url = 'http:'. trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- }
|