/** * 所有中间处理逻辑,生产本地ticket * @param $pageId //页面id号 * @param ShakeAroundClient $shakeAroundClient * @param SharkService $sharkService * @throws Exception */ public function redirectCallback(ShakeAroundClient $shakeAroundClient, SharkService $sharkService) { $requestData = $this->input; if (!array_key_exists('ticket', $requestData)) { throw new Exception("param miss ticket"); } if (!array_key_exists('guid', $requestData)) { throw new Exception("param miss guid"); } $objPage = WeixinPage::where('guid', $requestData['guid'])->first(); if (!$objPage) { return RestHelp::encodeResult(23001, sprintf(" guid %s not found", $requestData['guid'])); } $uuid = Uuid::v4(false); //set into redis try { $sharkService->setInfoInRedis($shakeAroundClient, $uuid, $requestData['ticket'], 0, $objPage->mp->appid, $objPage->mp->appsecret); } catch (Exception $e) { echo $e->getMessage() . $e->getCode(); exit; } try { $ret = $sharkService->setInfoInDB($shakeAroundClient, $requestData['ticket'], 0, $objPage->mp->appid, $objPage->mp->appsecret); } catch (Exception $e) { echo $e->getMessage() . $e->getCode(); exit; } $appid = $objPage->mp->appid; $appsecret = $objPage->mp->appsecret; $sharkService->saveUserInfo(new WeixinUserService(), $ret->openid, $appid, $appsecret, $objPage->mp->mp_id, $objPage->mp->id); //判断该设备有没有设置重定向,如果设置了,直接跳转 $wxDevice = WeixinDevice::where('uuid', $ret->uuid)->where('major', $ret->major)->where('minor', $ret->minor)->first(); if (!$wxDevice) { return "device not found"; exit; } $url = $wxDevice->redirect_url ? $wxDevice->redirect_url : $objPage->url; $url = preg_match("/\\?/", $url) ? $url . "&ticket=" . $uuid : $url . "?ticket=" . $uuid; //redirect return redirect($url); }
/** * 将微信返回的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); }
/** * 绑定页面 * @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); }
/** * 获取微信设备AJAX * @author Hanxiang */ public function wxdevicesAjax() { $input = Input::all(); $sn = $input['sn']; $device = Device::where('sn', $sn)->first(); if (!$device) { return response()->json(['wx_devices' => [], 'first' => new \stdClass()]); } $app_id = DeviceApp::where('device_id', $device->id)->lists('app_id'); $wx_mp_id = WeixinMp::where('app_id', $app_id)->lists('id'); $wxdevices = WeixinDevice::where('wx_mp_id', $wx_mp_id)->get(); if (count($wxdevices) > 0) { foreach ($wxdevices as $wx) { $wx->sn = Device::where("wx_device_id", $wx->id)->get(); } } $firstWxDevice = self::getFirstAvailableWxDevice(); return response()->json(['wx_devices' => $wxdevices->toArray(), 'first' => $firstWxDevice]); }
/** * * @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]; }