MainController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /**
  162. * 职责:生成并获取绿植花材的太阳码海报及短链接
  163. * 入参:HTTP GET (参数可选:env_version)
  164. * 返回:太阳码海报图片的完整 OSS 地址以及短链接 URL
  165. * 副作用:当没有缓存时,会触发向微信 API 请求并同步上传生成的图片至阿里 OSS。
  166. * 关键边界:引入了店员+环境+门店级别的最终海报数据 Redis 强缓存(3天有效期),避免每次高频无端向微信和 OSS 发起同步请求。
  167. */
  168. public function actionGetItemPoster()
  169. {
  170. $shop = $this->shop;
  171. $shopId = $this->shopId;
  172. $pfShopId = $shop->pfShopId ?? 0;
  173. $adminId = $this->shopAdminId;
  174. $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
  175. // ==================== 【性能优化】引入最终海报结果的 Redis 全局强缓存 ====================
  176. // 缓存键包含店员ID、门店ID、环境版本,保证多维度的唯一性
  177. $posterCacheKey = "wx_mini_item_poster_url_{$shopId}_{$adminId}_{$envVersion}";
  178. $cachedPoster = Yii::$app->redis->executeCommand('GET', [$posterCacheKey]);
  179. if (!empty($cachedPoster)) {
  180. $respond = json_decode($cachedPoster, true);
  181. if (!empty($respond['flowerUrl']) && !empty($respond['flowerLink'])) {
  182. util::success($respond); // 缓存直接命中,毫秒级快速返回!
  183. }
  184. }
  185. $merchant = WxOpenClass::getMallWxInfo();
  186. $ptStyle = dict::getDict('ptStyle', 'mall');
  187. $scene = 'a=' . $adminId . '&s=' . $shopId;
  188. $flowerPage = 'pages/item/item';
  189. $flowerImgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $flowerPage, $scene, $ptStyle, $envVersion);
  190. $sjName = $shop->merchantName ?? '';
  191. $shopName = $shop->shopName ?? '';
  192. $name = $shopName == '首店' ? $sjName : $sjName . $shopName;
  193. $prefix = imgUtil::getPrefix();
  194. $titleBase64 = stringUtil::ossBase64($name);
  195. if ($pfShopId > 0) {
  196. $flowerUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  197. $flowerMiniCodeBase64 = stringUtil::ossBase64($flowerImgUrl);
  198. $flowerUrl .= '/watermark,image_' . $flowerMiniCodeBase64 . ',g_north,x_0,y_40';
  199. $flowerUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  200. } else {
  201. $flowerUrl = imgUtil::groupImg($flowerImgUrl);
  202. }
  203. if (getenv('YII_ENV') != 'production' && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on')) {
  204. $flowerLink = 'www.google.com';
  205. } else {
  206. $imageShortUrl = 'wx_mini_image_short_url' . $shopId;
  207. $flowerLink = Yii::$app->redis->executeCommand('GET', [$imageShortUrl]);
  208. if (empty($flowerLink)) {
  209. $item = miniUtil::generateShortLink($merchant, 'pages/item/item?account=' . $shopId, '', $ptStyle);
  210. $flowerLink = $item['link'] ?? '';
  211. Yii::$app->redis->executeCommand('SETEX', [$imageShortUrl, 86400 * 3, $flowerLink]);
  212. }
  213. }
  214. $respond = [
  215. 'flowerUrl' => $flowerUrl,
  216. 'flowerLink' => $flowerLink,
  217. ];
  218. // ==================== 【性能优化】保存最终拼接和生成结果到 Redis 中,过期时间 3 天 ====================
  219. Yii::$app->redis->executeCommand('SETEX', [$posterCacheKey, 86400 * 3, json_encode($respond)]);
  220. util::success($respond);
  221. }
  222. //会员注册码 ssh 20220607
  223. public function actionGetMemberPoster()
  224. {
  225. $merchant = WxOpenClass::getMallWxInfo();
  226. $page = 'pages/login/index';
  227. $ptStyle = dict::getDict('ptStyle', 'mall');
  228. $scene = 'a=' . $this->shopAdminId . '&s=' . $this->shopId;
  229. $imgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle);
  230. $prefix = imgUtil::getPrefix();
  231. $titleBase64 = stringUtil::ossBase64('扫码注册');
  232. $memberUrl = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
  233. $memberMiniCodeBase64 = stringUtil::ossBase64($imgUrl);
  234. $memberUrl .= '/watermark,image_' . $memberMiniCodeBase64 . ',g_north,x_0,y_40';
  235. $memberUrl .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
  236. $respond = ['imgUrl' => $memberUrl];
  237. util::success($respond);
  238. }
  239. public function actionDemo()
  240. {
  241. return $this->renderPartial('demo');
  242. }
  243. //新客户 ssh 2019.12.29
  244. public function actionVisit()
  245. {
  246. $data = json_encode(array(
  247. 'type' => 'msg',
  248. 'msg' => 'aaaa',
  249. ));
  250. $id = 'merchantConsole_' . $this->sjId;
  251. $online = Gateway::getClientIdByUid($id);
  252. if ($online) {
  253. //直接发送
  254. Gateway::sendToUid($id, json_encode($data));
  255. } else {
  256. //保存
  257. }
  258. }
  259. //新订单 ssh 2019.12.29
  260. public function actionOrder()
  261. {
  262. $data = json_encode(array(
  263. 'type' => 'msg',
  264. 'msg' => 'aaaa',
  265. ));
  266. $id = 'merchantConsole_' . $this->sjId;
  267. $online = Gateway::getClientIdByUid($id);
  268. if ($online) {
  269. //直接发送
  270. Gateway::sendToUid($id, json_encode($data));
  271. } else {
  272. //保存
  273. }
  274. }
  275. //我的 ssh 2021.4.8
  276. public function actionMy()
  277. {
  278. $lookMoney = \bizGhs\shop\classes\ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  279. $shop = $this->shop;
  280. $shopId = $this->shopId;
  281. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  282. $main = $this->main;
  283. $mainId = $main->id ?? 0;
  284. $balance = $main->balance ?? 0.00;
  285. $money = $main->money ?? 0;
  286. $txBalance = $main->txBalance ?? 0.00;
  287. $sj = isset($this->sj) && !empty($this->sj) ? $this->sj->attributes : [];
  288. $shopImg = Yii::$app->params['hdImgHost'] . 'retail/default-img.png';
  289. if (isset($shop->avatar) && !empty($shop->avatar)) {
  290. $shopImg = Yii::$app->params['ghsImgHost'] . $shop->avatar;
  291. }
  292. // 到期时间取当前门店 xhShop.deadline(续费、业务校验均以此为准)
  293. $deadline = $shop->deadline ?? '';
  294. $relation = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  295. $adminName = $relation['name'] ?? '';
  296. $eId = $relation['id'] ?? 0;
  297. $mobile = $relation['mobile'] ?? '';
  298. $sjName = $sj['name'] ?? '';
  299. $shopName = $shop->shopName ?? '';
  300. $cashAccount = $main->cashAccount ?? '';
  301. $walletBalance = $main->walletBalance ?? '0.00';
  302. $data = [
  303. 'balance' => $balance,
  304. 'txBalance' => $txBalance,
  305. 'walletBalance' => $walletBalance,
  306. 'shopImg' => $shopImg,
  307. 'deadline' => $deadline,
  308. 'adminName' => $adminName,
  309. 'adminMobile' => $mobile,
  310. 'sjName' => $sjName,
  311. 'shopName' => $shopName,
  312. 'cashAccount' => $cashAccount,
  313. 'shopAdminId' => $eId,
  314. 'shopId' => $this->shopId,
  315. 'sjId' => $this->sjId,
  316. 'shopInfo' => $shop,
  317. 'shopExt' => $ext,
  318. 'money' => $money,
  319. 'mainId' => $mainId,
  320. 'lookMoney' => $lookMoney,
  321. ];
  322. util::success($data);
  323. }
  324. //经营概况 ssh 2021.1.27
  325. public function actionProfile()
  326. {
  327. //余额
  328. $shopInfo = ShopClass::getById($this->shopId);
  329. //库存情况
  330. $stockInfo = ProductClass::getProductStockByShopId($this->sjId, $this->shopId);
  331. //今日销售金额
  332. $today = StatIncomeClass::getAmountToday($this->shopId);
  333. //近七天销售金额
  334. $latestSeven = StatIncomeClass::getAmountWeek($this->shopId);
  335. //30天销售金额
  336. $latestThirty = StatIncomeClass::getAmountThirty($this->shopId);
  337. $base = [
  338. 'balance' => $shopInfo['balance'],
  339. 'mayGathering' => $shopInfo['mayGathering'] ?? 0.00,
  340. 'mayPay' => $shopInfo['mayPay'] ?? 0.00,
  341. 'stockCost' => $stockInfo['stockCost'],
  342. 'stockNum' => 0,
  343. 'stockBigNum' => $stockInfo['stockBigNum'],
  344. 'stockSmallNum' => $stockInfo['stockSmallNum'],
  345. 'stockWarning' => $stockInfo['stockWarning'],
  346. 'today' => $today,
  347. 'todayCompareYesterday' => 0,
  348. 'latestSeven' => $latestSeven,
  349. 'latestSevenSameTerm' => 0,
  350. 'latestThirty' => $latestThirty,
  351. 'latestThirtySameTerm' => 0,
  352. ];
  353. //今日订单数
  354. $saleNum = StatOrderClass::getRiseNumToday($this->shopId);
  355. //今日销售金额
  356. $saleAmount = $today;
  357. //昨日订单数
  358. $ySaleNum = StatOrderClass::getRiseNumYesterday($this->shopId);
  359. //昨日销售金额
  360. $ySaleAmount = StatIncomeClass::getAmountYesterday($this->shopId);
  361. //7天订单数
  362. $sevenSaleNum = StatOrderClass::getRiseNumWeek($this->shopId);
  363. //7天销售金额
  364. $sevenSaleAmount = $latestSeven;
  365. //30天订单数
  366. $thirtySaleNum = StatOrderClass::getRiseNumThirty($this->shopId);
  367. //30天销售金额
  368. $thirtySaleAmount = $latestThirty;
  369. //今日采购
  370. $todayCg = StatCgClass::getTodayCg($this->shop);
  371. //昨日采购
  372. $yesCg = StatCgClass::getYesterdayCg($this->shop);
  373. //7日采购
  374. $sevenCg = StatCgClass::getSevenCg($this->shop);
  375. //30日采购
  376. $thirtyCg = StatCgClass::getThirtyCg($this->shop);
  377. $more = [
  378. 'today' => [
  379. 'income' => 0,
  380. 'expend' => 0,
  381. 'saleNum' => $saleNum,
  382. 'saleAmount' => $saleAmount,
  383. 'purchaseNum' => $todayCg['num'] ?? 0,
  384. 'purchaseAmount' => $todayCg['amount'] ?? 0.00,
  385. ],
  386. 'yesterday' => [
  387. 'income' => 0,
  388. 'expend' => 0,
  389. 'saleNum' => $ySaleNum,
  390. 'saleAmount' => $ySaleAmount,
  391. 'purchaseNum' => $yesCg['num'] ?? 0,
  392. 'purchaseAmount' => $yesCg['amount'] ?? 0.00,
  393. ],
  394. 'latestSeven' => [
  395. 'income' => 0,
  396. 'expend' => 0,
  397. 'saleNum' => $sevenSaleNum,
  398. 'saleAmount' => $sevenSaleAmount,
  399. 'purchaseNum' => $sevenCg['num'] ?? 0,
  400. 'purchaseAmount' => $sevenCg['amount'] ?? 0.00,
  401. ],
  402. 'latestThirty' => [
  403. 'income' => 0,
  404. 'expend' => 0,
  405. 'saleNum' => $thirtySaleNum,
  406. 'saleAmount' => $thirtySaleAmount,
  407. 'purchaseNum' => $thirtyCg['num'] ?? 0,
  408. 'purchaseAmount' => $thirtyCg['amount'] ?? 0.00,
  409. ],
  410. ];
  411. util::success(['base' => $base, 'more' => $more]);
  412. }
  413. // 查询小程序的同城配送的微信门店编号
  414. public function actionWxStoreId()
  415. {
  416. $main = MainClass::getById($this->mainId, true, 'id, wxStoreId');
  417. util::success(['wx' => $main]);
  418. }
  419. }