WechatUserDao.php 4.4 KB

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