WechatUser.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\wap\model\user;
  12. use basic\ModelBasic;
  13. use service\WechatService;
  14. use service\WechatSubscribe;
  15. use service\CacheService as Cache;
  16. use think\Session;
  17. use traits\ModelTrait;
  18. /**微信信息表
  19. * Class WechatUser
  20. * @package app\wap\model\user
  21. */
  22. class WechatUser extends ModelBasic
  23. {
  24. use ModelTrait;
  25. protected $insert = ['add_time'];
  26. public static function setAddTimeAttr($value)
  27. {
  28. return time();
  29. }
  30. /**
  31. * 添加一个新用户
  32. * @param array $wechatInfo
  33. * @return boolen
  34. * */
  35. public static function setNewUserInfo($wechatInfo)
  36. {
  37. if (self::be(['openid' => $wechatInfo['openid']])) {
  38. self::where(['openid' => $wechatInfo['openid']])->update(['uid' => $wechatInfo]);
  39. } else {
  40. self::set($wechatInfo);
  41. }
  42. }
  43. /**
  44. * .添加新用户
  45. * @param $openid
  46. * @return object
  47. */
  48. public static function setNewUser($openid)
  49. {
  50. $userInfo = WechatService::getUserInfo($openid);
  51. $userInfo['nickname'] = '';
  52. $userInfo['headimgurl'] = '/system/images/user_log.jpg';
  53. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  54. exception('请关注公众号!');
  55. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  56. self::beginTrans();
  57. $wechatUser = User::setWechatUser($userInfo);
  58. if (!$wechatUser) {
  59. self::rollbackTrans();
  60. exception('用户信息储存失败!');
  61. }
  62. $wechatUser = self::set($wechatUser);
  63. if (!$wechatUser) {
  64. self::rollbackTrans();
  65. exception('用户储存失败!');
  66. }
  67. self::commitTrans();
  68. return $wechatUser;
  69. }
  70. /**
  71. * 更新用户信息
  72. * @param $openid
  73. * @return bool
  74. */
  75. public static function updateUser($openid)
  76. {
  77. $userInfo = WechatService::getUserInfo($openid);
  78. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  79. return self::edit($userInfo, $openid, 'openid');
  80. }
  81. /**
  82. * 用户存在就更新 不存在就添加
  83. * @param $openid
  84. */
  85. public static function saveUser($openid)
  86. {
  87. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  88. }
  89. /**
  90. * 用户取消关注
  91. * @param $openid
  92. * @return bool
  93. */
  94. public static function unSubscribe($openid)
  95. {
  96. return self::edit(['subscribe' => 0], $openid, 'openid');
  97. }
  98. /**
  99. * 用uid获得openid
  100. * @param $uid
  101. * @return mixed
  102. */
  103. public static function uidToOpenid($uid, $update = false)
  104. {
  105. $cacheName = 'openid_' . $uid;
  106. $openid = Cache::get($cacheName);
  107. if ($openid && !$update) return $openid;
  108. $openid = self::where('uid', $uid)->value('openid');
  109. Cache::set($cacheName, $openid, 0);
  110. return $openid;
  111. }
  112. /**
  113. * 用uid获得Unionid
  114. * @param $uid
  115. * @return mixed
  116. */
  117. public static function uidToUnionid($uid, $update = false)
  118. {
  119. $cacheName = 'unionid_' . $uid;
  120. $unionid = Cache::get($cacheName);
  121. if ($unionid && !$update) return $unionid;
  122. $unionid = self::where('uid', $uid)->value('unionid');
  123. if (!$unionid) exception('对应的unionid不存在!');
  124. Cache::set($cacheName, $unionid, 0);
  125. return $unionid;
  126. }
  127. /**
  128. * 用openid获得uid
  129. * @param $uid
  130. * @return mixed
  131. */
  132. public static function openidToUid($openid, $update = false)
  133. {
  134. $cacheName = 'uid_' . $openid;
  135. $uid = Cache::get($cacheName);
  136. if ($uid && !$update) return $uid;
  137. $uid = self::where('openid', $openid)->value('uid');
  138. if (!$uid) exception('对应的uid不存在!');
  139. Cache::set($cacheName, $uid, 0);
  140. return $uid;
  141. }
  142. /**
  143. * 获取用户信息
  144. * @param $openid
  145. * @return array
  146. */
  147. public static function getWechatInfo($openid)
  148. {
  149. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  150. $wechatInfo = self::where('openid', $openid)->find();
  151. if (!$wechatInfo) {
  152. self::setNewUser($openid);
  153. $wechatInfo = self::where('openid', $openid)->find();
  154. }
  155. if (!$wechatInfo) exception('获取用户信息失败!');
  156. return $wechatInfo->toArray();
  157. }
  158. }