123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace app\admin\model\wechat;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use crmeb\services\WechatService;
- use think\facade\Route as Url;
- class WechatReply extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'wechat_reply';
- use ModelTrait;
- public static $reply_type = ['text', 'image', 'news', 'voice'];
-
- public static function getDataByKey($key)
- {
- $resdata = ['data' => ''];
- $resdata = self::where('key', $key)->find();
- $resdata['data'] = json_decode($resdata['data'], true);
- return $resdata;
- }
- public function getUrlAttr($value, $data)
- {
- return $value == '' ? Url::buildUrl('index/index/news', ['id' => $data['id']]) : $value;
- }
-
- public static function redact($data, $key, $type, $status = 1)
- {
- $method = 'tidy' . ucfirst($type);
- $res = self::$method($data, $key);
- if (!$res) return false;
- $count = self::where('key', $key)->count();
- if ($count) {
- $res = self::edit(['type' => $type, 'data' => json_encode($res), 'status' => $status], $key, 'key');
- if (!$res) return self::setErrorInfo('保存失败!');
- } else {
- $res = self::create([
- 'key' => $key,
- 'type' => $type,
- 'data' => json_encode($res),
- 'status' => $status,
- ]);
- if (!$res) return self::setErrorInfo('保存失败!');
- }
- return true;
- }
-
- public static function changeHide($key, $field = 'id', $hide = 0)
- {
- return self::edit(compact('hide'), $key, $field);
- }
-
- public static function tidyText($data, $key)
- {
- $res = [];
- if (!isset($data['content']) || $data['content'] == '')
- return self::setErrorInfo('请输入回复信息内容');
- $res['content'] = $data['content'];
- return $res;
- }
-
- public static function tidyImage($data, $key)
- {
- if (!isset($data['src']) || $data['src'] == '')
- return self::setErrorInfo('请上传回复的图片');
- $reply = self::get(['key' => $key]);
- if ($reply) $reply['data'] = json_decode($reply['data'], true);
- if ($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']) {
- $res = $reply['data'];
- } else {
- $res = [];
-
- $res['src'] = $data['src'];
- $material = (WechatService::materialService()->uploadImage(url_to_path($data['src'])));
- $res['media_id'] = $material->media_id;
- $dataEvent = ['media_id' => $material->media_id, 'path' => $res['src'], 'url' => $material->url];
- $type = 'image';
- event('WechatMaterialAfter', [$dataEvent, $type]);
- }
- return $res;
- }
-
- public static function tidyVoice($data, $key)
- {
- if (!isset($data['src']) || $data['src'] == '')
- return self::setErrorInfo('请上传回复的声音');
- $reply = self::get(['key' => $key]);
- if ($reply) $reply['data'] = json_decode($reply['data'], true);
- if ($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']) {
- $res = $reply['data'];
- } else {
- $res = [];
-
- $res['src'] = $data['src'];
- $material = (WechatService::materialService()->uploadVoice(url_to_path($data['src'])));
- $res['media_id'] = $material->media_id;
- $dataEvent = ['media_id' => $material->media_id, 'path' => $res['src']];
- $type = 'voice';
- event('WechatMaterialAfter', [$dataEvent, $type]);
- }
- return $res;
- }
-
- public static function tidyNews($data, $key = '')
- {
- if (!count($data))
- return self::setErrorInfo('请选择图文消息');
- $siteUrl = sys_config('site_url');
- foreach ($data as $k => $v) {
- if (empty($v['url'])) $data[$k]['url'] = $siteUrl . '/news_detail/' . $v['id'];
- if ($v['image']) $data[$k]['image'] = $v['image'];
- }
- return $data;
- }
-
- public static function getKeyAll($where = array())
- {
- $model = new self;
- if ($where['key'] !== '') $model = $model->where('key', 'LIKE', "%$where[key]%");
- if ($where['type'] !== '') $model = $model->where('type', $where['type']);
- $model = $model->where('key', '<>', 'subscribe');
- $model = $model->where('key', '<>', 'default');
- return self::page($model);
- }
-
- public static function reply($key, $default = '')
- {
- $res = self::where('key', $key)->where('status', '1')->find();
- if (empty($res)) $res = self::where('key', 'default')->where('status', '1')->find();
- if (empty($res)) return WechatService::transfer();
- $res['data'] = json_decode($res['data'], true);
- if ($res['type'] == 'text') {
- return WechatService::textMessage($res['data']['content']);
- } else if ($res['type'] == 'image') {
- return WechatService::imageMessage($res['data']['media_id']);
- } else if ($res['type'] == 'news') {
- return WechatService::newsMessage($res['data']);
- } else if ($res['type'] == 'voice') {
- return WechatService::voiceMessage($res['data']['media_id']);
- }
- }
- }
|