/** * @设备绑定页面 * @param DeviceService $deviceService * @param ShakeAroundClient $shakeAroundClient * @param string $sn * @return string */ public function bindPage(DeviceService $deviceService, ShakeAroundClient $shakeAroundClient, $sn = '') { if (!$sn) { return RestHelp::parametersIllegal("sn is required"); } $device = Device::where("sn", $sn)->first(); if (!$device) { return RestHelp::encodeResult(24001, sprintf("sn %s not found", $sn)); } $requestData = json_decode($this->inputData, true); $pageIds = $requestData['page_ids']; if (!$pageIds or !is_array($requestData['page_ids'])) { return RestHelp::encodeResult(24002, 'page_ids must be arrary'); } foreach ($pageIds as $pid) { if (!WeixinPage::find($pid)) { return RestHelp::encodeResult(23001, sprintf("page id :%s not found", $pid)); } } $validator = Validator::make($requestData, ['page_ids' => 'required', 'bind' => 'required|boolean', 'append' => 'required|boolean']); if ($validator->fails()) { return RestHelp::parametersIllegal($validator->messages()->first()); } try { $deviceService->bindPage($shakeAroundClient, WeixinDevice::find($device->wx_device_id), $requestData['page_ids'], $requestData['bind'], $requestData['append']); } catch (\Exception $e) { return RestHelp::encodeResult($e->getCode(), $e->getMessage()); } return RestHelp::success(); }
/** 基于设备的数据统计 * @param ShakeAroundClient $shakeAroundClient * @param $deviceId * @param $beginDate * @param $endDate * @param null $appid * @param null $appsecret * @return mixed * @throws \Exception * @author zhengqian@dajiayao.cc */ public function deviceStatistic($deviceId, $beginDate, $endDate, $appid = null, $appsecret = null) { $wxDevice = WeixinDevice::find($deviceId); if (!$wxDevice) { throw new \Exception(sprintf("device Id: %s not found in db", $deviceId), 24001); } $deviceIdentifier = new DeviceIdentifier($wxDevice->device_id, $wxDevice->uuid, $wxDevice->major, $wxDevice->minor); $token = $this->getWeixinToken($appid, $appsecret); $ret = $this->sharkroundClient->statisticsDevice($deviceIdentifier, intval($beginDate), intval($endDate), $token); if ($ret->errcode != 0) { throw new \Exception('weixin error:' . $ret->errmsg, $ret->errcode); } return $ret->data; }
/** * 修改备注 * @param ShakeAroundClient $shakeAroundClient * @param DeviceService $deviceService * @return \Illuminate\Http\RedirectResponse * @author zhengqian@dajiayao.cc */ public function update(ShakeAroundClient $shakeAroundClient, DeviceService $deviceService) { $deviceId = Input::get('device_id'); $objDevice = WeixinDevice::find($deviceId); $comment = Input::get('comment'); if (!$comment) { return redirect(route('adminGetUpdate'))->with('result', false)->with('msg', "备注不能为空"); } $device = new DeviceIdentifier($objDevice->device_id, $objDevice->uuid, $objDevice->major, $objDevice->minor); try { $deviceService->updateWeixinDevice($device, $shakeAroundClient, $comment, $objDevice->mp->appid, $objDevice->mp->appsecret); } catch (\Exception $e) { return redirect(route('adminWxDevicesIndex'))->with('result', false)->with('msg', $e->getMessage()); } $objDevice->comment = $comment; $objDevice->save(); return redirect(route('adminWxDevicesIndex'))->with('result', true)->with('msg', '操作成功'); }
/** * 设备烧号 * @author Hanxiang */ public function burnin() { $input = Input::all(); if (!isset($input['uuid'])) { return response()->json(['result' => false]); } $device = Device::find($input['id']); if (!$device) { return response()->json(['result' => false]); } $wxDevice = WeixinDevice::find($input['wx_device_id']); if (!$wxDevice) { return response()->json(['result' => false]); } $device->model_id = $input['model_id']; $device->manufacturer_sn = $input['man_sn']; $device->uuid = $input['uuid']; $device->major = $input['major']; $device->minor = $input['minor']; $device->wx_device_id = $input['wx_device_id']; $device->password = $input['password']; $device->power_outage_date = self::convtDate($input['battery_expire']); // $device->status = 2; $device->comment = $wxDevice->comment; $device->save(); return response()->json(['result' => true]); }
/** * @param ShakeAroundClient $shakeAroundClient * @param WeixinPage $weixinPage,本地数据库页面实体 * @param array $wx_device_ids ,本地数据库库主键设备id * @param int $bind * @param int $append * @throws \Exception */ public function bindDevice(ShakeAroundClient $shakeAroundClient, WeixinPage $weixinPage, array $wx_device_ids, $bind = 1, $append = 1, $appid = null, $appsecret = null) { //TODO 检查 wx_mp_id 是否设备-页面一一致 $token = $this->getWeixinToken($appid, $appsecret); foreach ($wx_device_ids as $id) { $wx_device = WeixinDevice::find($id); if (!$wx_device) { throw new \Exception(sprintf("device id %s not found", $id), 24001); } $ret = $shakeAroundClient->bindPage(new DeviceIdentifier($wx_device->device_id, $wx_device->uuid, $wx_device->major, $wx_device->minor), [$weixinPage->page_id], (int) $bind, (int) $append, $token); if ($ret->errcode != 0) { throw new \Exception('weixin error' . $ret->errmsg, 90000); } } //本地数据库记录 if ($append == 0) { DevicePage::where('wx_page_id', $weixinPage->id)->delete(); } foreach ($wx_device_ids as $id) { if ($bind == 1) { $devicePage = new DevicePage(); $devicePage->wx_device_id = $id; $devicePage->wx_page_id = $weixinPage->id; $devicePage->save(); } elseif ($bind == 0) { DevicePage::where('wx_device_id', $id)->where('wx_page_id', $weixinPage->id)->delete(); } } }