如果Ecshop實現了用手機號碼來登陸,那么就需要在注冊時保證會員所填寫的手機號是唯一的,也就是說手機號還未被注冊,那么該怎么來檢測填寫的手機號是否注冊過了呢?
一、參考ecshop檢測郵箱
因為注冊頁面,有檢查用戶名和郵箱是否重復的步驟,初步想法是參考檢測郵箱的方式來解決,但是查看user_passport.dwt,如下:

似乎可以像上面一樣開為手機號的input標簽中添加一個onblur事件,但是找了又找,並沒有發現手機號碼的input標簽在哪里,倒是發現了如下的代碼:

恍然大悟,因為默認的ecshop注冊登錄界面模板頁面上的手機號並不是必填的選項,而且可以在后台進行管理的,且這些選項在ecshop數據表esc_reg_fields表中,因此參考檢測email的方法失敗!
二、解決方案
通過查看頁面的代碼,用戶點擊注冊按鈕的時候,有一個return register();該方法在js/user.js文件中,故我們可以從此方法入手,在驗證完手機號的正則匹配后,進行手機號是否被注冊的驗證。
2.1 、修改user.js文件
在user.js文件中找到如下代碼:
if (mobile_phone.length>0)
{
var reg = /^[\d|\-|\s]+$/;
if (!reg.test(mobile_phone))
{
msg += mobile_phone_invalid + '\n';
}
}
將其替換為如下代碼:if (mobile_phone.length>0)
{
var reg = /(^1[3|5|8][0-9]{9}$)/;;
if (!reg.test(mobile_phone))
{
msg += mobile_phone_invalid + '\n';
}
else{
//該請求必須為同步請求,否側msg賦值失敗,注冊提交。
$.ajax({
type: 'GET',
url: 'user.php?act=check_mobile_phone',
data: {mobile_phone:mobile_phone},
async:false,
dataType: 'text',
success: function(data){
if (data == 'false')
{
msg += mobile_phone_invalid2+'\n';
}
else
{
}
}
});
}
}
2.2、在user.php中添加 check_mobile_phone的處理
在user.php中找到如下代碼:
/* 驗證用戶郵箱地址是否被注冊 */
elseif($action == 'check_email')
{
$email = trim($_GET['email']);
if ($user->check_email($email))
{
echo 'false';
}
else
{
echo 'ok';
}
}
復制一份,並添加在其下面,修改為如下:
/* 驗證用戶手機號是否被注冊 */
elseif($action == 'check_mobile_phone')
{
$mobile_phone = trim($_GET['mobile_phone']);
if ($user->check_mobile_phone($mobile_phone))//如果已經被注冊
{
echo 'false';
}
else
{
echo 'ok';
}
}
2.3、在integrate.php中添加 check_mobile_phone函數
在includes/modules/integrates/integrate.php中找到如下代碼:
function check_email($email)
{
if (!empty($email))
{
/* 檢查email是否重復 */
$sql = "SELECT " . $this->field_id .
" FROM " . $this->table($this->user_table).
" WHERE " . $this->field_email . " = '$email' ";
if ($this->db->getOne($sql, true) > 0)
{
$this->error = ERR_EMAIL_EXISTS;
return true;
}
return false;
}
}
復制一份,並添加在其下面,修改為如下:
function check_mobile_phone($mobile_phone)
{
if (!empty($mobile_phone))
{
/* 檢查手機號是否重復 */
$sql = "SELECT " . $this->field_id .
" FROM " . $this->table($this->user_table).
" WHERE mobile_phone= '$mobile_phone' ";
if ($this->db->getOne($sql, true) > 0)
{
return true;
}
return false;
}
}
至此,問題得以解決,此方案中並沒有使用ecshop自身封裝好的Ajax.call(...)方法,Ajax.call(...)方法其實用起來相當的方便,但是根據其回調函數的返回值才改變register()函數中的msg的值,這個我沒有方法可以做到,也算是一點小小的遺憾吧,這里提出來