//获取手机号
public function getPhoneNumber(Request $request)
{
$arr = User::where('id',1)->first();
$session_key = $arr['session_key'];
$appid = config('wx.AppID');
$sessionKey = $session_key;
$encryptedData = $request->post('encryptedData');
$iv = $request->post('iv');
$pc = new WxApp($appid,$sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
$data = json_decode($data,true);
if($errCode == 0){
$arr->update(['mobile'=>$data['phoneNumber']]);
return ['code'=>200,'msg'=>'成功','data'=>$data['phoneNumber']];
}else{
return ['code'=>10000,'msg'=>'失败','data'=>''];
}
}
public function sendSms(Request $request)
{
$mobile = $request->get('phone');
$userInfo = User::where('mobile',$mobile)->get()->toArray();
if (empty($userInfo)){
$res = (new SmsService())->sendSms($mobile);
$code = Cache::get($mobile);
return $code;
}else{
return ['status'=>500,'msg'=>'该手机号已被绑定','data'=>[]];
}
}
<?php
namespace App\Http\Controllers\service;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class SmsService extends Controller
{
public function sendSms($mobile)
{
$statusStr = array(
"0" => "短信发送成功",
"-1" => "参数不全",
"-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
"30" => "密码错误",
"40" => "账号不存在",
"41" => "余额不足",
"42" => "帐户已过期",
"43" => "IP地址限制",
"50" => "内容含有敏感词"
);
$smsapi = "http://api.smsbao.com/";
$user = "19913628375"; //短信平台帐号
$pass = md5("1314521@@++"); //短信平台密码
$code = rand(1000,9999);
$content = "您的验证码为".$code."该验证码五分钟内有效,请勿泄露给他人";//要发送的短信内容
$phone = $mobile;//要发送短信的手机号码
$sendurl = $smsapi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" . urlencode($content);
$result = file_get_contents($sendurl);
if ($result == 0){
Cache::add($mobile,$code,now()->addMinutes(5));
return $statusStr[$result];
}
return $statusStr[$result];
}
}
<?php
namespace App\Comment;
class ErrorCode
{
public static $OK = 0;
public static $IllegalAesKey = -41001;
public static $IllegalIv = -41002;
public static $IllegalBuffer = -41003;
public static $DecodeBase64Error = -41004;
}
<?php
namespace App\Comment;
include_once "ErrorCode.php";
class WxApp
{
private $appid;
private $sessionKey;
/**
* 构造函数
* @param $sessionKey string 用户在小程序登录后获取的会话密钥
* @param $appid string 小程序的appid
*/
public function __construct( $appid, $sessionKey)
{
$this->sessionKey = $sessionKey;
$this->appid = $appid;
}
/**
* 检验数据的真实性,并且获取解密后的明文.
* @param $encryptedData string 加密的用户数据
* @param $iv string 与用户数据一同返回的初始向量
* @param $data string 解密后的原文
*
* @return int 成功0,失败返回对应的错误码
*/
public function decryptData( $encryptedData, $iv, &$data )
{
if (strlen($this->sessionKey) != 24) {
return ErrorCode::$IllegalAesKey;
}
$aesKey=base64_decode($this->sessionKey);
if (strlen($iv) != 24) {
return ErrorCode::$IllegalIv;
}
$aesIV=base64_decode($iv);
$aesCipher=base64_decode($encryptedData);
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj=json_decode( $result );
if( $dataObj == NULL )
{
return ErrorCode::$IllegalBuffer;
}
if( $dataObj->watermark->appid != $this->appid )
{
return ErrorCode::$IllegalBuffer;
}
$data = $result;
return ErrorCode::$OK;
}
}
public function getPhone($phone,$code)
{
$smsapi = "http://api.smsbao.com/";
$user = "lzc7758521"; //短信平台帐号
$pass = md5("asd7758521"); //短信平台密码
$content="<测试短信内容>你的验证码为:".$code;//要发送的短信内容
//要发送短信的手机号码
$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
$result =file_get_contents($sendurl);
return $result;
}
|