日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

阿里云短信服务与微信小程序对接进行注册

發布時間:2023/12/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阿里云短信服务与微信小程序对接进行注册 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

一、首先需要阿里云官網開通短信服務,然后記錄下以下關鍵字段【accessKeyId】,【accessKeySecret】,【短信簽名】,【模板CODE】

二、進入官方開發指南下載SDKDemo

做好以上準備工作后就開始準備擼碼了

下載好的SDK包應該包含以下文件

  • api_demo(短信服務API接口調用DEMO工程)
  • api_sdk(短信服務API接口依賴的SDK)
  • msg_demo(短信回執消息的DEMO)
  • msg_sdk(短信回執消息的SDK)
  • 我們只需要前面兩個api開頭的文件就行了。后面的暫時不用,可以刪掉。

    先把最重要的api_sdk文件夾隨便放到一個服務器的路徑下(方便調用)

    然后我們就需要修改api_demo文件目錄下的SmsDemo.php文件了

    <?phpini_set("display_errors", "on"); //自動加載api_adk require_once $_SERVER['DOCUMENT_ROOT'].'/api_sdk/vendor/autoload.php';use Aliyun\Core\Config; use Aliyun\Core\Profile\DefaultProfile; use Aliyun\Core\DefaultAcsClient; use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest; use Aliyun\Api\Sms\Request\V20170525\SendBatchSmsRequest; use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;// 加載區域結點配置 Config::load();/*** Class SmsDemo** 這是短信服務API產品的DEMO程序,直接執行此文件即可體驗短信服務產品API功能* (只需要將AK替換成開通了云通信-短信服務產品功能的AK即可)* 備注:Demo工程編碼采用UTF-8*/ class SmsDemo {static $acsClient = null;/*** 取得AcsClient** @return DefaultAcsClient*/public static function getAcsClient() {//產品名稱:云通信短信服務API產品,開發者無需替換$product = "Dysmsapi";//產品域名,開發者無需替換$domain = "dysmsapi.aliyuncs.com";// TODO 此處需要替換成開發者自己的AK (https://ak-console.aliyun.com/)$accessKeyId = "xxxxxxxxxxxxx"; // AccessKeyId$accessKeySecret = "xxxxxxxxxx"; // AccessKeySecret// 暫時不支持多Region$region = "cn-hangzhou";// 服務結點$endPointName = "cn-hangzhou";if(static::$acsClient == null) {//初始化acsClient,暫不支持region化$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);// 增加服務結點DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);// 初始化AcsClient用于發起請求static::$acsClient = new DefaultAcsClient($profile);}return static::$acsClient;}/*** 發送短信* @return stdClass*/public static function sendSms($mobile,$sendCode) {// 初始化SendSmsRequest實例用于設置發送短信的參數$request = new SendSmsRequest();//可選-啟用https協議//$request->setProtocol("https");// 必填,設置短信接收號碼$request->setPhoneNumbers($mobile);// 必填,設置簽名名稱,應嚴格按"簽名名稱"填寫,請參考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign$request->setSignName("優維網絡");// 必填,設置模板CODE,應嚴格按"模板CODE"填寫, 請參考: https://dysms.console.aliyun.com/dysms.htm#/develop/template$request->setTemplateCode("SMS_20019xxxx");// 可選,設置模板參數, 假如模板中存在變量需要替換則為必填項$request->setTemplateParam(json_encode(array( // 短信模板中字段的值"code"=>$sendCode['code'],"product"=>"dsd"), JSON_UNESCAPED_UNICODE));// 可選,設置流水號$request->setOutId("yourOutId");// 選填,上行短信擴展碼(擴展碼字段控制在7位或以下,無特殊需求用戶請忽略此字段)$request->setSmsUpExtendCode("1234567");// 發起訪問請求$acsResponse = static::getAcsClient()->getAcsResponse($request);} }

    修改好api文件后,我們就需要創建一個php文件處理小程序傳入的值和調用api了

    <?php //引入剛才修改好的SmsDemo.php文件 require_once "SmsDemo.php"; //實例化類 $sms = new SmsDemo(); //接收小程序傳入的電話號碼 $mobile = $_POST['mobile']; //生產驗證碼 $code = rand(1000,9999); $sendCode = array("code"=>$code); //調用剛才更改的SmsDemo類中的發送驗證碼方法 $res = $sms->sendSms($mobile,$sendCode); //給小程序返回生成的驗證碼 $res['phone_code']= $code; $res['mobile']= $mobile; echo json_encode($res);

    至此服務器端就寫完了,下面是小程序的代碼

    首先上wxml文件代碼

    <!--pages/register/register.wxml--> <view class="mobile-container"><view class='row'><input class='line' type='text' placeholder='用戶名' confirm-type='確認' bindinput='userChange'></input></view><view class='row'><input class='line' type='password' placeholder='請輸入您的密碼' confirm-type='確認' bindinput='passwordChange'></input></view><view class='row'><input class='line' type='password' placeholder='再次確認密碼' confirm-type='確認' bindinput='confirmPassword'></input></view><view class='row'><input class='line' type='email' placeholder='郵箱' confirm-type='確認' bindinput='bindEmail'></input></view><view class='row'><input class='line' type='number' placeholder='手機號' confirm-type='確認' bindinput='mobileChange'></input></view><view class='row'><input class='code-input' type='number' placeholder='驗證碼' confirm-type='確認' bindinput='codeChange'></input><button class='code-btn' bindtap="sendCode" disabled='{{sendDisabled}}'>{{codeText}}</button></view><view class='row'><button class='submit' bindtap="bindRegister" type='primary'>提交</button></view> </view>

    wxss代碼:

    /* pages/register/register.wxss */ .mobile-container {height: 100%;display: flex;flex-direction: column;box-sizing: border-box;padding: 0 40rpx;font-size: 28rpx;}input {border-bottom: 1px solid #E5E5E5;height: 80rpx;}.row {display: flex;height: 80rpx;margin: 20rpx 0;}.row .line {flex: 5;}.row .code-input {flex: 5;}.row .code-btn {flex: 2;font-size: 24rpx;text-align: center;line-height: 60rpx;}.row .submit {width: 100%;border-radius: 20rpx;height: 90rpx;line-height: 60rpx;font-size: 35rpx;}

    最重要的js代碼:

    // pages/register/register.js var app = getApp() var url = "https://www.ailaiyun.com/api_demo/" Page({/*** 頁面的初始數據*/data: {//用戶名user_name:'',//密碼password:'',//確認密碼confirm_password:'',//郵箱email:'',//用戶輸入的手機號mobile: '',//用戶輸入的驗證碼code: '',//服務器返回的驗證碼res_code: '',//用來控制獲取驗證碼倒計時按鈕的時效性sendDisabled: false,forbiddenTime: 0,codeText: '獲取驗證碼'},/*** 用戶名輸入*/userChange:function(e){var that = this;that.setData({user_name: e.detail.value});},/*** 密碼輸入*/passwordChange:function(e){var that = this;that.setData({password: e.detail.value});},/*** 密碼確認輸入*/confirmPassword:function(e){var that = this;that.setData({confirm_password: e.detail.value});},/*** 郵箱輸入*/bindEmail: function (e) {var that = this;that.setData({email: e.detail.value}); },/*** 手機號輸入*/mobileChange: function (e) {var that = this;that.setData({mobile: e.detail.value});},/*** 驗證碼輸入*/codeChange: function (e) {var that = this;that.setData({code: e.detail.value});},/*** 獲取驗證碼*/sendCode: function () {var that = this;let mobile = that.data.mobile;if (!mobile) {wx.showToast({title: '請輸入手機號',icon: 'none'})return;}if (!that.mobileValid(mobile)) {wx.showToast({title: '請輸入正確的手機號',icon: 'none'})return;}wx.request({url: url + 'SendCode.php',data: {mobile: mobile},method: 'POST',header: { "Content-Type": "application/x-www-form-urlencoded" },success: function (res) {console.log(res.data);let data = res.data;that.setData({sendDisabled: true,res_code: data.phone_code,});that.reflashTime(90);}})},/*** 提交注冊*/bindRegister: function () {var that = thislet mobile = that.data.mobile;let user_name = that.data.user_name;let password = that.data.password;let confirm_password = that.data.confirm_password;let code = that.data.code;let res_code = that.data.res_code;let email = that.data.email;if (!user_name) {wx.showToast({title: '請輸入用戶名',icon: 'none'})return;}if (!password) {wx.showToast({title: '請輸入密碼',icon: 'none'})return;}if (!confirm_password) {wx.showToast({title: '您必須再次輸入密碼',icon: 'none'})return;}if (password != confirm_password) {wx.showToast({title: '兩次輸入密碼不一致',icon: 'none'})return;}if (!email) {wx.showToast({title: '請輸入您的郵箱',icon: 'none'})return;}if (!that.emailValid(email)) {wx.showToast({title: '請輸入正確的郵箱地址',icon: 'none'})return;}if (!mobile) {wx.showToast({title: '請輸入手機號',icon: 'none'})return;}if (!code) {wx.showToast({title: '請輸入驗證碼',icon: 'none'})return;}if (code == res_code) {var openid = wx.getStorageSync('openid');//將手機號添加入用戶信息表中wx.request({url: url + 'addUserPhone.php',data: {user_name: that.data.user_name,password: that.data.password,mobile: that.data.mobile,email: that.data.email},method: 'POST',header: { "Content-Type": "application/x-www-form-urlencoded" },success: function (res) {console.log(res);//提示用戶wx.showToast({title: '注冊成功',icon: 'success',success: function () {setTimeout(function () {wx.navigateTo({url: '../login/login',});}, 1000);}});}})} else {wx.showToast({title: '驗證碼錯誤!',icon:'none'})return;}},/*** 刷新驗證碼重新獲取時間*/reflashTime(time) {var that = this;let index = setInterval(function () {time -= 1;if (time <= 0) {that.setData({forbiddenTime: 0,sendDisabled: false,codeText: '獲取驗證碼'});index = null;} else {that.setData({forbiddenTime: time,codeText: '重新獲取' + time + 'S'});}}, 1000);},/*** 郵箱校驗*/emailValid(email) {var reMail = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;if (!reMail.test(email)) {return false;} else {return true;} },/*** 手機號校驗*/mobileValid(mobile) {var rePhone = /^[1][3,4,5,7,8][0-9]{9}$/;if (!rePhone.test(mobile)) {return false;} else {return true;}},/*** 生命周期函數--監聽頁面加載*/onLoad: function (options) {var that = this},/*** 生命周期函數--監聽頁面初次渲染完成*/onReady: function () {},/*** 生命周期函數--監聽頁面顯示*/onShow: function () {},/*** 生命周期函數--監聽頁面隱藏*/onHide: function () {},/*** 生命周期函數--監聽頁面卸載*/onUnload: function () {},/*** 頁面相關事件處理函數--監聽用戶下拉動作*/onPullDownRefresh: function () {},/*** 頁面上拉觸底事件的處理函數*/onReachBottom: function () {},/*** 用戶點擊右上角分享*/onShareAppMessage: function () {} })

    到這里已經能實現調用阿里云的短信功能進行發送短信驗證了

    點擊【提交】按鈕我這里簡單寫個addUserPhone.php文件測試能接收到小程序的值。后面的處理我就不詳述了。

    ?

    <?php $mobile = $_POST['mobile']; $user_name = $_POST['user_name']; $password= $_POST["password"]; $email= $_POST["email"]; } echo json_encode($mobile.$user_name.$password.$email);

    原文地址:https://www.ailaiyun.com/%e9%98%bf%e9%87%8c%e4%ba%91%e7%9f%ad%e4%bf%a1%e6%9c%8d%e5%8a%a1%e4%b8%8e%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%af%b9%e6%8e%a5%e8%bf%9b%e8%a1%8c%e6%b3%a8%e5%86%8c.html?

    總結

    以上是生活随笔為你收集整理的阿里云短信服务与微信小程序对接进行注册的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。