WechatUserDao.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. * 根据微信用户OpenID获取用户信息。
  39. *
  40. * 本函数旨在通过微信用户OpenID查询数据库,以获取对应的用户信息。
  41. * 这对于需要通过微信登录或授权的系统来说是一个关键功能,它允许系统识别和验证微信用户。
  42. *
  43. * @param string $openId 微信用户的OpenID,用于唯一标识一个微信用户。
  44. * @return object 返回包含用户信息的对象,如果找不到用户,则返回空。
  45. */
  46. public function openIdByWechatUser(string $openId)
  47. {
  48. // 使用WechatUser类的getDB方法获取数据库实例,并根据OpenID查询用户信息。
  49. return WechatUser::getDB()->where('openid', $openId)->find();
  50. }
  51. /**
  52. * 根据UnionID获取微信用户信息。
  53. *
  54. * 本函数通过查询数据库,使用给定的UnionID作为标识,检索对应的微信用户信息。
  55. * UnionID是微信公众号平台用于标识用户的一个唯一标识,不同公众号或小程序下的同一个人,会拥有相同的UnionID。
  56. * 这个方法主要用于在多公众号或小程序的场景下,将用户信息进行统一管理。
  57. *
  58. * @param string $unionId 微信用户的UnionID,用于唯一标识一个用户。
  59. * @return object 返回查询结果,包含该UnionID对应的微信用户信息。如果未找到,则返回空。
  60. */
  61. public function unionIdByWechatUser(string $unionId)
  62. {
  63. // 使用WechatUser类的getDB方法获取数据库连接,并构造查询语句,查询unionid为$unionId的用户信息。
  64. return WechatUser::getDB()->where('unionid', $unionId)->find();
  65. }
  66. /**
  67. * 根据OpenID获取微信用户ID
  68. *
  69. * 本函数旨在通过微信用户的OpenID从数据库中检索对应的微信用户ID。这在与微信平台交互时非常有用,
  70. * 因为OpenID是微信用户在公众号中的唯一标识,通过这个标识可以关联到具体的用户。
  71. *
  72. * @param string $openId 微信用户的OpenID
  73. * @return int 微信用户的ID
  74. */
  75. public function openIdById(string $openId)
  76. {
  77. // 使用查询构建器查询数据库,根据OpenID获取wechat_user_id字段的值
  78. return WechatUser::getDB()->where('openid', $openId)->value('wechat_user_id');
  79. }
  80. /**
  81. * 根据微信用户的OpenID获取小程序用户的ID。
  82. *
  83. * 本函数旨在通过微信用户的小程序OpenID来查询并返回该用户在小程序中的ID。
  84. * 这对于需要对接微信小程序并根据用户的微信信息进行操作的场景非常有用。
  85. *
  86. * @param string $openId 微信用户的OpenID,这是用户在小程序中的唯一标识。
  87. * @return array 返回包含小程序用户ID等相关信息的数组。如果找不到对应用户,则返回空数组。
  88. */
  89. public function routineIdByWechatUser(string $openId)
  90. {
  91. // 使用WechatUser类的getDB方法获取数据库实例,并根据$openId查询用户信息。
  92. // 这里的where和find方法用于指定查询条件并执行查询操作。
  93. return WechatUser::getDB()->where('routine_openid', $openId)->find();
  94. }
  95. /**
  96. * 根据UnionID获取微信用户ID。
  97. *
  98. * 本函数旨在通过用户的UnionID,从数据库中检索并返回相应的微信用户ID。
  99. * UnionID是微信公众号平台为用户分配的唯一标识,不同公众号下同一用户的UnionID是一致的,这允许我们在多个公众号下识别同一用户。
  100. *
  101. * @param string $unionId 用户的UnionID。
  102. * @return string|false 返回微信用户ID,如果未找到则返回false。
  103. */
  104. public function unionIdById(string $unionId)
  105. {
  106. // 使用WechatUser模型的getDB方法获取数据库查询对象,然后通过where方法指定查询条件为'unionid'等于传入的$unionId,
  107. // 最后使用value方法仅获取'wechat_user_id'列的值。
  108. return WechatUser::getDB()->where('unionid', $unionId)->value('wechat_user_id');
  109. }
  110. /**
  111. * 根据用户ID获取微信用户的OpenID
  112. *
  113. * 本函数旨在通过内部用户ID,从微信用户表中检索对应的OpenID。OpenID是微信用户的一个唯一标识,
  114. * 用于在微信生态系统中识别用户。此函数返回查询结果中的OpenID字段值。
  115. *
  116. * @param int $id 用户ID,用于查询微信用户表。
  117. * @return string 查询到的微信用户的OpenID,如果未找到对应用户则返回空。
  118. */
  119. public function idByOpenId(int $id)
  120. {
  121. // 使用WechatUser类的getDB方法获取数据库实例,并根据wechat_user_id为$id$查询微信用户表中openid对应的值
  122. return WechatUser::getDB()->where('wechat_user_id', $id)->value('openid');
  123. }
  124. /**
  125. * 根据任务ID获取微信用户的routine_openid
  126. *
  127. * 本函数旨在通过任务ID(wechat_user_id)从数据库中检索特定微信用户的相关信息。
  128. * 具体来说,它查询并返回该用户的routine_openid,这是一个用于标识微信小程序用户的关键字段。
  129. *
  130. * @param int $id 任务ID,用于在数据库中定位特定的微信用户记录。
  131. * @return string 返回查询到的微信用户的routine_openid,如果未找到相关记录,则返回空。
  132. */
  133. public function idByRoutineId(int $id)
  134. {
  135. // 使用WechatUser类的getDB方法获取数据库对象,并通过where方法指定查询条件,最后使用value方法获取'routine_openid'字段的值
  136. return WechatUser::getDB()->where('wechat_user_id', $id)->value('routine_openid');
  137. }
  138. /**
  139. * 取消用户的订阅状态
  140. *
  141. * 本函数用于更新微信用户表中特定用户的订阅状态,将用户的订阅状态设置为0,即取消订阅。
  142. * 主要适用于微信公众号的用户管理,当用户选择取消订阅时,通过调用此函数来更新数据库中的用户状态。
  143. *
  144. * @param string $openId 用户的OpenID,是微信用户的一个唯一标识。
  145. * @return int 返回更新操作的影响行数,用于确认更新是否成功。
  146. */
  147. public function unsubscribe(string $openId)
  148. {
  149. // 通过OpenID查询微信用户表,并更新用户的订阅状态为0(取消订阅)
  150. return WechatUser::getDB()->where('openid', $openId)->update(['subscribe' => 0]);
  151. }
  152. /**
  153. * 检查微信用户是否订阅了公众号
  154. *
  155. * 本函数通过查询微信用户表,确定给定ID的用户是否订阅了公众号。
  156. * 它使用了多个条件来筛选用户:用户ID、OpenID不为空、订阅状态为1。
  157. * 如果满足这些条件的用户数量大于0,则认为该用户订阅了公众号。
  158. *
  159. * @param int $id 微信用户的ID
  160. * @return bool 如果用户已订阅公众号则返回true,否则返回false
  161. */
  162. public function isSubscribeWechatUser($id)
  163. {
  164. // 计算满足条件的用户数量,判断是否大于0
  165. return WechatUser::getDB()->where('wechat_user_id', $id)->whereNotNull('openid')->where('subscribe', 1)->count() > 0;
  166. }
  167. /**
  168. * 根据条件搜索微信用户信息。
  169. *
  170. * 该方法用于构建一个查询微信用户数据的条件,支持多种条件组合查询。查询结果根据微信用户ID降序排列。
  171. * 参数$where是一个数组,包含各种搜索条件。每个条件都是可选的,可以根据传入的条件动态构建SQL查询语句。
  172. *
  173. * @param array $where 搜索条件数组,包含各种可能的搜索条件如昵称、添加时间、标签ID列表、分组ID、性别和订阅状态。
  174. * @return \think\db\Query 返回构建好的查询对象,可以进一步操作如获取数据。
  175. */
  176. public function search(array $where)
  177. {
  178. // 初始化查询对象,从WechatUser类中获取数据库实例,并设置查询条件为openid和routine_openid不为空,按wechat_user_id降序排列
  179. $query = WechatUser::getDB()->whereNotNull('openid')->whereNotNull('routine_openid')->order('wechat_user_id desc');
  180. // 如果指定了昵称,则添加LIKE条件搜索昵称
  181. if (isset($where['nickname']) && $where['nickname']) $query->where('nickname', 'LIKE', "%$where[nickname]%");
  182. // 如果指定了添加时间,则处理并添加时间条件搜索
  183. if (isset($where['add_time']) && $where['add_time']) getModelTime($query, $where['add_time']);
  184. // 如果指定了标签ID列表,则遍历列表,为每个标签ID添加LIKE条件搜索
  185. if (isset($where['tagid_list']) && $where['tagid_list']) {
  186. $tagid_list = explode(',', $where['tagid_list']);
  187. foreach ($tagid_list as $v) {
  188. $query->where('tagid_list', 'LIKE', "%$v%");
  189. }
  190. }
  191. // 如果指定了分组ID,则添加等于条件搜索
  192. if (isset($where['groupid']) && $where['groupid']) $query->where('groupid', $where['groupid']);
  193. // 如果指定了性别,则添加等于条件搜索
  194. if (isset($where['sex']) && $where['sex']) $model = $query->where('sex', $where['sex']);
  195. // 如果指定了订阅状态,则添加等于条件搜索
  196. if (isset($where['subscribe']) && $where['subscribe']) $query->where('subscribe', $where['subscribe']);
  197. // 返回构建好的查询对象
  198. return $query;
  199. }
  200. }