Ejemplo n.º 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();
 }
Ejemplo n.º 2
0
 /** 基于设备的数据统计
  * @param ShakeAroundClient $shakeAroundClient
  * @param $deviceId
  * @param $beginDate
  * @param $endDate
  * @param null $appid
  * @param null $appsecret
  * @return mixed
  * @throws \Exception
  * @author zhengqian@dajiayao.cc
  */
 public function deviceStatistic($deviceId, $beginDate, $endDate, $appid = null, $appsecret = null)
 {
     $wxDevice = WeixinDevice::find($deviceId);
     if (!$wxDevice) {
         throw new \Exception(sprintf("device Id: %s not found in db", $deviceId), 24001);
     }
     $deviceIdentifier = new DeviceIdentifier($wxDevice->device_id, $wxDevice->uuid, $wxDevice->major, $wxDevice->minor);
     $token = $this->getWeixinToken($appid, $appsecret);
     $ret = $this->sharkroundClient->statisticsDevice($deviceIdentifier, intval($beginDate), intval($endDate), $token);
     if ($ret->errcode != 0) {
         throw new \Exception('weixin error:' . $ret->errmsg, $ret->errcode);
     }
     return $ret->data;
 }
Ejemplo n.º 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);
 }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
0
 /**
  * 绑定页面
  * @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);
 }
Ejemplo n.º 7
0
 /**
  * 修改备注
  * @param ShakeAroundClient $shakeAroundClient
  * @param DeviceService $deviceService
  * @return \Illuminate\Http\RedirectResponse
  * @author zhengqian@dajiayao.cc
  */
 public function update(ShakeAroundClient $shakeAroundClient, DeviceService $deviceService)
 {
     $deviceId = Input::get('device_id');
     $objDevice = WeixinDevice::find($deviceId);
     $comment = Input::get('comment');
     if (!$comment) {
         return redirect(route('adminGetUpdate'))->with('result', false)->with('msg', "备注不能为空");
     }
     $device = new DeviceIdentifier($objDevice->device_id, $objDevice->uuid, $objDevice->major, $objDevice->minor);
     try {
         $deviceService->updateWeixinDevice($device, $shakeAroundClient, $comment, $objDevice->mp->appid, $objDevice->mp->appsecret);
     } catch (\Exception $e) {
         return redirect(route('adminWxDevicesIndex'))->with('result', false)->with('msg', $e->getMessage());
     }
     $objDevice->comment = $comment;
     $objDevice->save();
     return redirect(route('adminWxDevicesIndex'))->with('result', true)->with('msg', '操作成功');
 }
Ejemplo n.º 8
0
 /**
  * 获取第一个可用的微信设备
  * @author Hanxiang
  */
 private static function getFirstAvailableWxDevice()
 {
     $wxdevices = WeixinDevice::all();
     if (count($wxdevices) > 0) {
         foreach ($wxdevices as $wx) {
             $wx->sn = Device::where("wx_device_id", $wx->id)->get();
         }
     }
     $usedWxDeviceIDs = Device::where("wx_device_id", ">", 0)->get(['wx_device_id']);
     $usedIDArray = [];
     if (count($usedWxDeviceIDs) > 0) {
         foreach ($usedWxDeviceIDs as $usedID) {
             array_push($usedIDArray, $usedID['wx_device_id']);
         }
     }
     $firstWxDevice = WeixinDevice::whereNotIn('id', $usedIDArray)->first();
     if (count($firstWxDevice) > 0) {
         $firstWxDevice = $firstWxDevice->toArray();
     } else {
         $firstWxDevice = [];
     }
     return $firstWxDevice;
 }
Ejemplo n.º 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];
 }
Ejemplo n.º 10
0
 /**
  * @param ShakeAroundClient $shakeAroundClient
  * @param WeixinPage $weixinPage,本地数据库页面实体
  * @param array $wx_device_ids ,本地数据库库主键设备id
  * @param int $bind
  * @param int $append
  * @throws \Exception
  */
 public function bindDevice(ShakeAroundClient $shakeAroundClient, WeixinPage $weixinPage, array $wx_device_ids, $bind = 1, $append = 1, $appid = null, $appsecret = null)
 {
     //TODO 检查 wx_mp_id 是否设备-页面一一致
     $token = $this->getWeixinToken($appid, $appsecret);
     foreach ($wx_device_ids as $id) {
         $wx_device = WeixinDevice::find($id);
         if (!$wx_device) {
             throw new \Exception(sprintf("device id %s not found", $id), 24001);
         }
         $ret = $shakeAroundClient->bindPage(new DeviceIdentifier($wx_device->device_id, $wx_device->uuid, $wx_device->major, $wx_device->minor), [$weixinPage->page_id], (int) $bind, (int) $append, $token);
         if ($ret->errcode != 0) {
             throw new \Exception('weixin error' . $ret->errmsg, 90000);
         }
     }
     //本地数据库记录
     if ($append == 0) {
         DevicePage::where('wx_page_id', $weixinPage->id)->delete();
     }
     foreach ($wx_device_ids as $id) {
         if ($bind == 1) {
             $devicePage = new DevicePage();
             $devicePage->wx_device_id = $id;
             $devicePage->wx_page_id = $weixinPage->id;
             $devicePage->save();
         } elseif ($bind == 0) {
             DevicePage::where('wx_device_id', $id)->where('wx_page_id', $weixinPage->id)->delete();
         }
     }
 }
Ejemplo n.º 11
0
 /**
  * 解除绑定
  * @param array $ids
  * @return mixed
  * @author zhengqian@dajiayao.cc
  */
 public function unsetRedirect(array $ids)
 {
     return WeixinDevice::whereIn('id', $ids)->update(['redirect_name' => '', 'redirect_url' => '']);
 }