diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb index 7800382ad..ab2ba49b4 100644 --- a/app/api/mobile/apis/users.rb +++ b/app/api/mobile/apis/users.rb @@ -12,7 +12,26 @@ module Mobile requires :password, type: String, desc: 'password' end post '/bindwx' do - + code = params[:code] || session[:wechat_code] + openid = get_openid_from_code(code) + + raise "无法获取到openid,请在微信中打开本页面" unless openid + raise "此微信号已绑定用户, 不能重复绑定" if user_binded?(openid) + + user, last_login_on = User.try_to_login(params[:username], params[:password]) + raise "用户名或密码错误,请重新输入" unless user + #补全用户信息 + + raise "此用户已经绑定过公众号, 请换一个帐户试试" if user.user_wechat + + UserWechat.create!( + openid: openid, + user: user + ) + ws = WechatService.new + ws.binding_succ_notice(user.id, "您已成功绑定Trustie平台", user.login, format_time(Time.now)) + present status: 0 + present message: '绑定成功' end desc "注册用户" diff --git a/public/javascripts/wechat/controllers/login.js b/public/javascripts/wechat/controllers/login.js index 011f11baa..e8efad177 100644 --- a/public/javascripts/wechat/controllers/login.js +++ b/public/javascripts/wechat/controllers/login.js @@ -22,7 +22,7 @@ app.controller('LoginController', ['$scope', '$http', '$location', '$routeParams console.log(apiUrl + "auth"); auth.getOpenId().then( - function(){ + function(openid){ return $http.post( config.rootUrl + "wxbind", {login: user.login, password: user.password, openid: openid}