WechatMessage.php 7.5 KB

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