CommonController.php
7.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
* 通用类
* By: Phpstorm
* Author: XiaoJie
* Datetime: 2018/8/16 4:18
*/
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use app\portal\model\MobileCodeModel;
use think\Config;
use think\Db;
/**
* @title 通用文件
* @description 接口说明
*/
class CommonController extends HomeBaseController
{
/**
* 上传照片
*/
public function uploadPic()
{
$file = request()->file('pic');
if(empty($file)){
$this->apiResponse(0,'上传文件不能为空','');
}
if($file){
$info = $file->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'header');
if($info){
$baseurl = $info->getSaveName();
$this->apiResponse(1,'上传成功',$baseurl);
}else{
$error = $file->getError();
$this->apiResponse(0,$error,'');
}
}
}
/**
* 发送手机验证码
*/
public function sendMobileCode()
{
Db::startTrans();
$mobile = $this->request->param('mobile','');
$mc = new MobileCodeModel();
$mobile_code = rand(100000, 999999);
$search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
if (!preg_match($search,$mobile)) {
$this->apiResponse(0,'手机号格式有误','');
}
if(empty($mobile)){
$this->apiResponse(0,'缺少必要参数:MOBILE','');
}
$content = "【振威】您的验证码为".$mobile_code.",如非本人操作请忽略本短信!";
$info = $mc->where('mobile',$mobile)->find();
if($info){
if($info['expire_time'] > time() and $info['is_use'] == 0){
$this->apiResponse(0,'不能频繁发送验证码','');
}
$res = $mc->where('id',$info['id'])->data([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => $info['count'] +1
])->update();
}else{
$res = $mc->insert([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => 1
]);
}
if($res){
//发送验证码
$is_ok = substr($this->sendCode($mobile,$content),0,1);
if($is_ok != 1){
Db::rollback();
$this->apiResponse(0,'短信发送失败','');
}
Db::commit();
$this->apiResponse(1,'发送成功','');
}
}
/**
* 验证手机验证码
*/
public function validateMobileCode($post)
{
$mc = new MobileCodeModel();
if(empty($post['mobile'])){
$this->apiResponse(0,'缺少必要参数:MOBILE','');
}
if(empty($post['mobile_code'])){
$this->apiResponse(0,'缺少必要参数:MOBILE_CODE','');
}
$res_find = $mc->where([
'mobile' => $post['mobile'],
'mobile_code' => $post['mobile_code'],
'is_use' => 0,
'expire_time' => ['gt',time()]
])->find();
if($res_find){
$res_update = $mc->where('id',$res_find['id'])->setField('is_use',1);
if($res_update){
return true;
}
}else{
$this->apiResponse(0,'验证未通过',$post);
}
}
/**
* 发送手机验证码
* @param $mobile
* @param $code
* @return mixed
*/
private function sendCode($mobile,$content){
date_default_timezone_set('PRC');//设置时区
$url = "http://www.ztsms.cn/sendNSms.do";//提交地址
$username = Config::get('MB_USERNAME');//用户名
$password = Config::get('MB_PASSWORD');//原密码
$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;
}
/**
* 微信上传图片
*/
public function upload_wx_pic(){
$access_token = $this->getAccessToken();
$img_str = $this->request->param('media','','string');
$img_arr = explode(',',rtrim($img_str,','));
$foldername = 'petavatar/'.date('Ymd');
$imgurls = '';
foreach($img_arr as $v) {
$url = $this->getmedia($access_token,$v,$foldername);
$imgurls .= ','. $url;
}
$data['imgurls'] = ltrim($imgurls,',');
$data['httpimgurls'] = cmf_get_image_preview_url($data['imgurls']);
$this->apiResponse(1,'上传成功',$data);
}
/**
* 根据media_id下载微信图片
* @param $access_token
* @param $media_id
* @param $foldername
* @return string
*/
private function getmedia($access_token,$media_id,$foldername){
$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$media_id;
if (!file_exists("./upload/".$foldername)) {
mkdir("./upload/".$foldername, 0777, true);
}
$file_name=date('YmdHis').rand(1000,9999).'.jpg';
$targetName = './upload/'.$foldername.'/'.$file_name;
$saveName = $foldername.'/'.$file_name;
$ch = curl_init($url); // 初始化
$fp = fopen($targetName, 'wb'); // 打开写入
curl_setopt($ch, CURLOPT_FILE, $fp); // 设置输出文件的位置,值是一个资源类型
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
return $saveName;
}
/**
* @title 获取signPackage
* @return $signPackage
*/
public function getSignPackage()
{
require_once EXTEND_PATH."WechatJssdk/jssdk.php";
$jssdk = new \JSSDK(Config::get('WX_APPID'), Config::get('WX_APP_SECRET'));
return $jssdk->getSignPackage();
}
/**
* @title 获取AccessToken
* @return $acessToken
*/
public function getAccessToken()
{
require_once EXTEND_PATH."WechatJssdk/jssdk.php";
$jssdk = new \JSSDK(Config::get('WX_APPID'), Config::get('WX_APP_SECRET'));
return $jssdk->getAccessToken();
}
}