/** * @设备绑定页面 * @param DeviceService $deviceService * @param ShakeAroundClient $shakeAroundClient * @param string $sn * @return string */ public function bindPage(DeviceService $deviceService, ShakeAroundClient $shakeAroundClient, $sn = '') { if (!$sn) { return RestHelp::parametersIllegal("sn is required"); } $device = Device::where("sn", $sn)->first(); if (!$device) { return RestHelp::encodeResult(24001, sprintf("sn %s not found", $sn)); } $requestData = json_decode($this->inputData, true); $pageIds = $requestData['page_ids']; if (!$pageIds or !is_array($requestData['page_ids'])) { return RestHelp::encodeResult(24002, 'page_ids must be arrary'); } foreach ($pageIds as $pid) { if (!WeixinPage::find($pid)) { return RestHelp::encodeResult(23001, sprintf("page id :%s not found", $pid)); } } $validator = Validator::make($requestData, ['page_ids' => 'required', 'bind' => 'required|boolean', 'append' => 'required|boolean']); if ($validator->fails()) { return RestHelp::parametersIllegal($validator->messages()->first()); } try { $deviceService->bindPage($shakeAroundClient, WeixinDevice::find($device->wx_device_id), $requestData['page_ids'], $requestData['bind'], $requestData['append']); } catch (\Exception $e) { return RestHelp::encodeResult($e->getCode(), $e->getMessage()); } return RestHelp::success(); }
/** 基于页面的数据统计 * @param ShakeAroundClient $shakeAroundClient * @param $pageId * @param $beginDate * @param $endDate * @param null $appid * @param null $appsecret * @return mixed * @throws \Exception * @author zhengqian@dajiayao.cc */ public function pageStatistic($pageId, $beginDate, $endDate, $appid = null, $appsecret = null) { $wxPage = WeixinPage::find($pageId); if (!$wxPage) { throw new \Exception(sprintf("page id : %s not found in db", $pageId), 23001); } $token = $this->getWeixinToken($appid, $appsecret); $ret = $this->sharkroundClient->statisticsPage($wxPage->page_id, intval($beginDate), intval($endDate), $token); if ($ret->errcode != 0) { throw new \Exception('weixin error:' . $ret->errmsg, $ret->errcode); } return $ret->data; }
/** * 绑定页面POST * @author Hanxiang * @param PageService $pageService * @param ShakeAroundClient $shakeAroundClient * @return json */ public function bindPost(PageService $pageService, ShakeAroundClient $shakeAroundClient) { $input = Input::all(); $validator = Validator::make($input, ['id' => 'required', 'wx_device_ids' => 'required']); if ($validator->fails()) { return response()->json(['result' => 0, 'msg' => '参数错误']); } if (!is_array($input['wx_device_ids'])) { return response()->json(['result' => 0, 'msg' => '参数错误']); } $wxpage = WeixinPage::find($input['id']); if (!$wxpage) { return response()->json(['result' => 0, 'msg' => '微信页面不存在']); } $wxMp = WeixinMp::find($wxpage->wx_mp_id); $appid = $wxMp->appid; $appsecret = $wxMp->appsecret; try { $pageService->bindDevice($shakeAroundClient, $wxpage, $input['wx_device_ids'], 1, 1, $appid, $appsecret); } catch (\Exception $e) { return response()->json(['result' => 0, 'msg' => $e->getMessage()]); } Session::flash('result', true); Session::flash('msg', "操作成功"); return response()->json(['result' => 1, 'msg' => '操作成功']); }
/** * 绑定页面 * @param DeviceService $deviceService * @param $wxDeviceId * @param $pageIds * @param int $flage * @author zhengqian@dajiayao.cc */ public function bindPages(ShakeAroundClient $shakeAroundClient, DeviceService $deviceService) { $inputData = Input::all(); $wxDeviceId = $inputData['device_id']; $objDev = WeixinDevice::find($wxDeviceId); if (!$objDev) { return redirect(route('adminGetBindPage', [$wxDeviceId]))->with('result', false)->with('msg', "设备id 不存在"); } $pageIds = isset($inputData['page_id']) ? $inputData['page_id'] : []; foreach ($pageIds as $id) { $objPage = WeixinPage::find($id); if (!$objPage) { return redirect(route('adminGetBindPage', [$wxDeviceId]))->with('result', false)->with('msg', sprintf("页面id:%s 不存在", $id)); } } //如果空page_ids,则解绑所有页面 if (empty($pageIds)) { $dp = DB::table('device_page')->where('wx_device_id', $wxDeviceId)->lists('wx_page_id'); try { $deviceService->bindPage($shakeAroundClient, $objDev, $dp, 0, 0, $objDev->mp->appid, $objDev->mp->appsecret); } catch (\Exception $e) { echo $e->getMessage(); exit; } return redirect(route('adminGetBindPage', [$wxDeviceId]))->with('result', true)->with('msg', "操作成功"); } try { $deviceService->bindPage($shakeAroundClient, $objDev, $pageIds, 1, 0, $objDev->mp->appid, $objDev->mp->appsecret); } catch (\Exception $e) { echo $e->getMessage(); exit; } return redirect(route('adminGetBindPage', [$wxDeviceId]))->with('result', true)->with('msg', "操作成功"); }
/** * 页面具体信息 * @param $pageId * @return string * @author zhengqian@dajiayao.cc */ public function getInfo($pageId) { if (!$pageId) { return RestHelp::parametersIllegal("page id is required"); } $objPage = WeixinPage::find($pageId); if (!$objPage) { return RestHelp::encodeResult(23001, sprintf("page id is %s not found", $pageId)); } $retPage = ['title' => $objPage->title, 'description' => $objPage->description, 'icon_url' => $objPage->icon_url, 'url' => $objPage->url, 'comment' => $objPage->comment]; $retDevice = array(); $devicePage = DevicePage::where('wx_page_id', $pageId)->get(); foreach ($devicePage as $dp) { $device = $dp->device->weixinDevice; foreach ($device as $d) { array_push($retDevice, ['sn' => $d->sn, 'device_id' => $d->wx_device_id, 'comment' => $d->comment, 'status' => $d->status]); } } return RestHelp::success(['page' => $retPage, 'device' => $retDevice]); }
/** * @param ShakeAroundClient $shakeAroundClient * @param array $ids,本地主键id * @throws \Exception */ public function deletePageOnline(ShakeAroundClient $shakeAroundClient, array $ids, $appid = null, $appsecret = null) { foreach ($ids as $v) { if (!WeixinPage::find($v)) { throw new \Exception(sprintf('page id %s not found', $v), 23001); } } $accessToken = $this->getWeixinToken($appid, $appsecret); if ($accessToken == NULL) { throw new \Exception("weixin get token error", 90000); } foreach ($ids as $k => &$v) { $ids[$k] = WeixinPage::find($v)->page_id; } $ret = $shakeAroundClient->deletePageByIds(['page_ids' => $ids], $accessToken); if ($ret->errcode != 0) { throw new \Exception('weixin error' . $ret->errmsg, 90000); } }
/** * @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(); } } }