Openid.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Models\User;
  3. use App\Exceptions\AlertException;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use App\Http\Controllers\Wechat\Fans;
  6. use Illuminate\Database\Eloquent\Model;
  7. /**
  8. * Class Openid
  9. *
  10. * @package App\Models\User
  11. * @method static Builder ofPublic(int $to_uid, string $public_id = "gh_b598cb7474d8")
  12. * @property int $id 主键
  13. * @property int $uid 用户ID
  14. * @property string $public_id 公众号原始ID
  15. * @property string $openid 用户的标识,对当前公众号唯一
  16. * @property int $subscribe 是否订阅该公众号
  17. * @property string $nickname 用户的昵称
  18. * @property int $sex 用户的性别
  19. * @property string $city 用户所在城市
  20. * @property string $country 用户所在国家
  21. * @property string $province 用户所在省份
  22. * @property string $language 用户的语言
  23. * @property string $headimgurl 用户头像
  24. * @property string $remark 公众号运营者对粉丝的备注
  25. * @property int $groupid 用户所在的分组ID
  26. * @property string $tagid_list 用户被打上的标签ID列表
  27. * @property string|null $unionid 微信开放平台帐号
  28. * @property int $subscribe_time 用户关注时间
  29. * @property int $interaction_time 用户最后交互时间
  30. * @property int $logintime 最近登录时间
  31. * @property string $subscribe_scene 用户关注的渠道来源
  32. * @property string $qr_scene 二维码扫码场景
  33. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid newModelQuery()
  34. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid newQuery()
  35. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid query()
  36. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereCity($value)
  37. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereCountry($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereGroupid($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereHeadimgurl($value)
  40. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereId($value)
  41. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereInteractionTime($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereLanguage($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereLogintime($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereNickname($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereOpenid($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereProvince($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid wherePublicId($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereQrScene($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereRemark($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereSex($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereSubscribe($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereSubscribeScene($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereSubscribeTime($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereTagidList($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereUid($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User\Openid whereUnionid($value)
  57. * @mixin \Eloquent
  58. */
  59. class Openid extends Model
  60. {
  61. protected $table = 'kddx_user_openid';
  62. public $timestamps = false;
  63. protected $fillable = ['uid', 'openid', 'public_id', 'subscribe'];
  64. /**
  65. * 获取用户uid
  66. * @param string $openid
  67. * @return array
  68. * @throws AlertException
  69. */
  70. public function getUidByopenid(string $openid)
  71. {
  72. $openinfo = $this->where('openid', $openid)->firstOrFail();
  73. $auth = AuthKey::where('auth_key', $openinfo->unionid)->firstOrFail();
  74. return array(
  75. 'uid' => $auth->uid,
  76. 'openid' => $openid,
  77. 'public_id' => $openinfo->public_id,
  78. 'interaction_time' => time()
  79. );
  80. }
  81. /**
  82. * 获取用户openid
  83. * @param int $uid 用户id
  84. * @return array
  85. * @throws AlertException
  86. */
  87. public function getOpenidByuid($uid)
  88. {
  89. $public_id = config('wechat.fpdx.public_id');
  90. /** @var self|null $openidEloqument */
  91. $openidEloqument = $this->where([['uid', $uid], ['public_id', $public_id]])->first();
  92. if (collect($openidEloqument)->isEmpty() || empty($openidEloqument->openid)) {
  93. $auth = AuthKey::where([['uid', $uid], ['auth_type', "kdgx_unionid"]])->first();
  94. if (collect($auth)->isEmpty()) {
  95. throw new AlertException("该用户已被注销", 401);
  96. }
  97. $openidEloqument = $this->where([['unionid', $auth->auth_key], ['public_id', $public_id]])->first();
  98. if (collect($openidEloqument)->isEmpty()) {
  99. throw new AlertException("用户未绑定微信号", 404);
  100. } else {
  101. $openidEloqument->uid = $uid;
  102. $openidEloqument->save();
  103. }
  104. } else {
  105. return [
  106. 'uid' => $openidEloqument->uid,
  107. 'openid' => $openidEloqument->openid,
  108. 'public_id' => $openidEloqument->public_id,
  109. 'interaction_time' => $openidEloqument->interaction_time
  110. ];
  111. }
  112. }
  113. /**
  114. * 绑定用户
  115. * @param string $public_id
  116. * @param int $uid
  117. * @param string $openid
  118. */
  119. public function bindUser(string $public_id, $uid, string $openid)
  120. {
  121. $where['openid'] = $openid;
  122. $save = [
  123. 'openid' => $openid,
  124. 'uid' => $uid,
  125. 'public_id' => $public_id,
  126. 'subscribe' => 1
  127. ];
  128. $openidEloqument = $this->where($where)->get();
  129. if ($openidEloqument->isEmpty() || empty($openidEloqument->uid)) {
  130. $this->updateOrCreate($where, $save);
  131. }
  132. }
  133. /**
  134. * 筛选某公众号的用户信息
  135. * @param $query
  136. * @param $uid
  137. * @param string $public_id
  138. * @return mixed
  139. */
  140. public function scopeOfPublic(/** @var Builder $query */ $query, $uid, $public_id = 'gh_b598cb7474d8')
  141. {
  142. return $query->where(['uid' => $uid, 'public_id' => $public_id]);
  143. }
  144. /**
  145. * 判断是否关注公众号
  146. * @param int $uid
  147. * @param string $public_id
  148. * @return bool
  149. */
  150. public static function isSubscribe(int $uid, string $public_id): bool
  151. {
  152. try {
  153. $auth = AuthKey::where(array(['uid', $uid], ['auth_type', 'kdgx_unionid']))->firstOrFail();
  154. if (self::where([['unionid', $auth->auth_key], ['public_id', $public_id], ['subscribe', 1]])->exists()) {
  155. return true;
  156. }
  157. return false;
  158. } catch (\Exception $exception) {
  159. return false;
  160. }
  161. }
  162. }