WechatMessage.php 8.5 KB

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