BeaconController.php 2.63 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/30
 * Time: 10:52
 */

namespace App\Http\Controllers\Backend\Beacon;

use App\Exceptions\GeneralException;
use EasyWeChat\Factory;
use App\Http\Controllers\Controller;
use App\Modules\Models\Beacon\Beacon;
use App\Repositories\Backend\Beacon\BeaconRepository;
use App\Http\Requests\Backend\Beacon\UpdatBeaconRequest;
use App\Http\Requests\Backend\Beacon\DevicedRequest;

class BeaconController extends Controller
{
  
    private $beacon;

    
    public function __construct(BeaconRepository $beacon)
    {
        $this->beacon = $beacon;
    }

    public function index()
    {
        return view('backend.beacon.index');
    }

    public function edit(Beacon $beacon)
    {

        $spot=$this->spot();
        return view('backend.beacon.edit', ['info' => $spot])->withBeacon($beacon);
    }

    public function update(Beacon $beacon, UpdatBeaconRequest $request)
    {

        $this->beacon->update($beacon,
            $request->only(
                'rssi',
                'sd',
                'explain_id'
            ));
        return redirect()->route('admin.beacon.index')->withFlashSuccess(trans('alerts.backend.beacon.updated'));

    }


    public function mark(Beacon $beacon, $status)
    {
        $this->beacon->mark($beacon, $status);
        return redirect()->route($status == 0 ? 'admin.beacon.index' : 'admin.beacon.deactivated')->withFlashSuccess(trans('alerts.backend.beacon.updated'));
    }

    public function getDeactivated()
    {
        return view('backend.beacon.deactivated');
    }


    public function binding()
    {
        return  view('backend.beacon.binding');
    }


    public  function deviced(DevicedRequest $request)
    {
         $data =$request->only('deviced_id');
         $deviced_id = explode(',',$data['deviced_id']);
             foreach ($deviced_id as $key=> $d){
                 $da[]['device_id']= (int)$d;
             }

        $wx_config = config('wechat.official_account.default');
        $app = Factory::officialAccount($wx_config);
        $shakearound = $app->shake_around;
        //添加设备到分组,每个分组能够持有的设备上限为10000,并且每次添加操作的添加上限为1000。
        $result = $shakearound->group->addDevices(3274117, $da);
               //如果失败就是超过1000次要换一个组,
       ;
        if($result['errcode']!=0){

            throw new GeneralException(trans('alerts.backend.beacon.binding_error'));
            die();
        }
        return redirect()->route('admin.beacon.index')->withFlashSuccess(trans('alerts.backend.beacon.binding_success'));
         
    }
}