WechatReply.php 6.7 KB

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