WechatReplyServices.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\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']]);
  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('保存失败!');
  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('保存失败!');
  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('请输入回复信息内容');
  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('请上传回复的图片');
  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. $material = WechatService::materialService()->uploadImage(url_to_path($data['src']));
  220. $res['media_id'] = $material->media_id;
  221. $dataEvent = ['type' => 'image', 'media_id' => $material->media_id, 'path' => $res['src'], 'url' => $material->url];
  222. /** @var WechatMediaServices $mateServices */
  223. $mateServices = app()->make(WechatMediaServices::class);
  224. $mateServices->save($dataEvent);
  225. }
  226. return $res;
  227. }
  228. /**
  229. * 整理声音资源
  230. * @param $data
  231. * @param $key
  232. * @return array|bool|mixed
  233. */
  234. public function tidyVoice($data, $id)
  235. {
  236. if (!isset($data['src']) || $data['src'] == '') {
  237. throw new AdminException('请上传回复的声音');
  238. }
  239. $reply = $this->dao->get((int)$id);
  240. if ($reply) $reply['data'] = json_decode($reply['data'], true);
  241. if ($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']) {
  242. $res = $reply['data'];
  243. } else {
  244. $res = [];
  245. //TODO 声音转media
  246. $res['src'] = $data['src'];
  247. $material = WechatService::materialService()->uploadVoice(url_to_path($data['src']));
  248. $res['media_id'] = $material->media_id;
  249. $dataEvent = ['media_id' => $material->media_id, 'path' => $res['src'], 'type' => 'voice'];
  250. /** @var WechatMediaServices $mateServices */
  251. $mateServices = app()->make(WechatMediaServices::class);
  252. $mateServices->save($dataEvent);
  253. }
  254. return $res;
  255. }
  256. /**
  257. * 整理图文资源
  258. * @param $data
  259. * @param $key
  260. * @return bool
  261. */
  262. public function tidyNews($data, $id = 0)
  263. {
  264. // $data = $data['list'][0];
  265. if (!count($data)) {
  266. throw new AdminException('请选择图文消息');
  267. }
  268. $siteUrl = sys_config('site_url');
  269. if (empty($data['url'])) $data['url'] = $siteUrl . '/pages/news_details/index?id=' . $data['id'];
  270. if (count($data['image_input'])) $data['image'] = $data['image_input'][0];
  271. return $data;
  272. }
  273. /**
  274. * 获取关键字
  275. * @param $key
  276. * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Transfer|\EasyWeChat\Message\Voice
  277. */
  278. public function reply($key, string $openId = '')
  279. {
  280. $res = $this->dao->getKey($key);
  281. if (empty($res)) {
  282. /** @var KefuServices $services */
  283. $services = app()->make(KefuServices::class);
  284. $services->replyTransferService($key, $openId);
  285. return WechatService::transfer();
  286. }
  287. return $this->replyDataByMessage($res->toArray());
  288. }
  289. /**
  290. * 根据关键字内容返回对应的内容
  291. * @param array $res
  292. * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Voice
  293. */
  294. public function replyDataByMessage(array $res)
  295. {
  296. $res['data'] = json_decode($res['data'], true);
  297. if ($res['type'] == 'text') {
  298. return WechatService::textMessage($res['data']['content']);
  299. } else if ($res['type'] == 'image') {
  300. return WechatService::imageMessage($res['data']['media_id']);
  301. } else if ($res['type'] == 'news') {
  302. $title = $res['data']['title'] ?? '';
  303. $image = $res['data']['image'] ?? '';
  304. $description = $res['data']['synopsis'] ?? '';
  305. $url = $res['data']['url'] ?? '';
  306. return WechatService::newsMessage($title, $description, $url, $image);
  307. } else if ($res['type'] == 'voice') {
  308. return WechatService::voiceMessage($res['data']['media_id']);
  309. }
  310. }
  311. }