WechatReplyRepository.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. namespace app\common\repositories\wechat;
  12. use app\common\dao\BaseDao;
  13. use app\common\dao\wechat\WechatReplyDao;
  14. use app\common\model\wechat\WechatReply;
  15. use app\common\repositories\BaseRepository;
  16. use crmeb\services\WechatService;
  17. use EasyWeChat\Message\Image;
  18. use EasyWeChat\Message\News;
  19. use EasyWeChat\Message\Text;
  20. use EasyWeChat\Message\Voice;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. use think\exception\ValidateException;
  25. use think\Model;
  26. /**
  27. * Class WechatReplyRepository
  28. * @package app\common\repositories\wechat
  29. * @author xaboy
  30. * @day 2020-04-24
  31. * @mixin WechatReplyDao
  32. */
  33. class WechatReplyRepository extends BaseRepository
  34. {
  35. /**
  36. * WechatReplyRepository constructor.
  37. * @param WechatReplyDao $dao
  38. */
  39. public function __construct(WechatReplyDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * @param array $where
  45. * @param int $page
  46. * @param int $limit
  47. * @return array
  48. * @throws DataNotFoundException
  49. * @throws DbException
  50. * @throws ModelNotFoundException
  51. * @author xaboy
  52. * @day 2020-04-27
  53. */
  54. public function getLst(array $where, int $page, int $limit)
  55. {
  56. $query = $this->dao->search($where);
  57. $count = $query->count();
  58. $list = $query->setOption('field', [])->field('wechat_reply_id,key,type,status,create_time')->page($page, $limit)->select();
  59. return compact('list', 'count');
  60. }
  61. /**
  62. * @param string $key
  63. * @param string $type
  64. * @param array $data
  65. * @param int $status
  66. * @param int $hidden
  67. * @throws DataNotFoundException
  68. * @throws DbException
  69. * @throws ModelNotFoundException
  70. * @author xaboy
  71. * @day 2020-04-24
  72. */
  73. public function save(string $key, string $type, array $data, int $status = 1, int $hidden = 0)
  74. {
  75. $method = 'tidy' . ucfirst($type);
  76. $reply = $this->dao->keyByReply($key);
  77. $data = $this->{$method}($data, $reply);
  78. if ($reply) {
  79. $reply->save(compact('status', 'data', 'hidden', 'type'));
  80. } else {
  81. $this->dao->create(compact('key', 'type', 'data', 'status', 'hidden'));
  82. }
  83. }
  84. /**
  85. * @param $id
  86. * @param array $data
  87. * @param int $hidden
  88. * @return bool
  89. * @throws DataNotFoundException
  90. * @throws DbException
  91. * @throws ModelNotFoundException
  92. * @author xaboy
  93. * @day 2020-04-27
  94. */
  95. public function updateReply($id, array $data, int $hidden = 0)
  96. {
  97. $method = 'tidy' . ucfirst($data['type']);
  98. $reply = $this->dao->get($id);
  99. $data['data'] = $this->{$method}($data['data'], $reply);
  100. $data['hidden'] = $hidden;
  101. return $reply->save($data);
  102. }
  103. /**
  104. * @param array $data
  105. * @param int $hidden
  106. * @return BaseDao|Model
  107. * @author xaboy
  108. * @day 2020-04-27
  109. */
  110. public function create(array $data, int $hidden = 0)
  111. {
  112. $method = 'tidy' . ucfirst($data['type']);
  113. $data['data'] = $this->{$method}($data['data'], null);
  114. $data['hidden'] = $hidden;
  115. return $this->dao->create($data);
  116. }
  117. /**
  118. * @param string $key
  119. * @return array|News|Text|Image|Voice|void
  120. * @throws DataNotFoundException
  121. * @throws DbException
  122. * @throws ModelNotFoundException
  123. * @author xaboy
  124. * @day 2020-04-27
  125. */
  126. public function reply(string $key)
  127. {
  128. $reply = $this->dao->keyByValidData($key);
  129. if (!$reply && $key != 'default')
  130. $reply = $this->dao->keyByValidData('default');
  131. if (!$reply) return;
  132. if ($reply['type'] == 'voice')
  133. return WechatService::voiceMessage($reply['data']['media_id']);
  134. else if ($reply['type'] == 'image')
  135. return WechatService::imageMessage($reply['data']['media_id']);
  136. else if ($reply['type'] == 'news')
  137. return WechatService::newsMessage($reply['data']['list']);
  138. else if (isset($reply['data']['content'])) {
  139. return WechatService::textMessage($reply['data']['content']);
  140. }
  141. }
  142. /**
  143. * @param $data
  144. * @param WechatReply|null $reply
  145. * @return array
  146. * @author xaboy
  147. * @day 2020-04-24
  148. */
  149. public function tidyText($data, ?WechatReply $reply)
  150. {
  151. $res = [];
  152. if (!isset($data['content']) || !$data['content'])
  153. throw new ValidateException('请输入回复信息内容');
  154. $res['content'] = $data['content'];
  155. return $res;
  156. }
  157. /**
  158. * @param $data
  159. * @param WechatReply|null $reply
  160. * @return array|mixed|null
  161. * @author xaboy
  162. * @day 2020-04-24
  163. */
  164. public function tidyImage($data, ?WechatReply $reply)
  165. {
  166. if (!isset($data['src']) || !$data['src'])
  167. throw new ValidateException('请上传回复的图片');
  168. $res = null;
  169. if ($reply) {
  170. $replyData = $reply->getAttr('data');
  171. if (isset($replyData['src']) && $replyData['src'] == $data['src'])
  172. $res = $replyData;
  173. }
  174. if (is_null($res)) {
  175. $res = [
  176. 'src' => $data['src']
  177. ];
  178. if (!file_exists(app()->getRootPath() . 'public' . $data['src']))
  179. throw new ValidateException('图片文件不存在');
  180. $material = WechatService::create()->getApplication()->material->uploadImage(app()->getRootPath() . 'public' . $data['src']);
  181. $res['media_id'] = $material->media_id;
  182. }
  183. return $res;
  184. }
  185. /**
  186. * @param $data
  187. * @param WechatReply|null $reply
  188. * @return array|mixed|null
  189. * @author xaboy
  190. * @day 2020-04-24
  191. */
  192. public static function tidyVoice($data, ?WechatReply $reply)
  193. {
  194. if (!isset($data['src']) || !$data['src'])
  195. throw new ValidateException('请上传回复的声音');
  196. $res = null;
  197. if ($reply) {
  198. $replyData = $reply->getAttr('data');
  199. if (isset($replyData['src']) && $replyData['src'] == $data['src'])
  200. $res = $replyData;
  201. }
  202. if (is_null($res)) {
  203. $res = [
  204. 'src' => $data['src']
  205. ];
  206. if (!file_exists(app()->getRootPath() . 'public' . $data['src']))
  207. throw new ValidateException('声音文件不存在');
  208. $material = WechatService::create()->getApplication()->material->uploadVoice(app()->getRootPath() . 'public' . $data['src']);
  209. $res['media_id'] = $material->media_id;
  210. }
  211. return $res;
  212. }
  213. /**
  214. * @param $params
  215. * @param WechatReply|null $reply
  216. * @return mixed
  217. * @author xaboy
  218. * @day 2020-04-24
  219. */
  220. public static function tidyNews($params, ?WechatReply $reply)
  221. {
  222. if (!isset($params['list']) || !count($params['list']))
  223. throw new ValidateException('请选择图文消息');
  224. $siteUrl = systemConfig('site_url');
  225. $data = $params['list'];
  226. foreach ($data as $k => $v) {
  227. if (empty($v['url'])) $data[$k]['url'] = rtrim($siteUrl, '/') . '/pages/news_details/index?id=' . $v['article_id'];
  228. if ($v['image_input']) $data[$k]['image'] = $v['image_input'];
  229. }
  230. $params['list'] = $data;
  231. return $params;
  232. }
  233. }