ShopController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use common\services\xhWifiService;
  5. use common\components\util;
  6. use common\components\weixinUtil;
  7. use common\components\page;
  8. use common\models\xhShop;
  9. use common\models\xhWifi;
  10. use common\services\xhMerchantService;
  11. use common\services\xhShopService;
  12. class ShopController extends BackendController{
  13. public function actionIndex()
  14. {
  15. return $this->render('index');
  16. }
  17. public function actionList(){
  18. $get = Yii::$app->request->get();
  19. $query = xhShop::find()->where(['and',['merchantId' => $this->merchantId]]);
  20. $countQuery = clone $query;
  21. $count = $countQuery->count();
  22. $page = isset($get['page']) ? $get['page'] : 1;
  23. $pageSize = 20;
  24. $offset = ($page - 1) * $pageSize;
  25. $shop = $query->offset($offset)->orderBy('id desc')->limit($pageSize)->asArray()->all();
  26. $page = new page($count,$page,$pageSize);
  27. $paging = $page->fpage();
  28. $now = time();
  29. return $this->render('list', ['shop' => $shop,'paging' => $paging,'now'=>$now]);
  30. }
  31. /**
  32. * 创建 与 修改Wifi设置
  33. */
  34. public function actionCreateWifi(){
  35. $post = Yii::$app->request->post();
  36. $data['shopId'] = $shopId = (string)$post['shopId'];
  37. $data['ssid'] = $ssid = (string)$post['wifiName'];
  38. $data['ssidPassword'] = $pwd = (string)$post['wifiCode'];
  39. $WeixinUtil = new weixinUtil($this->merchant);
  40. if($post['action'] == 'add'){
  41. $re = $WeixinUtil->addPasswordDevice($shopId, $ssid, $pwd);
  42. }elseif($post['action'] == 'update'){
  43. $wifiData = xhWifi::getByCondition(['shopId' => $shopId]);
  44. $oldSsid = $wifiData['ssid'];
  45. $re = $WeixinUtil->updateDevice($shopId, $oldSsid, $ssid, $pwd);
  46. }
  47. $preMsg = ($post['action'] == 'add' ? '创建' : '更新');
  48. if($re['errcode'] == 0){
  49. $cond = ['shopId' => $data['shopId']];
  50. xhWifi::updateByCondition($cond, $data);
  51. util::successInfo($preMsg.'成功', 'success', $data);
  52. }else{
  53. util::failInfo($preMsg.'失败', 'error');
  54. }
  55. }
  56. /**
  57. * 获取微信wifi数据
  58. */
  59. public function actionGetWifiCode(){
  60. $shopId = Yii::$app->request->post('shopId');
  61. $where = ['shopId' => $shopId];
  62. $wifi = xhWifi::getByCondition($where);
  63. //访问微信接口
  64. if(!empty($wifi['ssid']) && !empty($wifi['ssidPassword'])){
  65. $wifi['avaibleDev'] = 1;
  66. $re = (new weixinUtil($this->merchant))->getWifiQrCode($shopId, $wifi['ssid'], 0);//返回的是数组
  67. if($re['errcode'] != 0){
  68. util::failInfo($re['errmsg'], 'getWifiQrCodeError');
  69. }else{
  70. if(!xhWifiService::existQrImage($shopId)){ //判断是否已经下载了该wifi二维码
  71. $re = xhWifiService::DownLoadWifiQrImg($shopId, $wifi['ssid'], $this->merchant);
  72. if($re['error'] > 0){
  73. //throw new \Exception('xhWifiService::DownLoadWifiQrImg error!');
  74. util::failInfo('下载Wifi二维码失败', 'downLoadImgError');
  75. }
  76. }
  77. }
  78. }
  79. if(empty($wifi)){
  80. util::failInfo('获取失败', 'error');
  81. }
  82. util::successInfo('成功获取', 'success', $wifi);
  83. }
  84. /**
  85. * 查询门店Wi-Fi信息
  86. */
  87. public function actionSelectWifi(){
  88. $weixinUtil = new weixinUtil($this->merchant);
  89. $data = ["shop_id" => '3268830'];
  90. $wifiData = $weixinUtil->getShopWifi($data);
  91. dump($wifiData);
  92. }
  93. /**
  94. * 门店wifi设备列表
  95. * @return string
  96. */
  97. public function actionWifiDeviceList(){
  98. return $this->render('wifiList');
  99. }
  100. //===================================== 暂放(后面再整) ===========================================
  101. public function actionApplyStore(){
  102. $post = Yii::$app->request->post();
  103. $branchName = $post['storeName'];//分店名
  104. $merchantInfo = $this->getStoreInfo($branchName);
  105. $weixinUtil = new weixinUtil($this->merchant);
  106. $data = $response = $weixinUtil->createStore($merchantInfo);
  107. $response = json_decode($response, true);
  108. if($response['errcode'] == 0){
  109. $mcInfo = $this->merchantInfoFormat($merchantInfo, $response['poi_id']);
  110. //$mcInfo['shopId'] = $weixinUtil->getShopId($mcInfo['poiId']);
  111. $re = xhShopService::add($mcInfo);//存入表xhShop
  112. $wifiData = ['shopId' => $mcInfo['shopId'], 'createTime' => date('Y-m-d H:i:s', time())];
  113. xhWifi::add($wifiData);//初始化商家wifi
  114. }
  115. echo $data;
  116. Yii::$app->end();
  117. }
  118. /**
  119. * 更新门店信息页面
  120. * 商户可以通过该接口,修改门店的服务信息,
  121. * 包括:sid、图片列表、营业时间、推荐、特色服务、简介、人均价格、电话8个字段(名称、坐标、地址等不可修改)修改后需要人工审核。
  122. */
  123. public function actionEditStore(){
  124. $get = Yii::$app->request->get();
  125. if(isset($get['poi_id']) == false){
  126. $this->redirect(Yii::$app->request->referrer,true);
  127. Yii::$app->end();
  128. }
  129. $poiId = (string)$get['poi_id'];
  130. $shopData = xhShopService::getByPoiId($poiId);
  131. dump($shopData);
  132. //return $this->render('edit',['shop' => $shopData]);
  133. }
  134. /**
  135. * 更新门店信息
  136. */
  137. public function actionSaveEditStroe(){
  138. $post = Yii::$app->request->post();
  139. $branchName = $post['branch_name'];//分店名
  140. $mcInfo = $this->getStoreInfo($branchName);
  141. $data = $response = (new weixinUtil($this->merchant))->updateStore($mcInfo);
  142. $response = json_decode($response, true);
  143. if($response['errcode'] == 0){
  144. //存入表xhShop
  145. $mcInfo = $this->merchantInfoFormat($mcInfo, $response['poi_id']);
  146. xhShopService::add($mcInfo);
  147. }
  148. echo $data;
  149. }
  150. /**
  151. * 删除门店
  152. */
  153. public function actionDelStore(){
  154. $post = Yii::$app->request->post();
  155. $poiId = $post['poiId'];
  156. $shopId = $post['shopId'];
  157. $response = (new weixinUtil($this->merchant))->delStore($poiId);
  158. $response = json_decode($response, true);
  159. if($response['errcode'] == 0){
  160. xhShop::deleteByCondition(['poiId' => $poiId]);
  161. xhWifi::deleteByCondition(['shopId' => $shopId]);
  162. util::successInfo('门店删除成功', 'success', $response);
  163. }else{
  164. util::failInfo('门店删除失败', 'error');
  165. }
  166. }
  167. public function actionStoreInfo(){
  168. $storeList = (new weixinUtil($this->merchant))->getStoreList(0, 50);
  169. $storeList = json_decode($storeList);
  170. //dump($this->merchant);
  171. dump($storeList);
  172. $storeInfo = (new weixinUtil($this->merchant))->getStoreInfo('472885421');
  173. $storeInfo = json_decode($storeInfo);
  174. //dump($storeInfo);
  175. //获取wifi门店列表
  176. $wifiStoreList = (new weixinUtil($this->merchant))->getShopWifiList(1, 10);
  177. dump($wifiStoreList);
  178. }
  179. /**
  180. * 生成门店信息(门店信息格式化)
  181. * @param $branch_name
  182. * @param string $mcInfo
  183. * @return array
  184. */
  185. private function getStoreInfo($branchName, $mcInfo=''){
  186. if(empty($branchName)){
  187. throw new Exception("分店名称未填写");
  188. }
  189. if(empty($mcInfo)){
  190. $account = $this->merchantId;
  191. $mcInfo = $merchantInfo = xhMerchantService::getByAccount($account);
  192. }
  193. if(empty($mcInfo['shopDist'])){
  194. throw new Exception("区(县)未设定");
  195. }
  196. $info = ["business" =>[
  197. "base_info" => [
  198. //"sid" => '123456',//非必须参数
  199. "business_name" => $mcInfo['merchantName'],//门店名称(仅为商户名,如:国美、麦当劳,不应包含地区、地址、分店名等信息,错误示例:北京国美) -- "15个汉字或30个英文字符内",
  200. "branch_name" => $branchName,//分店名称(不应包含地区信息,不应与门店名有重复,错误示例:北京王府井店) -- "不超过10个字,不能含有括号和特殊字符",
  201. "province" => $mcInfo['shopProvince'], //"不超过10个字",
  202. "city" => $mcInfo['shopCity'],//"不超过30个字",
  203. "district" => $mcInfo['shopDist'],//"不超过10个字",
  204. "address" => $mcInfo['shopAddress'],//"门店所在的详细街道地址(不要填写省市信息):不超过80个字",
  205. "telephone" => '18259179840',//"不超53个字符(不可以出现文字)",
  206. "categories" => '休闲娱乐,其它娱乐休闲',
  207. "offset_type" => 1,
  208. "longitude" => $mcInfo['shopLong'],
  209. "latitude" => $mcInfo['shopLat'],
  210. //"photo_list" => ["photo_url"=>"https:// 不超过20张.com", "photo_url"=>"https://XXX.com"],
  211. //"recommend" => "不超过200字。麦辣鸡腿堡套餐,麦乐鸡,全家桶",
  212. //"special" => "不超过200字。免费wifi,外卖服务",
  213. //"introduction" => "不超过300字。",
  214. //"open_time" => "8:00-20:00",
  215. //"avg_price" => 100
  216. ]
  217. ]
  218. ];
  219. return $info;
  220. }
  221. //格式化从微信接口取得的门店信息,然后可能存入 xhShop表
  222. public function merchantInfoFormat($merchantInfo, $poiId){
  223. if(!isset($merchantInfo['business_name'])){
  224. $merchantInfo = $merchantInfo['business']['base_info'];
  225. }
  226. $weixinUtil = new weixinUtil($this->merchant);
  227. $shopId = $weixinUtil->getShopId($poiId);
  228. $time = time();
  229. $formatData = [
  230. 'merchantId' => $merchantId,
  231. 'poiId' => $poiId,
  232. 'shopId' => (string)$shopId,
  233. 'businessName' => $merchantInfo['business_name'],
  234. 'branchName' => $merchantInfo['branch_name'],
  235. 'shopLat' => $merchantInfo['latitude'],
  236. 'shopLong' => $merchantInfo['longitude'],
  237. 'shopProvince' => $merchantInfo['province'],
  238. 'shopCity' => $merchantInfo['city'],
  239. 'shopDist' => $merchantInfo['district'],
  240. 'shopAddress' => $merchantInfo['address'],
  241. 'telephone' => $merchantInfo['telephone'],
  242. 'introduction' => isset($merchantInfo['introduction']) ? $merchantInfo['introduction'] : '',
  243. 'status' => isset($merchantInfo['status']) ? $merchantInfo['status'] : 0,
  244. 'addTime' => $time,
  245. 'createTime' => date('Y-m-d H:i:s', $time)
  246. ];
  247. return $formatData;
  248. }
  249. }