/** * 绑定页面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 PageService $pageService * @param ShakeAroundClient $shakeAroundClient * @param null $pageId * @return string */ public function bindDevice(PageService $pageService, ShakeAroundClient $shakeAroundClient, $pageId = null) { if (!$pageId) { return RestHelp::parametersIllegal("sn is required"); } $page = WeixinPage::find($pageId); if (!$page) { return RestHelp::encodeResult(23001, "page not found"); } $requestData = json_decode($this->inputData, true); $pageIds = $requestData['sn']; if (!$pageIds or !is_array($requestData['sn'])) { return RestHelp::encodeResult(24002, 'sn must be arrary'); } $validator = Validator::make($requestData, ['sn' => 'required', 'bind' => 'required|boolean', 'append' => 'required|boolean']); if ($validator->fails()) { return RestHelp::parametersIllegal($validator->messages()->first()); } foreach ($requestData['sn'] as $k => &$v) { $device = Device::where('sn', $v)->first(); if (!$device) { return RestHelp::encodeResult(24003, sprintf("sn %s not found", $v)); } $requestData['sn'][$k] = $device->wx_device_id; } try { $pageService->bindDevice($shakeAroundClient, $page, $requestData['sn'], $requestData['bind'], $requestData['append']); } catch (\Exception $e) { return RestHelp::encodeResult($e->getCode(), $e->getMessage()); } return RestHelp::success(); }