WechatReply.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\wechat;
  12. use app\admin\model\system\SystemConfig;
  13. use traits\ModelTrait;
  14. use basic\ModelBasic;
  15. use service\HookService;
  16. use service\UtilService;
  17. use service\WechatService;
  18. use think\Url;
  19. /**
  20. * 关键字 model
  21. * Class WechatReply
  22. * @package app\admin\model\wechat
  23. */
  24. class WechatReply extends ModelBasic
  25. {
  26. use ModelTrait;
  27. public static $reply_type = ['text','image','news','voice'];
  28. public function getUrlAttr($value,$data)
  29. {
  30. return $value == '' ? \think\Url::build('index/index/news',['id'=>$data['id']]) : $value;
  31. }
  32. /**
  33. * @param $data
  34. * @param $key
  35. * @param $type
  36. * @param int $status
  37. * @return bool
  38. */
  39. public static function redact($data,$key,$type,$status = 1)
  40. {
  41. $method = 'tidy'.ucfirst($type);
  42. $res = self::$method($data,$key);
  43. if(!$res) return false;
  44. $count = self::where('key',$key)->count();
  45. if($count){
  46. $res = self::edit(['type'=>$type,'data'=>json_encode($res),'status'=>$status],$key,'key');
  47. if(!$res) return self::setErrorInfo('保存失败!');
  48. }else{
  49. $res = self::set([
  50. 'key'=>$key,
  51. 'type'=>$type,
  52. 'data'=>json_encode($res),
  53. 'status'=>$status,
  54. ]);
  55. if(!$res) return self::setErrorInfo('保存失败!');
  56. }
  57. return true;
  58. }
  59. /**
  60. * @param $key
  61. * @param string $field
  62. * @param int $hide
  63. * @return bool
  64. */
  65. public static function changeHide($key,$field='id',$hide = 0)
  66. {
  67. return self::edit(compact('hide'),$key,$field);
  68. }
  69. /**
  70. * 整理文本输入的消息
  71. * @param $data
  72. * @param $key
  73. * @return array|bool
  74. */
  75. public static function tidyText($data,$key)
  76. {
  77. $res = [];
  78. if(!isset($data['content']) || $data['content'] == '')
  79. return self::setErrorInfo('请输入回复信息内容');
  80. $res['content'] = $data['content'];
  81. return $res;
  82. }
  83. /**
  84. * 整理图片资源
  85. * @param $data
  86. * @param $key
  87. * @return array|bool|mixed
  88. */
  89. public static function tidyImage($data,$key)
  90. {
  91. if(!isset($data['src']) || $data['src'] == '')
  92. return self::setErrorInfo('请上传回复的图片');
  93. $reply = self::get(['key'=>$key]);
  94. if($reply) $reply['data'] = json_decode($reply['data'],true);
  95. if($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']){
  96. $res = $reply['data'];
  97. }else {
  98. $res = [];
  99. //TODO 图片转media
  100. $res['src'] = $data['src'];
  101. $material = (WechatService::materialService()->uploadImage(UtilService::urlToPath($data['src'])));
  102. $res['media_id'] = $material->media_id;
  103. HookService::afterListen('wechat_material',
  104. ['media_id' => $material->media_id, 'path' => $res['src'], 'url' => $material->url], 'image');
  105. }
  106. return $res;
  107. }
  108. /**
  109. * 整理声音资源
  110. * @param $data
  111. * @param $key
  112. * @return array|bool|mixed
  113. */
  114. public static function tidyVoice($data,$key)
  115. {
  116. if(!isset($data['src']) || $data['src'] == '')
  117. return self::setErrorInfo('请上传回复的声音');
  118. $reply = self::get(['key'=>$key]);
  119. if($reply) $reply['data'] = json_decode($reply['data'],true);
  120. if($reply && isset($reply['data']['src']) && $reply['data']['src'] == $data['src']){
  121. $res = $reply['data'];
  122. }else{
  123. $res = [];
  124. //TODO 声音转media
  125. $res['src'] = $data['src'];
  126. $material = (WechatService::materialService()->uploadVoice(UtilService::urlToPath($data['src'])));
  127. $res['media_id'] = $material->media_id;
  128. HookService::afterListen('wechat_material',['media_id'=>$material->media_id,'path'=>$res['src']],'voice');
  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 = SystemConfig::getValue('site_url');
  143. foreach ($data as $k=>$v){
  144. if(empty($v['url'])) $data[$k]['url'] = $siteUrl.Url::build('wap/article/details',['id'=>$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. $model = new self;
  156. if($where['key'] !== '') $model = $model->where('key','LIKE',"%$where[key]%");
  157. if($where['type'] !== '') $model = $model->where('type',$where['type']);
  158. $model = $model->where('key','<>','subscribe');
  159. $model = $model->where('key','<>','default');
  160. return self::page($model);
  161. }
  162. /**
  163. * 获取关键字
  164. * @param $key
  165. * @param string $default
  166. * @return array|\EasyWeChat\Message\Image|\EasyWeChat\Message\News|\EasyWeChat\Message\Text|\EasyWeChat\Message\Voice
  167. */
  168. public static function reply($key,$default=''){
  169. $res = self::where('key',$key)->where('status','1')->find();
  170. if(empty($res)) $res = self::where('key','default')->where('status','1')->find();
  171. if(empty($res)){
  172. die("success");
  173. return WechatService::textMessage($default);
  174. }
  175. $res['data'] = json_decode($res['data'],true);
  176. if($res['type'] == 'text'){
  177. return WechatService::textMessage($res['data']['content']);
  178. }else if($res['type'] == 'image'){
  179. return WechatService::imageMessage($res['data']['media_id']);
  180. }else if($res['type'] == 'news'){
  181. return WechatService::newsMessage($res['data']);
  182. }else if($res['type'] == 'voice'){
  183. return WechatService::voiceMessage($res['data']['media_id']);
  184. }
  185. }
  186. }