| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\wechat_mp;
- use yii\base\Model as YModel;
- use app\models\Option;
- use app\constants\OptionSetting;
- use EasyWeChat\Factory;
- use app\utils\CurlHelper;
- class Model extends YModel
- {
- public $need_mp = true;
- public $wechat_mp;
- public function rules()
- {
- return [
- [['wechat_mp'], 'safe'],
- ];
- }
-
- public function wechatConf() {
- $conf = Option::get(OptionSetting::WECHAT_MP, 0, 'saas')['value'];
- if($conf){
- $conf = json_decode($conf, true);
- $conf['response_type'] = 'array';
- // $conf['log'] = [
- // 'default' => '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;
- }
- }
|