WechatUserDao.php 4.9 KB

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