Example #1
0
 /**
  * @设备绑定页面
  * @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();
 }
Example #2
0
 /** 基于页面的数据统计
  * @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;
 }
Example #3
0
 public function index()
 {
     $user = Auth::user();
     if ($user->role == User::ROLE_ADMIN) {
         $devicesCount = Device::all()->count();
         $wxDevicesCount = WeixinDevice::all()->count();
         $appsCount = App::all()->count();
         $wxPagesCount = WeixinPage::all()->count();
     } else {
         $apps = App::where('user_id', $user->id)->lists('id');
         $devicesCount = DeviceApp::whereIn('app_id', $apps)->count();
         $mps = WeixinMp::whereIn('app_id', $apps)->lists('id');
         $wxDevicesCount = WeixinDevice::whereIn('wx_mp_id', $mps)->count();
         $appsCount = count($apps);
         $wxPagesCount = WeixinPage::whereIn('wx_mp_id', $mps)->count();
     }
     return view('admin.index')->with('devicesCount', $devicesCount)->with('wxDevicesCount', $wxDevicesCount)->with('appsCount', $appsCount)->with('wxPagesCount', $wxPagesCount);
 }
Example #4
0
 /**
  * 将微信返回的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);
 }
Example #5
0
 /**
  * 所有中间处理逻辑,生产本地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);
 }
Example #6
0
 /**
  * 绑定页面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', "操作成功");
 }
Example #8
0
 /**
  * 页面具体信息
  * @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]);
 }
Example #9
0
 /**
  *
  * @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];
 }
Example #10
0
 /**
  * @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);
     }
 }
Example #11
0
 /**
  * @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();
         }
     }
 }