WechatUserDao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\dao\wechat;
  13. use app\dao\BaseDao;
  14. use app\model\wechat\WechatUser;
  15. /**
  16. *
  17. * Class UserWechatUserDao
  18. * @package app\dao\user
  19. */
  20. class WechatUserDao extends BaseDao
  21. {
  22. /**
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return WechatUser::class;
  28. }
  29. /**
  30. * 获取wechat_user表数据
  31. * @param array $where
  32. * @param string $field
  33. * @param string $order
  34. * @param int $page
  35. * @param int $limit
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getList(array $where, string $field = '*', string $order = 'id desc', int $page = 0, int $limit = 0)
  42. {
  43. return $this->getModel()->where($where)->field($field)->order($order)->when($page && $limit, function ($query) use ($page, $limit) {
  44. $query->page($page, $limit);
  45. })->select()->toArray();
  46. }
  47. /**
  48. * 获取微信用户统计数据
  49. * @param $time
  50. * @param $where
  51. * @param $timeType
  52. * @param $key
  53. * @return mixed
  54. */
  55. public function getWechatTrendData($time, $where, $timeType, $key)
  56. {
  57. return $this->getModel()->where('user_type', 'wechat')
  58. ->where($where)
  59. ->where(function ($query) use ($time) {
  60. if ($time[0] == $time[1]) {
  61. $query->whereDay('add_time', $time[0]);
  62. } else {
  63. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  64. $query->whereTime('add_time', 'between', $time);
  65. }
  66. })->field("FROM_UNIXTIME(add_time,'$timeType') as days,count(uid) as $key")
  67. ->group('days')->select()->toArray();
  68. }
  69. /**
  70. * 地域全部用户
  71. * @param $time
  72. * @param $userType
  73. * @return mixed
  74. */
  75. public function getRegionAll($time, $userType)
  76. {
  77. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  78. $query->where('user_type', $userType);
  79. })->where(function ($query) use ($time) {
  80. $query->whereTime('add_time', '<', strtotime($time[1]) + 86400)->whereOr('add_time', NULL);
  81. })->field('count(uid) as allNum,province')
  82. ->group('province')->select()->toArray();
  83. }
  84. /**
  85. * 地域新增用户
  86. * @param $time
  87. * @param $userType
  88. * @return mixed
  89. */
  90. public function getRegionNew($time, $userType)
  91. {
  92. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  93. $query->where('user_type', $userType);
  94. })->where(function ($query) use ($time) {
  95. if ($time[0] == $time[1]) {
  96. $query->whereDay('add_time', $time[0]);
  97. } else {
  98. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  99. $query->whereTime('add_time', 'between', $time);
  100. }
  101. })->field('count(uid) as newNum,province')
  102. ->group('province')->select()->toArray();
  103. }
  104. /**
  105. * 获取用户性别
  106. * @param $time
  107. * @param $userType
  108. * @return mixed
  109. */
  110. public function getSex($time, $userType)
  111. {
  112. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  113. $query->where('user_type', $userType);
  114. })->where(function ($query) use ($time) {
  115. if ($time[0] == $time[1]) {
  116. $query->whereDay('add_time', $time[0]);
  117. } else {
  118. $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  119. $query->whereTime('add_time', 'between', $time);
  120. }
  121. })->field('count(uid) as value,sex as name')
  122. ->group('sex')->select()->toArray();
  123. }
  124. /**
  125. * 获取公众号或者小程序的openid
  126. * @param int $uid
  127. * @return mixed
  128. */
  129. public function getWechatOpenid(int $uid, string $userType = 'wechat')
  130. {
  131. return $this->getModel()->where('uid', $uid)->where('user_type', $userType)->value('openid');
  132. }
  133. }