MainController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. namespace hd\controllers;
  3. use biz\main\classes\MainClass;
  4. use biz\stat\classes\StatCgClass;
  5. use bizHd\shop\classes\ShopExtClass;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use common\components\dict;
  8. use common\components\imgUtil;
  9. use common\components\miniUtil;
  10. use common\components\stringUtil;
  11. use GatewayClient\Gateway;
  12. use common\components\util;
  13. use biz\shop\classes\ShopClass;
  14. use bizGhs\product\classes\ProductClass;
  15. use bizHd\stat\classes\StatIncomeClass;
  16. use bizHd\stat\classes\StatOrderClass;
  17. use Yii;
  18. class MainController extends BaseController
  19. {
  20. public $guestAccess = ['my'];
  21. //获取商城的短链接 ssh 20250826
  22. public function actionGetShortUrl()
  23. {
  24. $shopId = $this->shopId;
  25. $merchant = WxOpenClass::getMallWxInfo();
  26. $ptStyle = dict::getDict('ptStyle', 'mall');
  27. if (getenv('YII_ENV') != 'production') {
  28. if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') {
  29. util::success(['goodsUrl' => 'www.google.com', 'imgUrl' => 'www.google.com', 'itemUrl' => 'www.google.com']);
  30. }
  31. }
  32. $goodsShortUrl = 'wx_mini_goods_short_url:' . $shopId;
  33. $itemShortUrl = 'wx_mini_item_short_url:' . $shopId;
  34. $imageShortUrl = 'wx_mini_image_short_url:' . $shopId;
  35. $goodsUrl = Yii::$app->redis->executeCommand('GET', [$goodsShortUrl]);
  36. $imgUrl = Yii::$app->redis->executeCommand('GET', [$itemShortUrl]);
  37. $itemUrl = Yii::$app->redis->executeCommand('GET', [$imageShortUrl]);
  38. if (empty($goodsUrl) || empty($imgUrl) || empty($itemUrl)) {
  39. $goods = miniUtil::generateShortLink($merchant, 'pages/home/category?account=' . $shopId, '', $ptStyle);
  40. $image = miniUtil::generateShortLink($merchant, 'pages/home/mall?account=' . $shopId, '', $ptStyle);
  41. $item = miniUtil::generateShortLink($merchant, 'pages/item/item?account=' . $shopId, '', $ptStyle);
  42. $goodsUrl = $goods['link'] ?? '';
  43. $imgUrl = $image['link'] ?? '';
  44. $itemUrl = $item['link'] ?? '';
  45. Yii::$app->redis->executeCommand('SETEX', [$goodsShortUrl, 86400 * 3, $goodsUrl]);
  46. Yii::$app->redis->executeCommand('SETEX', [$itemShortUrl, 86400 * 3, $imgUrl]);
  47. Yii::$app->redis->executeCommand('SETEX', [$imageShortUrl, 86400 * 3, $itemUrl]);
  48. }
  49. util::success(['goodsUrl' => $goodsUrl, 'imgUrl' => $imgUrl, 'itemUrl' => $itemUrl]);
  50. }
  51. //main信息 ssh 20230406
  52. public function actionInfo()
  53. {
  54. $main = $this->main;
  55. util::success(['info' => $main]);
  56. }
  57. /**
  58. * 职责:生成并获取花束商城太阳码海报及商城短链接
  59. * 入参:HTTP GET (参数可选:env_version)
  60. * 返回:太阳码海报图片的完整 OSS 地址以及短链接 URL
  61. * 副作用:当没有缓存时,会触发向微信 API 请求并同步上传生成的图片至阿里 OSS。
  62. * 关键边界:引入了店员+门店+环境级别的最终海报数据 Redis 强缓存(3天有效期),避免每次高频无端向微信和 OSS 发起同步请求。
  63. */
  64. public function actionGetMallPoster()
  65. {
  66. $shop = $this->shop;
  67. $shopId = $this->shopId;
  68. $pfShopId = $shop->pfShopId ?? 0;
  69. $adminId = $this->shopAdminId;
  70. $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
  71. // ==================== 【性能优化】引入最终海报结果的 Redis 全局强缓存 ====================
  72. // 缓存键包含店员ID、门店ID、环境版本,保证多维度的唯一性
  73. $posterCacheKey = "wx_mini_mall_poster_url:{$shopId}:{$adminId}:{$envVersion}";
  74. $cachedPoster = Yii::$app->redis->executeCommand('GET', [$posterCacheKey]);
  75. if (!empty($cachedPoster)) {
  76. $respond = json_decode($cachedPoster, true);
  77. if (!empty($respond['goodsUrl']) && !empty($respond['goodsLink'])) {
  78. util::success($respond); // 缓存直接命中,毫秒级快速返回!
  79. }
  80. }
  81. $merchant = WxOpenClass::getMallWxInfo();
  82. $goodsPage = 'pages/home/index';
  83. $ptStyle = dict::getDict('ptStyle', 'mall');
  84. $scene = 'a=' . $adminId . '&s=' . $shopId;
  85. $goodsImgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $goodsPage, $scene, $ptStyle, $envVersion);
  86. $sjName = $shop->merchantName ?? '';
  87. $shopName = $shop->shopName ?? '';
  88. $name = $shopName == '首店' ? $sjName : $sjName . $shopName;
  89. $prefix = imgUtil::getPrefix();
  90. $titleBase64 = stringUtil::ossBase64($name);
  91. if ($pfShopId > 0) {
  92. $goodsUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  93. $goodsMiniCodeBase64 = stringUtil::ossBase64($goodsImgUrl);
  94. $goodsUrl .= '/watermark,image_' . $goodsMiniCodeBase64 . ',g_north,x_0,y_40';
  95. $goodsUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  96. } else {
  97. $goodsUrl = imgUtil::groupImg($goodsImgUrl);
  98. }
  99. if (getenv('YII_ENV') != 'production' && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) {
  100. $goodsLink = 'www.google.com';
  101. } else {
  102. $goodsShortUrl = 'wx_mini_goods_short_url:' . $shopId;
  103. $goodsLink = Yii::$app->redis->executeCommand('GET', [$goodsShortUrl]);
  104. if (empty($goodsLink)) {
  105. $goods = miniUtil::generateShortLink($merchant, 'pages/home/index?account=' . $shopId, '', $ptStyle);
  106. $goodsLink = $goods['link'] ?? '';
  107. Yii::$app->redis->executeCommand('SETEX', [$goodsShortUrl, 86400 * 3, $goodsLink]);
  108. }
  109. }
  110. $respond = [
  111. 'goodsUrl' => $goodsUrl,
  112. 'goodsLink' => $goodsLink,
  113. ];
  114. // ==================== 【性能优化】保存最终拼接和生成结果到 Redis 中,过期时间 3 天 ====================
  115. Yii::$app->redis->executeCommand('SETEX', [$posterCacheKey, 86400 * 3, json_encode($respond)]);
  116. util::success($respond);
  117. }
  118. //相册太阳码 ssh
  119. public function actionGetAlbumPoster()
  120. {
  121. $shop = $this->shop;
  122. $shopId = $this->shopId;
  123. $pfShopId = $shop->pfShopId ?? 0;
  124. $merchant = WxOpenClass::getMallWxInfo();
  125. $goodsPage = 'pages/home/mall';
  126. $ptStyle = dict::getDict('ptStyle', 'mall');
  127. $scene = 'a=' . $this->shopAdminId . '&s=' . $this->shopId;
  128. $goodsImgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $goodsPage, $scene, $ptStyle);
  129. $prefix = imgUtil::getPrefix();
  130. $titleBase64 = stringUtil::ossBase64('相册');
  131. if ($pfShopId > 0) {
  132. $albumUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  133. $goodsMiniCodeBase64 = stringUtil::ossBase64($goodsImgUrl);
  134. $albumUrl .= '/watermark,image_' . $goodsMiniCodeBase64 . ',g_north,x_0,y_40';
  135. $albumUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  136. } else {
  137. $albumUrl = imgUtil::groupImg($goodsImgUrl);
  138. }
  139. if (getenv('YII_ENV') != 'production' && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) {
  140. $albumLink = 'www.google.com';
  141. } else {
  142. $itemShortUrl = 'wx_mini_item_short_url:' . $shopId;
  143. $albumLink = Yii::$app->redis->executeCommand('GET', [$itemShortUrl]);
  144. if (empty($albumLink)) {
  145. $image = miniUtil::generateShortLink($merchant, 'pages/home/mall?account=' . $shopId, '', $ptStyle);
  146. $albumLink = $image['link'] ?? '';
  147. Yii::$app->redis->executeCommand('SETEX', [$itemShortUrl, 86400 * 3, $albumLink]);
  148. }
  149. }
  150. $respond = [
  151. 'albumUrl' => $albumUrl,
  152. 'albumLink' => $albumLink,
  153. ];
  154. util::success($respond);
  155. }
  156. /**
  157. * 职责:生成并获取绿植花材的太阳码海报及短链接
  158. * 入参:HTTP GET (参数可选:env_version)
  159. * 返回:太阳码海报图片的完整 OSS 地址以及短链接 URL
  160. * 副作用:当没有缓存时,会触发向微信 API 请求并同步上传生成的图片至阿里 OSS。
  161. * 关键边界:引入了店员+环境+门店级别的最终海报数据 Redis 强缓存(3天有效期),避免每次高频无端向微信和 OSS 发起同步请求。
  162. */
  163. public function actionGetItemPoster()
  164. {
  165. $shop = $this->shop;
  166. $shopId = $this->shopId;
  167. $pfShopId = $shop->pfShopId ?? 0;
  168. $adminId = $this->shopAdminId;
  169. $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
  170. // ==================== 【性能优化】引入最终海报结果的 Redis 全局强缓存 ====================
  171. // 缓存键包含店员ID、门店ID、环境版本,保证多维度的唯一性
  172. $posterCacheKey = "wx_mini_item_poster_url_{$shopId}_{$adminId}_{$envVersion}";
  173. $cachedPoster = Yii::$app->redis->executeCommand('GET', [$posterCacheKey]);
  174. if (!empty($cachedPoster)) {
  175. $respond = json_decode($cachedPoster, true);
  176. if (!empty($respond['flowerUrl']) && !empty($respond['flowerLink'])) {
  177. util::success($respond); // 缓存直接命中,毫秒级快速返回!
  178. }
  179. }
  180. $merchant = WxOpenClass::getMallWxInfo();
  181. $ptStyle = dict::getDict('ptStyle', 'mall');
  182. $scene = 'a=' . $adminId . '&s=' . $shopId;
  183. $flowerPage = 'pages/item/item';
  184. $flowerImgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $flowerPage, $scene, $ptStyle, $envVersion);
  185. $sjName = $shop->merchantName ?? '';
  186. $shopName = $shop->shopName ?? '';
  187. $name = $shopName == '首店' ? $sjName : $sjName . $shopName;
  188. $prefix = imgUtil::getPrefix();
  189. $titleBase64 = stringUtil::ossBase64($name);
  190. if ($pfShopId > 0) {
  191. $flowerUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  192. $flowerMiniCodeBase64 = stringUtil::ossBase64($flowerImgUrl);
  193. $flowerUrl .= '/watermark,image_' . $flowerMiniCodeBase64 . ',g_north,x_0,y_40';
  194. $flowerUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  195. } else {
  196. $flowerUrl = imgUtil::groupImg($flowerImgUrl);
  197. }
  198. if (getenv('YII_ENV') != 'production' && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) {
  199. $flowerLink = 'www.google.com';
  200. } else {
  201. $imageShortUrl = 'wx_mini_image_short_url:' . $shopId;
  202. $flowerLink = Yii::$app->redis->executeCommand('GET', [$imageShortUrl]);
  203. if (empty($flowerLink)) {
  204. $item = miniUtil::generateShortLink($merchant, 'pages/item/item?account=' . $shopId, '', $ptStyle);
  205. $flowerLink = $item['link'] ?? '';
  206. Yii::$app->redis->executeCommand('SETEX', [$imageShortUrl, 86400 * 3, $flowerLink]);
  207. }
  208. }
  209. $respond = [
  210. 'flowerUrl' => $flowerUrl,
  211. 'flowerLink' => $flowerLink,
  212. ];
  213. // ==================== 【性能优化】保存最终拼接和生成结果到 Redis 中,过期时间 3 天 ====================
  214. Yii::$app->redis->executeCommand('SETEX', [$posterCacheKey, 86400 * 3, json_encode($respond)]);
  215. util::success($respond);
  216. }
  217. //会员注册码 ssh 20220607
  218. public function actionGetMemberPoster()
  219. {
  220. $merchant = WxOpenClass::getMallWxInfo();
  221. $page = 'pages/login/index';
  222. $ptStyle = dict::getDict('ptStyle', 'mall');
  223. $scene = 'a=' . $this->shopAdminId . '&s=' . $this->shopId;
  224. $imgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  225. $prefix = imgUtil::getPrefix();
  226. $titleBase64 = stringUtil::ossBase64('扫码注册');
  227. $memberUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  228. $memberMiniCodeBase64 = stringUtil::ossBase64($imgUrl);
  229. $memberUrl .= '/watermark,image_' . $memberMiniCodeBase64 . ',g_north,x_0,y_40';
  230. $memberUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  231. $respond = ['imgUrl' => $memberUrl];
  232. util::success($respond);
  233. }
  234. public function actionDemo()
  235. {
  236. return $this->renderPartial('demo');
  237. }
  238. //新客户 ssh 2019.12.29
  239. public function actionVisit()
  240. {
  241. $data = json_encode(array(
  242. 'type' => 'msg',
  243. 'msg' => 'aaaa',
  244. ));
  245. $id = 'merchantConsole_' . $this->sjId;
  246. $online = Gateway::getClientIdByUid($id);
  247. if ($online) {
  248. //直接发送
  249. Gateway::sendToUid($id, json_encode($data));
  250. } else {
  251. //保存
  252. }
  253. }
  254. //新订单 ssh 2019.12.29
  255. public function actionOrder()
  256. {
  257. $data = json_encode(array(
  258. 'type' => 'msg',
  259. 'msg' => 'aaaa',
  260. ));
  261. $id = 'merchantConsole_' . $this->sjId;
  262. $online = Gateway::getClientIdByUid($id);
  263. if ($online) {
  264. //直接发送
  265. Gateway::sendToUid($id, json_encode($data));
  266. } else {
  267. //保存
  268. }
  269. }
  270. //我的 ssh 2021.4.8
  271. public function actionMy()
  272. {
  273. $lookMoney = \bizGhs\shop\classes\ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  274. $shop = $this->shop;
  275. $shopId = $this->shopId;
  276. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  277. $main = $this->main;
  278. $mainId = $main->id ?? 0;
  279. $balance = $main->balance ?? 0.00;
  280. $money = $main->money ?? 0;
  281. $txBalance = $main->txBalance ?? 0.00;
  282. $sj = isset($this->sj) && !empty($this->sj) ? $this->sj->attributes : [];
  283. $shopImg = Yii::$app->params['hdImgHost'] . 'retail/default-img.png';
  284. if (isset($shop->avatar) && !empty($shop->avatar)) {
  285. $shopImg = Yii::$app->params['ghsImgHost'] . $shop->avatar;
  286. }
  287. // 到期时间取当前门店 xhShop.deadline(续费、业务校验均以此为准)
  288. $deadline = $shop->deadline ?? '';
  289. $relation = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  290. $adminName = $relation['name'] ?? '';
  291. $eId = $relation['id'] ?? 0;
  292. $mobile = $relation['mobile'] ?? '';
  293. $sjName = $sj['name'] ?? '';
  294. $shopName = $shop->shopName ?? '';
  295. $cashAccount = $main->cashAccount ?? '';
  296. $walletBalance = $main->walletBalance ?? '0.00';
  297. $data = [
  298. 'balance' => $balance,
  299. 'txBalance' => $txBalance,
  300. 'walletBalance' => $walletBalance,
  301. 'shopImg' => $shopImg,
  302. 'deadline' => $deadline,
  303. 'adminName' => $adminName,
  304. 'adminMobile' => $mobile,
  305. 'sjName' => $sjName,
  306. 'shopName' => $shopName,
  307. 'cashAccount' => $cashAccount,
  308. 'shopAdminId' => $eId,
  309. 'shopId' => $this->shopId,
  310. 'sjId' => $this->sjId,
  311. 'shopInfo' => $shop,
  312. 'shopExt' => $ext,
  313. 'money' => $money,
  314. 'mainId' => $mainId,
  315. 'lookMoney' => $lookMoney,
  316. ];
  317. util::success($data);
  318. }
  319. //经营概况 ssh 2021.1.27
  320. public function actionProfile()
  321. {
  322. //余额
  323. $shopInfo = ShopClass::getById($this->shopId);
  324. //库存情况
  325. $stockInfo = ProductClass::getProductStockByShopId($this->sjId, $this->shopId);
  326. //今日销售金额
  327. $today = StatIncomeClass::getAmountToday($this->shopId);
  328. //近七天销售金额
  329. $latestSeven = StatIncomeClass::getAmountWeek($this->shopId);
  330. //30天销售金额
  331. $latestThirty = StatIncomeClass::getAmountThirty($this->shopId);
  332. $base = [
  333. 'balance' => $shopInfo['balance'],
  334. 'mayGathering' => $shopInfo['mayGathering'] ?? 0.00,
  335. 'mayPay' => $shopInfo['mayPay'] ?? 0.00,
  336. 'stockCost' => $stockInfo['stockCost'],
  337. 'stockNum' => 0,
  338. 'stockBigNum' => $stockInfo['stockBigNum'],
  339. 'stockSmallNum' => $stockInfo['stockSmallNum'],
  340. 'stockWarning' => $stockInfo['stockWarning'],
  341. 'today' => $today,
  342. 'todayCompareYesterday' => 0,
  343. 'latestSeven' => $latestSeven,
  344. 'latestSevenSameTerm' => 0,
  345. 'latestThirty' => $latestThirty,
  346. 'latestThirtySameTerm' => 0,
  347. ];
  348. //今日订单数
  349. $saleNum = StatOrderClass::getRiseNumToday($this->shopId);
  350. //今日销售金额
  351. $saleAmount = $today;
  352. //昨日订单数
  353. $ySaleNum = StatOrderClass::getRiseNumYesterday($this->shopId);
  354. //昨日销售金额
  355. $ySaleAmount = StatIncomeClass::getAmountYesterday($this->shopId);
  356. //7天订单数
  357. $sevenSaleNum = StatOrderClass::getRiseNumWeek($this->shopId);
  358. //7天销售金额
  359. $sevenSaleAmount = $latestSeven;
  360. //30天订单数
  361. $thirtySaleNum = StatOrderClass::getRiseNumThirty($this->shopId);
  362. //30天销售金额
  363. $thirtySaleAmount = $latestThirty;
  364. //今日采购
  365. $todayCg = StatCgClass::getTodayCg($this->shop);
  366. //昨日采购
  367. $yesCg = StatCgClass::getYesterdayCg($this->shop);
  368. //7日采购
  369. $sevenCg = StatCgClass::getSevenCg($this->shop);
  370. //30日采购
  371. $thirtyCg = StatCgClass::getThirtyCg($this->shop);
  372. $more = [
  373. 'today' => [
  374. 'income' => 0,
  375. 'expend' => 0,
  376. 'saleNum' => $saleNum,
  377. 'saleAmount' => $saleAmount,
  378. 'purchaseNum' => $todayCg['num'] ?? 0,
  379. 'purchaseAmount' => $todayCg['amount'] ?? 0.00,
  380. ],
  381. 'yesterday' => [
  382. 'income' => 0,
  383. 'expend' => 0,
  384. 'saleNum' => $ySaleNum,
  385. 'saleAmount' => $ySaleAmount,
  386. 'purchaseNum' => $yesCg['num'] ?? 0,
  387. 'purchaseAmount' => $yesCg['amount'] ?? 0.00,
  388. ],
  389. 'latestSeven' => [
  390. 'income' => 0,
  391. 'expend' => 0,
  392. 'saleNum' => $sevenSaleNum,
  393. 'saleAmount' => $sevenSaleAmount,
  394. 'purchaseNum' => $sevenCg['num'] ?? 0,
  395. 'purchaseAmount' => $sevenCg['amount'] ?? 0.00,
  396. ],
  397. 'latestThirty' => [
  398. 'income' => 0,
  399. 'expend' => 0,
  400. 'saleNum' => $thirtySaleNum,
  401. 'saleAmount' => $thirtySaleAmount,
  402. 'purchaseNum' => $thirtyCg['num'] ?? 0,
  403. 'purchaseAmount' => $thirtyCg['amount'] ?? 0.00,
  404. ],
  405. ];
  406. util::success(['base' => $base, 'more' => $more]);
  407. }
  408. // 查询小程序的同城配送的微信门店编号
  409. public function actionWxStoreId()
  410. {
  411. $main = MainClass::getById($this->mainId, true, 'id, wxStoreId');
  412. util::success(['wx' => $main]);
  413. }
  414. }