| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- namespace backend\controllers;
- use Yii;
- use common\services\xhWifiService;
- use common\components\util;
- use common\components\weixinUtil;
- use common\components\page;
- use common\models\xhShop;
- use common\models\xhWifi;
- use common\services\xhMerchantService;
- use common\services\xhShopService;
- class ShopController extends BackendController{
- public function actionIndex()
- {
- return $this->render('index');
- }
- public function actionList(){
- $get = Yii::$app->request->get();
- $query = xhShop::find()->where(['and',['merchantId' => $this->merchantId]]);
- $countQuery = clone $query;
- $count = $countQuery->count();
- $page = isset($get['page']) ? $get['page'] : 1;
- $pageSize = 20;
- $offset = ($page - 1) * $pageSize;
- $shop = $query->offset($offset)->orderBy('id desc')->limit($pageSize)->asArray()->all();
- $page = new page($count,$page,$pageSize);
- $paging = $page->fpage();
- $now = time();
- return $this->render('list', ['shop' => $shop,'paging' => $paging,'now'=>$now]);
- }
- /**
- * 创建 与 修改Wifi设置
- */
- public function actionCreateWifi(){
- $post = Yii::$app->request->post();
- $data['shopId'] = $shopId = (string)$post['shopId'];
- $data['ssid'] = $ssid = (string)$post['wifiName'];
- $data['ssidPassword'] = $pwd = (string)$post['wifiCode'];
- $WeixinUtil = new weixinUtil($this->merchant);
- if($post['action'] == 'add'){
- $re = $WeixinUtil->addPasswordDevice($shopId, $ssid, $pwd);
- }elseif($post['action'] == 'update'){
- $wifiData = xhWifi::getByCondition(['shopId' => $shopId]);
- $oldSsid = $wifiData['ssid'];
- $re = $WeixinUtil->updateDevice($shopId, $oldSsid, $ssid, $pwd);
- }
- $preMsg = ($post['action'] == 'add' ? '创建' : '更新');
- if($re['errcode'] == 0){
- $cond = ['shopId' => $data['shopId']];
- xhWifi::updateByCondition($cond, $data);
- util::successInfo($preMsg.'成功', 'success', $data);
- }else{
- util::failInfo($preMsg.'失败', 'error');
- }
- }
- /**
- * 获取微信wifi数据
- */
- public function actionGetWifiCode(){
- $shopId = Yii::$app->request->post('shopId');
- $where = ['shopId' => $shopId];
- $wifi = xhWifi::getByCondition($where);
- //访问微信接口
- if(!empty($wifi['ssid']) && !empty($wifi['ssidPassword'])){
- $wifi['avaibleDev'] = 1;
- $re = (new weixinUtil($this->merchant))->getWifiQrCode($shopId, $wifi['ssid'], 0);//返回的是数组
- if($re['errcode'] != 0){
- util::failInfo($re['errmsg'], 'getWifiQrCodeError');
- }else{
- if(!xhWifiService::existQrImage($shopId)){ //判断是否已经下载了该wifi二维码
- $re = xhWifiService::DownLoadWifiQrImg($shopId, $wifi['ssid'], $this->merchant);
- if($re['error'] > 0){
- //throw new \Exception('xhWifiService::DownLoadWifiQrImg error!');
- util::failInfo('下载Wifi二维码失败', 'downLoadImgError');
- }
- }
- }
- }
- if(empty($wifi)){
- util::failInfo('获取失败', 'error');
- }
- util::successInfo('成功获取', 'success', $wifi);
- }
- /**
- * 查询门店Wi-Fi信息
- */
- public function actionSelectWifi(){
- $weixinUtil = new weixinUtil($this->merchant);
- $data = ["shop_id" => '3268830'];
- $wifiData = $weixinUtil->getShopWifi($data);
- dump($wifiData);
- }
- /**
- * 门店wifi设备列表
- * @return string
- */
- public function actionWifiDeviceList(){
- return $this->render('wifiList');
- }
- //===================================== 暂放(后面再整) ===========================================
- public function actionApplyStore(){
- $post = Yii::$app->request->post();
- $branchName = $post['storeName'];//分店名
- $merchantInfo = $this->getStoreInfo($branchName);
- $weixinUtil = new weixinUtil($this->merchant);
- $data = $response = $weixinUtil->createStore($merchantInfo);
- $response = json_decode($response, true);
- if($response['errcode'] == 0){
- $mcInfo = $this->merchantInfoFormat($merchantInfo, $response['poi_id']);
- //$mcInfo['shopId'] = $weixinUtil->getShopId($mcInfo['poiId']);
- $re = xhShopService::add($mcInfo);//存入表xhShop
- $wifiData = ['shopId' => $mcInfo['shopId'], 'createTime' => date('Y-m-d H:i:s', time())];
- xhWifi::add($wifiData);//初始化商家wifi
- }
- echo $data;
- Yii::$app->end();
- }
- /**
- * 更新门店信息页面
- * 商户可以通过该接口,修改门店的服务信息,
- * 包括:sid、图片列表、营业时间、推荐、特色服务、简介、人均价格、电话8个字段(名称、坐标、地址等不可修改)修改后需要人工审核。
- */
- public function actionEditStore(){
- $get = Yii::$app->request->get();
- if(isset($get['poi_id']) == false){
- $this->redirect(Yii::$app->request->referrer,true);
- Yii::$app->end();
- }
- $poiId = (string)$get['poi_id'];
- $shopData = xhShopService::getByPoiId($poiId);
- dump($shopData);
- //return $this->render('edit',['shop' => $shopData]);
- }
- /**
- * 更新门店信息
- */
- public function actionSaveEditStroe(){
- $post = Yii::$app->request->post();
- $branchName = $post['branch_name'];//分店名
- $mcInfo = $this->getStoreInfo($branchName);
- $data = $response = (new weixinUtil($this->merchant))->updateStore($mcInfo);
- $response = json_decode($response, true);
- if($response['errcode'] == 0){
- //存入表xhShop
- $mcInfo = $this->merchantInfoFormat($mcInfo, $response['poi_id']);
- xhShopService::add($mcInfo);
- }
- echo $data;
- }
- /**
- * 删除门店
- */
- public function actionDelStore(){
- $post = Yii::$app->request->post();
- $poiId = $post['poiId'];
- $shopId = $post['shopId'];
- $response = (new weixinUtil($this->merchant))->delStore($poiId);
- $response = json_decode($response, true);
- if($response['errcode'] == 0){
- xhShop::deleteByCondition(['poiId' => $poiId]);
- xhWifi::deleteByCondition(['shopId' => $shopId]);
- util::successInfo('门店删除成功', 'success', $response);
- }else{
- util::failInfo('门店删除失败', 'error');
- }
- }
- public function actionStoreInfo(){
- $storeList = (new weixinUtil($this->merchant))->getStoreList(0, 50);
- $storeList = json_decode($storeList);
- //dump($this->merchant);
- dump($storeList);
- $storeInfo = (new weixinUtil($this->merchant))->getStoreInfo('472885421');
- $storeInfo = json_decode($storeInfo);
- //dump($storeInfo);
- //获取wifi门店列表
- $wifiStoreList = (new weixinUtil($this->merchant))->getShopWifiList(1, 10);
- dump($wifiStoreList);
- }
- /**
- * 生成门店信息(门店信息格式化)
- * @param $branch_name
- * @param string $mcInfo
- * @return array
- */
- private function getStoreInfo($branchName, $mcInfo=''){
- if(empty($branchName)){
- throw new Exception("分店名称未填写");
- }
- if(empty($mcInfo)){
- $account = $this->merchantId;
- $mcInfo = $merchantInfo = xhMerchantService::getByAccount($account);
- }
- if(empty($mcInfo['shopDist'])){
- throw new Exception("区(县)未设定");
- }
- $info = ["business" =>[
- "base_info" => [
- //"sid" => '123456',//非必须参数
- "business_name" => $mcInfo['merchantName'],//门店名称(仅为商户名,如:国美、麦当劳,不应包含地区、地址、分店名等信息,错误示例:北京国美) -- "15个汉字或30个英文字符内",
- "branch_name" => $branchName,//分店名称(不应包含地区信息,不应与门店名有重复,错误示例:北京王府井店) -- "不超过10个字,不能含有括号和特殊字符",
- "province" => $mcInfo['shopProvince'], //"不超过10个字",
- "city" => $mcInfo['shopCity'],//"不超过30个字",
- "district" => $mcInfo['shopDist'],//"不超过10个字",
- "address" => $mcInfo['shopAddress'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
- "telephone" => '18259179840',//"不超53个字符(不可以出现文字)",
- "categories" => '休闲娱乐,其它娱乐休闲',
- "offset_type" => 1,
- "longitude" => $mcInfo['shopLong'],
- "latitude" => $mcInfo['shopLat'],
- //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
- //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
- //"special" => "不超过200字。免费wifi,外卖服务",
- //"introduction" => "不超过300字。",
- //"open_time" => "8:00-20:00",
- //"avg_price" => 100
- ]
- ]
- ];
- return $info;
- }
- //格式化从微信接口取得的门店信息,然后可能存入 xhShop表
- public function merchantInfoFormat($merchantInfo, $poiId){
- if(!isset($merchantInfo['business_name'])){
- $merchantInfo = $merchantInfo['business']['base_info'];
- }
- $weixinUtil = new weixinUtil($this->merchant);
- $shopId = $weixinUtil->getShopId($poiId);
- $time = time();
- $formatData = [
- 'merchantId' => $merchantId,
- 'poiId' => $poiId,
- 'shopId' => (string)$shopId,
- 'businessName' => $merchantInfo['business_name'],
- 'branchName' => $merchantInfo['branch_name'],
- 'shopLat' => $merchantInfo['latitude'],
- 'shopLong' => $merchantInfo['longitude'],
- 'shopProvince' => $merchantInfo['province'],
- 'shopCity' => $merchantInfo['city'],
- 'shopDist' => $merchantInfo['district'],
- 'shopAddress' => $merchantInfo['address'],
- 'telephone' => $merchantInfo['telephone'],
- 'introduction' => isset($merchantInfo['introduction']) ? $merchantInfo['introduction'] : '',
- 'status' => isset($merchantInfo['status']) ? $merchantInfo['status'] : 0,
- 'addTime' => $time,
- 'createTime' => date('Y-m-d H:i:s', $time)
- ];
- return $formatData;
- }
- }
|