WechatReply.php 8.9 KB

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