Eric-Guo / wechat

API, command and message handling for WeChat in Rails
MIT License
1.31k stars 372 forks source link

wechat_api 接口对自定义小程序配置无法正确读取 #280

Closed z-programer closed 4 years ago

z-programer commented 4 years ago

如果使用 wechat_api 调用小程序相关接口,无法读取自定义的type类型。 例如: 如果在wechat.yml 中的 default 配置下配置 type 为 mp,则 wechat 会被初始化为 MpApi。 如果在wechat.yml 中自定义 cust_miniapp 配置下配置 type 为 mp,则 wechat 会被初始为默认值。

Eric-Guo commented 4 years ago

default下不要配置type的情况下自定义配置下的type才会起效

z-programer commented 4 years ago

我这边 default 没有配置 type 的情况下,自定义下的 type 仍然读取不到。 这是我这边配置文件

default: &default
  corpid: "corpid"
  corpsecret: "corpsecret"
  agentid:  1
  appid: "appid"
  secret: "secret"
  token:    "my_token"
  access_token: "C:/Users/[username]/wechat_access_token"
  encrypt_mode: false # if true must fill encoding_aes_key
  encoding_aes_key:  "my_encoding_aes_key"
  jsapi_ticket: "C:/Users/[user_name]/wechat_jsapi_ticket"

cust_miniapp_development:
  appid: "appid"
  secret: "secrets"
  type: 'mp'

在 controller 里的简单调用

  class WechatAppController < ActionController::API

    wechat_api(account: :cust_miniapp)

    def code_to_session
      res = wechat.jscode2session(params[:code])
      render json: res
    rescue StandardError => e
      render json: { error: e.message}, status: :internal_server_error
    end

  end

返回的错误信息就是

{
    "error": "undefined method `jscode2session' for #<Wechat::Api:0x00007fcf24b1faf8>"
}
Eric-Guo commented 4 years ago

你需要写成

default: &default
  corpid: "corpid"
  corpsecret: "corpsecret"
  agentid:  1
  appid: "appid"
  secret: "secret"
  token:    "my_token"
  access_token: "C:/Users/[username]/wechat_access_token"
  encrypt_mode: false # if true must fill encoding_aes_key
  encoding_aes_key:  "my_encoding_aes_key"
  jsapi_ticket: "C:/Users/[user_name]/wechat_jsapi_ticket"

development:
  <<: *default
  trusted_domain_fullname: "https://ericguo.ggrok.com"

cust_miniapp_development:
  appid: "appid"
  secret: "secrets"
  type: 'mp'

因为这里的default只是挂着,你需要真的到development里面引用它。

z-programer commented 4 years ago

好的,

z-programer commented 4 years ago

我后来在代码里用 Wechat.api 调用来完成的。 controller 里面使用代码如下:

::Wechat.api('cust_miniapp').jscode2session(code)

我看了下 gem 源码 wechat_api 初始化调用的是

ApiLoader.config

Wechat.api('cust_miniapp') 调用的是

ApiLoader.with

对 type 的处理的部分只有在 ApiLoader.with 里面有,是不是需要两边都要处理下?

Eric-Guo commented 4 years ago

以前有问题的调用是怎么调用的?

Eric-Guo commented 4 years ago

我知道了,load_controller_wechat这里的确没有处理MpApi,你有兴趣提个PR么?

Eric-Guo commented 4 years ago

Release v0.11.11, which contain #280 fix, thanks!