123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace App\Http\Admin;
- use Endroid\QrCode\QrCode;
- use Illuminate\Support\Facades\Request;
- /**
- * 微信公众号授权
- *
- * Class WechatController
- * @package App\Http\Seller\Controllers
- */
- class WechatController extends AdminBaseController
- {
- protected $app = '';
- protected $miniProgram = ''; //公众号操作
- protected $textMenuData = [];
- public function __construct()
- {
- parent::__construct();
- $config = config('wechat.official_account.default');
- $this->app = Factory::officialAccount($config);
- }
- ################################################################################################################
- ###### 代公众号实现业务 公众号相关 ##################################################################################################
- ################################################################################################################
- /**
- * 获取公众号的基本信息
- */
- public function getBasicInfo()
- {
- try {
- $info = $this->getOpenPlatform()->getAuthorizer($this->storeInfo['g_authorizer_appid']);
- } catch (\Exception $e) {
- return responseMessage(2001, $e->getMessage());
- }
- return responseMessage(1001, 'success', $info);
- }
- /**
- * @return String
- */
- public function getQrCode()
- {
- $path = Request::post('path');
- if (empty($path)) {
- return responseMessage(2001, '参数错误,请重试!');
- }
- $qrCode = new QrCode($path);
- return responseMessage(1001, '', 'data:image/jpeg;base64,' . base64_encode($qrCode->writeString()));
- }
- ################################################################################################################
- ###### 素材管理 ##################################################################################################
- ################################################################################################################
- /**
- * 获取永久素材列表
- *
- * @return String
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getMaterialList()
- {
- $requestData = Request::all();
- $type = $requestData['type'] ?? ''; // 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
- $page = $requestData['page'] ?? 1;
- $count = 20;
- $offset = ($page - 1) * $count;
- if (empty($type)) {
- return responseMessage(2001, '请求参数错误!');
- }
- $list = $this->app->material->list($type, $offset, $count);
- return responseMessage(1001, 'success', $list);
- }
- /**
- * 上传图片
- *
- * @return String
- */
- public function uploadMaterialImage()
- {
- return $this->uploadMaterial('image');
- }
- /**
- * 上传语音
- *
- * @return String
- */
- public function uploadMaterialVoice()
- {
- return $this->uploadMaterial('voice');
- }
- /**
- * 上传视频
- *
- * @return String
- */
- public function uploadMaterialVideo()
- {
- return $this->uploadMaterial('video');
- }
- /**
- * 上传缩略图
- *
- * @return String
- */
- public function uploadMaterialThumb()
- {
- return $this->uploadMaterial('thumb');
- }
- /**
- * 上传文章图片
- *
- * @return String
- */
- public function uploadArticleImage()
- {
- return $this->uploadMaterial('articleImage');
- }
- /**
- * 上传文章
- *
- * @return String
- */
- public function uploadArticle()
- {
- $requestData = Request::all();
- $data = $requestData['data'] ?? [];
- if (empty($data)) {
- return responseMessage(2001, '图文消息内容不完整');
- }
- return $this->uploadMaterial('article', $data);
- }
- /**
- * 获取临时素材详情
- *
- * @return String
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function getMaterialInfo()
- {
- $requestData = Request::all();
- $type = $requestData['type'] ?? '';
- $mediaId = $requestData['mediaId'] ?? '';
- if (empty($mediaId)) {
- return responseMessage(2001, '参数错误!');
- }
- $stream = $this->app->media->get($mediaId);
- if ($type == 'img') {
- $filename = mt_rand(1, 9) . '.jpg';
- } elseif ($type == 'voice') {
- $filename = mt_rand(1, 9) . '.mp3';
- } elseif ($type == 'news') {
- return responseMessage(1001, 'success', $stream);
- } elseif ($type == 'video') {
- return responseMessage(1001, 'success', $stream);
- }
- $stream->saveAs(public_path('static/upload/tmp/' . $this->storeId), $filename);
- return responseMessage(1001, 'success!', '/upload/tmp/' . $this->storeId . $filename);
- }
- /**
- * 删除素材
- *
- * @return String
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function delMaterial()
- {
- $requestData = Request::all();
- $mediaId = $requestData['mediaId'] ?? '';
- if (empty($mediaId)) {
- return responseMessage(2001, '参数错误!');
- }
- $result = $this->app->material->delete($mediaId);
- if ($result['errcode'] == 0) {
- return responseMessage(1001, '删除成功!');
- } else {
- return responseMessage(2002, '失败 - ' . $result['errmsg']);
- }
- }
- /**
- * @param $type
- * @param array $data
- * @return String
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- private function uploadMaterial($type, $data = [])
- {
- if ($type == 'article') {
- $articleArr = [];
- foreach ($data as $item) {
- if (!isset($item['title']) || empty($item['title'])) {
- return responseMessage(2001, '图文消息,标题不能为空');
- }
- if (!isset($item['thumb_media_id']) || empty($item['thumb_media_id'])) {
- return responseMessage(2001, '图文消息,封面图片必须填写');
- }
- $article = new Article($item);
- $articleArr[] = $article;
- }
- $result = $this->app->material->uploadArticle($articleArr);
- if ($result['errcode'] == 0) {
- return responseMessage(1001, 'success', $result);
- } else {
- return responseMessage(3002, '上传失败-' . $result['errmsg']);
- }
- } else {
- $upload = Request::file('file');
- if ($upload) {
- $upload_dir = '/static/upload/wechat/' . $type . '/' . mt_rand(1, 99) . '/';
- $disk_dir = public_path($upload_dir);
- if (!file_exists($disk_dir) || !is_dir($disk_dir)) {
- @mkdir($disk_dir, 0777, true);
- }
- $file_name = md5(microtime()) . '.' . $upload->getClientOriginalExtension();
- $upload->move($disk_dir, $file_name);
- $path = $upload_dir . $file_name;
- if ($upload->getError()) {
- return responseMessage(2002, $upload->getErrorMessage());
- }
- $absolutePath = public_path($path);
- if ($type == 'image') {
- $result = $this->app->material->uploadImage($absolutePath);
- } elseif ($type == 'voice') {
- $result = $this->app->material->uploadVoice($absolutePath);
- } elseif ($type == 'video') {
- $title = $data['title'];
- $desc = $data['desc'];
- $result = $this->app->material->uploadVideo($absolutePath, $title, $desc);
- } elseif ($type == 'thumb') {
- $result = $this->app->material->uploadThumb($absolutePath);
- } elseif ($type == 'articleImage') {
- $result = $this->app->material->uploadArticleImage($absolutePath);
- } else {
- return responseMessage(3001, '参数错误');
- }
- if (!isset($result['errcode'])) {
- return responseMessage(1001, 'success', $result);
- } else {
- return responseMessage(3002, '上传失败-' . $result['errmsg']);
- }
- } else {
- return responseMessage(2000, '上传的文件不存在,请重试!');
- }
- }
- }
- ################################################################################################################
- ###### 菜单管理 ##################################################################################################
- ################################################################################################################
- /**
- * 获取当前菜单
- */
- public function menuCurrent()
- {
- $result = $this->app->menu->list();
- return responseMessage(1001, 'success', $result);
- }
- /**
- * 创建普通菜单
- */
- public function menuCreate()
- {
- $requestData = Request::all();
- $data = $requestData['data'] ?? [];
- if (empty($data)) {
- return responseMessage(2001, '菜单内容不完整');
- }
- // 参数设置
- foreach ($data as $key => $row) {
- if (isset($row['sub_button']) && $row['sub_button']) {
- foreach ($row['sub_button'] as $skey => $sRow) {
- if ($sRow['type'] == 'click') {
- $key_val = getRandNumber();
- $this->textMenuData[$key_val] = $sRow['value'];
- $data[$key]['sub_button'][$skey]['key'] = $key_val;
- }
- }
- } else if ($row['type'] == 'click') {
- $key_val = getRandNumber();
- $this->textMenuData[$key_val] = $row['value'];
- $data[$key]['key'] = $key_val;
- }
- }
- $result = $this->app->menu->create($data);
- if ($result['errcode'] == 0) {
- return responseMessage(1001, '创建成功!');
- } else {
- return responseMessage(2003, '上传失败-' . $result['errmsg']);
- }
- }
- /**
- * 全部删除
- */
- public function menuDelete()
- {
- $result = $this->app->menu->delete();
- return responseMessage(1001, 'success', $result);
- }
- }
|