个人资料修改手机号

This commit is contained in:
xxq250 2022-11-24 11:09:58 +08:00
parent aeff21f7df
commit fd2b20ed87
5 changed files with 63 additions and 4 deletions

View File

@ -78,4 +78,13 @@ class Api::V1::UsersController < Api::V1::BaseController
return render_error('更改邮箱失败!') return render_error('更改邮箱失败!')
end end
end end
def update_phone
@result_object = Api::V1::Users::UpdatePhoneService.call(@observe_user, params)
if @result_object
return render_ok
else
return render_error('更改手机号失败!')
end
end
end end

View File

@ -105,7 +105,7 @@ class ApplicationController < ActionController::Base
sigle_para = {phone: value} sigle_para = {phone: value}
# status = Gitlink::Sms.send(mobile: value, code: code) # status = Gitlink::Sms.send(mobile: value, code: code)
# tip_exception(-2, code_msg(status)) if status != 0 # tip_exception(-2, code_msg(status)) if status != 0
status = Sms::UcloudService.call(value, code) status = Sms::UcloudService.call(value, code, send_type)
tip_exception(-2, ucloud_code_msg(status)) if status != 0 tip_exception(-2, ucloud_code_msg(status)) if status != 0
when 8, 3, 5 when 8, 3, 5
# 邮箱类型的发送 # 邮箱类型的发送

View File

@ -0,0 +1,35 @@
class Api::V1::Users::UpdatePhoneService < ApplicationService
include ActiveModel::Model
attr_reader :user, :password, :phone, :code, :verify_code
validates :password, :code, presence: true
validates :phone, presence: true, format: { with: CustomRegexp::PHONE }
def initialize(user, params)
@user = user
@password = params[:password]
@phone = params[:phone]
@code = params[:code]
@verify_code = VerificationCode.where(phone: @phone, code: @code, code_type: 4).last
end
def call
raise Error, errors.full_messages.join(",") unless valid?
raise Error, "密码不正确." unless @user.check_password?(@password)
exist_owner = Owner.find_by(phone: @phone)
raise Error, "手机号已被使用." if exist_owner
is_debug = @code == "123123" && EduSetting.get("code_debug") # 万能验证码,用于测试 # TODO 万能验证码,用于测试
raise Error, "验证码不正确." if @verify_code&.code != @code && !is_debug
raise Error, "验证码已失效." if !@verify_code&.effective? && !is_debug
begin
ActiveRecord::Base.transaction do
@user.update_attributes!({phone: @phone})
end
return true
rescue
raise Error, "服务器错误,请联系系统管理员!"
end
end
end

View File

@ -1,9 +1,10 @@
class Sms::UcloudService < ApplicationService class Sms::UcloudService < ApplicationService
attr_reader :phone, :code attr_reader :phone, :code, :send_type
def initialize(phone, code) def initialize(phone, code, send_type)
@phone = phone @phone = phone
@code = code @code = code
@send_type = send_type
end end
def call def call
@ -14,7 +15,7 @@ class Sms::UcloudService < ApplicationService
sign_params = { sign_params = {
"Action" => "SendUSMSMessage", "Action" => "SendUSMSMessage",
"ProjectId" => project_id, "ProjectId" => project_id,
"TemplateId" => "UTA221114S2MGTY", "TemplateId" => get_template_id(@send_type),
"PublicKey" => public_key, "PublicKey" => public_key,
"PhoneNumbers.0" => @phone, "PhoneNumbers.0" => @phone,
"TemplateParams.0" => "#{@code}", "TemplateParams.0" => "#{@code}",
@ -95,4 +96,17 @@ class Sms::UcloudService < ApplicationService
end end
end end
# 1注册手机验证码 2找回密码手机验证码 4绑定手机 9验证手机号有效
def get_template_id(send_type)
case send_type
when 1, 2, 9
"UTA221114S2MGTY"
when 4
"UTA22112486FXLZ"
else
"UTA221114S2MGTY"
end
end
end end

View File

@ -9,6 +9,7 @@ defaults format: :json do
post :check_email post :check_email
post :check_email_verify_code post :check_email_verify_code
patch :update_email patch :update_email
patch :update_phone
end end
end end
scope module: :users do scope module: :users do