WechatReply.php 6.8 KB

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