| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- /**
- * ghsApp 门店邀请 / 我的分佣(邀请花店开店)
- * 数据与 hd 共用 xhMainInvite、xhMainInviteFlow 等表
- */
- namespace ghs\controllers;
- use bizHd\shop\classes\MainInviteClass;
- use bizHd\shop\classes\MainInviteCommissionClass;
- use bizHd\shop\classes\MainInviteFlowClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\dirUtil;
- use common\components\imgUtil;
- use common\components\miniUtil;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- class MainInviteController extends BaseController
- {
- public $guestAccess = [];
- /**
- * 我的分佣首页
- */
- public function actionIndex()
- {
- $mainId = (int) $this->mainId;
- $invite = MainInviteClass::getByMainId($mainId);
- $inviteData = [
- 'inviteCode' => '',
- 'inviteCodeMask' => '',
- 'totalCommission' => '0.00',
- 'ableCommission' => '0.00',
- 'commissionAmount' => '0.00',
- 'hdDiscountAmount' => '0.00',
- 'inviteCount' => 0,
- ];
- if (!empty($invite)) {
- $code = $invite['inviteCode'] ?? '';
- $inviteData = [
- 'inviteCode' => $code,
- 'inviteCodeMask' => MainInviteCommissionClass::maskInviteCode($code),
- 'totalCommission' => round((float) ($invite['totalCommission'] ?? 0), 2),
- 'ableCommission' => round((float) ($invite['ableCommission'] ?? 0), 2),
- 'commissionAmount' => round((float) ($invite['commissionAmount'] ?? 0), 2),
- 'hdDiscountAmount' => round((float) ($invite['hdDiscountAmount'] ?? 0), 2),
- 'inviteCount' => (int) ($invite['inviteCount'] ?? 0),
- ];
- }
- $recentRaw = MainInviteCommissionClass::getRecentList($mainId, 5);
- $recentList = [];
- foreach ($recentRaw as $row) {
- $recentList[] = MainInviteCommissionClass::formatRecentRow($row);
- }
- util::success([
- 'invite' => $inviteData,
- 'recentList' => $recentList,
- ]);
- }
- /**
- * 邀约花店列表
- */
- public function actionInviteShopList()
- {
- $get = Yii::$app->request->get();
- $keyword = trim($get['keyword'] ?? '');
- $data = MainInviteCommissionClass::getInviteShopList($this->mainId, $keyword);
- util::success($data);
- }
- /**
- * 佣金变动明细分页
- */
- public function actionCommissionList()
- {
- $list = MainInviteFlowClass::getFlowList($this->mainId);
- util::success($list);
- }
- /**
- * 可提现佣金全额存入中央钱包余额
- */
- public function actionDepositBalance()
- {
- $mainId = (int) $this->mainId;
- $shopId = (int) $this->shopId;
- $shopName = trim((string) ($this->shop->name ?? ''));
- try {
- $result = MainInviteClass::depositAbleCommissionToWallet($mainId, $shopId, $shopName);
- $invite = MainInviteClass::getByMainId($mainId);
- $inviteData = [
- 'totalCommission' => round((float) ($invite['totalCommission'] ?? 0), 2),
- 'ableCommission' => round((float) ($invite['ableCommission'] ?? 0), 2),
- ];
- util::success(array_merge($result, ['invite' => $inviteData]));
- } catch (\Exception $e) {
- util::fail($e->getMessage());
- }
- }
- /**
- * 生成 hdApp 邀请开店海报(参考 main/get-mall-poster:太阳码 + OSS 背景合成)
- * GET:可选 env_version=develop|trial|release
- */
- public function actionGetInviteHdPoster()
- {
- $mainId = (int) $this->mainId;
- $invite = MainInviteClass::getByMainId($mainId);
- $inviteCode = trim((string) ($invite['inviteCode'] ?? ''));
- if ($inviteCode === '') {
- util::fail('暂无邀请码');
- }
- $envVersion = miniUtil::normalizeMiniEnvVersion(Yii::$app->request->get('env_version', 'release'));
- $posterCacheKey = 'wx_mini_invite_hd_poster_v3_' . $mainId . '_' . $inviteCode . '_' . $envVersion;
- $cachedPoster = Yii::$app->redis->executeCommand('GET', [$posterCacheKey]);
- if (!empty($cachedPoster)) {
- $respond = json_decode($cachedPoster, true);
- if (!empty($respond['imgUrl']) && !empty($respond['miniCodeUrl'])) {
- util::success($respond);
- }
- }
- $merchant = WxOpenClass::getWxInfo();
- $page = 'pagesClient/official/applyInvite';
- $ptStyle = dict::getDict('ptStyle', 'hd');
- $scene = 'inviteCode=' . $inviteCode;
- // 开通页可能尚未发布到体验版,check_path=false 仍可生成码图;并校验本地 JPEG 避免缓存错误 JSON
- $imgUrl = $this->ensureValidInviteHdMiniCode($merchant, $page, $scene, $ptStyle, $envVersion);
- $miniCodeUrl = imgUtil::groupImg($imgUrl);
- $titleBase64 = stringUtil::ossBase64('邀请开店');
- $prefix = imgUtil::getPrefix();
- $url = $prefix . 'shop/miniCodeBg.jpg?x-oss-process=image';
- $miniCodeBase64 = stringUtil::ossBase64($imgUrl);
- $url .= '/watermark,image_' . $miniCodeBase64 . ',g_north,x_0,y_40';
- $url .= '/watermark,text_' . $titleBase64 . ',g_north,x_0,y_530,size_50';
- $respond = [
- 'imgUrl' => $url,
- 'miniCodeUrl' => $miniCodeUrl,
- 'inviteCode' => $inviteCode,
- ];
- Yii::$app->redis->executeCommand('SETEX', [$posterCacheKey, 86400 * 3, json_encode($respond)]);
- util::success($respond);
- }
- /**
- * 生成 hd 邀请开店小程序码相对路径,无效缓存文件时删除并重试一次
- */
- private function ensureValidInviteHdMiniCode($merchant, $page, $scene, $ptStyle, $envVersion)
- {
- $relative = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion, false);
- $fullPath = dirUtil::getImgDir() . $relative;
- if ($this->isValidJpegFile($fullPath)) {
- return $relative;
- }
- if (is_file($fullPath)) {
- @unlink($fullPath);
- }
- $relative = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $ptStyle, $envVersion, false);
- $fullPath = dirUtil::getImgDir() . $relative;
- if (!$this->isValidJpegFile($fullPath)) {
- if (is_file($fullPath)) {
- @unlink($fullPath);
- }
- util::fail('小程序码生成失败,请确认花店端已上传体验版并包含开通页');
- }
- return $relative;
- }
- /** 判断本地文件是否为有效 JPEG(微信失败时可能写入 JSON 文本) */
- private function isValidJpegFile($fullPath)
- {
- if (!is_file($fullPath) || filesize($fullPath) < 100) {
- return false;
- }
- $head = @file_get_contents($fullPath, false, null, 0, 2);
- return $head === "\xFF\xD8";
- }
- }
|