Skip to content

Commit dd132f0

Browse files
committed
短信验证码功能
1 parent 7d7586f commit dd132f0

File tree

6 files changed

+174
-39
lines changed

6 files changed

+174
-39
lines changed

server/apps/classes/UserBase.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class UserBase extends Swoole\Controller
77
{
88
public $uid;
9+
protected $user;
910

1011
function __construct($swoole)
1112
{
@@ -18,5 +19,43 @@ function __construct($swoole)
1819
Swoole::$php->session->start();
1920
Swoole\Auth::loginRequire();
2021
$this->uid = $_SESSION['user_id'];
22+
$this->user = Model('UserInfo')->get($this->uid)->get();
23+
$this->tpl->assign('_user', $this->user);
24+
}
25+
26+
function infoPage($info, $detail = '', $error = false)
27+
{
28+
$this->assign('info', $info);
29+
$this->assign('detail', $detail);
30+
$this->display('include/page.php');
31+
}
32+
33+
protected function isActiveMenu($m, $v = '')
34+
{
35+
if ($this->env['mvc']['controller'] == $m)
36+
{
37+
if (!empty($v))
38+
{
39+
return $this->env['mvc']['view'] == $v;
40+
}
41+
return true;
42+
}
43+
else
44+
{
45+
return false;
46+
}
47+
}
48+
49+
protected function getMenuItem($c, $v, $name)
50+
{
51+
$html = "<li ";
52+
if ($this->isActiveMenu($c, $v))
53+
{
54+
$html .= 'class="active"';
55+
}
56+
$html .= '>';
57+
$html .= '<a href="/' . $c . '/' . $v . '/" title="Dashboard"><i class="fa fa-lg fa-fw fa-home"></i> <span
58+
class="menu-item-parent">' . $name . '</span></a></li>';
59+
return $html;
2160
}
2261
}

server/apps/controllers/Ajax.php

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,68 @@ function check_email()
1414
return $this->model->UserInfo->exists($_GET['email']);
1515
}
1616
}
17+
18+
function check_vcode()
19+
{
20+
if (!empty($_GET['vcode']))
21+
{
22+
\Swoole::$php->session->start();
23+
return array('data' => strtoupper($_GET['vcode']) == $_SESSION['authcode']);
24+
}
25+
return "bad request";
26+
}
27+
28+
function send_smscode()
29+
{
30+
require_once WEBPATH.'/vendor/autoload.php';
31+
$code = rand(1000, 9999);
32+
Swoole::$php->session->start();
33+
if (!$this->user->isLogin())
34+
{
35+
return ['code' => 1001, 'message' => '未登录'];
36+
}
37+
if (empty($_GET['mobile']) or empty($_GET['vcode']))
38+
{
39+
return ['code' => 1002, 'message' => '缺少参数'];
40+
}
41+
if (strtoupper($_GET['vcode']) != $_SESSION['authcode'])
42+
{
43+
return ['code' => 1003, 'message' => '错误的图形验证码'];
44+
}
45+
if (strlen($_GET['mobile']) != 11)
46+
{
47+
return ['code' => 1004, 'message' => '错误的手机号码,必须为11位有效号码'];
48+
}
49+
$table = table('user_smscode');
50+
if ($table->count([
51+
'sms_code' => $_POST['smscode'],
52+
'mobile' => $_POST['mobile'],
53+
'where' => [['unix_timestamp(created_time) > ' . strtotime(date('Y-m-d'))]],
54+
]) > 5)
55+
{
56+
return ['code' => 1005, 'message' => '发生次数超过限制,同一个手机号每天只允许发送5条短信。'];
57+
}
58+
$user = Model('UserInfo')->get($this->user->getUid())->get();
59+
if ($user['mobile_verification'])
60+
{
61+
return ['code' => 1006, 'message' => '您的手机号码已通过验证,无需再次验证。'];
62+
}
63+
try
64+
{
65+
if (!table('user_smscode')->put(['sms_code' => $code, 'uid' => $this->user->getUid(), 'mobile' => $_GET['mobile']]))
66+
{
67+
return ['code' => 6001, 'message' => '写入数据库失败'];
68+
}
69+
$sender = new \Qcloud\Sms\SmsSingleSender($this->config['sms']['appid'], $this->config['sms']['appkey']);
70+
$ret = $sender->send(0, "86", $_GET['mobile'], "您的验证码为{$code},请于15分钟内填写", "", "");
71+
$result = json_decode($ret, true);
72+
return ['code' => $result['result'], 'message' => $result['errmsg']];
73+
}
74+
catch (\Exception $e)
75+
{
76+
return array('code' => $e->getCode(), 'message' => $e->getMessage());
77+
}
78+
}
1779

1880
function comment()
1981
{
@@ -24,9 +86,9 @@ function comment()
2486
$post['app'] = $_POST['app'];
2587
$post['content'] = $_POST['content'];
2688
$post['uid'] = $uid;
27-
$post['uname'] = $_SESSION['user']['nickname'];
28-
if($post['app']==='mblog')
29-
{
89+
$post['uname'] = $_SESSION['user']['nickname'];
90+
if ($post['app'] === 'mblog')
91+
{
3092
$m = createModel('MicroBlog');
3193
$entity = $m->get($post['aid']);
3294
$entity->reply_count ++;

server/apps/controllers/Person.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ function profile()
227227
$set['intro'] = trim($_POST['intro']);
228228
$set['company'] = $_POST['company'];
229229
$set['blog'] = $_POST['blog'];
230-
$set['mobile'] = $_POST['mobile'];
231230
$set['sex'] = (int)$_POST['sex'];
232231
$set['education'] = (int)$_POST['education'];
233232
$set['skill'] = implode(',',$_POST['skill']);
@@ -241,20 +240,19 @@ function profile()
241240
}
242241
else
243242
{
244-
require WEBPATH.'/dict/forms.php';
243+
$forms = require WEBPATH . '/dict/forms.php';
245244
$_u = model('UserInfo');
246245
$u = $_u->get($this->uid)->get();
247246

248247
$_skill = model('UserSkill')->getMap(array());
249-
$_forms['sex'] = Swoole\Form::radio('sex',$forms['sex'],$u['sex']);
250-
$_forms['education'] = Swoole\Form::select('education',$forms['education'],$u['education']);
251-
$_forms['skill'] = Swoole\Form::checkbox('skill',$_skill,$u['skill']);
252-
$_forms['level'] = Swoole\Form::radio('php_level',$forms['level'],$u['php_level']);
248+
$_forms['sex'] = Swoole\Form::radio('sex', $forms['sex'], $u['sex']);
249+
$_forms['education'] = Swoole\Form::select('education', $forms['education'], $u['education']);
250+
$_forms['skill'] = Swoole\Form::checkbox('skill', $_skill, $u['skill']);
251+
$_forms['level'] = Swoole\Form::radio('php_level', $forms['level'], $u['php_level']);
253252

254-
$this->swoole->tpl->assign('user',$u);
255-
$this->swoole->tpl->assign('forms',$_forms);
253+
$this->swoole->tpl->assign('user', $u);
254+
$this->swoole->tpl->assign('forms', $_forms);
256255
$this->swoole->tpl->display();
257-
//$this->view->showTrace();
258256
}
259257
}
260258
function index()
@@ -400,4 +398,47 @@ function myfriends()
400398
$gw->action_list();
401399
$this->swoole->tpl->display();
402400
}
401+
402+
function mobile_verify()
403+
{
404+
if ($this->user['mobile_verification'])
405+
{
406+
$this->infoPage('您的手机号码已通过验证,无需再次验证');
407+
return;
408+
}
409+
if ($_POST)
410+
{
411+
$this->validate($_POST, array(
412+
'mobile' => 'required|mobile',
413+
'smscode' => 'required|int'
414+
));
415+
416+
$table = table('user_smscode');
417+
$data = $table->gets([
418+
'sms_code' => $_POST['smscode'],
419+
'mobile' => $_POST['mobile'],
420+
'where' => [['unix_timestamp(created_time) > ' . strtotime(date('Y-m-d'))]],
421+
]);
422+
if (empty($data))
423+
{
424+
$this->assign('msg', $this->message(4001, '错误的验证码'));
425+
goto display;
426+
}
427+
if ($data['verified'])
428+
{
429+
$this->assign('msg', $this->message(4002, '该手机号码已验证过,无需再次验证'));
430+
goto display;
431+
}
432+
$table->set($data[0]['id'], ['verified' => 1]);
433+
$user = table('user_login')->get($this->uid);
434+
$user->mobile = $_POST['mobile'];
435+
$user->mobile_verification = 1;
436+
$user->save();
437+
$this->infoPage('验证通过');
438+
return;
439+
}
440+
display:
441+
$this->assign('_user', $this->user);
442+
$this->display();
443+
}
403444
}

server/apps/controllers/Wiki.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Wiki extends Swoole\Controller
1111
public $if_filter = false;
1212

1313
protected $project_id;
14+
protected $wiki_id;
1415
protected $project;
1516
protected $pageInfo;
1617
protected $nodeInfo;
@@ -38,7 +39,7 @@ function index()
3839
$_GET['id'] = $this->project['home_id'];
3940
$this->getPageInfo();
4041
$this->getComments();
41-
$this->swoole->tpl->display("wiki/noframe/index.html");
42+
$this->display("wiki/index.php");
4243
}
4344

4445
/**
@@ -128,7 +129,7 @@ function search()
128129
$this->assign('count', $total);
129130
$this->assign('link_tpl', $link_tpl);
130131
$this->assign('pager', $pager->render());
131-
$this->display("wiki/noframe/search.php");
132+
$this->display("wiki/search.php");
132133
}
133134

134135
function main()
@@ -138,7 +139,7 @@ function main()
138139

139140
protected function getComments()
140141
{
141-
$thread_key = 'wiki-'.$this->tpl->_tpl_vars['id'];
142+
$thread_key = 'wiki-'.$this->wiki_id;
142143
$t = table('duoshuo_posts');
143144
$list = $t->gets(array('thread_key' => $thread_key, 'order' => 'id asc'));
144145
$uids = [];
@@ -169,14 +170,14 @@ protected function getComments()
169170
$li['author_url'] = "http://www.swoole.com/page/user/uid-" . $li['uid'];
170171
}
171172
}
172-
$this->tpl->assign('comments', $list);
173+
$this->assign('comments', $list);
173174
}
174175

175176

176177
function page()
177178
{
178179
$this->getPageInfo();
179-
if(!empty($this->nodeInfo))
180+
if (!empty($this->nodeInfo))
180181
{
181182
$this->project_id = $this->nodeInfo['project_id'];
182183
}
@@ -188,7 +189,7 @@ function page()
188189
$this->getTreeData();
189190
$this->getProjectLinks();
190191
$this->getComments();
191-
$this->swoole->tpl->display("wiki/noframe/index.html");
192+
$this->display("wiki/index.php");
192193
}
193194

194195
//获取项目信息
@@ -199,8 +200,8 @@ protected function getProjectInfo()
199200
{
200201
$this->http->finish("您访问的项目不存在");
201202
}
202-
$this->swoole->tpl->assign("project_id", $this->project_id);
203-
$this->swoole->tpl->assign("project", $this->project);
203+
$this->assign("project_id", $this->project_id);
204+
$this->assign("project", $this->project);
204205
}
205206

206207
//相关的项目
@@ -218,7 +219,7 @@ protected function getProjectLinks()
218219
$projects_link = array_merge($projects_link, $_projects_link);
219220
}
220221
}
221-
$this->swoole->tpl->assign("projects", $projects_link);
222+
$this->assign("projects", $projects_link);
222223
}
223224

224225
private function getPageInfo()
@@ -246,6 +247,7 @@ private function getPageInfo()
246247
$node = $_tree->get($wiki_id)->get();
247248
}
248249
$this->pageInfo = $_cont->get($wiki_id)->get();
250+
$this->wiki_id = $wiki_id;
249251

250252
if (empty($this->pageInfo))
251253
{
@@ -265,12 +267,12 @@ private function getPageInfo()
265267
{
266268
$text = $this->pageInfo['content'];
267269
}
268-
$this->swoole->tpl->assign("id", $wiki_id);
269-
$this->swoole->tpl->assign("wiki_page", $this->pageInfo);
270+
$this->assign("id", $wiki_id);
271+
$this->assign("wiki_page", $this->pageInfo);
270272

271273
markdown:
272274
$html = App\Content::getWikiHtml($wiki_id, $text);
273-
$this->swoole->tpl->assign("content", $html);
275+
$this->assign("content", $html);
274276
}
275277

276278
private function getTreeData()
@@ -282,21 +284,14 @@ private function getTreeData()
282284
//仅当前树
283285
$data = App\Content::getTree3($this->project_id, $node_id);
284286
$tree = App\Content::parseTreeArray($this->project['home_id'], $data);
285-
//debug($tree);
286-
// echo json_encode($tree);exit;
287-
$this->swoole->tpl->assign("tree", $tree);
287+
$this->assign("tree", $tree);
288288
}
289289

290290
function upload()
291291
{
292292
return App\Content::upload();
293293
}
294294

295-
protected function createPage($pid)
296-
{
297-
298-
}
299-
300295
function edit()
301296
{
302297
$this->session->start();

server/apps/templates/person_left.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<div class="memberbox">
22
<div class="memberpic"> <a href="/page/user/uid-{{$smarty.session.user_id}}"><img src="{{$smarty.session.user.avatar|default:'/static/images/default.gif'}}" /></a>
33
</div>
4-
<p>id:{{$smarty.session.user_id}} {{if empty($smarty.session.user.avatar)}}<a href="/person/profile/">上传头像</a>{{/if}}</p>
4+
<p>id: {{$smarty.session.user_id}}
5+
{{if $_user.mobile_verification}}<span style="color: green">[已验证]</span>{{else}}
6+
<a href="/person/mobile_verify/"> <span style="color: red">[未验证]</span></a>
7+
{{/if}}
8+
{{if empty($smarty.session.user.avatar)}}<a href="/person/profile/">上传头像</a>{{/if}}</p>
59
</div>
610
<ul>
711
<li><h2>空间管理</h2>
@@ -10,8 +14,6 @@
1014
<a href="/person/index/">我的空间首页</a></li>
1115
<li><img src="/static/images/user.png" width="16" height="16" />
1216
<a href="/person/profile/">编辑个人资料</a></li>
13-
14-
1517
<li><img src="/static/images/asterisk_orange.png" width="16" height="16" />
1618
<a href="/person/passwd/">修改登录密码</a>
1719
</li>

server/apps/templates/person_profile.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@
5656
<td>学历</td>
5757
<td>{{$forms.education}}</td>
5858
</tr>
59-
<tr>
60-
<td width="100">手机号码 </td>
61-
<td><input name="mobile" type="text" id="mobile" value="{{$user.mobile}}" height="30" /></td>
62-
</tr>
6359
<tr>
6460
<td width="100" valign="top">PHP等级</td>
6561
<td>{{$forms.level}}</td>

0 commit comments

Comments
 (0)