DefaultController.php
6.87 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
<?php
namespace app\wx\modules\check\controllers;
use Yii;
use common\helpers\ImageManager;
use common\helpers\WxHelper;
use domain\smart\ScanRecords;
use domain\smart\SellerInputRecordRepository;
use stdClass;
/**
* 校验-控制器
*/
class DefaultController extends BaseController
{
/**
* 校验首页
*/
public function actionIndex()
{
return $this->render('index');
}
/**
* 查询是否激活 -(对应首页扫码验证接口)
* @return string
*/
public function actionCheckActive()
{
$e = new stdClass();
$e->success = 2;
$e->scan_count = 0;
$e->message = "";
$uuid = Yii::$app->request->post("uuid");
if (empty($uuid)) {
$e->message = '缺少必要参数!';
return $this->renderJson($e);
}
// @todo 校验UUID是否有效 2 无效 0 有效未激活 1 有效已激活
$hasRecord = SellerInputRecordRepository::findOne(["uuid" => $uuid]);
if (empty($hasRecord)) {
$e->success = 0;
$e->message = '暂无激活!';
return $this->renderJson($e);
} else {
// 记录扫码次数
$scan_count = ScanRecords::updateCount($uuid);
if ($scan_count) {
$e->scan_count = (int) $scan_count;
}
$e->success = 1;
$e->message = '已激活';
}
return $this->renderJson($e);
}
/**
* 上传照片检查商品二维码真伪 -(对应提交图片校验submit-controller.js中的提交接口)
* @return string
*/
public function actionCheckActived()
{
$e = new stdClass();
$e->success = false;
$e->scan_count = 0;
$e->message = "";
$uploadFile = Yii::$app->request->post("upload_file");
if (empty($uploadFile)) {
$e->message = '缺少必要参数!';
return $this->renderJson($e);
}
// 获取图片路径
//$uploadFile = ImageManager::getUrl($uploadFile);
// @todo 校验UUID是否有效 2 无效 0 有效未激活 1 有效已激活
$hasRecord = true;
if (!$hasRecord) {
$e->success = false;
$e->message = '暂无激活!';
return $this->renderJson($e);
} else {
$e->success = true;
$e->message = '已激活';
}
return $this->renderJson($e);
}
/**
* 上传微信JSAPI提交的校验图片 -(对应submit-controller.js页面图片上传接口)
* @return string
*/
public function actionUpdateServiceid()
{
$e = new stdClass();
$e->success = false;
$e->img_path = "";
$e->show_path = "";
$e->message = '';
$wechat = WxHelper::getWxPHPSDK();
$uploadFile = $this->request->post('service_id');
$uuid = $this->request->post('uuid');
if (empty($uploadFile)) {
$e->message = '请上传商品图片或视频!';
return $this->renderJson($e);
}
if (empty($uuid)) {
$e->message = '缺少必要参数!';
return $this->renderJson($e);
}
$filename = $this->saveWxImgToRemoveServer($wechat, $uploadFile, $uuid);
if (!$filename) {
$e->message = '保存失败!';
return $this->renderJson($e);
}
$e->show_path = ImageManager::getUrl($filename, ImageManager::$STYLE_180);
$e->img_path = $filename;
$e->success = true;
$e->message = '上传成功';
return $this->renderJson($e);
}
/**
* 保存微信图片到远程服务器
* @param $wechat 微信基础类
* @param string $tr 微信公众号的 media_id
* @return bool|string 返回保存到数据库里面的文件名称是的带URL的,同时会把图片传到阿里云的图片服务器上面
* $type 这个是类型,根据图片是头像还是身份证来存放,$stat_path 必须根据$type 而改变
*/
private function saveWxImgToRemoveServer($wechat, $tr = '', $uuid)
{
if (empty($tr)) {
return false;
}
$qrcodeArr = $wechat->getMedia($tr, 1);
if (empty($qrcodeArr['body'])) {
return false;
}
$filename_root = $_SERVER['DOCUMENT_ROOT'];
$path = $filename_root . '/tmp';
if (!is_dir($path)) {
@mkdir($path, 0777);
}
$dir = $path;
$tt = time();
$filename = 'smart_'.$tt.md5($tr).'.jpg';
$saveFilePath = $dir.'/'.$filename;
$local_file = fopen($saveFilePath, 'w');
if (false !== $local_file) {
if (false !== fwrite($local_file, $qrcodeArr['body'])) {
fclose($local_file);
}
}
$user_id = $userId = $this->getUserId();
$stat_path = ImageManager::getSmartCheckImgPath($user_id, $uuid);
ImageManager::add($saveFilePath, $stat_path);
// 删除服务器文件
@unlink($saveFilePath);
return $stat_path;
}
/**
* 获取消息 - (success-controller.js 中校验用到,info-controller.js 查询用到)
* @return string
*/
public function actionGetInfo()
{
$e = new stdClass();
$e->success = false;
$e->data = "";
$e->message = '';
$numberCode = $this->request->post('number_code');
$uuid = $this->request->post('uuid');
if (empty($numberCode) || empty($uuid)) {
$e->message = '缺少必要参数!';
return $this->renderJson($e);
}
$hasRecord = SellerInputRecordRepository::findOne(["uuid" => $uuid]);
if (empty($hasRecord)) {
$e->message = '该二维码暂未激活!';
return $this->renderJson($e);
}
if ($hasRecord->extraction_code != $numberCode) {
$e->message = '提取码错误!';
return $this->renderJson($e);
}
$imageData = [];
if ($hasRecord->product_image_path) {
$imageData = json_decode($hasRecord->product_image_path, true);
foreach ($imageData as $key => $imageItem) {
if (isset($imageItem["path"]) && !empty($imageItem["path"])) {
$imageData[$key]["path"] = ImageManager::getUrl($imageItem["path"]);
}
}
}
$hasRecord->product_image_path = $imageData;
$hasRecord->product_vedio_path = (!empty($hasRecord->product_vedio_path)) ? ImageManager::getUrl($hasRecord->product_vedio_path) : "";
$dataReturn = $hasRecord->toArray();
$dataReturn["first_product_image_path"] = !empty($imageData) && isset($imageData[0]["path"]) ? $imageData[0]["path"] : "";
$e->success = true;
$e->data = $dataReturn;
$e->message = '获取成功!';
return $this->renderJson($e);
}
}