Files
huangjingfen/pro_v3.5.1_副本/app/services/wechat/WechatServices.php
apple 434aa8c69d feat(fsgx): 完成全部24项开发任务 Phase1-7
Phase1 后端核心:
- 新增 fsgx_v1.sql 迁移脚本(is_queue_goods/frozen_points/available_points/no_assess)
- SystemConfigServices 返佣设置扩展(周期人数/分档比例/范围/时机)
- StoreOrderCreateServices 周期循环佣金计算
- StoreOrderTakeServices 佣金发放后同步冻结积分
- StoreProductServices/StoreProduct 保存 is_queue_goods

Phase2 后端接口:
- GET /api/hjf/brokerage/progress 佣金周期进度
- GET /api/hjf/assets/overview 资产总览
- HjfPointsServices 每日 frozen_points 0.4‰ 释放定时任务
- PUT /adminapi/hjf/member/{uid}/no_assess 不考核接口
- GET /adminapi/hjf/points/release_log 积分日志接口

Phase3 前端清理:
- hjfCustom.js 路由精简(仅保留 points/log)
- hjfQueue.js/hjfMember.js API 清理/重定向至 CRMEB 原生接口
- pages.json 公排→推荐佣金/佣金记录/佣金规则

Phase4-5 前端改造:
- queue/status.vue 推荐佣金进度页整体重写
- 商品详情/订单确认/支付结果页文案与逻辑改造
- 个人中心/资产页/引导页/规则页文案改造
- HjfQueueProgress/HjfRefundNotice/HjfAssetCard 组件改造
- 推广中心嵌入佣金进度摘要
- hjfMockData.js 全量更新(公排字段→佣金字段)

Phase6 Admin 增强:
- 用户列表新增 frozen_points/available_points 列及不考核操作按钮
- hjfPoints.js USE_MOCK=false 对接真实积分日志接口

Phase7 配置文档:
- docs/fsgx-phase7-config-checklist.md 后台配置与全链路验收清单

Made-with: Cursor
2026-03-23 22:32:19 +08:00

479 lines
18 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\services\wechat;
use app\services\BaseServices;
use app\dao\wechat\WechatUserDao;
use app\services\user\UserServices;
use app\services\user\UserVisitServices;
use crmeb\services\CacheService;
use crmeb\services\CacheService as Cache;
use crmeb\services\wechat\OfficialAccount;
use crmeb\services\wechat\Work;
use crmeb\utils\Canvas;
use EasyWeChat\Kernel\Exceptions\BadRequestException;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use GuzzleHttp\Exception\GuzzleException;
use think\annotation\Inject;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\ValidateException;
use think\facade\Log;
/**
*
* Class WechatServices
* @package app\services\wechat
* @mixin WechatUserDao
*/
class WechatServices extends BaseServices
{
/**
* @var WechatUserDao
*/
#[Inject]
protected WechatUserDao $dao;
/**
* 微信公众号服务
* @return \think\Response
* @throws BadRequestException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws \ReflectionException
*/
public function serve()
{
ob_clean();
$res = OfficialAccount::serve();
//
return $res;
}
/**
* 企业微信服务
* @return \think\Response
* @throws BadRequestException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws \ReflectionException
*/
public function workServe()
{
ob_clean();
return Work::serve();
}
/**
* 公众号权限配置信息获取
* @param $url
* @return array
*/
public function config($url): array
{
return OfficialAccount::jsSdk($url);
}
/**
* 获取授权信息
* @param string $code
* @return array
*/
public function getAuthWechatInfo()
{
try {
$userInfoConfig = OfficialAccount::tokenFromCode();
} catch (\Throwable $e) {
\think\facade\Log::error(
json_encode([
'error' => '授权失败:' . $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine()
], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
);
throw new ValidateException('授权失败');
}
if (!isset($userInfoConfig['openid']) || !$userInfoConfig['openid']) {
throw new ValidateException('openid获取失败');
}
return $userInfoConfig;
}
/**
* 获取返回信息
* @param $user
* @param string $userType
* @return array
*/
public function getReturnInfo($user, string $userType = 'wechat')
{
if (!$user || !isset($user['uid']) || !$user['uid']) {
throw new ValidateException('获取用户信息失败');
}
$uid = (int)$user['uid'];
$token = $this->createToken($uid, $userType, $user['pwd'] ?? '');
if (!$token) {
throw new ValidateException('登录失败!');
}
/** @var UserVisitServices $visitServices */
$visitServices = app()->make(UserVisitServices::class);
$visitServices->loginSaveVisit($user);
$token['store_user_avatar'] = 0;
$token['userInfo'] = is_object($user) && method_exists($user, 'toArray') ? $user->toArray() : $user;
$token['expires_time'] = $token['params']['exp'] ?? 0;
// 用户登录成功事件
event('user.login', [$uid, app('request')->ip()]);
return $token;
}
/**
* 公众号授权登录返回是否需要绑定手机号token
* @param $spread_uid
* @return array
* @throws DataNotFoundException
* @throws InvalidConfigException
* @throws ModelNotFoundException
* @throws \think\db\exception\DbException
*/
public function authLogin($spread_uid = '')
{
$wechatInfo = OfficialAccount::userFromCode();
if (!isset($wechatInfo['nickname'])) {
$wechatInfo = OfficialAccount::instance()->user()->get($wechatInfo['openid']);
if (!isset($wechatInfo['nickname']))
throw new ValidateException('授权失败');
if (isset($wechatInfo['tagid_list']))
$wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
} else {
if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
}
$wechatInfo['user_type'] = 'wechat';
$openid = $wechatInfo['openid'];
/** @var WechatUserServices $wechatUserServices */
$wechatUserServices = app()->make(WechatUserServices::class);
$user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
$createData = [$openid, $wechatInfo, $spread_uid, 'wechat', 'wechat'];
//获取是否强制绑定手机号
$storeUserMobile = sys_config('store_user_mobile');
if ($storeUserMobile && (($user && $user['phone'] == '') || !$user)) {
$userInfoKey = md5($openid . '_' . time() . '_wechat');
CacheService::set($userInfoKey, $createData, 7200);
return ['bindPhone' => true, 'key' => $userInfoKey];
}
if (!$user) {
$user = $wechatUserServices->wechatOauthAfter($createData);
} else {
$wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
}
return $this->getReturnInfo($user);
}
/**
* 公众号授权登陆
* @return mixed
* @throws DataNotFoundException
* @throws ModelNotFoundException
* @throws DbException|InvalidConfigException
*/
public function auth($spread_uid, $login_type)
{
$wechatInfo = $this->getAuthWechatInfo();
if (!isset($wechatInfo['nickname'])) {
$wechatInfo = OfficialAccount::instance()->user()->get($wechatInfo['openid'])->toArray();
if (!isset($wechatInfo['nickname']))
throw new ValidateException('授权失败');
if (isset($wechatInfo['tagid_list']))
$wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
} else {
if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
/** @var WechatUserServices $wechatUser */
$wechatUser = app()->make(WechatUserServices::class);
if (!$wechatUser->getOne(['openid' => $wechatInfo['openid']])) {
$wechatInfo['subscribe'] = 0;
}
}
$wechatInfo['user_type'] = 'wechat';
$openid = $wechatInfo['openid'];
/** @var WechatUserServices $wechatUserServices */
$wechatUserServices = app()->make(WechatUserServices::class);
$user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
$createData = [$openid, $wechatInfo, $spread_uid, $login_type, 'wechat'];
if (!$user) {
$user = $wechatUserServices->wechatOauthAfter($createData);
} else {
//更新用户信息
$wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
}
return $this->getReturnInfo($user);
}
/**
* 新公众号授权登录
* @param $spread_uid
* @param $login_type
* @return mixed
* @throws DataNotFoundException
* @throws InvalidConfigException
* @throws ModelNotFoundException
*/
public function newAuth($spread_uid, $login_type)
{
$wechatInfo = OfficialAccount::userFromCode();
if (!isset($wechatInfo['nickname'])) {
$wechatInfo = OfficialAccount::instance()->user()->get($wechatInfo['openid']);
if (!isset($wechatInfo['nickname']))
throw new ValidateException('授权失败');
if (isset($wechatInfo['tagid_list']))
$wechatInfo['tagid_list'] = implode(',', $wechatInfo['tagid_list']);
} else {
if (isset($wechatInfo['privilege'])) unset($wechatInfo['privilege']);
}
$wechatInfo['user_type'] = 'wechat';
$openid = $wechatInfo['openid'];
/** @var WechatUserServices $wechatUserServices */
$wechatUserServices = app()->make(WechatUserServices::class);
$user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
$createData = [$openid, $wechatInfo, $spread_uid, $login_type, 'wechat'];
//获取是否强制绑定手机号
$storeUserMobile = sys_config('store_user_mobile');
if ($storeUserMobile && !$user) {
$userInfoKey = md5($openid . '_' . time() . '_wechat');
Cache::setTokenBucket($userInfoKey, $createData, 7200);
return ['key' => $userInfoKey];
} else if (!$user) {
$user = $wechatUserServices->wechatOauthAfter($createData);
} else {
//更新用户信息
$wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
}
return $this->getReturnInfo($user);
}
/**
* 生成关注二维码图片
* @return array
* @throws ValidateException
*/
public function follow()
{
$canvas = Canvas::instance();
$path = 'uploads/follow/';
$imageType = 'jpg';
$name = 'follow';
$siteUrl = sys_config('site_url');
$imageUrl = $path . $name . '.' . $imageType;
$canvas->setImageUrl(public_path() . 'statics/qrcode/follow.png')->setImageHeight(720)->setImageWidth(500)->pushImageValue();
$wechatQrcode = sys_config('wechat_qrcode');
if (($strlen = stripos($wechatQrcode, 'uploads')) !== false) {
$wechatQrcode = substr($wechatQrcode, $strlen);
}
if (!$wechatQrcode)
throw new ValidateException('请上传二维码');
$canvas->setImageUrl($wechatQrcode)->setImageHeight(344)->setImageWidth(344)->setImageLeft(76)->setImageTop(76)->pushImageValue();
$image = $canvas->setFileName($name)->setImageType($imageType)->setPath($path)->setBackgroundWidth(500)->setBackgroundHeight(720)->starDrawChart();
return ['path' => $image ? $siteUrl . '/' . $image : ''];
}
/**
* 微信公众号静默授权
* @param $spread_uid
* @param bool $notLogin
* @return array
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
public function silenceAuth($spread_uid, bool $notLogin = false, string $snsapi = '')
{
if ($snsapi) {
$wechatInfoConfig = $this->getAuthWechatInfo();
} else {
$wechatInfoConfig = OfficialAccount::userFromCode();
}
$openid = $wechatInfoConfig['openid'];
try {
$wechatInfo = OfficialAccount::instance()->user()->get($wechatInfoConfig['openid'])->toArray();
} catch (\Throwable $e) {
$createData = [$openid, [], $spread_uid, '', 'wechat'];
$userInfoKey = md5($openid . '_' . time() . '_wechat');
Cache::setTokenBucket($userInfoKey, $createData, 7200);
return ['auth_login' => 1, 'key' => $userInfoKey];
}
/** @var WechatUserServices $wechatUserServices */
$wechatUserServices = app()->make(WechatUserServices::class);
$user = $wechatUserServices->getAuthUserInfo($openid, 'wechat');
if (!$user) {
$wechatInfo['headimgurl'] = isset($wechatInfo['headimgurl']) && $wechatInfo['headimgurl'] != '' ? $wechatInfo['headimgurl'] : sys_config('h5_avatar');
$createData = [$openid, $wechatInfo, $spread_uid, '', 'wechat'];
//获取是否强制绑定手机号
$storeUserMobile = sys_config('store_user_mobile');
if ($notLogin || $storeUserMobile) {
$userInfoKey = md5($openid . '_' . time() . '_wechat');
Cache::setTokenBucket($userInfoKey, $createData, 7200);
return ['auth_login' => 1, 'key' => $userInfoKey];
} else {
//写入用户信息
$user = $wechatUserServices->wechatOauthAfter($createData);
}
} else {
//更新用户信息
$wechatUserServices->wechatUpdata([$user['uid'], ['spread_uid' => $spread_uid]]);
}
return $this->getReturnInfo($user);
}
/**
* 微信公众号静默授权
* @param $spread
* @param $phone
* @return array
* @throws DataNotFoundException
* @throws ModelNotFoundException|\Psr\SimpleCache\InvalidArgumentException
*/
public function silenceAuthBindingPhone($key, $phone)
{
if (!$key) {
throw new ValidateException('请刷新页面或者重新授权');
}
[$openid, $wechatInfo, $spread_uid, $login_type, $userType] = $createData = CacheService::getTokenBucket($key);
if (!$createData) {
throw new ValidateException('请刷新页面或者重新授权');
}
$wechatInfo['phone'] = $phone;
/** @var WechatUserServices $wechatUser */
$wechatUser = app()->make(WechatUserServices::class);
//更新用户信息
$user = $wechatUser->wechatOauthAfter([$openid, $wechatInfo, $spread_uid, $login_type, $userType]);
return $this->getReturnInfo($user);
}
/**
* @param array $userData
* @param string $phone
* @param string $userType
* @return array
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
public function appAuth(array $userData, string $phone, string $userType = 'app')
{
$openid = $userData['openId'] ?? "";
$userInfo = [
'phone' => $phone,
'unionid' => $userData['unionId'] ?? '',
'headimgurl' => $userData['avatarUrl'] ?? '',
'nickname' => $userData['nickName'] ?? '',
'province' => $userData['province'] ?? '',
'country' => $userData['country'] ?? '',
'city' => $userData['city'] ?? '',
'openid' => $openid,
];
$login_type = $userType;
$spread_uid = $userInfo['spread_uid'] ?? "";
if (!$phone) {
//获取是否强制绑定手机号
$storeUserMobile = sys_config('store_user_mobile');
if ($userInfo['unionid'] && $storeUserMobile) {
/** @var UserServices $userServices */
$userServices = app()->make(UserServices::class);
$uid = $this->dao->value(['unionid' => $userInfo['unionid'], 'is_del' => 0], 'uid');
$res = $userServices->value(['uid' => $uid], 'phone');
if (!$uid || !$res) {
return false;
}
}
//openid存在加数据情况
if ($openid) {
/** @var UserServices $userServices */
$userServices = app()->make(UserServices::class);
$uid = $this->dao->value(['openid' => $openid, 'is_del' => 0], 'uid');
$res = $userServices->value(['uid' => $uid], 'phone');
if (!$uid || !$res) {
return false;
}
}
}
/** @var WechatUserServices $wechatUser */
$wechatUser = app()->make(WechatUserServices::class);
//更新用户信息
$user = $wechatUser->wechatOauthAfter([$openid, $userInfo, $spread_uid, $login_type, $userType]);
$token = $this->getReturnInfo($user);
$token['isbind'] = false;
return $token;
}
/**
* 是否关注
* @param int $uid
* @return bool
*/
public function isSubscribe(int $uid)
{
if ($uid) {
$subscribe = (bool)$this->dao->value(['uid' => $uid], 'subscribe');
} else {
$subscribe = true;
}
return $subscribe;
}
/**
* 更新公众号用户信息
* @param int $uid
* @return array
*/
public function updateUserInfo(int $uid)
{
$wechatInfoConfig = OfficialAccount::userFromCode();
$openid = $wechatInfoConfig['openid'] ?? null;
try {
$wechatInfo = OfficialAccount::instance()->user()->get($wechatInfoConfig['openid']);
} catch (\Throwable $e) {
throw new ValidateException('更新公众号用户信息失败:' . $e->getMessage());
}
if (!$openid) {
throw new ValidateException('更新公众号用户信息失败没有获取到openid');
}
$wechatInfo['nickname'] = $wechatInfoConfig['nickname'] ?? $wechatInfo['nickname'];
$wechatInfo['headimgurl'] = $wechatInfoConfig['headimgurl'] ?? $wechatInfo['headimgurl'];
/** @var WechatUserServices $wechatUserServices */
$wechatUserServices = app()->make(WechatUserServices::class);
$id = $wechatUserServices->value(['openid' => $openid, 'uid' => $uid, 'user_type' => 'wechat'], 'id');
if (!$id) {
throw new ValidateException('没有查到用户信息');
}
/** @var UserServices $userService */
$userService = app()->make(UserServices::class);
$user = $userService->getUserInfo($uid);
if (isset($user['status']) && !$user['status']) {
throw new ValidateException('您已被禁止登录,请联系管理员');
}
if ($user) {
//更新用户信息
$wechatUserServices->wechatUpdata([$user['uid'], $wechatInfo]);
}
return [
'nickname' => $wechatInfo['nickname'],
'avatar' => $wechatInfo['headimgurl'],
'is_complete' => 1
];
}
}