SpotRepository.php
10.9 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/1/17
* Time: 11:06
*/
namespace App\Repositories\Backend\Spot;
use EasyWeChat\Factory;
use App\Modules\Models\Spot\Spot;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Events\Backend\Access\Spot\SpotCreated;
use App\Events\Backend\Access\Spot\SpotDeleted;
use App\Events\Backend\Access\Spot\SpotUpdated;
use App\Modules\Repositories\Spot\BaseSpotRepository;
use Illuminate\Support\Facades\Log;
use Intervention\Image\ImageManagerStatic as Image;
class SpotRepository extends BaseSpotRepository
{
private $img_url;
private $img_page;
private $img_code;
private $img_pageupdate;
const MODEL = Spot::class;
public function getForDataTable()
{
return $this->query()->get();
}
public function getinfo(){
return $this->query()->select('spotname', 'listen_img','detail_img','business_name','spot.id','go_url','wx_logo','vic_title','main_title')->leftjoin('business','spot.business_id','=','business.id')->get();
}
public function create($input)
{
$this->img($input);
//微信图片不是放在data里面的
$key =array_keys($input['data']);
$res=in_array('detail_img',$key);
$res1=in_array('listen_img',$key);
if($res){
$backet="detail_img";
} else{
throw new GeneralException(trans('alerts.backend.finance.backet_error'));
die();
}
if($res1){
$backet1="listen_img";
} else{
throw new GeneralException(trans('alerts.backend.finance.backet_error'));
die();
}
$img=$this->imgup($backet);
$img1=$this->imgup($backet1);
$data = $input;
$data['data']['detail_img'] = $img['path'];
$data['data']['listen_img'] = $img1['path'];
$spot = $this->createUserStub($data);
DB::transaction(function () use ($spot, $data) {
if ($spot->save()) {
//Attach new roles
event(new SpotCreated($spot));
return true;
}
throw new GeneralException(trans('exceptions.backend.spot.create_error'));
});
}
public function img($input){
//图片上传
$allow_file_type =['jpg','png','gif','jpeg'];
if(!$_FILES||!$_FILES['wx_logo']){
throw new GeneralException(trans('exceptions.backend.spot.create_error')); //请选上传文件
}
$file_name = $_FILES['wx_logo']['name'];
$tmp_file_extend = explode('.',$file_name);
$file_type = strtolower(end($tmp_file_extend));
if(!in_array(strtolower(end($tmp_file_extend)),$allow_file_type)) {
throw new GeneralException(trans('exceptions.backend.spot.type_error')); // '请选择指定上传的图片类型'
}
$img = Image::make($_FILES['wx_logo']['tmp_name']); // Image::make() 支持这种方式
$img->fit(200, 200);
$name = public_path().'/img/spot/';
$folder_name = uniqid();
$upload_dir = $name;
Log::info('directory is '.$upload_dir);
if(!file_exists($upload_dir)){
Log::info('directory is noExists : '.$upload_dir);
mkdir($upload_dir,0777);
chmod($upload_dir,0777);
} ;
$pic=$upload_dir.$folder_name.".".$file_type;
$img->save($pic);
//todo 以后 要清除数据本地
$wx_config=config('wechat.official_account.default');
$app = Factory::officialAccount($wx_config);
$shakearound = $app->shake_around;
//上传微信
$result = $shakearound->material->uploadImage($pic);
if(!$result){
throw new GeneralException(trans('exceptions.backend.spot.wx_error'));
}
//获取微信的图片
$pic_url = $result['data']['pic_url'];
$this->img_url=$pic_url;
$code =uniqid();
$this->img_code =$code;
//微信上传素材
$page_data =[
'title'=>$input['data']['main_title'],
'description'=>$input['data']['vic_title'],
'icon_url'=> $pic_url,
'comment'=>$input['data']['spotname'],
'page_url' => 'http://www.playxx.cn/mpview' . '?code=' . $code,
//注意这是正式服,测试服没有写
];
$result_pic = $shakearound->page->create($page_data);
if(!$result_pic){
throw new GeneralException(trans('exceptions.backend.spot.wxpic_error'));
}
$page_id =$result_pic['data']['page_id'];
$this->img_page=$page_id;
}
//修改图片
public function updateimg($input){
if(isset($input['wx_logo'])){
$allow_file_type =['jpg','png','gif','jpeg'];
$file_name = $_FILES['wx_logo']['name'];
$tmp_file_extend = explode('.',$file_name);
$file_type = strtolower(end($tmp_file_extend));
if(!in_array(strtolower(end($tmp_file_extend)),$allow_file_type)) {
throw new GeneralException(trans('exceptions.backend.spot.type_error')); // '请选择指定上传的图片类型'
}
$img = Image::make($_FILES['wx_logo']['tmp_name']); // Image::make() 支持这种方式
$img->fit(200, 200);
$name = public_path().'\\img\\spot\\';
$folder_name = uniqid();
$upload_dir = $name;
if(!file_exists($upload_dir)){
mkdir($upload_dir,0777);
chmod($upload_dir,0777);
} ;
$pic=$upload_dir.$folder_name.".".$file_type;
$img->save($pic);
$wx_config=config('wechat.official_account.default');
$app = Factory::officialAccount($wx_config);
$shakearound = $app->shake_around;
//上传微信
$result = $shakearound->material->uploadImage($pic);
// var_dump($result);exit();
if(!$result){
throw new GeneralException(trans('exceptions.backend.spot.wx_error'));
}
$pic_url = $result['data']['pic_url'];
$this->img_pageupdate =$pic_url;
$page_Id=$input['page_id'];
$data=[
'title' =>$input['main_title'],
'description'=>$input['vic_title'],
'page_url'=>'http://www.playxx.cn/mpview'.'?code='.$input['code'],
'comment'=>'修改',
'icon_url'=>$pic_url
];
//调用 微信接口
$resultupdate= $shakearound->page->update( $page_Id, $data);
if(!$resultupdate){
throw new GeneralException(trans('exceptions.backend.spot.wxupdate_error'));
return false;
}
}else{
$wx_config=config('wechat.official_account.default');
$app = Factory::officialAccount($wx_config);
$shakearound = $app->shake_around;
$page_Id=$input['page_id'];
$data=[
'title' =>$input['main_title'],
'description'=>$input['vic_title'],
'page_url'=>'http://www.playxx.cn/mpview' . '?code=' . $input['code'],
'comment'=>'修改',
'icon_url'=>$input['wx_logo'],
];
//调用 微信接口
$resultupdate= $shakearound->page->update( $page_Id, $data);
if(!$resultupdate){
throw new GeneralException(trans('exceptions.backend.spot.wxupdate_error'));
return false;
}
$this->img_pageupdate=$input['wx_logo'];
}
}
public function update(Spot $spot, $input)
{
// echo 'update.wx_logo';
// var_dump($input);
// echo '------------------'.'</br>';
// exit();
if(isset($input['wx_logo'])){
$this->updateimg($input);
$input['wx_logo']= $this->img_pageupdate;
}
if(isset($input['detail_img'])){
$key =array_keys($input);
$res=in_array('detail_img',$key);
if($res){
$backet="detail_img";
$img=$this->imgup($backet);
$input['detail_img'] = $img['path'];
} else{
throw new GeneralException(trans('alerts.backend.finance.backet_error'));
die();
}
}
if(isset($input['listen_img'])){
$key1 =array_keys($input);
$res1=in_array('listen_img',$key1);
if($res1){
$backet1="listen_img";
$img1=$this->imgup($backet1);
$input['listen_img'] = $img1['path'];
} else{
throw new GeneralException(trans('alerts.backend.finance.backet_error'));
die();
}
}
DB::transaction(function () use ($spot, $input) {
if ($spot->update($input)) {
event(new SpotUpdated($spot));
return true;
}
throw new GeneralException(trans('exceptions.backend.spot.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)
{
$over = $input['data']['time'].":00";
$overtime = $input['data']['overtime'].":00";
$spot = self::MODEL;
$spot = new $spot;
$spot->spotname = $input['data']['spotname'];
$spot->business_id = $input['data']['business_id'];
$spot->main_title =$input['data']['main_title'];
$spot->vic_title =$input['data']['vic_title'];
$spot->go_url =$input['data']['go_url'];
$spot->detail_img =$input['data']['detail_img'];
$spot->listen_img =$input['data']['listen_img'];
$spot->address =$input['data']['address'];
$spot->wx_logo=$this->img_url;
$spot->page_id=$this->img_page;
$spot->code=$this->img_code;
$spot->time= $over;
$spot->overtime = $overtime ;
return $spot;
}
/**
* @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'));
}
}
}
}