思路一:
1. 在小程序端定义button
2. js中调获取破解用户手机号的接口(可以在接口里直接破解之后存储用户手机号)
2.1 破解手机号,返回给小程序端
2.2 将手机号存到数据库
//解密手机号码
public function doPagegetPhoneNumber()
{
global $_GPC, $_W;
require_once dirname(__FILE__) . '/mail/WXBizDataCrypt.php';
$uniacid = $_W['uniacid'];
$appid = $_GPC['appid'];
$sessionKey = $_GPC['session_key'];
$encryptedData = $_GPC['encryptedData'];
$openid = $_GPC['openid'];
$iv = $_GPC['iv'];
// echo json_encode($_GPC);exit();
$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
// echo $errCode;exit();
if ($errCode == 0) {
$d = json_decode($data,true); //将json字符串转化为数组
$arr['user_tel'] = $d['phoneNumber'];
//根据openid查询对应用户信息
if($openid){
//如果存在openid,则查询对应的数据
$info = pdo_fetchall("select id from " . tablename('yzpx_sun_user') . " where `uniacid`='$uniacid' and openid='$openid'");
if (empty($info)) {
//如果没有该openid对应的数据则插入该条数据
$arr['time'] = time();
$res = pdo_insert('yzpx_sun_user', $arr);
} else {
如果没有该openid对应的数据则更新该条数据
$arr['time'] = time();
$res = pdo_update("yzpx_sun_user", $arr, array('openid' => $openid));
}
}
print($data . "\n"); //将数据返回给小程序端
} else {
print($errCode . "\n");
}
}
(这种办法小程序端可以少掉一次存储用户信息的接口)
思路二:
1. 在小程序端定义button
2. js中调获取破解用户手机号的接口, 破解手机号,返回给小程序端
//解密手机号码
public function doPagegetPhoneNumber()
{
global $_GPC, $_W;
require_once dirname(__FILE__) . '/mail/WXBizDataCrypt.php';
$appid = $_GPC['appid'];
$sessionKey = $_GPC['session_key'];
$encryptedData = $_GPC['encryptedData'];
$iv = $_GPC['iv'];
// echo json_encode($_GPC);exit();
$pc = new WXBizDataCrypt($appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
// echo $errCode;exit();
if ($errCode == 0) {
print($data . "\n");
} else {
print($errCode . "\n");
}
}
3.小程序端将返回的手机号传回后端,调用存储用户信息的接口,存储手机号