WechatMessage.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/28
  5. */
  6. namespace app\models\wechat;
  7. use app\models\user\User;
  8. use think\facade\Cache;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. use app\models\wechat\WechatUser as UserModel;
  12. /**
  13. * 微信用户行为记录 model
  14. * Class WechatMessage
  15. * @package app\models\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 merSet($mer_id)
  53. {
  54. return $mer_id ? self::where('mer_id', $mer_id) : new self;
  55. }
  56. public static function setAddTimeAttr($value)
  57. {
  58. return time();
  59. }
  60. public static function setMessage($result, $openid, $type)
  61. {
  62. if (is_object($result) || is_array($result)) $result = json_encode($result);
  63. $add_time = time();
  64. $data = compact('result', 'openid', 'type', 'add_time');
  65. return self::create($data);
  66. }
  67. public static function setOnceMessage($result, $openid, $type, $unique, $cacheTime = 172800)
  68. {
  69. $cacheName = 'wechat_message_' . $type . '_' . $unique;
  70. if (Cache::has($cacheName)) return true;
  71. $res = self::setMessage($result, $openid, $type);
  72. if ($res) Cache::set($cacheName, 1, $cacheTime);
  73. return $res;
  74. }
  75. /**
  76. * 按钮事件
  77. * @param $Event
  78. * @return mixed
  79. */
  80. public static function tidyEvent($Event)
  81. {
  82. $res = array(
  83. 'msg' => $Event['EventKey'],
  84. );
  85. return $res;
  86. }
  87. /**
  88. * 取消关注事件扫码
  89. * @param $Event
  90. * @return mixed
  91. */
  92. public static function tidyNull()
  93. {
  94. $res = array(
  95. 'msg' => '无',
  96. );
  97. return $res;
  98. }
  99. /**
  100. * 整理文本显示的数据
  101. * @param $text 收到的文本消息
  102. * return 返回收到的消息
  103. */
  104. public static function tidyText($text)
  105. {
  106. $res = array(
  107. 'rep_id' => '1',
  108. 'MsgId' => $text['MsgId'],
  109. 'Content' => $text['Content'],
  110. 'msg' => $text['Content'],
  111. );
  112. return $res;
  113. }
  114. /**
  115. * 整理图片显示的数据
  116. * @param $image
  117. * @return mixed
  118. */
  119. public static function tidyImage($image)
  120. {
  121. $res = array(
  122. 'rep_id' => '2',
  123. 'MsgId' => $image['MsgId'],
  124. 'PicUrl' => $image['PicUrl'],
  125. 'MediaId' => $image['MediaId'],
  126. 'msg' => '媒体ID:' . $image['MediaId'],
  127. );
  128. return $res;
  129. }
  130. /**
  131. * 整理视屏显示的数据
  132. * @param $video
  133. * @return mixed
  134. */
  135. public static function tidyVideo($video)
  136. {
  137. $res = array(
  138. 'rep_id' => '3',
  139. 'MsgId' => $video['MsgId'],
  140. 'MediaId' => $video['MediaId'],
  141. 'msg' => '媒体ID:' . $video['MediaId'],
  142. );
  143. return $res;
  144. }
  145. /**
  146. * 整理声音显示的数据
  147. * @param $voice
  148. * @return mixed
  149. */
  150. public static function tidyVoice($voice)
  151. {
  152. $res = array(
  153. 'rep_id' => '4',
  154. 'MsgId' => $voice['MsgId'],
  155. 'MediaId' => $voice['MediaId'],
  156. 'msg' => '媒体ID:' . $voice['MediaId'],
  157. );
  158. return $res;
  159. }
  160. /**
  161. * 地理位置
  162. * @param $location
  163. * @return array
  164. */
  165. public static function tidyLocation($location)
  166. {
  167. $res = array(
  168. 'rep_id' => '5',
  169. 'MsgId' => $location['MsgId'],
  170. 'Label' => $location['Label'],
  171. 'msg' => $location['Label'],
  172. );
  173. return $res;
  174. }
  175. /**
  176. * 获取用户扫码点击事件
  177. * @param array $where
  178. * @return array
  179. */
  180. public static function systemPage($where = array())
  181. {
  182. $model = new self;
  183. $model = $model->alias('m');
  184. if ($where['nickname'] !== '') {
  185. $user = UserModel::where('nickname', 'LIKE', "%$where[nickname]%")->field('openid')->select();
  186. if (empty($user->toArray())) $model = $model->where('m.id', 0);
  187. foreach ($user as $v) {
  188. $model = $model->where('m.openid', $v['openid']);
  189. }
  190. }
  191. if ($where['type'] !== '') $model = $model->where('m.type', $where['type']);
  192. $model = self::getModelTime($where, $model, "m.add_time");
  193. $model = $model->field('u.nickname,m.*')->join('WechatUser u', 'u.openid=m.openid')->order('m.id desc');
  194. $count = $model->count();
  195. $list = $model->page((int)$where['page'], (int)$where['limit'])
  196. ->select()
  197. ->each(function ($item) {
  198. switch ($item['type']) {
  199. case 'text':
  200. $item['result_arr'] = self::tidyText(json_decode($item['result'], true));
  201. break;
  202. case 'image':
  203. $item['result_arr'] = self::tidyImage(json_decode($item['result'], true));
  204. break;
  205. case 'video':
  206. $item['result_arr'] = self::tidyVideo(json_decode($item['result'], true));
  207. break;
  208. case 'voice':
  209. $item['result_arr'] = self::tidyVoice(json_decode($item['result'], true));
  210. break;
  211. case 'location':
  212. $item['result_arr'] = self::tidyLocation(json_decode($item['result'], true));
  213. break;
  214. case 'event_click':
  215. $item['result_arr'] = self::tidyEvent(json_decode($item['result'], true));
  216. break;
  217. case 'event_view':
  218. $item['result_arr'] = self::tidyEvent(json_decode($item['result'], true));
  219. break;
  220. case 'event_subscribe':
  221. $item['result_arr'] = self::tidyNull();
  222. break;
  223. case 'event_unsubscribe':
  224. $item['result_arr'] = self::tidyNull();
  225. break;
  226. case 'event_scan':
  227. $item['result_arr'] = self::tidyNull();
  228. break;
  229. default :
  230. $item['result_arr'] = ['msg' => $item['type']];
  231. break;
  232. }
  233. $item['type_name'] = isset(self::$mold[$item['type']]) ? self::$mold[$item['type']] : '未知';
  234. });
  235. return compact('count', 'list');
  236. }
  237. /*
  238. * 获取应为记录数据
  239. *
  240. */
  241. public static function getViweList($date, $class = [], $mer_id = '')
  242. {
  243. $model = new self();
  244. if ($mer_id) {
  245. $model = $model->merSet($mer_id);
  246. }
  247. switch ($date) {
  248. case null:
  249. case 'today':
  250. case 'week':
  251. case 'year':
  252. if ($date == null) $date = 'month';
  253. $model = $model->whereTime('add_time', $date);
  254. break;
  255. case 'quarter':
  256. $time = User::getMonth('n');
  257. $model = $model->where('add_time', 'between', $time);
  258. break;
  259. default:
  260. list($startTime, $endTime) = explode('-', $date);
  261. $model = $model->where('add_time', '>', strtotime($startTime));
  262. $model = $model->where('add_time', '<', strtotime($endTime));
  263. break;
  264. }
  265. $list = $model->field(['type', 'count(*) as num', 'result'])->group('type')->limit(0, 20)->select()->toArray();
  266. $viwe = [];
  267. foreach ($list as $key => $item) {
  268. $now_list['name'] = isset(self::$mold[$item['type']]) ? self::$mold[$item['type']] : '未知';
  269. $now_list['value'] = $item['num'];
  270. $now_list['class'] = isset($class[$key]) ? $class[$key] : '';
  271. $viwe[] = $now_list;
  272. }
  273. return $viwe;
  274. }
  275. }