/** * 获取绑定的设备数量 * @return mixed */ public function getDeviceCount() { return DevicePage::where('wx_page_id', $this->id)->count(); }
/** * 绑定页面 * @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 $deviceId * @author zhengqian@dajiayao.cc */ public function getInfo($wxDeviceId) { if (!$wxDeviceId) { return RestHelp::parametersIllegal("device id is required"); } $devices = Device::where('wx_device_id', $wxDeviceId)->get(); if (!$devices) { return RestHelp::encodeResult(24001, sprintf("device id: %s not foun", $wxDeviceId)); } $arrDevice = array(); foreach ($devices as $v) { array_push($arrDevice, ['sn' => $v->sn, 'comment' => $v->comment, 'status' => $v->status]); } unset($v); $arrPage = array(); $devicePage = DevicePage::where('wx_device_id', $wxDeviceId)->get(); foreach ($devicePage as $v) { $page = $v->page; if (!$page) { return RestHelp::encodeResult(23001, sprintf("page id: %s not found", $v->page_id)); } array_push($arrPage, ['page_id' => $page->id, 'title' => $page->title, 'description' => $page->description, 'icon_url' => $page->icon_url, 'url' => $page->url, 'comment' => $page->comment]); } return RestHelp::success(['device' => $arrDevice, 'page' => $arrPage]); }
/** * @param null $wxDeviceId * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View * @author zhengqian@dajiayao.cc */ public function pageRelation($wxDeviceId = null) { if (!$wxDeviceId) { return redirect(route('adminWxDevicesIndex'))->with('result', false)->with('msg', "id 不存在"); } // $arrMp = $this->mp; $pageIds = DB::table('device_page')->where("wx_device_id", $wxDeviceId)->lists('wx_page_id'); $mp = WeixinDevice::find($wxDeviceId)->mp; $pages = WeixinPage::where('wx_mp_id', $mp->id)->whereNotIn('id', $pageIds)->get(); $bindPage = DevicePage::where('wx_device_id', $wxDeviceId)->get(); return view('admin.wx_device.relation_page')->with('bind_pages', $bindPage)->with('pages', $pages)->with('device_id', $wxDeviceId); }
/** * 页面具体信息 * @param $pageId * @return string * @author zhengqian@dajiayao.cc */ public function getInfo($pageId) { if (!$pageId) { return RestHelp::parametersIllegal("page id is required"); } $objPage = WeixinPage::find($pageId); if (!$objPage) { return RestHelp::encodeResult(23001, sprintf("page id is %s not found", $pageId)); } $retPage = ['title' => $objPage->title, 'description' => $objPage->description, 'icon_url' => $objPage->icon_url, 'url' => $objPage->url, 'comment' => $objPage->comment]; $retDevice = array(); $devicePage = DevicePage::where('wx_page_id', $pageId)->get(); foreach ($devicePage as $dp) { $device = $dp->device->weixinDevice; foreach ($device as $d) { array_push($retDevice, ['sn' => $d->sn, 'device_id' => $d->wx_device_id, 'comment' => $d->comment, 'status' => $d->status]); } } return RestHelp::success(['page' => $retPage, 'device' => $retDevice]); }
/** * * @author zhengqian@dajiayao.cc */ public function sync($appid, $appsecret) { $mp_id = WeixinMp::where('appid', $appid)->where('appsecret', $appsecret)->first()->id; $bid = 0; $countPage = 0; //定义缓存数组 $cacheArrayPage = []; while (true) { $ret = $this->syncPage($appid, $appsecret, $bid, 20); $pages = $ret->pages; $bid = $bid + count($pages); if (!$pages) { break; } //开始同步页面 foreach ($pages as $page) { $objPage = WeixinPage::where('page_id', $page->page_id)->first(); if (!$objPage) { $objPage = new WeixinPage(); $objPage->guid = Uuid::v4(false); $objPage->title = $page->title; $objPage->description = $page->description; $objPage->icon_url = $page->icon_url; $objPage->url = $page->page_url; $objPage->comment = $page->comment; $objPage->page_id = $page->page_id; $objPage->wx_mp_id = $mp_id; $objPage->save(); $countPage++; } //生产缓存数组 if (!array_key_exists($page->page_id, $cacheArrayPage)) { $cacheArrayPage[$page->page_id] = $objPage->id; } } } unset($ret); $countDevice = 0; $bid = 0; while (True) { $ret = $this->syncDevice($appid, $appsecret, $bid); $devices = $ret->devices; $bid = $bid + count($devices); if (!$devices) { break; } //开始同步设备 foreach ($devices as $device) { $objWxDevice = WeixinDevice::where('device_id', $device->device_id)->first(); if (!$objWxDevice) { $objWxDevice = new WeixinDevice(); $objWxDevice->uuid = $device->uuid; $objWxDevice->major = $device->major; $objWxDevice->minor = $device->minor; $objWxDevice->comment = $device->comment; $objWxDevice->poi_id = $device->poi_id; $objWxDevice->wx_mp_id = $mp_id; $objWxDevice->device_id = $device->device_id; $objWxDevice->apply_id = $device->device_id; $countDevice++; } $objWxDevice->status = $device->status; $objWxDevice->save(); //处理页面-设备关系 if (!empty($device->page_ids)) { $arrPageIds = explode(',', $device->page_ids); foreach ($arrPageIds as $pageId) { $devicePage = DevicePage::where('wx_device_id', $objWxDevice->id)->where('wx_page_id', $cacheArrayPage[$pageId])->first(); if (!$devicePage) { $devicePage = new DevicePage(); $devicePage->wx_device_id = $objWxDevice->id; $devicePage->wx_page_id = $cacheArrayPage[$pageId]; $devicePage->save(); } } } } } return ['count_page' => $countPage, 'count_device' => $countDevice]; }
/** * @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(); } } }
/** * @param ShakeAroundClient $shakeAroundClient * @param Device $device ,自己构建实体 * @param array $page_ids ,要绑定的页面id,主键id * @param int $bind * @param int $append * @throws \Exception */ public function bindPage(ShakeAroundClient $shakeAroundClient, WeixinDevice $device, array $page_ids, $bind = 1, $append = 1, $appid = null, $appsecret = null) { //TODO 检查 wx_mp_id 是否设备-页面一一致 $token = $this->getWeixinToken($appid, $appsecret); if ($token == NULL) { throw new \Exception("weixin get token error", 90000); } //page_ids转变为wx_page_ids foreach ($page_ids as $k => &$v) { $wx_page_ids[$k] = WeixinPage::find($v)->page_id; } $ret = $shakeAroundClient->bindPage(new DeviceIdentifier($device->device_id, $device->uuid, $device->major, $device->minor), $wx_page_ids, (int) $bind, (int) $append, $token); if ($ret->errcode != 0) { throw new \Exception('weixin error:' . $ret->errmsg, 90000); } //本地数据库记录 if ($append == 0) { DevicePage::where('wx_device_id', $device->id)->delete(); } foreach ($page_ids as $id) { if ($bind == 1) { $devicePage = new DevicePage(); $devicePage->wx_device_id = $device->id; $devicePage->wx_page_id = $id; $devicePage->save(); } elseif ($bind == 0) { DevicePage::where('wx_device_id', $device->id)->where('wx_page_id', $id)->delete(); } } }