WechatReply.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace app\admin\model\wechat;
  3. use liuniu\WechatService;
  4. use liuniu\BaseModel;
  5. use think\Request;
  6. class WechatReply extends BaseModel
  7. {
  8. public static $reply_type = ['text', 'image', 'news', 'voice'];
  9. /**
  10. * 根据关键字查询一条
  11. *
  12. * @param $key
  13. * @return array|null|\think\Model
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public static function getDataByKey($key)
  19. {
  20. $resdata = ['data' => ''];
  21. $resdata = self::where('key', $key)->find();
  22. $resdata['data'] = json_decode($resdata['data'], true);
  23. return $resdata;
  24. }
  25. public function getUrlAttr($value, $data)
  26. {
  27. return $value == '' ? url('index/index/news', ['id' => $data['id']]) : $value;
  28. }
  29. /**
  30. * @param $data
  31. * @param $key
  32. * @param $type
  33. * @param int $status
  34. * @return bool
  35. */
  36. public static function redact($data, $key, $type, $status = 1)
  37. {
  38. $method = 'tidy' . ucfirst($type);
  39. $res = self::$method($data, $key);
  40. if (!$res) return false;
  41. $count = self::where('key', $key)->count();
  42. if ($count) {
  43. $res = self::edit(['type' => $type, 'data' => json_encode($res), 'status' => $status], $key, 'key');
  44. if (!$res) return self::setErrorInfo('保存失败!');
  45. } else {
  46. $res = self::create([
  47. 'key' => $key,
  48. 'type' => $type,
  49. 'data' => json_encode($res),
  50. 'status' => $status,
  51. ]);
  52. if (!$res) return self::setErrorInfo('保存失败!');
  53. }
  54. return true;
  55. }
  56. /**
  57. * @param $key
  58. * @param string $field
  59. * @param int $hide
  60. * @return bool
  61. */
  62. public static function changeHide($key, $field = 'id', $hide = 0)
  63. {
  64. return self::edit(compact('hide'), $key, $field);
  65. }
  66. /**
  67. * 整理文本输入的消息
  68. * @param $data
  69. * @param $key
  70. * @return array|bool
  71. */
  72. public static function tidyText($data, $key)
  73. {
  74. $res = [];
  75. if (!isset($data['content']) || $data['content'] == '')
  76. return self::setErrorInfo('请输入回复信息内容');
  77. $res['content'] = $data['content'];
  78. return $res;
  79. }
  80. /**
  81. * 整理图片资源
  82. * @param $data
  83. * @param $key
  84. * @return array|bool|mixed
  85. */
  86. public static function tidyImage($data, $key)
  87. {
  88. if (!isset($data['src']) || $data['src'] == '')
  89. return self::setErrorInfo('请上传回复的图片');
  90. $reply = self::get(['key' => $key]);
  91. if ($reply) $reply['data'] = json_decode($reply['data'], true);
  92. if ($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']) {
  93. $res = $reply['data'];
  94. } else {
  95. $res = [];
  96. //TODO 图片转media
  97. $res['src'] = $data['src'];
  98. $material = (WechatService::materialService()->uploadImage(url_to_path($data['src'])));
  99. $res['media_id'] = $material->media_id;
  100. $dataEvent = ['media_id' => $material->media_id, 'path' => $res['src'], 'url' => $material->url];
  101. $type = 'image';
  102. event('WechatMaterialAfter', [$dataEvent, $type]);
  103. }
  104. return $res;
  105. }
  106. /**
  107. * 整理声音资源
  108. * @param $data
  109. * @param $key
  110. * @return array|bool|mixed
  111. */
  112. public static function tidyVoice($data, $key)
  113. {
  114. if (!isset($data['src']) || $data['src'] == '')
  115. return self::setErrorInfo('请上传回复的声音');
  116. $reply = self::get(['key' => $key]);
  117. if ($reply) $reply['data'] = json_decode($reply['data'], true);
  118. if ($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']) {
  119. $res = $reply['data'];
  120. } else {
  121. $res = [];
  122. //TODO 声音转media
  123. $res['src'] = $data['src'];
  124. $material = (WechatService::materialService()->uploadVoice(url_to_path($data['src'])));
  125. $res['media_id'] = $material->media_id;
  126. $dataEvent = ['media_id' => $material->media_id, 'path' => $res['src']];
  127. $type = 'voice';
  128. event('WechatMaterialAfter', [$dataEvent, $type]);
  129. }
  130. return $res;
  131. }
  132. /**
  133. * 整理图文资源
  134. * @param $data
  135. * @param $key
  136. * @return bool
  137. */
  138. public static function tidyNews($data, $key = '')
  139. {
  140. if (!count($data))
  141. return self::setErrorInfo('请选择图文消息');
  142. $siteUrl = Request::instance()->domain();
  143. foreach ($data as $k => $v) {
  144. if (empty($v['url'])) $data[$k]['url'] = $siteUrl . '/news_detail/' . $v['id'];
  145. if ($v['image']) $data[$k]['image'] = $v['image'];
  146. }
  147. return $data;
  148. }
  149. /**
  150. * 获取所有关键字
  151. * @param array $where
  152. * @return array
  153. */
  154. public static function getKeyAll($where = array())
  155. {
  156. $model = new self;
  157. if ($where['key'] !== '') $model = $model->where('key', 'LIKE', "%$where[key]%");
  158. if ($where['type'] !== '') $model = $model->where('type', $where['type']);
  159. $model = $model->where('key', '<>', 'subscribe');
  160. $model = $model->where('key', '<>', 'default');
  161. return self::page($model);
  162. }
  163. /**
  164. * 获取关键字
  165. * @param $key
  166. * @param string $default
  167. * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Voice
  168. */
  169. public static function reply($key, $default = '')
  170. {
  171. $res = self::where('key', $key)->where('status', '1')->find();
  172. if (empty($res)) $res = self::where('key', 'default')->where('status', '1')->find();
  173. if (empty($res)) return WechatService::transfer();
  174. $res['data'] = json_decode($res['data'], true);
  175. if ($res['type'] == 'text') {
  176. return WechatService::textMessage($res['data']['content']);
  177. } else if ($res['type'] == 'image') {
  178. return WechatService::imageMessage($res['data']['media_id']);
  179. } else if ($res['type'] == 'news') {
  180. return WechatService::newsMessage($res['data']);
  181. } else if ($res['type'] == 'voice') {
  182. return WechatService::voiceMessage($res['data']['media_id']);
  183. }
  184. }
  185. }