/** * @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(); } } }
/** * * @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 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(); } } }