小程序随笔(关于香港手机号无法接收短信)
时隔一年,又回来了,这次决心把遇到的问题都记录下来
小程序问题:
香港手机号不能接收到短信
短信用的阿里的 有开通港澳台的限制
(小程序目前只支持大陆号码)
香港手机号为8为,内地为11位
待测方法:
输入的香港手机号前面加上区号,为11位,似乎不行。。。
这次采用的方法是,后端配合,多传一个电话类型phoneType给后端,大陆phoneType为0,香港phoneType为1,
具体实现:
样式上做修改,大陆为+86,香港为+852,之后输入手机号,传对应值
代码如下
wxml:
js:
data:[
....
currentArea:0,
clickArea:false,
currentPhoneType:0,
area:[{
name:'中国(+86)',
phoneType:0,
},{
name: '香港(+852)',
phoneType: 1,
}]
]
// 手机号地区选择
clickArea:function(){
that.setData({
clickArea:!that.data.clickArea
})
},
chooseArea:function(e){
let index = e.currentTarget.dataset.index
let phoneType = e.currentTarget.dataset.type
this.setData({
currentArea:index,
currentPhoneType: phoneType,
clickArea: false
})
},
getCode: function(){
var phoneNum = that.data.phoneNum;
if (!phoneNum || phoneNum.length < 8) {
utils.showFail("手机号不正确");
return;
}
var clicked = that.data.clicked;
if(clicked){ // 防止频繁获取验证码
return;
}
that.sendCode();
var time = 60;
that.setData({
clicked: true,
text: time + ' s'
})
interval = setInterval(function(){
that.setData({
text: --time + ' s'
})
if(time <= 0){
clearInterval(interval);
that.setData({
text: '获取验证码',
clicked: false
})
}
}, 1000);
},
sendCode: function(){
console.log("发送验证码");
var phoneNum = that.data.phoneNum;
let phoneType = that.data.currentPhoneType
sms.send(phoneNum,phoneType,function(){
console.log("发送成功");
});
},
sms.js:
import utils from "../utils/utils.js";
var app = getApp();
function send(phoneNum, phoneType,callback) {
utils.operating("请稍后...");
utils.post(app.globalData.rootSite + "/sms/sendCode?phoneType="+phoneType, {
phoneNum: phoneNum
}, function (res) {
if (res.success) {
callback();
wx.hideLoading();
}else{
utils.showFail(res.message);
}
})
}
function verifyCode(phoneNum, code, callback) {
utils.operating("请稍后...");
utils.post(app.globalData.rootSite + "/sms/verifyCode", {
phoneNum: phoneNum,
code: code
}, function (res) {
if (res.success) {
callback();
wx.hideLoading();
}else{
utils.showFail(res.message);
}
})
}
module.exports.verifyCode = verifyCode
module.exports.send = send
大功告成!!