MainController.php 18 KB

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