/** * 设备列表 * 设备列表 * @param DeviceService $deviceService * @return $this * @author zhengqian@dajiayao.cc */ public function index(Request $request, DeviceService $deviceService) { $inputData = $request->all(); $arrMp = $this->mp; $wxDevices = WeixinDevice::whereIn('wx_mp_id', $arrMp); //过滤微信号 if (isset($inputData['wx_mp_id']) && $inputData['wx_mp_id'] != 0) { $wxDevices = $wxDevices->where('wx_mp_id', $inputData['wx_mp_id']); } //过滤绑定条件 if (isset($inputData['bind']) && $inputData['bind'] == 1) { $wxDevices = $wxDevices->whereIn('id', DB::table('device_page')->lists('wx_device_id')); } elseif (isset($inputData['bind']) && $inputData['bind'] == '-1') { $wxDevices = $wxDevices->whereNotIn('id', DB::table('device_page')->lists('wx_device_id')); } // //过滤sn // if(isset($inputData['sn']) && $inputData['sn'] != ''){ // $sn = DB::table('devices')->where('sn','like',"%".$inputData['sn']."%")->lists('wx_device_id'); // $wxDevices = $wxDevices->whereIn('id',$sn); // } // // //过滤minor // if(isset($inputData['minor']) && $inputData['minor'] != ''){ // $wxDevices = $wxDevices->where('minor',$inputData['minor']); // } if (isset($inputData['kw']) && $inputData['kw'] != '') { $kw = $inputData['kw']; $ids = DB::table('devices')->where('sn', 'like', "%" . $kw . "%")->lists('wx_device_id'); $wxDevices = $wxDevices->where(function ($query) use($kw, $ids) { $query->where('uuid', 'like', "%" . $kw . "%")->orwhere('device_id', 'like', "%" . $kw . "%")->orwhere('major', 'like', "%" . $kw . "%")->orwhere('minor', 'like', "%" . $kw . "%")->orwhere('comment', 'like', "%" . $kw . "%")->orwhereIn('id', $ids); }); } $wxDevices = $wxDevices->paginate(20); //取得是否烧号?烧号后显示出sn foreach ($wxDevices as $d) { $d->sn = Device::where('wx_device_id', $d->id)->get(); $d->devPage = DevicePage::where('wx_device_id', $d->id)->get()->toArray(); } return view('admin.wx_device.index')->with('wx_devices', $wxDevices)->with('wx_mp_id', isset($inputData['wx_mp_id']) ? $inputData['wx_mp_id'] : 0)->with('bind', isset($inputData['bind']) ? $inputData['bind'] : 0)->with('kw', isset($inputData['kw']) ? $inputData['kw'] : '')->with('mps', $arrMp); }
/** * 更新设备信息 * @param ShakeAroundClient $shakeAroundClient * @param DeviceService $deviceService * @param null $sn * @return string * @author zhengqian@dajiayao.cc */ public function updateComment(ShakeAroundClient $shakeAroundClient, DeviceService $deviceService, $sn = null) { if (!$sn) { return RestHelp::parametersIllegal("sn is required"); } $requestData = json_decode($this->inputData, true); if (!isset($requestData['comment'])) { return RestHelp::parametersIllegal("comment is required"); } $objDevice = Device::where('sn', $sn)->first(); if (!$objDevice) { return RestHelp::encodeResult(24001, sprintf("sn: %s not foun", $sn)); } try { $dev = new DeviceIdentifier($objDevice->weixinDevice->device_id, $objDevice->weixinDevice->uuid, $objDevice->weixinDevice->major, $objDevice->weixinDevice->minor); $deviceService->updateWeixinDevice($dev, $shakeAroundClient, $requestData['comment']); } catch (\Exception $e) { return RestHelp::encodeResult($e->getCode(), $e->getMessage()); } $objDevice->comment = $requestData['comment']; $objDevice->save(); return RestHelp::success(); }
/** * 绑定页面 * @author Hanxiang * @param $id * @return view */ public function bind($id) { $wxpage = WeixinPage::find($id); if (!$wxpage) { return redirect('/admin/wxpages')->with('result', false)->with('msg', "操作失败,页面不存在"); } $mp = $wxpage->mp; $wxDevices = WeixinDevice::where('wx_mp_id', $mp->id)->get(); foreach ($wxDevices as $wxdvc) { $count = DevicePage::where('wx_device_id', $wxdvc->id)->where('wx_page_id', $id)->count(); if ($count > 0) { $wxdvc->bind_status = 1; } else { $wxdvc->bind_status = 0; } $wxdvc->device = Device::where('wx_device_id', $wxdvc->id)->get(); } return view('admin.wxpages.bind')->with('id', $id)->with('wxdevices', $wxDevices); }
/** * @param PageService $pageService * @param ShakeAroundClient $shakeAroundClient * @param null $pageId * @return string */ public function bindDevice(PageService $pageService, ShakeAroundClient $shakeAroundClient, $pageId = null) { if (!$pageId) { return RestHelp::parametersIllegal("sn is required"); } $page = WeixinPage::find($pageId); if (!$page) { return RestHelp::encodeResult(23001, "page not found"); } $requestData = json_decode($this->inputData, true); $pageIds = $requestData['sn']; if (!$pageIds or !is_array($requestData['sn'])) { return RestHelp::encodeResult(24002, 'sn must be arrary'); } $validator = Validator::make($requestData, ['sn' => 'required', 'bind' => 'required|boolean', 'append' => 'required|boolean']); if ($validator->fails()) { return RestHelp::parametersIllegal($validator->messages()->first()); } foreach ($requestData['sn'] as $k => &$v) { $device = Device::where('sn', $v)->first(); if (!$device) { return RestHelp::encodeResult(24003, sprintf("sn %s not found", $v)); } $requestData['sn'][$k] = $device->wx_device_id; } try { $pageService->bindDevice($shakeAroundClient, $page, $requestData['sn'], $requestData['bind'], $requestData['append']); } catch (\Exception $e) { return RestHelp::encodeResult($e->getCode(), $e->getMessage()); } return RestHelp::success(); }
/** * 获取第一个可用的微信设备 * @author Hanxiang */ private static function getFirstAvailableWxDevice() { $wxdevices = WeixinDevice::all(); if (count($wxdevices) > 0) { foreach ($wxdevices as $wx) { $wx->sn = Device::where("wx_device_id", $wx->id)->get(); } } $usedWxDeviceIDs = Device::where("wx_device_id", ">", 0)->get(['wx_device_id']); $usedIDArray = []; if (count($usedWxDeviceIDs) > 0) { foreach ($usedWxDeviceIDs as $usedID) { array_push($usedIDArray, $usedID['wx_device_id']); } } $firstWxDevice = WeixinDevice::whereNotIn('id', $usedIDArray)->first(); if (count($firstWxDevice) > 0) { $firstWxDevice = $firstWxDevice->toArray(); } else { $firstWxDevice = []; } return $firstWxDevice; }
/** * 根据sn获取设备id * @param $sn * @author zhengqian@dajiayao.cc */ public function getDeviceBySn($sn) { $dev = Device::where('sn', $sn)->first(); if (!$dev) { throw new \Exception(24001, sprintf("the device sn: %s not found", $sn)); } return $dev; }