WechatReplyRepository.php 7.2 KB

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