WechatController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace App\Http\Admin;
  3. use Endroid\QrCode\QrCode;
  4. use Illuminate\Support\Facades\Request;
  5. /**
  6. * 微信公众号授权
  7. *
  8. * Class WechatController
  9. * @package App\Http\Seller\Controllers
  10. */
  11. class WechatController extends AdminBaseController
  12. {
  13. protected $app = '';
  14. protected $miniProgram = ''; //公众号操作
  15. protected $textMenuData = [];
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $config = config('wechat.official_account.default');
  20. $this->app = Factory::officialAccount($config);
  21. }
  22. ################################################################################################################
  23. ###### 代公众号实现业务 公众号相关 ##################################################################################################
  24. ################################################################################################################
  25. /**
  26. * 获取公众号的基本信息
  27. */
  28. public function getBasicInfo()
  29. {
  30. try {
  31. $info = $this->getOpenPlatform()->getAuthorizer($this->storeInfo['g_authorizer_appid']);
  32. } catch (\Exception $e) {
  33. return responseMessage(2001, $e->getMessage());
  34. }
  35. return responseMessage(1001, 'success', $info);
  36. }
  37. /**
  38. * @return String
  39. */
  40. public function getQrCode()
  41. {
  42. $path = Request::post('path');
  43. if (empty($path)) {
  44. return responseMessage(2001, '参数错误,请重试!');
  45. }
  46. $qrCode = new QrCode($path);
  47. return responseMessage(1001, '', 'data:image/jpeg;base64,' . base64_encode($qrCode->writeString()));
  48. }
  49. ################################################################################################################
  50. ###### 素材管理 ##################################################################################################
  51. ################################################################################################################
  52. /**
  53. * 获取永久素材列表
  54. *
  55. * @return String
  56. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  57. * @throws \GuzzleHttp\Exception\GuzzleException
  58. */
  59. public function getMaterialList()
  60. {
  61. $requestData = Request::all();
  62. $type = $requestData['type'] ?? ''; // 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
  63. $page = $requestData['page'] ?? 1;
  64. $count = 20;
  65. $offset = ($page - 1) * $count;
  66. if (empty($type)) {
  67. return responseMessage(2001, '请求参数错误!');
  68. }
  69. $list = $this->app->material->list($type, $offset, $count);
  70. return responseMessage(1001, 'success', $list);
  71. }
  72. /**
  73. * 上传图片
  74. *
  75. * @return String
  76. */
  77. public function uploadMaterialImage()
  78. {
  79. return $this->uploadMaterial('image');
  80. }
  81. /**
  82. * 上传语音
  83. *
  84. * @return String
  85. */
  86. public function uploadMaterialVoice()
  87. {
  88. return $this->uploadMaterial('voice');
  89. }
  90. /**
  91. * 上传视频
  92. *
  93. * @return String
  94. */
  95. public function uploadMaterialVideo()
  96. {
  97. return $this->uploadMaterial('video');
  98. }
  99. /**
  100. * 上传缩略图
  101. *
  102. * @return String
  103. */
  104. public function uploadMaterialThumb()
  105. {
  106. return $this->uploadMaterial('thumb');
  107. }
  108. /**
  109. * 上传文章图片
  110. *
  111. * @return String
  112. */
  113. public function uploadArticleImage()
  114. {
  115. return $this->uploadMaterial('articleImage');
  116. }
  117. /**
  118. * 上传文章
  119. *
  120. * @return String
  121. */
  122. public function uploadArticle()
  123. {
  124. $requestData = Request::all();
  125. $data = $requestData['data'] ?? [];
  126. if (empty($data)) {
  127. return responseMessage(2001, '图文消息内容不完整');
  128. }
  129. return $this->uploadMaterial('article', $data);
  130. }
  131. /**
  132. * 获取临时素材详情
  133. *
  134. * @return String
  135. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  136. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  137. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  138. * @throws \GuzzleHttp\Exception\GuzzleException
  139. */
  140. public function getMaterialInfo()
  141. {
  142. $requestData = Request::all();
  143. $type = $requestData['type'] ?? '';
  144. $mediaId = $requestData['mediaId'] ?? '';
  145. if (empty($mediaId)) {
  146. return responseMessage(2001, '参数错误!');
  147. }
  148. $stream = $this->app->media->get($mediaId);
  149. if ($type == 'img') {
  150. $filename = mt_rand(1, 9) . '.jpg';
  151. } elseif ($type == 'voice') {
  152. $filename = mt_rand(1, 9) . '.mp3';
  153. } elseif ($type == 'news') {
  154. return responseMessage(1001, 'success', $stream);
  155. } elseif ($type == 'video') {
  156. return responseMessage(1001, 'success', $stream);
  157. }
  158. $stream->saveAs(public_path('static/upload/tmp/' . $this->storeId), $filename);
  159. return responseMessage(1001, 'success!', '/upload/tmp/' . $this->storeId . $filename);
  160. }
  161. /**
  162. * 删除素材
  163. *
  164. * @return String
  165. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  166. * @throws \GuzzleHttp\Exception\GuzzleException
  167. */
  168. public function delMaterial()
  169. {
  170. $requestData = Request::all();
  171. $mediaId = $requestData['mediaId'] ?? '';
  172. if (empty($mediaId)) {
  173. return responseMessage(2001, '参数错误!');
  174. }
  175. $result = $this->app->material->delete($mediaId);
  176. if ($result['errcode'] == 0) {
  177. return responseMessage(1001, '删除成功!');
  178. } else {
  179. return responseMessage(2002, '失败 - ' . $result['errmsg']);
  180. }
  181. }
  182. /**
  183. * @param $type
  184. * @param array $data
  185. * @return String
  186. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  187. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  188. * @throws \GuzzleHttp\Exception\GuzzleException
  189. */
  190. private function uploadMaterial($type, $data = [])
  191. {
  192. if ($type == 'article') {
  193. $articleArr = [];
  194. foreach ($data as $item) {
  195. if (!isset($item['title']) || empty($item['title'])) {
  196. return responseMessage(2001, '图文消息,标题不能为空');
  197. }
  198. if (!isset($item['thumb_media_id']) || empty($item['thumb_media_id'])) {
  199. return responseMessage(2001, '图文消息,封面图片必须填写');
  200. }
  201. $article = new Article($item);
  202. $articleArr[] = $article;
  203. }
  204. $result = $this->app->material->uploadArticle($articleArr);
  205. if ($result['errcode'] == 0) {
  206. return responseMessage(1001, 'success', $result);
  207. } else {
  208. return responseMessage(3002, '上传失败-' . $result['errmsg']);
  209. }
  210. } else {
  211. $upload = Request::file('file');
  212. if ($upload) {
  213. $upload_dir = '/static/upload/wechat/' . $type . '/' . mt_rand(1, 99) . '/';
  214. $disk_dir = public_path($upload_dir);
  215. if (!file_exists($disk_dir) || !is_dir($disk_dir)) {
  216. @mkdir($disk_dir, 0777, true);
  217. }
  218. $file_name = md5(microtime()) . '.' . $upload->getClientOriginalExtension();
  219. $upload->move($disk_dir, $file_name);
  220. $path = $upload_dir . $file_name;
  221. if ($upload->getError()) {
  222. return responseMessage(2002, $upload->getErrorMessage());
  223. }
  224. $absolutePath = public_path($path);
  225. if ($type == 'image') {
  226. $result = $this->app->material->uploadImage($absolutePath);
  227. } elseif ($type == 'voice') {
  228. $result = $this->app->material->uploadVoice($absolutePath);
  229. } elseif ($type == 'video') {
  230. $title = $data['title'];
  231. $desc = $data['desc'];
  232. $result = $this->app->material->uploadVideo($absolutePath, $title, $desc);
  233. } elseif ($type == 'thumb') {
  234. $result = $this->app->material->uploadThumb($absolutePath);
  235. } elseif ($type == 'articleImage') {
  236. $result = $this->app->material->uploadArticleImage($absolutePath);
  237. } else {
  238. return responseMessage(3001, '参数错误');
  239. }
  240. if (!isset($result['errcode'])) {
  241. return responseMessage(1001, 'success', $result);
  242. } else {
  243. return responseMessage(3002, '上传失败-' . $result['errmsg']);
  244. }
  245. } else {
  246. return responseMessage(2000, '上传的文件不存在,请重试!');
  247. }
  248. }
  249. }
  250. ################################################################################################################
  251. ###### 菜单管理 ##################################################################################################
  252. ################################################################################################################
  253. /**
  254. * 获取当前菜单
  255. */
  256. public function menuCurrent()
  257. {
  258. $result = $this->app->menu->list();
  259. return responseMessage(1001, 'success', $result);
  260. }
  261. /**
  262. * 创建普通菜单
  263. */
  264. public function menuCreate()
  265. {
  266. $requestData = Request::all();
  267. $data = $requestData['data'] ?? [];
  268. if (empty($data)) {
  269. return responseMessage(2001, '菜单内容不完整');
  270. }
  271. // 参数设置
  272. foreach ($data as $key => $row) {
  273. if (isset($row['sub_button']) && $row['sub_button']) {
  274. foreach ($row['sub_button'] as $skey => $sRow) {
  275. if ($sRow['type'] == 'click') {
  276. $key_val = getRandNumber();
  277. $this->textMenuData[$key_val] = $sRow['value'];
  278. $data[$key]['sub_button'][$skey]['key'] = $key_val;
  279. }
  280. }
  281. } else if ($row['type'] == 'click') {
  282. $key_val = getRandNumber();
  283. $this->textMenuData[$key_val] = $row['value'];
  284. $data[$key]['key'] = $key_val;
  285. }
  286. }
  287. $result = $this->app->menu->create($data);
  288. if ($result['errcode'] == 0) {
  289. return responseMessage(1001, '创建成功!');
  290. } else {
  291. return responseMessage(2003, '上传失败-' . $result['errmsg']);
  292. }
  293. }
  294. /**
  295. * 全部删除
  296. */
  297. public function menuDelete()
  298. {
  299. $result = $this->app->menu->delete();
  300. return responseMessage(1001, 'success', $result);
  301. }
  302. }