customer_login
生成 客户登录 表单。
表单输入
| 输入项 | 输入类型 | 字段名 | 是否必填 | 备注 |
|---|---|---|---|---|
| 邮箱 | customer[email] | 商家开启邮箱注册时必填 | ||
| 手机号 | phone | customer[phone] | 商家开启手机号注册时必填 | 手机号格式为: 国际封闭电话号码, 例如大陆手机号码:15600000000 |
| 区号 | text | customer[code] | 商家开启手机号注册时必填 | 区号为国际电话区号,例如大陆手机区号:86 |
| 密码 | password | customer[password] | 必填 |
例子
客户登录
{{#form 'customer_login' id="customer-login-form"}}
<div class="account">
<label for="email">{{ t 'user.username' }}</label>
<input type="text" name="customer[email]" placeholder="{{ t 'user.username' }}" />
</div>
<div class="password">
<label for="password">{{ t 'user.password' }}</label>
<input type="password" name="customer[password]" placeholder="{{ t 'user.password' }}" />
</div>
<div class="submit">
<input class="submit-button" type="submit" value="{{ t 'user.signIn' }}">
</div>
{{/form}}
配合使用
以上代码需要结合下面的脚本一起使用
class Login {
constructor() {
this.form = document.querySelector('#customer-login-form');
this.form.addEventListener('submit', this.onSubmitHandler.bind(this));
this.login = new window.Shopline.customerAccount.Login(this.form)
}
// 登录逻辑
onSubmitHandler(e) {
e.preventDefault();
this.login.submit()
.then(response => {
window.location.href = '/user/center';
})
.catch(error => {
// 处理登录失败异常场景
})
}
}
// 初始化用户登录 JS SDK
window.Shopline.loadFeatures(
[
{
name: 'customer-account-api',
version: '0.3'
}
],
function(error) {
if (error) {
throw error;
}
new Login();
}
);
第三方登录
{{#form 'customer_login' id="customer-login-form"}}
<div class="account">
<label for="email">{{ t 'user.username' }}</label>
<input type="text" name="customer[email]" placeholder="{{ t 'user.username' }}" />
</div>
<div class="password">
<label for="password">{{ t 'user.password' }}</label>
<input type="password" name="customer[password]" placeholder="{{ t 'user.password' }}" />
</div>
<div class="submit">
<input class="submit-button" type="submit" value="{{ t 'user.signIn' }}">
</div>
<div id="third-login-container"></div>
{{/form}}
配合使用
以上代码需要结合下面的脚本一起使用
class Login {
constructor() {
this.form = document.querySelector('#customer-login-form');
this.form.addEventListener('submit', this.onSubmitHandler.bind(this));
this.login = new window.Shopline.customerAccount.Login(this.form, {
thirdLogin: {
container: this.form.querySelector('#third-login-container'), // 第三方登录按钮容器
handleSuccess: () => { // 第三方登录成功,可以自定义跳转地址
window.location.href = '/user/center';
},
handleError: (error) => { // 第三方登录失败,返回错误信息
console.log(error)
}
}
})
}
// 登录逻辑
onSubmitHandler(e) {
e.preventDefault();
this.login.submit()
.then(response => {
window.location.href = '/user/center';
})
.catch(error => {
// 处理登录失败异常场景
console.log(error)
})
}
}
// 初始化用户登录 JS SDK
window.Shopline.loadFeatures(
[
{
name: 'customer-account-api',
version: '0.3'
}
],
function(error) {
if (error) {
throw error;
}
new Login();
}
);
未注册客户在登录页激活 账号
Shopline Admin可配置支持未注册客户在登录页激活账号
{{#form 'customer_login' id="customer-login-form"}}
<div class="account">
<label for="email">{{ t 'user.username' }}</label>
<input type="text" name="customer[acct]" placeholder="{{ t 'user.username' }}" />
</div>
<div class="password">
<label for="password">{{ t 'user.password' }}</label>
<input type="password" name="customer[password]" placeholder="{{ t 'user.password' }}" />
</div>
{{#if form.register_config.activation_tag}}
<div class="verifycode" style="display: none;">
<input type="text" name="customer[verifycode]" placeholder="{{ t 'user.code' }}" />
<button class="verifycode-button" id="customer-login-activate-send-btn">{{ t 'user.send' }}</button>
</div>
{{/if}}
<div class="submit">
<input class="submit-button" type="submit" value="{{ t 'user.signIn' }}">
</div>
{{/form}}
配合使用
以上代码需要结合下面的脚本一起使用
class Login {
constructor() {
this.form = document.querySelector('#customer-login-form');
this.form.addEventListener('submit', this.onSubmitHandler.bind(this));
this.verifyCodeFormItem = this.form.querySelector('.verifycode');
this.submitBtn = this.form.querySelector('.submit-button');
this.verifyCodeButton = this.form.querySelector('.verifycode-button');
this.verifyCodeButton.addEventListener(
'click', this.onSendVerifyCodeHandler.bind(this)
);
this.login = new window.Shopline.customerAccount.Login(this.form, {
activate: {
verifyCodeBtn: 'customer-login-activate-send-btn'
}
})
}
// 发送邮箱或短信验证码逻辑
onSendVerifyCodeHandler(e) {
e.preventDefault();
if (this.verifyCodeButton.getAttribute('aria-disabled') === 'true') return;
this.verifyCodeButton.setAttribute('aria-disabled', true);
this.login.sendVerifyCode()
.then(response => {
if (response.errorMessage) {
this.verifyCodeError = true;
// 处理发送失败异常场景
}
})
.finally(() => {
if (!this.verifyCodeError) {
this.verifyCodeButton.removeAttribute('aria-disabled');
}
})
}
// 登录逻辑
onSubmitHandler(e) {
e.preventDefault();
this.login.submit()
.then(response => {
// window.location.href = '/user/center';
})
.catch(error => {
// 处理登录失败异常场景
console.log('----submit error', error);
if (error.code === 'needActivate') {
// 需要激活账号。显示验证码输入框,并按需修改文案
this.verifyCodeFormItem.style.display = 'block';
this.submitBtn.value = 'activate';
}
})
}
}
// 初始化用户登录 JS SDK
window.Shopline.loadFeatures(
[
{
name: 'customer-account-api',
version: '0.3'
}
],
function(error) {
if (error) {
throw error;
}
new Login();
}
);
这篇文章对你有帮助吗?
SHOPLINE