Common.php
4.9 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\api\controller;
use app\admin\model\MobileCode;
use think\Db;
/**
* 公共方法
*/
class Common
{
/**
* 查找单条数据(有排序)
* @ApiInternal
*/
public static function findWhereData($table,$where,$field,$order='id desc'){
$res = Db::name($table)
->where($where)
->field($field)
->order($order)
->find();
return $res;
}
/**
* 查找多条数据(无条件)
* @ApiInternal
*/
public static function selectData($table,$field,$order='id desc'){
$res = Db::name($table)
->field($field)
->order($order)
->select();
return $res;
}
/**
* 查找多条数据
* @ApiInternal
*/
public static function selectWhereData($table,$where,$field,$order='id desc'){
$res = Db::name($table)
->where($where)
->field($field)
->order($order)
->select();
return $res;
}
/**
* 查找多条数据
* @ApiInternal
*/
public static function selectWherePageData($table,$where,$page,$field,$order='id desc'){
$limit = config('verify.limit');
$res = Db::name($table)
->where($where)
->page($page,$limit)
->field($field)
->order($order)
->select();
return $res;
}
/**
* 统计总页码
* @ApiInternal
*/
public static function totalPage($table,$where){
$limit = config('verify.limit');
$count = Db::name($table)->where($where)->count();
$totalPage = ceil($count/$limit);
return $totalPage;
}
/**
* 单张相对路径转绝对路径
* @ApiInternal
*/
public static function absolutionUrlOne($image){
$url = '';
if(!empty($image)){
$host = config('verify.host');
$url = $host.$image;
}
return $url;
}
/**
* 发送手机验证码
* @ApiInternal
*/
public static function sendCode($mobile,$content){
date_default_timezone_set('PRC');//设置时区
$url = "http://www.ztsms.cn/sendNSms.do";//提交地址
$username = 'jiazhengcm';//用户名
$password = 'Cxz307312';//原密码
//短信平台绑定手机号18612918096
$data = array(
'content' => $content,//短信内容
'mobile' => $mobile,//手机号码
'productid' => '676767',//产品id
'xh' => ''//小号
);
$isTranscoding = false;
$data['content'] = $isTranscoding === true ? mb_convert_encoding($data['content'], "UTF-8") : $data['content'];
$data['username']=$username;
$data['tkey'] = date('YmdHis');
$data['password'] = md5(md5($password) . $data['tkey']);
$curl = curl_init();// 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); // 执行操作
return $result;
}
/**
* 验证验证码是否正确
* @ApiInternal
*/
public static function validateMobileCode($post){
$mobileCodeModel = new MobileCode();
$res_find = $mobileCodeModel->where(['mobile' => $post['mobile'], 'mobile_code' => $post['mobile_code'], 'is_use' => 0, 'create_date' => date('Y-m-d'),])
->where('expire_time','gt',time())->find();
if($res_find){
$res_update = $mobileCodeModel->where('id',$res_find['id'])->setField('is_use',1);
if($res_update){
return true;
}
}
return false;
}
}