| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- namespace common\services;
- use Yii;
- use linslin\yii2\curl;
- use common\models\xhShop;
- use common\models\xhWifi;
- use common\components\dict;
- use common\components\util;
- use common\components\wxUtil;
- class xhShopService {
- public static function add($data)
- {
- $shop = xhShop::add($data);
- return $shop;
- }
- public static function updateById($id,$data)
- {
- xhShop::updateById($id, $data);
- self::refresh($id);
- }
- public static function updateByPoiId($poiId,$data)
- {
- xhShop::updateByPoiId($poiId, $data);
- self::refresh($poiId, true);
- }
- public static function getById($id){
- $cacheKey = dict::getCacheKey('xhShop').$id;
- $cacheData = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($cacheData)){
- $data = [];
- $list = [];
- $x = 0;
- foreach($cacheData as $cKey => $cVal){
- if($cKey%2 == 0){
- $list[$x]['key'] = $cVal;
- }else{
- $list[$x]['value'] = $cVal;
- $x++;
- }
- }
- foreach($list as $lKey => $lVal){
- $data[$lVal['key']] = $lVal['value'];
- }
- unset($data['info_already_get_by_sql']);
- return $data;
- }
- $merchant = xhShop::getByCondition(['id' => $id]);
- $arr = [$cacheKey];
- $arr[] = 'info_already_get_by_sql';
- $arr[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($merchant)){
- foreach($merchant as $key => $val){
- $arr[] = $key;
- $arr[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET', $arr);
- return $merchant;
- }
- public static function getByPoiId($poiId)
- {
- $cacheKey = dict::getCacheKey('xhShop').$poiId;
- $shop = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($shop)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($shop as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $shopArr = [];
- foreach($arr as $key => $val){
- $shopArr[$val['key']] = $val['value'];
- }
- unset($shopArr['info_already_get_by_sql']);
- return $shopArr;
- }
- $shop = xhShop::find()->where(['poiId' => $poiId])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($shop)){
- foreach($shop as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);
- return $shop;
- }
- /**
- * 更新redis缓存
- * @param $id shopId 或者 poiId
- * @param bool $poiId 默认(false)是shopId, true是poiId
- */
- public static function refresh($id, $poiId = false)
- {
- if(!$poiId){
- $shop = xhShop::getById($id);
- $poiId = $shop['poiId'];
- }else{
- $shop = xhShop::getByPoiId($id);
- $poiId = $id;
- $id = $shop['id'];
- }
- $idKey = dict::getCacheKey('xhShop').$id;
- $poidIdKey = dict::getCacheKey('xhShop').$poiId;
- Yii::$app->redis->executeCommand('DEL', [$idKey]);
- Yii::$app->redis->executeCommand('DEL', [$poidIdKey]);
- self::getById($id);
- self::getByPoiId($poiId);
- }
- //微信门店拉取并保存
- public function saveShopList($merchant)
- {
- $faileApply = 0;
- $successApply = 0;
- $applying = 0;
- $sjId = $merchant['id'];
- $wx = new wxUtil($merchant);
- $storeList = $wx->getStoreList(0, 50);//获取门店列表
- $storeList = json_decode($storeList, true);
- if($storeList['errcode'] != 0){
- util::fail('门店拉取失败');
- }
- if($storeList['total_count'] == 0){
- //util::fail('商户还未新建门店');
- return false;
- }
- $shopList = $storeList['business_list'];
- $now = time();
- $date = date("Y-m-d H:i:s", $now);
- $wxUtil = new wxUtil($merchant);
- $wifiList = $wxUtil->getShopWifiListAll();
- foreach($shopList as $key => $val){
- $shop = $val['base_info'];
- $poiId = $shop['poi_id'];
- $data = [];
- $data['businessName'] = $shop['business_name'];
- $data['branchName'] = $shop['branch_name'];
- $data['sjId'] = $sjId;
- $data['poiId'] = $poiId;//poiId适用于门店
- $data['lat'] = (string)$shop['latitude'];
- $data['long'] = (string)$shop['longitude'];
- $data['province'] = $shop['province'];
- $data['city'] = $shop['city'];
- $data['dist'] = $shop['district'];
- $data['address'] = $shop['address'];
- $data['telephone'] = $shop['telephone'];
- $data['introduction'] = $shop['introduction'];
- $data['addTime'] = $now;
- $data['status'] = $this->getAvailableState($shop['available_state']);
- $data['createTime'] = $date;
- //取shopId
- $data['shopId'] = $wxUtil->getShopId($poiId, $wifiList);
- if($data['status'] == 1){
- ++$applying;
- $info = self::getByPoiId($poiId);
- if(!empty($info)){
- continue;
- }
- self::add($data);
- $shopId = $wxUtil->getShopId($poiId);
- $wifiData = ['shopId' => $shopId, 'createTime' => date('Y-m-d H:i:s', time())];
- xhWifi::add($wifiData);//初始化商家wifi
- }elseif($data['status'] == 2){
- ++$successApply;
- $info = self::getByPoiId($poiId);
- if(!empty($info)){
- continue;
- }
- self::add($data);
- $shopId = $wxUtil->getShopId($poiId);
- $wifiData = ['shopId' => $shopId, 'createTime' => date('Y-m-d H:i:s', time())];
- xhWifi::add($wifiData);//初始化商家wifi
- }else{
- ++$faileApply;
- }
- }
- if(($num = count($wifiList)) > 0){
- return ['total' => $num, 'success' => $successApply, 'applying' => $applying, 'fail' => $faileApply];
- }
- return false;
- }
- //运营平台后台中调用的,用于自动生成门店数据
- public function platformInitStore($mcInfo, $merchant, $branchName = ''){
- $wxUtil = new wxUtil($merchant);
- $xhShopService = new xhShopService();
- $formatStoreData = $xhShopService->getStoreInfo($mcInfo['id'], '', $mcInfo);
- $response = $wxUtil->createStore($formatStoreData);
- $response = json_decode($response, true);
- if($response['errcode'] != 0){
- return $response['errmsg'];
- }
- $now = time();
- $data = [];
- $data['businessName'] = $mcInfo['name'];
- $data['branchName'] = $branchName;
- $data['sjId'] = $mcInfo['id'];
- $data['poiId'] = '';
- $data['shopId'] = '';
- $data['lat'] = (string)$mcInfo['lat'];
- $data['long'] = (string)$mcInfo['long'];
- $data['province'] = $mcInfo['province'];
- $data['city'] = $mcInfo['city'];
- $data['dist'] = $mcInfo['dist'];
- $data['address'] = $mcInfo['address'];
- $data['telephone'] = '';
- $data['introduction'] = '';
- $data['addTime'] = $now;
- $data['status'] = 0;
- $data['createTime'] = date('Y-m-d h:i:s', $now);
- $re = xhShopService::add($data);
- if($re){
- return true;
- }
- return 'shopData save error';
- }
- //从 xhShop 取数据 给微信接口传门店信息 (门店信息格式化)
- public function getShopData($shopId){
- $shop = xhShopService::getById($shopId);
- $info = ["business" =>[
- "base_info" => [
- //"sid" => '123456',//非必须参数
- "business_name" => $shop['businessName'],//门店名称
- "branch_name" => $shop['branchName'],
- "province" => $shop['province'], //"不超过10个字",
- "city" => $shop['city'],//"不超过30个字",
- "district" => $shop['dist'],//"不超过10个字",
- "address" => $shop['address'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
- "telephone" => $shop['telephone'],//"不超53个字符(不可以出现文字)",
- "categories" => '休闲娱乐,其它娱乐休闲',
- "offset_type" => 1,//坐标类型
- "longitude" => $shop['long'],
- "latitude" => $shop['lat'],
- //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
- //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
- //"special" => "不超过200字。免费wifi,外卖服务",
- "introduction" => $shop['introduction'],
- //"open_time" => "8:00-20:00",
- //"avg_price" => 100
- ]
- ]
- ];
- return $info;
- }
- /**
- * 从 xhMerchant取数据
- * 给微信接口传门店信息 (门店信息格式化)
- * @param $sjId 商户号
- * @param $branchName 分店名称
- * @param $mcInfo
- * @return array
- * @throws \Exception
- */
- public function getStoreInfo($sjId, $branchName='', $mcInfo=''){
- /*if(empty($branchName)){
- throw new \Exception("分店名称未填写");
- }*/
- if(empty($mcInfo)){
- $mcInfo = $merchantInfo = xhMerchantService::getById($sjId);
- }
- if(empty($mcInfo['dist'])){
- throw new \Exception("区(县)未设定");
- }
- $info = ["business" =>[
- "base_info" => [
- //"sid" => '123456',//非必须参数
- "business_name" => $mcInfo['name'],//门店名称
- "branch_name" => $branchName,
- "province" => $mcInfo['province'], //"不超过10个字",
- "city" => $mcInfo['city'],//"不超过30个字",
- "district" => empty($mcInfo['dist']) ? '' : $mcInfo['dist'],//"不超过10个字",
- "address" => $mcInfo['address'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
- "telephone" => '18259179840',//"不超53个字符(不可以出现文字)",
- "categories" => '休闲娱乐,其它娱乐休闲',
- "offset_type" => 1,
- "longitude" => $mcInfo['long'],
- "latitude" => $mcInfo['lat'],
- //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
- //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
- //"special" => "不超过200字。免费wifi,外卖服务",
- //"introduction" => "不超过300字。麦当劳是全球大型跨国连锁餐厅,1940 年创立于美国,在世界上大约拥有3 万间分店。主要售卖汉堡包,以及薯条、炸鸡、汽水、冰品、沙拉、 水果等快餐食品",
- //"open_time" => "8:00-20:00",
- //"avg_price" => 100
- ]
- ]
- ];
- return $info;
- }
- /**
- * 微信门店的审核状态转换
- * @param $n
- * @return int
- */
- private function getAvailableState($n){
- switch ($n){
- case 1:
- return 4;
- break;
- case 2:
- return 1;
- break;
- case 3:
- return 2;
- break;
- case 4:
- return 3;
- break;
- default:
- throw new \Exception('weixin available_state not exist!');
- break;
- }
- }
- }
|