WechatMessage.php 7.9 KB

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