WechatReplyRepository.php 7.3 KB

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