You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/weapps/code_sessions_controller.rb

26 lines
871 B

5 years ago
class Weapps::CodeSessionsController < Weapps::BaseController
def create
logged = false
5 years ago
return render_error('code不能为空') if params[:code].blank?
result = Wechat::Weapp.jscode2session(params[:code])
# 已授权,绑定过账号
open_user = OpenUsers::Wechat.find_by(uid: result['unionid'])
5 years ago
if open_user.present? && open_user.user
successful_authentication(open_user.user)
5 years ago
set_session_unionid(result['unionid'])
logged = true
5 years ago
else
# 新用户
user_info = Wechat::Weapp.decrypt(result['session_key'], params[:encrypted_data], params[:iv])
set_session_unionid(user_info['unionId'])
end
5 years ago
set_session_openid(result['openid'])
set_weapp_session_key(result['session_key']) # weapp session_key写入缓存 后续解密需要
render_ok(openid: result['openid'], logged: logged)
5 years ago
end
end