首页 短信插件 正文
  • 本文约1315字,阅读需7分钟
  • 418
  • 0

小储云对接短信汇短信插件

温馨提示:本文最后更新于2024年4月18日 14:36,若内容或图片失效,请在下方留言或联系博主。
摘要

短信汇提供小储云商城系统短信渠道,可自行创建/覆盖文件,也可以下载下方插件覆盖到网站的根目录(操作之前记得备份) 短信插件覆盖完成后即可使用短信汇短信插件 小储云对接短信汇短信插件代码如下: admin- admin.sms.set.php ?php /* 短信配置 */ $title = 短信通知 ; include header.php ; ...

 

小储云商城短信插件

 

短信汇提供小储云商城系统短信渠道,可自行创建/覆盖文件,也可以下载下方插件覆盖到网站的根目录(操作之前记得备份)

短信插件覆盖完成后即可使用短信汇短信插件

小储云对接短信汇短信插件代码如下:

admin->admin.sms.set.php

<?php

/**
 * 短信配置
 */
$title = '短信通知';
include 'header.php';
$SmsHuiSms = require('../SmsHuiSMS.php');
global $conf;
?>
<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-body">
                <div class="mdui-tab mdui-tab-scrollable mdui-tab-centered mdui-p-l-0" mdui-tab>
                    <a href="#Se1" class="mdui-ripple">短信基础配置</a>
                    <a href="#Se2" class="mdui-ripple">自定义短信配置</a>
                </div>
                <div id="Se1" class="mdui-p-a-2">
                    <form class="form-horizontal layui-form">
                        <div class="form-group mb-3">
                            <label for="example-input-normal">短信开关(购买提醒/用户)</label>
                            <select class="custom-select mt-3" lay-search name="sms_switch_order">
                                <option <?= $conf['sms_switch_order'] == -1 ? 'selected' : '' ?> value="-1">关闭短信验证
                                </option>
                                <option <?= $conf['sms_switch_order'] == 1 ? 'selected' : '' ?> value="1">开启短信验证
                                </option>
                            </select>
                        </div>

                        <div class="form-group mb-3">
                            <label for="example-input-normal"
                                   style="font-weight: 500">手机号绑定(可用来登录站长后台,需消耗你的短信额度)</label>
                            <input type="text" name="Mobile" lay-verify="required" class="form-control" value=""
                                   placeholder="请输入你的手机号!">
                        </div>

                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">我的短信额度</label>
                            <input type="text" name="sms" class="form-control" value="0条" disabled
                                   placeholder="剩余短信额度!">
                        </div>

                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">今日发送短信</label>
                            <input type="text" name="sms_today" class="form-control" value="0条" disabled
                                   placeholder="今日发送短信!">
                        </div>

                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">累计发送短信</label>
                            <input type="text" name="sms_total" class="form-control" value="0条" disabled
                                   placeholder="累计发送短信!">
                        </div>

                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">服务端余额</label>
                            <input type="text" name="money" class="form-control" value="0元" disabled
                                   placeholder="服务端余额!">
                        </div>

                        <button type="submit" lay-submit lay-filter="Notification_set"
                                class="btn btn-block btn-xs btn-success">保存内容
                        </button>
                    </form>
                </div>
                <div id="Se2" class="mdui-p-a-2">
                    <form class="form-horizontal layui-form">
                        <div class="form-group mb-3">
                            <label for="example-input-normal">短信对接通道</label>
                            <select class="custom-select mt-3" lay-filter="SMSChannelConfiguration" lay-search name="SMSChannelConfiguration">
                                <option  <?= $conf['SMSChannelConfiguration'] == 9 ? 'selected' : '' ?>
                                        value="9">SmsHuiSMS
                                </option>
                                <option <?= $conf['SMSChannelConfiguration'] == 1 ? 'selected' : '' ?> value="1">官方内置[切换此接口时,下方无需配置]
                                </option>
                                <option <?= $conf['SMSChannelConfiguration'] == 2 ? 'selected' : '' ?> value="2">阿里云
                                </option>
                                <option disabled <?= $conf['SMSChannelConfiguration'] == 3 ? 'selected' : '' ?>
                                        value="3">腾讯云【未开放】
                                </option>
                                <option disabled <?= $conf['SMSChannelConfiguration'] == 4 ? 'selected' : '' ?>
                                        value="4">七牛云【未开放】
                                </option>
                            </select>
                        </div>

                        <div id="SMSChannelSmsHui" style="display: none;">

                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">通道域名</label>
                                <input type="text" name="SmsHuiSms_Url" class="form-control"
                                       value="<?= $SmsHuiSms['SmsHuiSms_Url'] ?>"
                                       placeholder="短信平台的域名 前有http(s):// 后有/,请确保协议头正确">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">通道ID</label>
                                <input type="text" name="SmsHuiSms_Channel" class="form-control"
                                       value="<?= $SmsHuiSms['SmsHuiSms_Channel'] ?>"
                                       placeholder="短信平台的通道ID,Channel开头的!">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">用户名</label>
                                <input type="text" name="SmsHuiSms_UserName" class="form-control"
                                       value="<?= $SmsHuiSms['SmsHuiSms_UserName'] ?>"
                                       placeholder="短信平台的用户名!">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">用户密钥</label>
                                <input type="text" name="SmsHuiSms_Key" class="form-control"
                                       value="<?= $SmsHuiSms['SmsHuiSms_Key'] ?>"
                                       placeholder="短信平台的用户Secret Key,非MD5!">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">短信签名【签名名称】</label>
                                <input type="text" name="SmsHuiSms_Sign" class="form-control"
                                       value="<?= $SmsHuiSms['SmsHuiSms_Sign'] ?>"
                                       placeholder="如:SmsHuiSms  无需【】">
                            </div>
                        </div>

                        <div id="SMSChannelOther" style="display: none;">
                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">短信签名【签名名称】</label>
                                <input type="text" name="SMSSignName" class="form-control"
                                       value="<?= $conf['SMSSignName'] ?>"
                                       placeholder="请输入短信签名名称!">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">短信KeyID</label>
                                <input type="text" name="SMSAccessKeyId" class="form-control"
                                       value="<?= $conf['SMSAccessKeyId'] ?>"
                                       placeholder="请输入短信KeyID">
                            </div>

                            <div class="form-group mb-3">
                                <label for="example-input-normal"
                                       style="font-weight: 500">短信KeySecret</label>
                                <input type="text" name="SMSAccessKeySecret" class="form-control"
                                       value="<?= $conf['SMSAccessKeySecret'] ?>"
                                       placeholder="请输入短信KeySec">
                            </div>

                        </div>

                        <div class="form-group mb-3">
                            <label for="example-input-normal"
                                   style="font-weight: 500">模板CODE[验证码]</label>
                            <input type="text" name="SMSTemplateCode" lay-verify="required" class="form-control"
                                   value="<?= $conf['SMSTemplateCode'] ?>"
                                   placeholder="模板示例:您此次验证码为{$code},请您尽快验证!">
                        </div>

                        <button type="submit" lay-submit lay-filter="Notification_set"
                                class="btn btn-block btn-xs btn-success">保存内容
                        </button>

                        <div style="margin-top: 1em;text-align: center">
                            <a href="https://www.SmsHui.com/docs/smsplugins/12.html" target="_blank ">https://www.SmsHui.com/docs/smsplugins/12.html</a> 对接SmsHuiSMS使用教程<br><br>

                            <a href="https://ram.console.aliyun.com/manage/ak" target="_blank ">https://ram.console.aliyun.com/manage/ak【阿里云】</a>取得您的AK信息<br>
                            <a href="https://blog.csdn.net/u010385623/article/details/113773339" target="_blank ">https://blog.csdn.net/u010385623/article/details/113773339【阿里云】</a>查看短信信息获取教程
                        </div>
                    </form>
                </div>
                <div style="text-align: center">
                    <span class="mt-3" style="
                        display: block">短信额度充值请前往授权服务端<a href="https://m3w.cn/xcy888" target="_blank" style="color: #000"><span class="badge badge-primary-lighten">晴玖服务端管理后台</span>( 进入)</a></span>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'bottom.php'; ?>

<script>
    layui.use('form', function () {
        var form = layui.form;
        if ($("select[name='SMSChannelConfiguration']").val() == "9") {
            $('#SMSChannelSmsHui').show();
        }
        form.on('select(SMSChannelConfiguration)', function(data) {
            if (data.value == '9') {
                $('#SMSChannelOther').hide(500);
                $('#SMSChannelSmsHui').show(500);
            } else {
                $('#SMSChannelOther').show(500);
                $('#SMSChannelSmsHui').hide(500);
            }
        });

        form.on('submit(Notification_set)', function (data) {
            layer.alert('是否要执行当前操作?', {
                icon: 3,
                btn: ['确定', '取消'],
                btn1: function (layero, index) {
                    var index = layer.msg('数据保存中,请稍后...', {
                        icon: 16,
                        time: 999999
                    });
                    if ($("select[name='SMSChannelConfiguration']").val() == "9") {
                        $.post('SmsHuiSMS.php', data.field, function (res) {});
                    }
                    $.post('ajax.php?act=config_set_sms', data.field, function (res) {
                        if (res.code == 1) {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn1: function (layero, index) {
                                    location.reload();
                                }
                            });
                        } else {
                            layer.close(index);
                            layer.alert(res.msg, {
                                btn1: function (layero, index) {
                                    location.reload();
                                }
                            });
                        }
                    });

                }
            });
            return false;
        })
    });
    $.post('ajax.php?act=sms_data', function (res) {
        if (res.code < 0) {
            layer.msg(res.msg);
        } else {
            $("input[name='money']").val(res.data.money + '元');
            $("input[name='sms']").val(res.data.sms + '条');
            $("input[name='Mobile']").val((res.data.Mobile == 0 ? '' : res.data.Mobile));
            $("input[name='sms_today']").val(res.data.sms_today + '条');
            $("input[name='sms_total']").val(res.data.sms_total + '条');
        }
    })
</script>

admin->SmsHui.php

<?php
$SmsHuiSms = '../SmsHuiSMS.php';
$config['SmsHuiSms_Url'] = $_POST['SmsHuiSms_Url'];
$config['SmsHuiSms_Channel'] = $_POST['SmsHuiSms_Channel'];
$config['SmsHuiSms_UserName'] = $_POST['SmsHuiSms_UserName'];
$config['SmsHuiSms_Key'] = $_POST['SmsHuiSms_Key'];
$config['SmsHuiSms_Sign'] = $_POST['SmsHuiSms_Sign'];
file_put_contents($SmsHuiSms, '<?php' . "\n\nreturn " . var_export($config, true) . ";");
?>

includes->extend->SMS.php

<?php
// +----------------------------------------------------------------------
// | Project: 晴玖商城系统
// +----------------------------------------------------------------------
// | Creation: 2022/1/14 9:17
// +----------------------------------------------------------------------
// | Filename: SMS.php
// +----------------------------------------------------------------------
// | Explain: 短信操作类
// +----------------------------------------------------------------------
namespace extend;

use Curl\Curl;
use Exception;
use lib\Guard\Guard;
use lib\Hook\Hook;
use lib\SourceShield\SourceShield;
use Medoo\DB\SQL;
use query;

/**
 * 短信操作类
 */
class SMS
{

    /**
     * @var int 发信类型 1验证码,2工单,3订单
     */
    public static $Type = 1;

    /**
     * 获取服务端短信数据
     */
    public static function SmsData()
    {
        $Data = Curl::curl(false, ['act' => 11], true, 1, 2);
        if (empty($Data) || empty($Data['code'])) {
            dies(-1, '短信数据获取失败!');
        }
        dier($Data);
    }

    /**
     * @param $phone
     * 修改手机号
     */
    public static function SmsUserSet($phone)
    {
        $Data = Curl::curl(false, ['act' => 12, 'Mobile' => $phone], true, 1, 2);
        if (empty($Data) || empty($Data['code'])) {
            dies(-1, '修改失败,数据获取异常!');
        } else if ((int)$Data['code'] === 1) {
            return true;
        }
        dier($Data);
    }

    /**
     * @param $phone
     * 手机号登录
     */
    public static function SmsAdminLogin($phone)
    {
        $Data = Curl::curl(false, ['act' => 13, 'mobile' => $phone, 'type' => 1], true, 1, 2);
        if (empty($Data) || empty($Data['code'])) {
            dies(-1, '短信发送失败,请使用其它方式登录!');
        }
        $_SESSION['ADMIN_PRODUCT_MS'] = time();
        dier($Data);
    }

    /**
     * @param $Code
     * 验证短信是否正确!
     */
    public static function SmsAdminVerify($Code)
    {
        global $date;
        $Data = Curl::curl(false, ['act' => 14, 'code' => $Code], true, 1, 2);

        if (empty($Data) || empty($Data['code'])) {
            dies(-1, '数据获取失败,请重新测试!');
        }

        if ($Data['code'] >= 0) {
            $DB = SQL::DB();
            $DB->insert('login', [
                'token' => $Data['token'],
                'ip' => userip(),
                'state' => 1,
                'finish_time' => $date,
                'date_created' => $date,
            ]);
            $_SESSION['ADMIN_TOKEN'] = $Data['token'];
            Guard::DataSync();
            SourceShield::DataSync();
            Maintain::run();
            dies(1, $Data['msg']);
        }
        dier($Data);
    }

    /**
     * @param $code
     * @param $phone
     * 发送短信
     */
    public static function SmsSend($code = '', $phone = '')
    {
        global $conf;
        //SmsSendAliyun

        if ((int)$conf['SMSChannelConfiguration'] === 2) {
            //阿里云
            $data = self::SmsSendAliyun($phone, $code);
            if ($data['Code'] == 'OK') {
                return [
                    'code' => 1, '验证码发送成功!'
                ];
            }
            return [
                'code' => -1, '验证码发送失败:' . $data['Message']
            ];
        }

        // SmsHuiSMS
        if ((int)$conf['SMSChannelConfiguration'] === 9) {

            $data = self::SendSmsHuiSms($phone, $code);
            if ($data['code'] == '1') {
                return [
                    'code' => 1, 'msg'=>'验证码发送成功!'
                ];
            }
            return [
                'code' => -1, 'msg'=>'验证码发送失败:' . $data['msg']
            ];
        }
        $DataPost = [
            'act' => 'send',
            'code' => $code,
            'mobile' => $phone,
            'type' => self::$Type,
        ];
        return Curl::curl(false, $DataPost, true, '/SMS/index', 2);
    }

    public static function SendSmsHuiSms($PhoneNumbers, $TemplateParam){
        global $conf;
        $SmsHuiSms = require('../SmsHuiSMS.php');
        if (empty($SmsHuiSms['SmsHuiSms_Url']) || empty($SmsHuiSms['SmsHuiSms_UserName']) || empty($SmsHuiSms['SmsHuiSms_Key']) || empty($SmsHuiSms['SmsHuiSms_Channel']) || empty($SmsHuiSms['SmsHuiSms_Sign'])) {
            return ['code' => '0','msg' => 'SmsHuiSms:缺少配置参数'];
        }
        if (!stristr($conf['SMSTemplateCode'], '{$code}')) {
            return ['code' => '0','msg' => 'SmsHuiSms:模板内容缺少{$code}变量符'];
        }
        $apiUrl = $SmsHuiSms['SmsHuiSms_Url'].'sendApi';
        $channel   = $SmsHuiSms['SmsHuiSms_Channel'];
        $username   = $SmsHuiSms['SmsHuiSms_UserName'];
        $key   = md5($SmsHuiSms['SmsHuiSms_Key']);
        $content = str_replace('{$code}',$TemplateParam, $conf['SMSTemplateCode']);
        $queryParams = [
            'channel' => $channel,
            'username' => $username,
            'key' => $key,
            'phone' =>$PhoneNumbers,
            'content' => urldecode('【'.$SmsHuiSms['SmsHuiSms_Sign'].'】'.$content),
        ];
        $url = $apiUrl . '?' . http_build_query($queryParams);
        $result  = file_get_contents($url);
        $result = json_decode($result, true);
        return $result;
    }

    /**
     * @param $code
     * 用户后台登录验证
     */
    public static function UserLoginVerify($code = '', $type = 1)
    {
        global $date, $conf;

        //验证码验证钩子
        Hook::execute('SmsVerify', [
            'code' => $code
        ]);

        if (empty($_SESSION['VerifyCodeLogin']) || empty($_SESSION['MobileLogin'])) {
            dies(-2, '请先发送验证码!');
        }
        if (empty($code)) {
            dies(-1, '请填写完整!');
        }
        if ($_SESSION['VerifyCodeLogin'] != $code) {
            dies(-1, '验证码有误,请核对后再输入!');
        }
        $DB = SQL::DB();
        $phone = (string)$_SESSION['MobileLogin'];
        //清理历史验证数据
        $_SESSION['MobileLogin'] = null;
        $_SESSION['VerifyCodeLogin'] = null;

        $Res = $DB->get('user', '*', [
            'mobile' => $phone,
        ]);
        if ($Res) {
            if ($Res['state'] != 1) {
                dies(-1, '您当前登录的账户已被禁止登录,请联系平台客服处理!');
            }
            userlog('后台登录', '用户于' . $date . '通过手机号' . ($type == 2 ? '账号找回功能' : '') . '成功登录后台!', $Res['id']);
            setcookie('THEKEY', $Res['user_idu'], time() + 3600 * 12 * 30, ROOT_DIR);
            query::OrderUser($Res['id']);
            GoodsCart::UserCookieDer($Res['id']);
            Hook::execute('UserLogin', [
                'name' => $Res['name'],
                'id' => $Res['id']
            ]);
            if ($type == 1) {
                dies(1, '短信验证通过,登录成功!');
            } else {
                //账号找回模式
                dier([
                    'code' => 1,
                    'msg' => '恭喜,账号找回成功,已自动为您登录,请刷新页面!',
                    'data' => [
                        'qq' => $Res['qq'] ?? '未绑定', //绑定QQ
                        'image' => $Res['image'], //头像
                        'name' => empty($Res['name']) ? '未设置' : $Res['name'],
                        'username' => empty($Res['username']) ? '未设置' : $Res['username'], //账户
                        'addtime' => $Res['found_date'], //注册时间
                        'lasttime' => $Res['recent_time'], //最近更新时间
                    ]
                ]);
            }
        } else {
            if ($type != 1) {
                dies(-1, '您的手机号尚未绑定平台账户,无法找回!');
            }
            if ((int)$conf['userregister'] !== 1) {
                dies(-1, '当前站点未开放注册');
            }
            if ($conf['inItRegister'] == 1 && empty($_COOKIE['INVITED_STATUS'])) {
                dies(-1, '当前站点开启了邀请注册功能,您只能够通过已注册用户的邀请链接才可以注册为平台用户!');
            }
            $uid_md = md5($phone . href());
            $Uty = UserConf::judge();
            if (!$Uty) {
                $Uty = 0;
            } else {
                $Uty = $Uty['id'];
            }

            $SQL = [
                'mobile' => $phone,
                'grade' => $conf['userdefaultgrade'],
                'user_idu' => $uid_md,
                'superior' => $Uty,
                'currency' => 0,
                'ip' => userip(),
                'image' => ImageUrl($conf['logo']),
                'name' => '平台用户',
                'state' => 1,
                'recent_time' => $date,
                'found_date' => $date
            ];
            $invite = -1;
            if (!empty($_COOKIE['INVITED_STATUS'])) {
                $InvId = $DB->get('user', ['id'], ['id' => (int)$_COOKIE['INVITED_STATUS'], 'LIMIT' => 1]);
                setcookie('INVITED_STATUS', null, time() - 1, ROOT_DIR);
                if ($InvId) {
                    $invite = $InvId['id'];
                }
            }

            $Res = $DB->insert('user', $SQL);
            if ($Res) {
                $ID = $DB->id();
                $IP = userip();
                $GETID = $DB->get('user', '*', ['id' => $ID]);
                if (!$GETID) {
                    dies(-1, '注册失败,请重新尝试!');
                }
                if ($invite >= 1) {
                    $FTR = $DB->get('invite', '*', ['ip' => $IP, 'LIMIT' => 1]);
                    if (!$FTR) {
                        $award = $conf['award'];
                        userlog('邀请奖励', '恭喜您成功邀请到用户[' . $phone . ']特奖励您' . $award . $conf['currency'] . '!,再接再厉哦', $invite, $award);
                        $DB->insert('invite', [
                            'uid' => $invite,
                            'invitee' => $ID,
                            'award' => $award,
                            'ip' => $IP,
                            'creation_time' => $date,
                        ]);
                        Hook::execute('UserInvite', [
                            'id' => $invite,
                            'yid' => $ID,
                            'num' => $award
                        ]);
                    } else {
                        userlog('失败邀请', '系统判断您的邀请对象:[' . $phone . ']已经在其他账号接收过邀请,无法奖励!,请邀请真实用户!', $_COOKIE['INVITED_STATUS']);
                    }
                }
                query::OrderUser($ID);
                GoodsCart::UserCookieDer($ID);
                setcookie('THEKEY', $uid_md, time() + 3600 * 12 * 30, ROOT_DIR);
                Hook::execute('UserRegister', [
                    'id' => $GETID['id'],
                    'name' => $GETID['name']
                ]);
                userlog('用户注册', '您于' . $date . '成功在本平台注册,使用的【手机号快捷注册】注册的,您的初始绑定手机号为:' . $phone . '!', $ID);
                dies(1, '恭喜你,注册成功,欢迎入驻本平台!');
            } else {
                dies(1, '平台账号创建失败,无法完成登录,请使用其他方式登录!');
            }
        }
    }

    //发送注册短信验证码
    public static function RegisterSms($phone = '')
    {
        global $conf, $times;
        $DB = SQL::DB();
        $Vs = $DB->get('user', ['id', 'state'], [
            'mobile' => $phone
        ]);
        if ($Vs) {
            dies(-1, '此手机号已绑定平台账号,无法再绑定新账号!');
        }
        if ((int)$conf['sms_switch_user'] !== 1) {
            dies(-1, '当前站点未开放手机号登录注册功能!');
        }

        if ((int)$conf['userregister'] !== 1) dies(-1, '当前站点未开启用户注册!');

        if ((int)$conf['AccountPasswordLogin'] !== 1) dies(-1, '当前站点未开启账号注册,请换一种注册方式!');

        $IP = userip();
        if (empty($phone)) {
            dies(-1, '请将手机号填写完整!');
        }
        $count = $DB->count('journal', [
            'name' => '短信验证',
            'date[>]' => $times,
            'ip' => $IP
        ]);
        if ($count >= (int)$conf['usersmslogin']) {
            dies(-1, '今日短信发送次数已经耗尽,每日只可发送' . (int)$conf['usersmslogin'] . '次短信验证码!');
        }
        $vis = self::randString();
        $_SESSION['VerifyCodeRegister'] = $vis; //验证码
        $_SESSION['PhoneRegister'] = $phone; //手机号
        Hook::execute('SmsSend', [
            'phone' => $phone,
            'code' => $vis, //验证码
            'ip' => $IP,
        ]);
        $Res = self::SmsSend($vis, $phone);
        if ((int)$Res['code'] === 1) {
            userlog('短信验证', $IP, '-1');
        }
        dier($Res);
    }

    /**
     * @param $phone
     * @param $num
     * 发送用户登录短信验证码
     */
    public static function UserLogin($phone = '', $num = 3, $type = 1)
    {
        global $times, $conf;
        $DB = SQL::DB();

        $Vs = $DB->get('user', ['id', 'state'], [
            'mobile' => $phone
        ]);

        if ($type === 1) {
            if ((int)$conf['sms_switch_user'] !== 1) {
                dies(-1, '当前站点未开启短信登录/注册方式,可使用下方QQ互联登录/注册!');
            }
        } else {
            //判断手机号是否存在!
            if (!$Vs) {
                dies(-1, '您输入的手机号尚未绑定平台账户,无法找回!');
            }
        }
        if ($Vs && $Vs['state'] != 1) {
            dies(-1, '您当前手机号绑定账户已被禁止登录,请联系客服处理!');
        }
        $IP = userip();
        if (empty($phone)) {
            dies(-1, '请将手机号填写完整!');
        }
        $count = $DB->count('journal', [
            'name' => '短信验证',
            'date[>]' => $times,
            'ip' => $IP
        ]);
        if ($count >= $num) {
            dies(-1, '今日短信登录次数已经耗尽,每日只可发送' . $num . '次短信验证码!');
        }

        //发送短信钩子
        $vis = self::randString();
        $_SESSION['VerifyCodeLogin'] = $vis;
        $_SESSION['MobileLogin'] = $phone;
        Hook::execute('SmsSend', [
            'phone' => $phone,
            'code' => $vis, //验证码
            'ip' => $IP,
        ]);
        $Res = self::SmsSend($_SESSION['VerifyCodeLogin'], $_SESSION['MobileLogin']);
        if ((int)$Res['code'] === 1) {
            userlog('短信验证', $IP, '-1');
        }
        dier($Res);
    }

    /**
     * @param $order
     * @param $name
     * @return bool
     * 发送订单邮件通知
     */
    public static function OrderEmailTips($order, $name)
    {
        global $conf;
        if ((int)$conf['weix_notice'] === -1) {
            return false;
        }
        $DB = SQL::DB();
        $Res = $DB->get('order', '*', [
            'order' => (string)$order
        ]);
        if (!$Res) {
            return false;
        }
        $data = [
            'act' => 15,
            'order' => $order,
            'price' => $Res['price'],
            'return' => ($Res['return'] === '' ? '未知下单返回' : $Res['return']),
            'uid' => $Res['uid'],
            'date' => $Res['addtitm'],
            'balance' => $Res['user_rmb'],
            'state' => $Res['state'],
            'money' => $Res['money'],
            'name' => $name,
            'payment' => $Res['payment'],
        ];
        Curl::curl(false, $data, true, 1, 2);
        return true;
    }

    /**
     * @param $uid
     * @param $order
     * 发送新订单通知【短信提醒】
     * @return bool
     */
    public static function OrderTips($uid, $order)
    {
        $DB = SQL::DB();
        $UID = $DB->get('user', [
            'mobile'
        ], [
            'id' => (int)$uid,
            'state' => 1
        ]);
        if (!$UID || empty($UID['mobile'])) {
            return false;
        }
        $order = $DB->get('order', ['id'], ['order' => (string)$order]);
        if (!$order) {
            return false;
        }
        $arr_post = [
            'act' => 'send',
            'code' => $order['id'],
            'mobile' => $UID['mobile'],
            'type' => 3,
        ];
        Curl::curl(false, $arr_post, true, '/SMS/index', 2);
        return true;
    }

    /**
     * @param $PhoneNumbers //手机号
     * @param $TemplateParam //验证码
     * 阿里云发送短信
     */
    public static function SmsSendAliyun($PhoneNumbers, $TemplateParam)
    {
        global $conf;
        $params = [];
        $params['SignName'] = $conf['SMSSignName'];
        $accessKeyId = $conf['SMSAccessKeyId'];
        $accessKeySecret = $conf['SMSAccessKeySecret'];
        $params['TemplateCode'] = $conf['SMSTemplateCode'];
        // *** 需用户填写部分 ***
        $security = false;
        $params['PhoneNumbers'] = $PhoneNumbers;
        $params['TemplateParam'] = [
            'code' => $TemplateParam,
        ];
        $params['OutId'] = '';
        $params['SmsUpExtendCode'] = '';
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if (!empty($params['TemplateParam'])) {
            $params['TemplateParam'] = json_encode($params['TemplateParam'], JSON_UNESCAPED_UNICODE);
        }

        return self::AliyRequest(
            $accessKeyId,
            $accessKeySecret,
            'dysmsapi.aliyuncs.com',
            array_merge($params, [
                'RegionId' => 'cn-hangzhou',
                'Action' => 'SendSms',
                'Version' => '2017-05-25',
            ]),
            $security
        );
    }

    /**
     * 产生随机数串
     * @param int $len 随机数字长度
     * @return string
     */
    public static function randString($len = 6)
    {
        $chars = str_repeat('123456789', 3);
        $chars = str_repeat($chars, $len);
        $chars = str_shuffle($chars);
        return substr($chars, 0, $len);
    }

    /**
     * 生成签名并发起请求
     * 阿里云
     * @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
     * @param $accessKeySecret string AccessKeySecret
     * @param $domain string API接口所在域名
     * @param $params array API具体参数
     * @param $security bool 使用https
     * @param $method bool 使用GET或POST方法请求,VPC仅支持POST
     * @return bool|stdClass 返回API接口调用结果,当发生错误时返回false
     */
    public static function AliyRequest($accessKeyId, $accessKeySecret, $domain, $params, $security = false, $method = 'POST')
    {
        $apiParams = array_merge([
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => uniqid(mt_rand(0, 0xffff), true),
            'SignatureVersion' => '1.0',
            'AccessKeyId' => $accessKeyId,
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'Format' => 'JSON',
        ], $params);
        ksort($apiParams);
        $sortedQueryStringTmp = '';
        foreach ($apiParams as $key => $value) {
            $sortedQueryStringTmp .= '&' . self::encode($key) . '=' . self::encode($value);
        }
        $stringToSign = "${method}&%2F&" . self::encode(substr($sortedQueryStringTmp, 1));
        $sign = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
        $signature = self::encode($sign);
        $url = ($security ? 'https' : 'http') . "://{$domain}/";
        try {
            $content = self::fetchContent($url, $method, "Signature={$signature}{$sortedQueryStringTmp}");
            return json_decode($content, TRUE);
        } catch (Exception $e) {
            return false;
        }
    }

    private static function encode($str)
    {
        $res = urlencode($str);
        $res = preg_replace('/\+/', '%20', $res);
        $res = preg_replace('/\*/', '%2A', $res);
        return preg_replace('/%7E/', '~', $res);
    }

    private static function fetchContent($url, $method, $body)
    {
        $ch = curl_init();
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        } else {
            $url .= '?' . $body;
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'x-sdk-client' => 'php/2.0.0'
        ));
        if (substr($url, 0, 5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $rtn = curl_exec($ch);
        if ($rtn === false) {
            trigger_error('[CURL_' . curl_errno($ch) . ']: ' . curl_error($ch), E_USER_ERROR);
        }
        curl_close($ch);
        return $rtn;
    }

}

根目录->SmsHui.php

<?php

return array (
  'SmsHuiSms_Url' => '',
  'SmsHuiSms_Channel' => '',
  'SmsHuiSms_UserName' => '',
  'SmsHuiSms_Key' => '',
  'SmshuiSms_Sign' => '',
);
评论
更换验证码