public function getMp() { if ($this->type == self::TYPE_WEIXIN) { return WeixinMp::where('app_id', $this->id)->first(); } return null; }
/** * $删除 * @param MpService $mpService * @param $wxId * @return string */ public function delete(MpService $mpService, $wxId) { $mp = WeixinMp::find($wxId); if (!$mp) { return RestHelp::encodeResult(22001, 'weixin not found'); } $mpService->delete($mp); return RestHelp::success(); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $token = $request->get('token'); if (!$token or !$this->verifyToken($token)) { return $this->encodeResult(20000, "Permission denied,TOKEN ERROR"); } $app = App::where('access_token', $token)->first(); Session::put('appid', $app->id); $mp = WeixinMp::where('app_id', $app->id)->first(); Session::put('wx_mp_id', $mp->id); Session::put('wx_appid', $mp->appid); Session::put('wx_appsecret', $mp->appsecret); return $next($request); }
/** * 同步微信数据 * @return \Illuminate\Http\RedirectResponse * @author zhengqian@dajiayao.cc */ public function syncWeixin() { ini_set('max_execution_time', 300); ignore_user_abort(true); $mpId = $this->request->get('mp'); $objMp = WeixinMp::find($mpId); if (!$objMp) { return redirect(route('adminSyncWeixin'))->with('result', false)->with('msg', '微信号未找到'); } try { $count = $this->weixinService->sync($objMp->appid, $objMp->appsecret); } catch (\Exception $e) { return redirect(route('adminSyncWeixin'))->with('result', false)->with('msg', $e->getMessage()); } return redirect(route('adminSyncWeixin'))->with('result', true)->with('msg', sprintf("同步页面 %s 个,设备 %s 个", $count['count_page'], $count['count_device'])); }
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); }
public function saveOrUpdateMp($app_id) { $input = Input::only('name', 'appid', 'appsecret', 'comment', 'mp_id'); $app = $this->appService->getAppById($app_id); if (!$app) { \App::abort(404, '没有该应用'); } if ($app->type != App::TYPE_WEIXIN) { return redirect()->back()->with('error_tips', '该应用类型不是微信')->withInput(); } $name = $input['name']; $appId = $input['appid']; $appSecret = $input['appsecret']; $comment = $input['comment']; $mpId = $input['mp_id']; if (!($name and $appId and $appSecret and $mpId)) { return redirect()->back()->with('error_tips', '提交数据不完整')->withInput(); } $mp = $this->mpService->getMpByAppId($app_id); if (!$mp) { $mp = new WeixinMp(); $mp->app_id = $app_id; } $mp->name = $name; $mp->appid = $appId; $mp->appsecret = $appSecret; $mp->comment = $comment; $mp->app_id = $app_id; $mp->mp_id = $mpId; $mp->save(); return redirect()->back()->with("success_tips", "操作成功!"); }
public function getMpByAppId($appId) { return WeixinMp::where('app_id', $appId)->first(); }
/** * 绑定页面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 * @author zhengqian@dajiayao.cc */ public function applyWxDevice(ShakeAroundClient $shakeAroundClient, DeviceService $deviceService) { $inputData = Input::only('wx_mp_id', 'sum'); $validator = Validator::make($inputData, ['wx_mp_id' => 'required', 'sum' => 'required|numeric']); if ($validator->fails()) { return response()->json(['msgcode' => -1, 'message' => $validator->messages()->first()]); } $mpId = $inputData['wx_mp_id']; $mp = WeixinMp::find($mpId); if (!$mp) { return response()->json(['msgcode' => -2, 'message' => '微信号未找到']); } if ($inputData['sum'] > 100) { return response()->json(['msgcode' => -3, 'message' => '一次最多申请100个']); } try { $ret = $deviceService->applyDeviceOnline($shakeAroundClient, (int) $inputData['sum'], '测试', 'comment', 0, $mp->appid, $mp->appsecret); } catch (\Exception $e) { return response()->json(['msgcode' => $e->getCode(), 'message' => $e->getMessage()]); } $applyId = $ret->apply_id; $devices = $ret->device_identifiers; foreach ($devices as $dev) { $d = new WeixinDevice(); $d->uuid = $dev->uuid; $d->major = $dev->major; $d->minor = $dev->minor; $d->device_id = $dev->device_id; $d->apply_id = $applyId; $d->wx_mp_id = $mpId; $d->save(); } return redirect(route('adminWxDevicesIndex'))->with('result', true)->with('msg', "操作成功"); }
/** * 获取微信设备AJAX * @author Hanxiang */ public function wxdevicesAjax() { $input = Input::all(); $sn = $input['sn']; $device = Device::where('sn', $sn)->first(); if (!$device) { return response()->json(['wx_devices' => [], 'first' => new \stdClass()]); } $app_id = DeviceApp::where('device_id', $device->id)->lists('app_id'); $wx_mp_id = WeixinMp::where('app_id', $app_id)->lists('id'); $wxdevices = WeixinDevice::where('wx_mp_id', $wx_mp_id)->get(); if (count($wxdevices) > 0) { foreach ($wxdevices as $wx) { $wx->sn = Device::where("wx_device_id", $wx->id)->get(); } } $firstWxDevice = self::getFirstAvailableWxDevice(); return response()->json(['wx_devices' => $wxdevices->toArray(), 'first' => $firstWxDevice]); }
/** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); // $this->call('UserTableSeeder'); DB::table('users')->truncate(); User::create(['username' => 'admin', 'email' => '*****@*****.**', 'password' => \Hash::make('11431143'), 'role' => User::ROLE_ADMIN, 'status' => User::STATUS_NORMAL]); User::create(['username' => 'yayao001', 'email' => '*****@*****.**', 'password' => \Hash::make('yayao001'), 'role' => User::ROLE_COMMON_USER, 'status' => User::STATUS_NORMAL]); DB::table('manufacturers')->truncate(); Manufacturer::create(['name' => '佰睿科技', 'website' => 'http://www.bytereal.com', 'address' => '深圳市南山区高新南六道航盛科技大厦', 'email' => '*****@*****.**', 'phone' => '400-8899-181']); Manufacturer::create(['name' => '微肯科技', 'website' => 'http://www.wizarcan.com', 'address' => '上海市黄浦区西藏南路760号安基大厦706室', 'email' => '*****@*****.**', 'phone' => '021-63309703']); DB::table('device_models')->truncate(); DeviceModel::create(['id' => 1, 'manufacturer_id' => 1, 'battery_lifetime' => 18, 'name' => 'BR', 'comment' => '佰睿']); DeviceModel::create(['id' => 2, 'manufacturer_id' => 2, 'battery_lifetime' => 18, 'name' => 'wizarcan', 'comment' => '微肯']); DB::table('apps')->truncate(); App::create(['app_id' => 'yyfa7b475d22b0ef1d', 'app_secret' => 'ec6a2a765bc197a273841cd41ed171a2 ', 'name' => '丫摇小店', 'type' => App::TYPE_WEIXIN, 'access_token' => '', 'expire_at' => '2030-05-01 00:00:00', 'comment' => '丫摇小店(消费者服务号)', 'user_id' => 2, 'status' => App::STATUS_NORMAL]); App::create(['app_id' => 'yy3a6b475fc2b19f2e', 'app_secret' => '3a76597a2784bc11edeccd416a2171a2', 'name' => '测试应用', 'type' => App::TYPE_WEIXIN, 'access_token' => '', 'expire_at' => '2030-05-01 00:00:00', 'comment' => '测试应用', 'user_id' => 2, 'status' => App::STATUS_NORMAL]); DB::table('wx_mp')->truncate(); WeixinMp::create(['appid' => 'wxfb42b0ea75d27f1d', 'appsecret' => 'a62a7197273e81cd4a1d14715bca2ec6', 'name' => '丫摇小店', 'mp_id' => 'gh_dd4f2e417685', 'comment' => '丫摇小店(消费者服务号)', 'app_id' => 1]); WeixinMp::create(['appid' => 'wxbc879a670cb32971', 'appsecret' => '20b517a9882cecc620e20175f8943ef0', 'name' => '测试号', 'mp_id' => 'gh_dxxxxxxxx', 'comment' => '测试', 'app_id' => 2]); DB::table('devices')->truncate(); // \Dajiayao\Model\Device::create([ // "id"=>1, // "model_id"=>1, // "manufacturer_sn"=>11111, // "sn"=>2015121212, // "wx_device_id"=>1, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10001, // "minor"=>44330, // "password"=>123456, // "comment"=>'comment', // "status"=>1 // ]); // // \Dajiayao\Model\Device::create([ // "id"=>2, // "model_id"=>1, // "manufacturer_sn"=>22222, // "sn"=>2015131313, // "wx_device_id"=>1, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10001, // "minor"=>44330, // "password"=>123456, // "comment"=>'comment', // "status"=>1 // ]); // // \Dajiayao\Model\Device::create([ // "id"=>3, // "model_id"=>1, // "manufacturer_sn"=>33333, // "sn"=>2015141414, // "wx_device_id"=>2, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10001, // "minor"=>44331, // "password"=>123456, // "comment"=>'comment', // "status"=>1 // ]); // // \Dajiayao\Model\Device::create([ // "id"=>4, // "model_id"=>1, // "manufacturer_sn"=>44444, // "sn"=>201515151515, // "wx_device_id"=>3, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10001, // "minor"=>44332, // "password"=>123456, // "comment"=>'comment', // "status"=>1 // ]); DB::table('wx_devices')->truncate(); // \Dajiayao\Model\WeixinDevice::create([ // 'id'=>1, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10001, // "minor"=>44330, // "device_id"=>657415, // "wx_mp_id"=>1 // ]); // // // \Dajiayao\Model\WeixinDevice::create([ // 'id'=>2, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10001, // "minor"=>44331, // "device_id"=>657416, // "wx_mp_id"=>1 // ]); // // \Dajiayao\Model\WeixinDevice::create([ // 'id'=>3, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10001, // "minor"=>44332, // "device_id"=>657417, // "wx_mp_id"=>1 // ]); // // \Dajiayao\Model\WeixinDevice::create([ // 'id'=>4, // "uuid"=>'FDA50693-A4E2-4FB1-AFCF-C6EB07647825', // "major"=>10007, // "minor"=>41549, // "device_id"=>681062, // "wx_mp_id"=>1, // "apply_id"=>26858 // ]); DB::table('wx_pages')->truncate(); // \Dajiayao\Model\WeixinPage::create([ // "wx_mp_id"=>1, // "page_id"=>81039, // 'title'=>'周边主标题', // 'description'=>'副标题', // 'icon_url'=>'http://shp.qpic.cn/wx_shake_bus/0/1431503979e9dd2797018cad79186e03e8c5aec8dc/120', // 'url'=>'http://www.baidu.com', // 'comment'=>'comment' // ]); // // DB::table('wx_mp')->truncate(); // \Dajiayao\Model\WeixinMp::create([ // "id"=>1, // "appid"=>'wxbc879a670cb32971', // 'appsecret'=>'20b517a9882cecc620e20175f8943ef0', // 'name'=>'大家摇', // 'mp_id'=>'原始id', // 'app_id'=>1 // ]); }
/** * * @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]; }