PageController.php
5.0 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
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <bronet@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use app\portal\service\PostService;
use think\Db;
use Yunpian\Sdk\YunpianClient;
class PageController extends HomeBaseController
{
public function index()
{
$postService = new PostService();
$pageId = $this->request->param('id', 0, 'intval');
$page = $postService->publishedPage($pageId);
if (empty($page)) {
abort(404, ' 页面不存在!');
}
$this->assign('page', $page);
$more = $page['more'];
$tplName = empty($more['template']) ? 'page' : $more['template'];
return $this->fetch("/$tplName");
}
public function lightQuery($id=11){
if ($id==""){
return false;
}else {
$light=Db::name('light')->where('id',$id)->find();
$client = fsockopen('114.215.223.17', 5002, $errno, $errmsg, 1);
$code=$light['addr'].'0300000010';
$crc=$this->crc16_modbus($code);
$queryCode=$code.substr($crc,2,2).substr($crc,0,2);
// var_dump($queryCode);
$data = array('uid' => $light['number'], 'percent' => "$queryCode");
// 发送数据,注意5678端口是Text协议的端口,Text协议需要在数据末尾加上换行符
fwrite($client, json_encode($data) . "\n");
// 读取推送结果
return fread($client, 8192);
}
}
public function lightChange($id=11){
//模拟控制
$status1='01';
$status2='01';
if ($id==""){
return false;
}else {
$light=Db::name('light')->where('id',$id)->find();
$client = fsockopen('114.215.223.17', 5002, $errno, $errmsg, 1);
$code=$light['addr'].'10000000102001'.$light['addr'].$status1.$status2.$this->strtoascii().'00';
$crc=$this->crc16_modbus($code);
$changeCode=$code.substr($crc,2,2).substr($crc,0,2);
// var_dump($queryCode);
$data = array('uid' => $light['number'], 'percent' => "$changeCode");
// 发送数据,注意5678端口是Text协议的端口,Text协议需要在数据末尾加上换行符
fwrite($client, json_encode($data) . "\n");
// 读取推送结果
return fread($client, 8192);
}
}
function crc16_modbus($msg)
{
$data = pack('H*',$msg);
$crc = 0xFFFF;
for ($i = 0; $i < strlen($data); $i++)
{
$crc ^=ord($data[$i]);
for ($j = 8; $j !=0; $j--)
{
if (($crc & 0x0001) !=0)
{
$crc >>= 1;
$crc ^= 0xA001;
}
else $crc >>= 1;
}
}
return sprintf('%04X', $crc);
}
public function asciitostr($sacii="c5cbbac6cec40000000000000000000000000000000000000000")
{
$asc_arr = str_split(strtolower($sacii), 2);
$str = '';
for($i=0;$i<count($asc_arr);$i=$i+2){
$str.=chr(hexdec($asc_arr[$i])).chr(hexdec($asc_arr[$i+1]));
}
$str=mb_convert_encoding($str,'UTF-8','GB2312');
dump($str);
}
public function strtoascii($str="潘浩文")
{
$str = mb_convert_encoding($str, 'GB2312');
$change_after = '';
for ($i = 0; $i < strlen($str); $i=$i+2) {
$change_after .= dechex(ord($str[$i])).dechex(ord($str[$i+1]));
}
for ($i=0;strlen($change_after)<52;$i++){
$change_after=$change_after.'00';
}
return strtoupper($change_after);
}
public function getCode()
{
$param=$this->request->param();
$param['mobile']='17694933850';
$clnt = YunpianClient::create('ae02d6c1c41748b663c3f31ba5a03da8');
$code = cmf_get_verification_code($param['mobile'], 4);
$content= '【十方台】您的验证码是'.$code.'。如非本人操作,请忽略本短信';
// ,YunpianClient::TPL_ID=>'2601908'
if ($code != false) {
$param = [YunpianClient::MOBILE => $param['mobile'], YunpianClient::TEXT => $content];
$clnt->tpl('2601908');
$r = $clnt->sms()->single_send($param);
cmf_verification_code_log($param['mobile'], $code, 120);
var_dump($r);
// if($r->isSucc()){
// $r->data();
// }
} else {
return 2222;
}
}
}