WechatMessage.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. {
  78. $res = array(
  79. 'msg' => $Event['EventKey'],
  80. );
  81. return $res;
  82. }
  83. /**
  84. * 取消关注事件扫码
  85. * @param $Event
  86. * @return mixed
  87. */
  88. public static function tidyNull()
  89. {
  90. $res = array(
  91. 'msg' => '无',
  92. );
  93. return $res;
  94. }
  95. /**
  96. * 整理文本显示的数据
  97. * @param $text 收到的文本消息
  98. * return 返回收到的消息
  99. */
  100. public static function tidyText($text)
  101. {
  102. $res = array(
  103. 'rep_id' => '1',
  104. 'MsgId' => $text['MsgId'],
  105. 'Content' => $text['Content'],
  106. 'msg' => $text['Content'],
  107. );
  108. return $res;
  109. }
  110. /**
  111. * 整理图片显示的数据
  112. * @param $image
  113. * @return mixed
  114. */
  115. public static function tidyImage($image)
  116. {
  117. $res = array(
  118. 'rep_id' => '2',
  119. 'MsgId' => $image['MsgId'],
  120. 'PicUrl' => $image['PicUrl'],
  121. 'MediaId' => $image['MediaId'],
  122. 'msg' => '媒体ID:' . $image['MediaId'],
  123. );
  124. return $res;
  125. }
  126. /**
  127. * 整理视屏显示的数据
  128. * @param $video
  129. * @return mixed
  130. */
  131. public static function tidyVideo($video)
  132. {
  133. $res = array(
  134. 'rep_id' => '3',
  135. 'MsgId' => $video['MsgId'],
  136. 'MediaId' => $video['MediaId'],
  137. 'msg' => '媒体ID:' . $video['MediaId'],
  138. );
  139. return $res;
  140. }
  141. /**
  142. * 整理声音显示的数据
  143. * @param $voice
  144. * @return mixed
  145. */
  146. public static function tidyVoice($voice)
  147. {
  148. $res = array(
  149. 'rep_id' => '4',
  150. 'MsgId' => $voice['MsgId'],
  151. 'MediaId' => $voice['MediaId'],
  152. 'msg' => '媒体ID:' . $voice['MediaId'],
  153. );
  154. return $res;
  155. }
  156. /**
  157. * 地理位置
  158. * @param $location
  159. * @return array
  160. */
  161. public static function tidyLocation($location)
  162. {
  163. $res = array(
  164. 'rep_id' => '5',
  165. 'MsgId' => $location['MsgId'],
  166. 'Label' => $location['Label'],
  167. 'msg' => $location['Label'],
  168. );
  169. return $res;
  170. }
  171. /**
  172. * 获取用户扫码点击事件
  173. * @param array $where
  174. * @return array
  175. */
  176. public static function systemPage($where = array())
  177. {
  178. $model = new self;
  179. $model = $model->alias('m');
  180. if ($where['nickname'] !== '') {
  181. $user = UserModel::where('nickname', 'LIKE', "%$where[nickname]%")->field('openid')->select();
  182. if (empty($user->toArray())) $model = $model->where('m.id', 0);
  183. foreach ($user as $v) {
  184. $model = $model->where('m.openid', $v['openid']);
  185. }
  186. }
  187. if ($where['type'] !== '') $model = $model->where('m.type', $where['type']);
  188. if ($where['data'] !== '') {
  189. list($startTime, $endTime) = explode(' - ', $where['data']);
  190. $model = $model->where('m.add_time', '>', strtotime($startTime));
  191. $model = $model->where('m.add_time', '<', strtotime($endTime));
  192. }
  193. $model = $model->field('u.nickname,m.*')->join('WechatUser u', 'u.openid=m.openid')->order('m.id desc');
  194. return self::page($model, function ($item) {
  195. switch ($item['type']) {
  196. case 'text':
  197. $item['result_arr'] = self::tidyText(json_decode($item['result'], true));
  198. break;
  199. case 'image':
  200. $item['result_arr'] = self::tidyImage(json_decode($item['result'], true));
  201. break;
  202. case 'video':
  203. $item['result_arr'] = self::tidyVideo(json_decode($item['result'], true));
  204. break;
  205. case 'voice':
  206. $item['result_arr'] = self::tidyVoice(json_decode($item['result'], true));
  207. break;
  208. case 'location':
  209. $item['result_arr'] = self::tidyLocation(json_decode($item['result'], true));
  210. break;
  211. case 'event_click':
  212. $item['result_arr'] = self::tidyEvent(json_decode($item['result'], true));
  213. break;
  214. case 'event_view':
  215. $item['result_arr'] = self::tidyEvent(json_decode($item['result'], true));
  216. break;
  217. case 'event_subscribe':
  218. $item['result_arr'] = self::tidyNull();
  219. break;
  220. case 'event_unsubscribe':
  221. $item['result_arr'] = self::tidyNull();
  222. break;
  223. case 'event_scan':
  224. $item['result_arr'] = self::tidyNull();
  225. break;
  226. default :
  227. $item['result_arr'] = ['msg' => $item['type']];
  228. break;
  229. }
  230. $item['type_name'] = isset(self::$mold[$item['type']]) ? self::$mold[$item['type']] : '未知';
  231. }, $where);
  232. }
  233. /*
  234. * 获取应为记录数据
  235. *
  236. */
  237. public static function getViweList($date, $class = [])
  238. {
  239. $model = new self();
  240. switch ($date) {
  241. case null:
  242. case 'today':
  243. case 'week':
  244. case 'year':
  245. if ($date == null) $date = 'month';
  246. $model = $model->whereTime('add_time', $date);
  247. break;
  248. case 'quarter':
  249. $time = User::getMonth('n');
  250. $model = $model->where('add_time', 'between', $time);
  251. break;
  252. default:
  253. list($startTime, $endTime) = explode('-', $date);
  254. $model = $model->where('add_time', '>', strtotime($startTime));
  255. $model = $model->where('add_time', '<', strtotime($endTime));
  256. break;
  257. }
  258. $list = $model->field(['type', 'count(*) as num', 'result'])->group('type')->limit(0, 20)->select()->toArray();
  259. $viwe = [];
  260. foreach ($list as $key => $item) {
  261. $now_list['name'] = isset(self::$mold[$item['type']]) ? self::$mold[$item['type']] : '未知';
  262. $now_list['value'] = $item['num'];
  263. $now_list['class'] = isset($class[$key]) ? $class[$key] : '';
  264. $viwe[] = $now_list;
  265. }
  266. return $viwe;
  267. }
  268. }