Module: Model::OauthUser::WechatUser
- Extended by:
- ActiveSupport::Concern
- Included in:
- Wechat::WechatUser
- Defined in:
- app/models/wechat/model/oauth_user/wechat_user.rb
Instance Method Summary collapse
- #assign_info(oauth_params) ⇒ Object
- #auto_join_organ ⇒ Object
- #auto_link ⇒ Object
- #name ⇒ Object
- #refresh_access_token ⇒ Object
- #sync_remark_later ⇒ Object
- #sync_remark_to_wechat ⇒ Object
- #sync_user_info ⇒ Object
- #sync_user_info_later ⇒ Object
- #try_match ⇒ Object
Instance Method Details
#assign_info(oauth_params) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 104 def assign_info(oauth_params) info_params = oauth_params.fetch('info', {}) self.name = info_params['nickname'] self.avatar_url = info_params['headimgurl'] raw_info = oauth_params.dig('extra', 'raw_info') || {} self.unionid = raw_info['unionid'] self.appid ||= raw_info['app_id'] credential_params = oauth_params.fetch('credentials', {}) self.access_token = credential_params['token'] self.refresh_token = credential_params['refresh_token'] self.expires_at = Time.current + credential_params['expires_in'].to_i end |
#auto_join_organ ⇒ Object
51 52 53 54 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 51 def auto_join_organ member = members.find_by(organ_id: member_inviter.organ_id) || members.build(organ_id: member_inviter.organ_id) member.save end |
#auto_link ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 42 def auto_link if self.unionid && self.same_oauth_user self.identity ||= same_oauth_user.identity self.name ||= same_oauth_user.name self.avatar_url ||= same_oauth_user.avatar_url end self.save end |
#name ⇒ Object
34 35 36 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 34 def name super.blank? ? "Wechat #{id}" : super end |
#refresh_access_token ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 89 def refresh_access_token params = { appid: appid, grant_type: 'refresh_token', refresh_token: refresh_token } response = HTTPX.get 'https://api.weixin.qq.com/sns/oauth2/refresh_token', params: params res = JSON.parse(response.to_s) self.assign_attributes res.slice('access_token', 'refresh_token') self.expires_at = Time.current + res['expires_in'].to_i self.save res end |
#sync_remark_later ⇒ Object
56 57 58 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 56 def sync_remark_later WechatUserJob.perform_later(self) end |
#sync_remark_to_wechat ⇒ Object
60 61 62 63 64 65 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 60 def sync_remark_to_wechat return unless app app.api.user_update_remark(uid, remark) rescue Wechat::WechatError => e logger.info e. end |
#sync_user_info ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 71 def sync_user_info params = { access_token: access_token, openid: uid } user_response = HTTPX.get('https://api.weixin.qq.com/sns/userinfo', params: params) res = JSON.parse(user_response.to_s) if res['errcode'].present? self.errors.add :base, "#{res['errcode']}, #{res['errmsg']}" end self.name = res['nickname'] self.avatar_url = res['headimgurl'] self.save res end |
#sync_user_info_later ⇒ Object
67 68 69 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 67 def sync_user_info_later UserInfoJob.perform_later(self) end |
#try_match ⇒ Object
38 39 40 |
# File 'app/models/wechat/model/oauth_user/wechat_user.rb', line 38 def try_match app.api.(uid) end |