WechatMessage.php 7.4 KB

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