WechatUserDao.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\common\dao\wechat;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\wechat\WechatUser;
  6. use think\db\BaseQuery;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Model;
  11. /**
  12. * Class WechatUserDao
  13. * @package app\common\dao\wechat
  14. * @author xaboy
  15. * @day 2020-04-28
  16. */
  17. class WechatUserDao extends BaseDao
  18. {
  19. /**
  20. * @return BaseModel
  21. * @author xaboy
  22. * @day 2020-03-30
  23. */
  24. protected function getModel(): string
  25. {
  26. return WechatUser::class;
  27. }
  28. /**
  29. * @param string $openId
  30. * @return array|Model|null
  31. * @throws DataNotFoundException
  32. * @throws DbException
  33. * @throws ModelNotFoundException
  34. * @author xaboy
  35. * @day 2020-04-28
  36. */
  37. public function openIdByWechatUser(string $openId)
  38. {
  39. return WechatUser::getDB()->where('openid', $openId)->find();
  40. }
  41. /**
  42. * @param string $unionId
  43. * @return array|Model|null
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. * @author xaboy
  48. * @day 2020-04-28
  49. */
  50. public function unionIdByWechatUser(string $unionId)
  51. {
  52. return WechatUser::getDB()->where('unionid', $unionId)->find();
  53. }
  54. /**
  55. * @param string $openId
  56. * @return mixed
  57. * @author xaboy
  58. * @day 2020-04-28
  59. */
  60. public function openIdById(string $openId)
  61. {
  62. return WechatUser::getDB()->where('openid', $openId)->value('wechat_user_id');
  63. }
  64. /**
  65. * @param string $openId
  66. * @return array|Model|null
  67. * @throws DataNotFoundException
  68. * @throws DbException
  69. * @throws ModelNotFoundException
  70. * @author xaboy
  71. * @day 2020-04-28
  72. */
  73. public function routineIdByWechatUser(string $openId)
  74. {
  75. return WechatUser::getDB()->where('routine_openid', $openId)->find();
  76. }
  77. /**
  78. * @param string $unionId
  79. * @return mixed
  80. * @author xaboy
  81. * @day 2020-04-28
  82. */
  83. public function unionIdById(string $unionId)
  84. {
  85. return WechatUser::getDB()->where('unionid', $unionId)->value('wechat_user_id');
  86. }
  87. /**
  88. * @param $id
  89. * @return mixed
  90. * @author xaboy
  91. * @day 2020/5/30
  92. */
  93. public function idByOpenId(int $id)
  94. {
  95. return WechatUser::getDB()->where('wechat_user_id', $id)->value('openid');
  96. }
  97. /**
  98. * @param $id
  99. * @return mixed
  100. * @author xaboy
  101. * @day 2020/5/30
  102. */
  103. public function idByRoutineId(int $id)
  104. {
  105. return WechatUser::getDB()->where('wechat_user_id', $id)->value('routine_openid');
  106. }
  107. /**
  108. * @param string $openId
  109. * @return int
  110. * @throws DbException
  111. * @author xaboy
  112. * @day 2020-04-28
  113. */
  114. public function unsubscribe(string $openId)
  115. {
  116. return WechatUser::getDB()->where('openid', $openId)->update(['subscribe' => 0]);
  117. }
  118. /**
  119. * @param $id
  120. * @return bool
  121. * @author xaboy
  122. * @day 2020-05-11
  123. */
  124. public function isSubscribeWechatUser($id)
  125. {
  126. return WechatUser::getDB()->where('wechat_user_id', $id)->whereNotNull('openid')->where('subscribe', 1)->count() > 0;
  127. }
  128. /**
  129. * @param array $where
  130. * @return BaseQuery
  131. * @author xaboy
  132. * @day 2020-04-29
  133. */
  134. public function search(array $where)
  135. {
  136. $query = WechatUser::getDB()->whereNotNull('openid')->whereNotNull('routine_openid')->order('wechat_user_id desc');
  137. if (isset($where['nickname']) && $where['nickname']) $query->where('nickname', 'LIKE', "%$where[nickname]%");
  138. if (isset($where['add_time']) && $where['add_time']) getModelTime($query, $where['add_time']);
  139. if (isset($where['tagid_list']) && $where['tagid_list']) {
  140. $tagid_list = explode(',', $where['tagid_list']);
  141. foreach ($tagid_list as $v) {
  142. $query->where('tagid_list', 'LIKE', "%$v%");
  143. }
  144. }
  145. if (isset($where['groupid']) && $where['groupid']) $query->where('groupid', $where['groupid']);
  146. if (isset($where['sex']) && $where['sex']) $model = $query->where('sex', $where['sex']);
  147. if (isset($where['subscribe']) && $where['subscribe']) $query->where('subscribe', $where['subscribe']);
  148. return $query;
  149. }
  150. }