'dev', // 默认使用的 channel,生产环境可以改为下面的 prod // 'channels' => [ // // 测试环境 // 'dev' => [ // 'driver' => 'single', // 'path' => \Yii::$app->runtimePath . '/logs/easywechat1.log', // 'level' => 'debug', // ], // // 生产环境 // 'prod' => [ // 'driver' => 'daily', // 'path' => '/tmp/easywechat.log', // 'level' => 'info', // ], // ], // ]; return $conf; } return []; } public function init() { parent::init(); $config = $this->wechatConf(); if($config){ $this->wechat_mp = Factory::officialAccount($config); } } public function wxUploadImage($url) { $path = $this->saveTempImage($url); if ($this->wechat_mp) { $api = $this->wechat_mp->material; $res = $api->uploadImage($path); \Yii::error([__METHOD__, $res]); if($res['errcode']){ return [ 'code' => 1, 'msg' => $res['errmsg'], 'res' => $res, ]; } return $res; } else { return [ 'code' => 1, 'msg' => '需要配置微信信息' ]; } } public function wxDelMaterial($media_ids) { $api = $this->wechat_mp->material; foreach ($media_ids as $media_id) { $res = $api->delete($media_id); \Yii::error([__METHOD__, $res]); } return $res; } //获取网络图片到临时目录 public 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; } }