DeviceRepository.php
4.73 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
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/1/26
* Time: 16:39
*/
namespace App\Repositories\Backend\Device;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Modules\Repositories\Device\BaseDeviceRepository;
use App\Events\Backend\Access\Device\DeviceCreated;
class DeviceRepository extends BaseDeviceRepository
{
private $apply_id;
public function getForDataTable()
{
return $this->query()->get();
}
public function select($device)
{
$wx_config = config('wechat.official_account.default');
$app = Factory::officialAccount($wx_config);
$shakearound = $app->shake_around;
$apply_id = $device->apply_id;
$result = $shakearound->device->status((int)$apply_id);
if (!$result) {
throw new GeneralException(trans('exceptions.backend.device.wx_beacon_info'));
die();
}
return $result;
}
public function create($input)
{
$info = $input['data'];
//调用shake_around 类
$wx_config = config('wechat.official_account.default');
$app = Factory::officialAccount($wx_config);
$shakearound = $app->shake_around;
//获取access_token
$accessToken = $app->access_token;
$token = $accessToken->getToken(); // token 字符串
$data = [
'access_token' => $token,
'quantity' => (int)$info['num'],
'apply_reason' => $info['remark'],
];
//发起页面申请
$result = $shakearound->device->apply($data);
if (!$result) {
throw new GeneralException(trans('exceptions.backend.device.wx_beacon_error'));
}
$apply_id = $result['data']['apply_id'];
$this->apply_id = (int)$apply_id;
$device = $this->createUserStub($info);
DB::transaction(function () use ($device, $info) {
if ($device->save()) {
//Attach new roles
event(new DeviceCreated($device));
return true;
}
throw new GeneralException(trans('exceptions.backend.device.create_error'));
});
//这里是微信查beacon插入数据库
$b = 0;
for ($i = 0; $i < 8; $i++) {
$res = $shakearound->device->listByApplyId( $this->apply_id, $b, 50);
$rows = [];
if ($res) {
foreach ($res['data']['devices'] as $dev) {
$row = [];
$row['apply_id'] = (int)$this->apply_id;
$row['device_id'] = $dev['device_id'];
$row['major'] = $dev['major'];
$row['minor'] = $dev['minor'];
$row['rssi'] = (int)75;
$row['sd'] = (int)5;
$row['status'] = (int)0;
$rows[] = $row;
$b = (int)$dev['device_id'];
}
$this->CreateBeacon($rows);
}
}
}
public function CreateBeacon($data)
{
$data = DB::table('beacon')->insert($data);
if (!$data) {
throw new GeneralException(trans('exceptions.backend.device.wx_beacon_insert'));
return false;
}
return true;
}
public function update(Business $business, $input)
{
DB::transaction(function () use ($business, $input) {
if ($business->update($input)) {
event(new BusinessUpdated($business));
return true;
}
throw new GeneralException(trans('exceptions.backend.business.update_error'));
});
}
public function delete(Spot $spot, $input)
{
DB::transaction(function () use ($spot, $input) {
if ($spot->delete($input)) {
event(new SpotDeleted($spot));
return true;
}
throw new GeneralException(trans('exceptions.backend.business.update_error'));
});
}
protected function createUserStub($input)
{
$device = self::MODEL;
$device = new $device;
$device->remark = $input['remark'];
$device->num = $input['num'];
$device->apply_id = $this->apply_id;
return $device;
}
/**
* @param $input
* @param $user
*
* @throws GeneralException
*/
protected function checkUserByUsername($input, $business)
{
if ($business->business_name != $input['business_name']) {
if ($this->query()->where('business_name', '=', $input['business_name'])->first()) {
throw new GeneralException(trans('exceptions.backend.business.business_name_error'));
}
}
}
}