WechatMessage.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/28
  5. */
  6. namespace app\admin\model\wechat;
  7. use app\admin\model\user\User;
  8. use think\facade\Cache;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. use app\admin\model\wechat\WechatUser as UserModel;
  12. /**
  13. * 微信用户行为记录 model
  14. * Class WechatMessage
  15. * @package app\admin\model\wechat
  16. */
  17. class WechatMessage extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'wechat_message';
  29. use ModelTrait;
  30. protected $insert = ['add_time'];
  31. /**
  32. * 微信用户操作的基本所有操作
  33. * @var array
  34. */
  35. public static $mold = array(
  36. 'event_subscribe'=>'关注微信号',
  37. 'event_unsubscribe'=>'取消关注微信号',
  38. 'event_scan'=>'扫码',
  39. 'event_templatesendjobfinish'=>'进入小程序',
  40. 'event_location'=>'获取位置',
  41. 'event_click'=>'点击微信菜单关键字',
  42. 'event_view'=>'点击微信菜单链接',
  43. 'event_view_miniprogram'=>'点击微信菜单进入小程序',
  44. 'text'=>'收到文本消息',
  45. 'image'=>'收到图片消息',
  46. 'video'=>'收到视频消息',
  47. 'voice'=>'收到声音消息',
  48. 'location'=>'收到位置消息',
  49. 'link'=>'收到链接消息',
  50. 'event_scan_subscribe'=>'扫码关注'
  51. );
  52. public static function setAddTimeAttr($value)
  53. {
  54. return time();
  55. }
  56. public static function setMessage($result,$openid,$type)
  57. {
  58. if(is_object($result) || is_array($result)) $result = json_encode($result);
  59. $add_time = time();
  60. $data = compact('result','openid','type','add_time');
  61. return self::create($data);
  62. }
  63. public static function setOnceMessage($result,$openid,$type,$unique,$cacheTime = 172800)
  64. {
  65. $cacheName = 'wechat_message_'.$type.'_'.$unique;
  66. if(Cache::has($cacheName)) return true;
  67. $res = self::setMessage($result,$openid,$type);
  68. if($res) Cache::set($cacheName,1,$cacheTime);
  69. return $res;
  70. }
  71. /**
  72. * 按钮事件
  73. * @param $Event
  74. * @return mixed
  75. */
  76. public static function tidyEvent($Event){
  77. $res = array(
  78. 'msg'=>$Event['EventKey'],
  79. );
  80. return $res;
  81. }
  82. /**
  83. * 取消关注事件扫码
  84. * @param $Event
  85. * @return mixed
  86. */
  87. public static function tidyNull(){
  88. $res = array(
  89. 'msg'=>'无',
  90. );
  91. return $res;
  92. }
  93. /**
  94. * 整理文本显示的数据
  95. * @param $text 收到的文本消息
  96. * return 返回收到的消息
  97. */
  98. public static function tidyText($text){
  99. $res = array(
  100. 'rep_id'=> '1',
  101. 'MsgId'=>$text['MsgId'],
  102. 'Content'=>$text['Content'],
  103. 'msg'=>$text['Content'],
  104. );
  105. return $res;
  106. }
  107. /**
  108. * 整理图片显示的数据
  109. * @param $image
  110. * @return mixed
  111. */
  112. public static function tidyImage($image){
  113. $res = array(
  114. 'rep_id'=> '2',
  115. 'MsgId'=>$image['MsgId'],
  116. 'PicUrl'=>$image['PicUrl'],
  117. 'MediaId'=>$image['MediaId'],
  118. 'msg'=>'媒体ID:'.$image['MediaId'],
  119. );
  120. return $res;
  121. }
  122. /**
  123. * 整理视屏显示的数据
  124. * @param $video
  125. * @return mixed
  126. */
  127. public static function tidyVideo($video){
  128. $res = array(
  129. 'rep_id'=> '3',
  130. 'MsgId'=>$video['MsgId'],
  131. 'MediaId'=>$video['MediaId'],
  132. 'msg'=>'媒体ID:'.$video['MediaId'],
  133. );
  134. return $res;
  135. }
  136. /**
  137. * 整理声音显示的数据
  138. * @param $voice
  139. * @return mixed
  140. */
  141. public static function tidyVoice($voice){
  142. $res = array(
  143. 'rep_id'=> '4',
  144. 'MsgId'=>$voice['MsgId'],
  145. 'MediaId'=>$voice['MediaId'],
  146. 'msg'=>'媒体ID:'.$voice['MediaId'],
  147. );
  148. return $res;
  149. }
  150. /**
  151. * 地理位置
  152. * @param $location
  153. * @return array
  154. */
  155. public static function tidyLocation($location){
  156. $res = array(
  157. 'rep_id'=> '5',
  158. 'MsgId'=>$location['MsgId'],
  159. 'Label'=>$location['Label'],
  160. 'msg'=>$location['Label'],
  161. );
  162. return $res;
  163. }
  164. /**
  165. * 获取用户扫码点击事件
  166. * @param array $where
  167. * @return array
  168. */
  169. public static function systemPage($where = array()){
  170. $model = new self;
  171. $model = $model->alias('m');
  172. if($where['nickname'] !== ''){
  173. $user = UserModel::where('nickname','LIKE',"%$where[nickname]%")->field('openid')->select();
  174. if(empty($user->toArray())) $model = $model->where('m.id',0);
  175. foreach ($user as $v){
  176. $model = $model->where('m.openid',$v['openid']);
  177. }
  178. }
  179. if($where['type'] !== '') $model = $model->where('m.type',$where['type']);
  180. if($where['data'] !== ''){
  181. list($startTime,$endTime) = explode(' - ',$where['data']);
  182. $model = $model->where('m.add_time','>',strtotime($startTime));
  183. $model = $model->where('m.add_time','<',strtotime($endTime));
  184. }
  185. $model = $model->field('u.nickname,m.*')->join('WechatUser u','u.openid=m.openid')->order('m.id desc');
  186. return self::page($model,function ($item){
  187. switch ($item['type']){
  188. case 'text': $item['result_arr'] = self::tidyText(json_decode($item['result'],true));break;
  189. case 'image': $item['result_arr'] = self::tidyImage(json_decode($item['result'],true));break;
  190. case 'video': $item['result_arr'] = self::tidyVideo(json_decode($item['result'],true));break;
  191. case 'voice': $item['result_arr'] = self::tidyVoice(json_decode($item['result'],true));break;
  192. case 'location': $item['result_arr'] = self::tidyLocation(json_decode($item['result'],true));break;
  193. case 'event_click': $item['result_arr'] = self::tidyEvent(json_decode($item['result'],true));break;
  194. case 'event_view': $item['result_arr'] = self::tidyEvent(json_decode($item['result'],true));break;
  195. case 'event_subscribe': $item['result_arr'] = self::tidyNull();break;
  196. case 'event_unsubscribe': $item['result_arr'] = self::tidyNull();break;
  197. case 'event_scan': $item['result_arr'] = self::tidyNull();break;
  198. default :$item['result_arr'] = ['msg'=>$item['type']];break;
  199. }
  200. $item['type_name'] = isset(self::$mold[$item['type']]) ? self::$mold[$item['type']] : '未知';
  201. },$where);
  202. }
  203. /*
  204. * 获取应为记录数据
  205. *
  206. */
  207. public static function getViweList($date,$class=[]){
  208. $model=new self();
  209. switch ($date){
  210. case null:case 'today':case 'week':case 'year':
  211. if($date==null) $date='month';
  212. $model=$model->whereTime('add_time',$date);
  213. break;
  214. case 'quarter':
  215. $time=User::getMonth('n');
  216. $model=$model->where('add_time','between', $time);
  217. break;
  218. default:
  219. list($startTime,$endTime)=explode('-',$date);
  220. $model = $model->where('add_time','>',strtotime($startTime));
  221. $model = $model->where('add_time','<',strtotime($endTime));
  222. break;
  223. }
  224. $list=$model->field(['type','count(*) as num','result'])->group('type')->limit(0,20)->select()->toArray();
  225. $viwe=[];
  226. foreach ($list as $key=>$item){
  227. $now_list['name']=isset(self::$mold[$item['type']]) ? self::$mold[$item['type']] : '未知';
  228. $now_list['value']=$item['num'];
  229. $now_list['class']=isset($class[$key])?$class[$key]:'';
  230. $viwe[]=$now_list;
  231. }
  232. return $viwe;
  233. }
  234. }