Sms.php
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
 * Created by PhpStorm.
 * User: billy
 * Date: 10/02/2017
 * Time: 6:23 PM
 */
namespace App\Common;
use Illuminate\Support\Facades\Log;
/**
 * Class Sms
 * @package App\Common
 */
class Sms
{
    /**
     * @return array
     */
    private static function getHeadArray()
    {
        $AppKey = config('constants.sms.app_key');
        $AppSecret = config('constants.sms.app_Secret');
        $Nonce = rand(100000, 999999);
        $CurTime = time();
        $CheckSum = strtolower(sha1($AppSecret . $Nonce . $CurTime));
        $head_arr = array();
        $head_arr[] = 'Content-Type: application/x-www-form-urlencoded';
        $head_arr[] = 'charset: utf-8';
        $head_arr[] = 'AppKey:' . $AppKey;
        $head_arr[] = 'Nonce:' . $Nonce;
        $head_arr[] = 'CurTime:' . $CurTime;
        $head_arr[] = 'CheckSum:' . $CheckSum;
        return $head_arr;
    }
    /**
     * @param $mobile
     * @return bool
     */
    public static function sendCode($mobile)
    {
        $target = config('constants.sms.base_url').config('constants.sms.code_url');
        $head_arr = self::getHeadArray();
        $post_data = "mobile=".$mobile."&codeLen=".config('constants.sms.code_len');
        if (config('constants.sms.template_id'))
        {
            $post_data .= "&templateid=".config('constants.sms.template_id');
        }
        $result = Http::PostWithHeader($post_data, $head_arr, $target);
        Log::info('send code result: '.$result);
        if (strstr($result,"\"code\":200"))
            return true;
        else
            return false;
    }
    /**
     * @param $mobile
     * @param $code
     * @return bool
     */
    public static function verifyCode($mobile, $code)
    {
        $target = config('constants.sms.base_url').config('constants.sms.verify_url');
        $head_arr = self::getHeadArray();
        $post_data = "mobile=".$mobile."&code=".$code;
        $result = Http::PostWithHeader($post_data, $head_arr, $target);
        if (strstr($result,"\"code\":200"))
            return true;
        else
            return false;
    }
    /**
     * @param $mobiles
     * @param $template_id
     * @param $params
     * @return bool
     */
    public static function sendTemplateMessage($mobiles, $template_id, $params)
    {
        $target = config('constants.sms.base_url').config('constants.sms.template_url');
        $head_arr = self::getHeadArray();
        $post_data = "templateid=".$template_id."&mobiles=".json_encode($mobiles)."¶ms=".json_encode($params);
        $result = Http::PostWithHeader($post_data, $head_arr, $target);
        if (strstr($result,"\"code\":200"))
            return true;
        else
            return false;
    }
}