/** * 将微信返回的ticket,取得数据,然后本地解析 * @param ShakeAroundClient $shakeAroundClient * @param $ticket * @param int $needPoi * @return string * @throws \Exception */ public function parseRetInfo(ShakeAroundClient $shakeAroundClient, $ticket, $needPoi, $appid = null, $appsecret = null) { $accessToken = $this->getWeixinToken($appid, $appsecret); $ret = $shakeAroundClient->getShakeInfo($ticket, $needPoi, $accessToken); if ($ret->errcode != 0) { throw new \Exception("weixin error:" . $ret->errmsg, 90000); } $wxPageId = $ret->data->page_id; $page = WeixinPage::where('page_id', $wxPageId)->first(); if (!$page) { throw new \Exception(sprintf("page_id %s not found", $wxPageId), 23001); } $objRet = new \stdClass(); $objRet->page_id = $page->id; $objRet->wx_page_id = $wxPageId; $objRet->title = $page->title; $objRet->description = $page->description; $objRet->icon_url = $page->icon_url; $objRet->url = $page->url; $objRet->comment = $page->comment; if ($needPoi == 1) { $objRet->poi_id = $ret->data->poi_id; } else { $objRet->poi_id = 0; } $deviceUuid = $ret->data->beacon_info->uuid; $deviceMajor = $ret->data->beacon_info->major; $deviceMinor = $ret->data->beacon_info->minor; $device = WeixinDevice::where('uuid', $deviceUuid)->where('major', $deviceMajor)->where('minor', $deviceMinor)->first(); if (!$device) { throw new \Exception(sprintf("device Uuid: %s not found", $deviceUuid), 24001); } $objRet->wx_device_id = $device->device_id; $objRet->device_id = $device->id; $objRet->major = $ret->data->beacon_info->major; $objRet->minor = $ret->data->beacon_info->minor; $objRet->uuid = $ret->data->beacon_info->uuid; $objRet->openid = $ret->data->openid; $objRet->distance = $ret->data->beacon_info->distance; $this->openid = $objRet->openid; return json_encode($objRet); }
/** * @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(); } } }