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