WechatReplyServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\wechat;
  13. use app\services\BaseServices;
  14. use app\dao\wechat\WechatReplyDao;
  15. use app\services\kefu\KefuServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\app\WechatService;
  18. /**
  19. *
  20. * Class UserWechatuserServices
  21. * @package app\services\user
  22. * @method delete($id, ?string $key = null) 删除
  23. * @method update($id, array $data, ?string $key = null) 更新数据
  24. */
  25. class WechatReplyServices extends BaseServices
  26. {
  27. /**
  28. * UserWechatuserServices constructor.
  29. * @param WechatReplyDao $dao
  30. */
  31. public function __construct(WechatReplyDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 消息类型
  37. * @var string[]
  38. */
  39. public function replyType()
  40. {
  41. return ['text', 'image', 'news', 'voice'];
  42. }
  43. /**
  44. * 自定义简单查询总数
  45. * @param array $where
  46. * @return int
  47. */
  48. public function getCount(array $where): int
  49. {
  50. return $this->dao->getCount($where);
  51. }
  52. /**
  53. * 复杂条件搜索列表
  54. * @param array $where
  55. * @param string $field
  56. * @return array
  57. */
  58. public function getWhereUserList(array $where, string $field): array
  59. {
  60. [$page, $limit] = $this->getPageValue();
  61. $list = $this->dao->getListByModel($where, $field, $page, $limit);
  62. $count = $this->dao->getCountByWhere($where);
  63. return [$list, $count];
  64. }
  65. /**
  66. * @param $key
  67. * @return array|\think\Model|null
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getDataByKey(string $key)
  73. {
  74. /** @var WechatKeyServices $services */
  75. $services = app()->make(WechatKeyServices::class);
  76. $data = $services->getOne(['keys' => $key]);
  77. $resdata = $this->dao->getOne(['id' => $data['reply_id'] ?? 0]);
  78. $resdata['data'] = isset($resdata['data']) ? json_decode($resdata['data'], true) : [];
  79. $resdata['key'] = $key;
  80. return $resdata;
  81. }
  82. /**
  83. * @param $data
  84. * @param $key
  85. * @param $type
  86. * @param int $status
  87. * @return bool
  88. */
  89. public function redact($data, $id, $key, $type, $status = 1)
  90. {
  91. $method = 'tidy' . ucfirst($type);
  92. if ($id == 'undefined') {
  93. $id = 0;
  94. }
  95. if ($data['content'] == '' && $data['src'] == '') $data = $data['list'][0] ?? [];
  96. try {
  97. $res = $this->{$method}($data, $id);
  98. } catch (\Throwable $e) {
  99. throw new AdminException($e->getMessage());
  100. }
  101. if (!$res) return false;
  102. $arr = [];
  103. /** @var WechatKeyServices $keyServices */
  104. $keyServices = app()->make(WechatKeyServices::class);
  105. $count = $this->dao->getCount(['id' => $id]);
  106. if ($count) {
  107. $keyServices->delete($id, 'reply_id');
  108. $insertData = explode(',', $key);
  109. foreach ($insertData as $k => $v) {
  110. $arr[$k]['keys'] = $v;
  111. $arr[$k]['reply_id'] = $id;
  112. }
  113. $res = $this->dao->update($id, ['type' => $type, 'data' => json_encode($res), 'status' => $status], 'id');
  114. $res1 = $keyServices->saveAll($arr);
  115. if (!$res || !$res1) {
  116. throw new AdminException(100006);
  117. }
  118. } else {
  119. $reply = $this->dao->save([
  120. 'type' => $type,
  121. 'data' => json_encode($res),
  122. 'status' => $status,
  123. ]);
  124. $insertData = explode(',', $key);
  125. foreach ($insertData as $k => $v) {
  126. $arr[$k]['keys'] = $v;
  127. $arr[$k]['reply_id'] = $reply->id;
  128. }
  129. $res = $keyServices->saveAll($arr);
  130. if (!$res) throw new AdminException(100006);
  131. }
  132. return true;
  133. }
  134. /**
  135. * 获取所有关键字
  136. * @param array $where
  137. * @return array
  138. */
  139. public function getKeyAll($where = array())
  140. {
  141. /** @var WechatReplyKeyServices $replyKeyServices */
  142. $replyKeyServices = app()->make(WechatReplyKeyServices::class);
  143. $data = $replyKeyServices->getReplyKeyAll($where);
  144. /** @var WechatKeyServices $keyServices */
  145. $keyServices = app()->make(WechatKeyServices::class);
  146. foreach ($data['list'] as &$item) {
  147. if ($item['data']) $item['data'] = json_decode($item['data'], true);
  148. switch ($item['type']) {
  149. case 'text':
  150. $item['typeName'] = '文字消息';
  151. break;
  152. case 'image':
  153. $item['typeName'] = '图片消息';
  154. break;
  155. case 'news':
  156. $item['typeName'] = '图文消息';
  157. break;
  158. case 'voice':
  159. $item['typeName'] = '声音消息';
  160. break;
  161. }
  162. $keys = $keyServices->getColumn(['reply_id' => $item['id']], 'keys');
  163. $item['key'] = implode(',', $keys);
  164. }
  165. return $data;
  166. }
  167. /**
  168. * 查询一条
  169. * @param $key
  170. * @return array|null|\think\Model
  171. * @throws \think\db\exception\DataNotFoundException
  172. * @throws \think\db\exception\ModelNotFoundException
  173. * @throws \think\exception\DbException
  174. */
  175. public function getKeyInfo(int $id)
  176. {
  177. $resdata = $this->dao->getOne(['id' => $id]);
  178. /** @var WechatKeyServices $keyServices */
  179. $keyServices = app()->make(WechatKeyServices::class);
  180. $keys = $keyServices->getColumn(['reply_id' => $resdata['id']], 'keys');
  181. $resdata['data'] = $resdata['data'] ? json_decode($resdata['data'], true) : [];
  182. $resdata['key'] = implode(',', $keys);
  183. return $resdata;
  184. }
  185. /**
  186. * 整理文本输入的消息
  187. * @param $data
  188. * @param $key
  189. * @return array|bool
  190. */
  191. public function tidyText($data, $id)
  192. {
  193. $res = [];
  194. if (!isset($data['content']) || $data['content'] == '') {
  195. throw new AdminException(400706);
  196. }
  197. $res['content'] = $data['content'];
  198. return $res;
  199. }
  200. /**
  201. * 整理图片资源
  202. * @param $data
  203. * @param $key
  204. * @return array|bool|mixed
  205. */
  206. public function tidyImage($data, $id)
  207. {
  208. if (!isset($data['src']) || $data['src'] == '') {
  209. throw new AdminException(400707);
  210. }
  211. $reply = $this->dao->get((int)$id);
  212. if ($reply) $reply['data'] = json_decode($reply['data'], true);
  213. if ($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']) {
  214. $res = $reply['data'];
  215. } else {
  216. $res = [];
  217. //TODO 图片转media
  218. $res['src'] = $data['src'];
  219. try {
  220. $material = WechatService::materialService()->uploadImage(url_to_path($data['src']));
  221. } catch (\Throwable $e) {
  222. throw new AdminException(WechatService::getMessage($e->getMessage()));
  223. }
  224. $res['media_id'] = $material->media_id;
  225. $dataEvent = ['type' => 'image', 'media_id' => $material->media_id, 'path' => $res['src'], 'url' => $material->url];
  226. /** @var WechatMediaServices $mateServices */
  227. $mateServices = app()->make(WechatMediaServices::class);
  228. $mateServices->save($dataEvent);
  229. }
  230. return $res;
  231. }
  232. /**
  233. * 整理声音资源
  234. * @param $data
  235. * @param $key
  236. * @return array|bool|mixed
  237. */
  238. public function tidyVoice($data, $id)
  239. {
  240. if (!isset($data['src']) || $data['src'] == '') {
  241. throw new AdminException(400708);
  242. }
  243. $reply = $this->dao->get((int)$id);
  244. if ($reply) $reply['data'] = json_decode($reply['data'], true);
  245. if ($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']) {
  246. $res = $reply['data'];
  247. } else {
  248. $res = [];
  249. //TODO 声音转media
  250. $res['src'] = $data['src'];
  251. try {
  252. $material = WechatService::materialService()->uploadVoice(url_to_path($data['src']));
  253. } catch (\Throwable $e) {
  254. throw new AdminException(WechatService::getMessage($e->getMessage()));
  255. }
  256. $res['media_id'] = $material->media_id;
  257. $dataEvent = ['media_id' => $material->media_id, 'path' => $res['src'], 'type' => 'voice'];
  258. /** @var WechatMediaServices $mateServices */
  259. $mateServices = app()->make(WechatMediaServices::class);
  260. $mateServices->save($dataEvent);
  261. }
  262. return $res;
  263. }
  264. /**
  265. * 整理图文资源
  266. * @param $data
  267. * @param $key
  268. * @return bool
  269. */
  270. public function tidyNews($data, $id = 0)
  271. {
  272. // $data = $data['list'][0];
  273. if (!count($data)) {
  274. throw new AdminException(400709);
  275. }
  276. $siteUrl = sys_config('site_url');
  277. if (empty($data['url'])) $data['url'] = $siteUrl . '/pages/news_details/index?id=' . $data['id'];
  278. if (count($data['image_input'])) $data['image'] = $data['image_input'][0];
  279. return $data;
  280. }
  281. /**
  282. * 获取关键字
  283. * @param $key
  284. * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Transfer|\EasyWeChat\Message\Voice
  285. */
  286. public function reply($key, string $openId = '')
  287. {
  288. $res = $this->dao->getKey($key);
  289. if (empty($res)) {
  290. /** @var KefuServices $services */
  291. $services = app()->make(KefuServices::class);
  292. $services->replyTransferService($key, $openId);
  293. return WechatService::transfer();
  294. }
  295. return $this->replyDataByMessage($res->toArray());
  296. }
  297. /**
  298. * 根据关键字内容返回对应的内容
  299. * @param array $res
  300. * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Voice
  301. */
  302. public function replyDataByMessage(array $res)
  303. {
  304. $res['data'] = json_decode($res['data'], true);
  305. if ($res['type'] == 'text') {
  306. return WechatService::textMessage($res['data']['content']);
  307. } else if ($res['type'] == 'image') {
  308. return WechatService::imageMessage($res['data']['media_id']);
  309. } else if ($res['type'] == 'news') {
  310. $title = $res['data']['title'] ?? '';
  311. $image = $res['data']['image'] ?? '';
  312. $description = $res['data']['synopsis'] ?? '';
  313. $url = $res['data']['url'] ?? '';
  314. return WechatService::newsMessage($title, $description, $url, $image);
  315. } else if ($res['type'] == 'voice') {
  316. return WechatService::voiceMessage($res['data']['media_id']);
  317. }
  318. }
  319. }