From f5e656be6111deb5dcb5fa3d434a12436747f599 Mon Sep 17 00:00:00 2001 From: leepood Date: Fri, 28 Jul 2023 15:46:43 +0800 Subject: [PATCH] Add new api & Compatible with WeCom 1. add `wxa_get_user_phone_number` 2. add `get_externalcontact` 3. `wechat_config_js` add `beta` option to make it compatible with WeCom --- lib/wechat/concern/common.rb | 5 +++++ lib/wechat/corp_api.rb | 5 +++++ lib/wechat/helpers.rb | 3 +++ spec/lib/wechat/api_spec.rb | 25 +++++++++++++++++++++++++ spec/lib/wechat/corp_api_spec.rb | 18 ++++++++++++++++++ 5 files changed, 56 insertions(+) diff --git a/lib/wechat/concern/common.rb b/lib/wechat/concern/common.rb index 59dd0cae..2f142d1c 100644 --- a/lib/wechat/concern/common.rb +++ b/lib/wechat/concern/common.rb @@ -88,6 +88,11 @@ def message_mass_get(msg_id) post 'message/mass/get', JSON.generate(msg_id: msg_id) end + def wxa_get_user_phone_number(code) + # https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html + post 'business/getuserphonenumber', JSON.generate(code: code), base: Wechat::Api::WXA_BASE + end + def wxa_get_wxacode(path, width = 430) post 'getwxacode', JSON.generate(path: path, width: width), base: Wechat::Api::WXA_BASE end diff --git a/lib/wechat/corp_api.rb b/lib/wechat/corp_api.rb index 95185821..de5e7e3b 100644 --- a/lib/wechat/corp_api.rb +++ b/lib/wechat/corp_api.rb @@ -13,6 +13,11 @@ def initialize(appid, secret, token_file, agentid, network_setting, jsapi_ticket @qcloud = nil end + def get_externalcontact(external_userid, cursor = nil) + # https://developer.work.weixin.qq.com/document/path/92114 + get 'externalcontact/get', params: { external_userid: external_userid, cursor: cursor } + end + def agent_list get 'agent/list' end diff --git a/lib/wechat/helpers.rb b/lib/wechat/helpers.rb index 3df7a752..2a048e93 100644 --- a/lib/wechat/helpers.rb +++ b/lib/wechat/helpers.rb @@ -27,8 +27,11 @@ def wechat_config_js(config_options = {}) page_url = page_url.split('#').first js_hash = api.jsapi_ticket.signature(page_url) + # Field `beta` please check https://developer.work.weixin.qq.com/document/path/90514#%E6%AD%A5%E9%AA%A4%E4%BA%8C%EF%BC%9A%E9%80%9A%E8%BF%87config%E6%8E%A5%E5%8F%A3%E6%B3%A8%E5%85%A5%E6%9D%83%E9%99%90%E9%AA%8C%E8%AF%81%E9%85%8D%E7%BD%AE + config_js = <<~WECHAT_CONFIG_JS wx.config({ + beta: #{config_options[:beta]}, debug: #{config_options[:debug]}, appId: "#{app_id}", timestamp: "#{js_hash[:timestamp]}", diff --git a/spec/lib/wechat/api_spec.rb b/spec/lib/wechat/api_spec.rb index 3a7d0143..3c5cacd0 100644 --- a/spec/lib/wechat/api_spec.rb +++ b/spec/lib/wechat/api_spec.rb @@ -939,4 +939,29 @@ expect(subject.translatecontent('xxxxxxxx')).to eq response_result end end + + describe '#wxa_get_user_phone_number' do + specify 'will post code with access_token to get user phone number' do + response_result = { + errcode: 0, + errmsg: 'ok', + phone_info: { + phoneNumber: '', + purePhoneNumber: '', + countryCode: '', + watermark: { + timestamp: 0, + appid: '' + } + } + } + expect(subject.client).to receive(:post) + .with('business/getuserphonenumber', JSON.generate(code: 'xxxxxxxx'), + hash_including(params: { access_token: 'access_token', }, base: Wechat::Api::WXA_BASE)) + .and_return(response_result) + expect(subject.wxa_get_user_phone_number('xxxxxxxx')).to eq response_result + end + + end + end diff --git a/spec/lib/wechat/corp_api_spec.rb b/spec/lib/wechat/corp_api_spec.rb index d414d904..8df17eb0 100644 --- a/spec/lib/wechat/corp_api_spec.rb +++ b/spec/lib/wechat/corp_api_spec.rb @@ -577,4 +577,22 @@ end end + describe '#get_externalcontact' do + specify 'use get method and params external_userid and cursor to externalcontact data' do + externalcontact_data = { + errcode: 0, + errmsg: 'ok', + external_contact: {} + } + + expect(subject.client).to receive(:get) + .with('externalcontact/get', + hash_including(params: { access_token: 'access_token', + external_userid: 'external_userid', + cursor: 'xxx', + })).and_return(externalcontact_data) + expect(subject.get_externalcontact('external_userid','xxx')).to eq externalcontact_data + end + end + end