/** * 将微信返回的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 */ public function index() { $wx_mps = self::getCurrentWxMps(); $input = Input::all(); if (isset($input['wx_mp_id']) && $input['wx_mp_id'] > 0) { $wx_mp_id = $input['wx_mp_id']; $wxpages = WeixinPage::where('wx_mp_id', $input['wx_mp_id']); //->get(); } else { $wx_mp_id = null; $wxpages = WeixinPage::where('id', '>', 0); } $user = Auth::user(); if ($user->role != User::ROLE_ADMIN) { $wxMpIDs = []; $apps = App::where('user_id', $user->id)->get(); foreach ($apps as $a) { $wxMps = $a->wxMp; foreach ($wxMps as $mp) { array_push($wxMpIDs, $mp->id); } } $wxpages = $wxpages->whereIn('wx_mp_id', $wxMpIDs); } if (isset($input['kw']) && $input['kw'] != '') { $kw = $input['kw']; $wxpages = $wxpages->where(function ($query) use($kw) { $query->where('title', 'like', "%" . $kw . "%")->orwhere('description', 'like', "%" . $kw . "%")->orwhere('comment', 'like', "%" . $kw . "%"); }); } $wxpages = $wxpages->orderBy('updated_at', 'desc')->get(); foreach ($wxpages as $wxpage) { $device_page_count = DevicePage::where('wx_page_id', $wxpage->id)->count(); if ($device_page_count > 0) { $wxpage->bind_dvc_sts = 1; } else { $wxpage->bind_dvc_sts = 0; } } return view('admin.wxpages.index')->with('wxpages', $wxpages)->with('wxmps', $wx_mps)->with('kw', isset($input['kw']) ? $input['kw'] : '')->with('wx_mp_id', $wx_mp_id); }
/** * 所有中间处理逻辑,生产本地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); }
/** * @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); }
/** * * @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]; }