1358 lines
46 KiB
PHP
1358 lines
46 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | 开源版本可自由商用,可去除界面版权logo
|
||
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
||
// | github下载:https://github.com/likeshop-github/likeadmin
|
||
// | 访问官网:https://www.likeadmin.cn
|
||
// | likeadmin团队 版权所有 拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeadminTeam
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\api\logic;
|
||
|
||
|
||
use app\common\{enum\notice\NoticeEnum,
|
||
enum\user\UserTerminalEnum,
|
||
enum\YesNoEnum,
|
||
logic\BaseLogic,
|
||
model\user\User,
|
||
model\user\UserInfo,
|
||
model\user\UserRelation,
|
||
model\user\UserRewardRecord,
|
||
model\user\UserMineRecord,
|
||
model\finance\UserFinance,
|
||
model\user\UserNotice,
|
||
model\user\UserAuth,
|
||
model\user\UserSigninRecord,
|
||
model\feedback\FeedbackCate,
|
||
model\feedback\FeedbackRecord,
|
||
service\sms\SmsDriver};
|
||
use app\common\model\setting\Language;
|
||
use app\common\model\item\{ItemRecord};
|
||
use think\facade\Config;
|
||
use app\common\cache\UserAccountSafeCache;
|
||
use app\common\model\member\UserMember;
|
||
use app\common\service\{UtilsService,ConfigService,FileService};
|
||
use app\common\model\decorate\{DecorateSwiper,DecorateNav,DecorateHint};
|
||
use app\common\model\notice\EmailRecord;
|
||
use think\facade\Db;
|
||
|
||
/**
|
||
* 会员逻辑层
|
||
* Class UserLogic
|
||
* @package app\shopapi\logic
|
||
*/
|
||
class UserLogic extends BaseLogic
|
||
{
|
||
|
||
/**
|
||
* @notes 首页数据
|
||
* @param $params
|
||
* @return array
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @author BD
|
||
* @date 2023/9/21 19:15
|
||
*/
|
||
public static function getIndexData(array $params)
|
||
{
|
||
//更新会员等级
|
||
UtilsService::set_user_member($params['user_id']);
|
||
|
||
//菜单按钮
|
||
$field2 = ['name','image','link','langs','image_ext'];
|
||
$navs = DecorateNav::field($field2)
|
||
->where(['is_show' => 1,'loca' => 2])
|
||
->order(['sort' => 'desc', 'id' => 'desc'])
|
||
->select()
|
||
->toArray();
|
||
foreach ($navs as &$nav) {
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($nav['langs'],$params['lang']);
|
||
$data_name = '';
|
||
|
||
if(count($data) > 0){
|
||
$data_name = $data['name'];
|
||
}
|
||
|
||
$nav['name'] = $data_name;
|
||
if($nav['image_ext'] != ''){
|
||
$nav['image'] = $nav['image_ext'];
|
||
}
|
||
|
||
unset($nav['langs']);
|
||
}
|
||
|
||
//菜单按钮
|
||
$field3 = ['name','image','link','langs','image_ext'];
|
||
$navs2 = DecorateNav::field($field3)
|
||
->where(['is_show' => 1,'loca' => 3])
|
||
->order(['sort' => 'desc', 'id' => 'desc'])
|
||
->select()
|
||
->toArray();
|
||
foreach ($navs2 as &$nav2) {
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($nav2['langs'],$params['lang']);
|
||
$data_name = '';
|
||
|
||
if(count($data) > 0){
|
||
$data_name = $data['name'];
|
||
}
|
||
$nav2['name'] = $data_name;
|
||
if($nav['image_ext'] != ''){
|
||
$nav['image'] = $nav['image_ext'];
|
||
}
|
||
unset($nav2['langs']);
|
||
}
|
||
|
||
|
||
$user = User::where(['id' => $params['user_id']])
|
||
->append(['team_num'])
|
||
->field('id,sn,account,mobile,avatar,user_money,user_point,total_income_invest as income,total_recharge,total_withdraw,country_code')
|
||
->findOrEmpty();
|
||
|
||
//查询会员等级
|
||
$member_id = UtilsService::get_user_member_id($params['user_id']);
|
||
$userMember = UserMember::where(['id' => $member_id])
|
||
->field('id,name,logo')
|
||
->findOrEmpty();
|
||
$userMember['logo'] = FileService::getFileUrl($userMember['logo']);
|
||
|
||
$user['member'] = $userMember;
|
||
|
||
$unread_notice = UserNotice::where(['user_id' => $params['user_id']])
|
||
->where(['read' => 0])
|
||
->count();
|
||
|
||
return [
|
||
'navs' => $navs,
|
||
'navs2' => $navs2,
|
||
'user' => $user,
|
||
'unreadNotice' => $unread_notice,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @notes 分享 首页数据
|
||
* @param array $params
|
||
* @return User|false
|
||
* @author BD
|
||
* @date 2023/9/21 16:53
|
||
*/
|
||
public static function getShareIndexData(array $params)
|
||
{
|
||
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
|
||
$front_link = ConfigService::get('website', 'front_link');
|
||
|
||
$userRes['avatar'] = $user['avatar'];
|
||
$userRes['mobile'] = $user['mobile'];
|
||
$userRes['sn'] = $user['sn'];
|
||
$userRes['link'] = $front_link.'/pages/register/register?code='.$user['sn'];
|
||
$userRes['qrcode'] = UtilsService::get_qrcode($userRes['link']);
|
||
|
||
//查询会员等级
|
||
$member_id = UtilsService::get_user_member_id($params['user_id']);
|
||
$userMember = UserMember::where(['id' => $member_id])
|
||
->field('name,logo')
|
||
->findOrEmpty();
|
||
$userMember['logo'] = FileService::getFileUrl($userMember['logo']);
|
||
|
||
|
||
return [
|
||
'user' => $userRes,
|
||
'member' => $userMember,
|
||
];
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 个人信息
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2023/9/20 19:45
|
||
*/
|
||
public static function info($params)
|
||
{
|
||
$user = User::where(['id' => $params['user_id']])
|
||
->field('id,sn,account,real_name,avatar,mobile,password_pay,create_time,user_money')
|
||
->findOrEmpty();
|
||
$front_link = ConfigService::get('website', 'front_link');
|
||
$user['link'] = $front_link.'/pages/register/register?code='.$user['sn'];
|
||
$user['qrcode'] = UtilsService::get_qrcode($user['link']);
|
||
//查询会员等级
|
||
$member_id = UtilsService::get_user_member_id($params['user_id']);
|
||
$userMember = UserMember::where(['id' => $member_id])
|
||
->field('id,name,bg_img,logo,text_color,price')
|
||
->findOrEmpty();
|
||
$userMember['logo'] = FileService::getFileUrl($userMember['logo']);
|
||
|
||
$user['member'] = $userMember;
|
||
|
||
$user['version'] = config('project.version');
|
||
|
||
//查询语言
|
||
$timeFormat = 'Y-m-d H:i:s';
|
||
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
|
||
if (!$language->isEmpty()) {
|
||
$timeFormat = $language['time_format'];
|
||
}
|
||
|
||
$user['time'] = date($timeFormat, strtotime($user['create_time']));
|
||
|
||
$userInfo = UserInfo::where(['user_id' => $params['user_id']])->findOrEmpty();
|
||
|
||
if(!$userInfo['google_key']){
|
||
$secretKey = UtilsService::get_google_secretKey();
|
||
UserInfo::update([
|
||
'id' => $userInfo['id'],
|
||
'google_key' => $secretKey,
|
||
'google_qrcode' => UtilsService::get_google_qrcode($user['account'],'',$secretKey),
|
||
]);
|
||
|
||
$userInfo = UserInfo::where(['user_id' => $params['user_id']])->findOrEmpty();
|
||
}
|
||
|
||
if($userInfo['auth_google'] == 1){
|
||
$userInfo['google_key'] = substr_replace($userInfo['google_key'], str_repeat('*', 8), 4, 8);
|
||
unset($userInfo['google_qrcode']);
|
||
}
|
||
|
||
$user['info'] = $userInfo;
|
||
|
||
|
||
//查询初始交易密码
|
||
$user['need_set_pwd'] = 0;
|
||
$pwd_pay = ConfigService::get('login', 'password_pay');
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
if ($user['password_pay'] == create_password($pwd_pay, $passwordSalt)) {
|
||
$user['need_set_pwd'] = 1;
|
||
}
|
||
|
||
unset($user['id']);
|
||
unset($user['password_pay']);
|
||
unset($userInfo['id']);
|
||
unset($userInfo['user_id']);
|
||
|
||
return $user->toArray();
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 设置用户信息
|
||
* @param int $userId
|
||
* @param array $params
|
||
* @return User|false
|
||
* @author BD
|
||
* @date 2023/9/21 16:53
|
||
*/
|
||
public static function setInfo(int $userId, array $params)
|
||
{
|
||
try {
|
||
return User::update([
|
||
'id' => $userId,
|
||
$params['field'] => $params['value']]
|
||
);
|
||
} catch (\Exception $e) {
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 重置登录密码
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/16 18:06
|
||
*/
|
||
public static function resetPassword(array $params)
|
||
{
|
||
try {
|
||
// // 校验验证码
|
||
// $smsDriver = new SmsDriver();
|
||
// if (!$smsDriver->verify($params['mobile'], $params['code'], NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA)) {
|
||
// throw new \Exception('123');//请输入正确的验证码
|
||
// }
|
||
|
||
// 重置密码
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
$password = create_password($params['password'], $passwordSalt);
|
||
|
||
// 更新
|
||
User::where('mobile', $params['mobile'])->update([
|
||
'password' => $password
|
||
]);
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 修改登录密码
|
||
* @param $params
|
||
* @param $userId
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/20 19:13
|
||
*/
|
||
public static function changePassword(array $params, int $userId)
|
||
{
|
||
try {
|
||
//账号安全机制,连续输错后锁定,防止账号密码暴力破解
|
||
$userAccountSafeCache = new UserAccountSafeCache();
|
||
if (!$userAccountSafeCache->isSafe()) {
|
||
return 'network.frequentOperation';
|
||
}
|
||
|
||
$user = User::findOrEmpty($userId);
|
||
if ($user->isEmpty()) {
|
||
throw new \Exception('pwd.userNoExist');//用户不存在
|
||
}
|
||
|
||
// 密码盐
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
|
||
if (!empty($user['password'])) {
|
||
if (empty($params['old_password'])) {
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('pwd.oldPwdEmpty');//请输入原密码
|
||
}
|
||
$oldPassword = create_password($params['old_password'], $passwordSalt);
|
||
if ($oldPassword != $user['password']) {
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('pwd.oldPwdError');//原密码不正确
|
||
}
|
||
}
|
||
|
||
// 保存密码
|
||
$password = create_password($params['password'], $passwordSalt);
|
||
$user->password = $password;
|
||
$user->save();
|
||
|
||
$userAccountSafeCache->relieve();
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 修改支付密码
|
||
* @param $params
|
||
* @param $userId
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/20 19:13
|
||
*/
|
||
public static function changePayPassword(array $params, int $userId)
|
||
{
|
||
try {
|
||
//账号安全机制,连续输错后锁定,防止账号密码暴力破解
|
||
$userAccountSafeCache = new UserAccountSafeCache();
|
||
if (!$userAccountSafeCache->isSafe()) {
|
||
throw new \Exception('network.frequentOperation');
|
||
}
|
||
|
||
$user = User::findOrEmpty($userId);
|
||
if ($user->isEmpty()) {
|
||
throw new \Exception('pwd.userNoExist');//用户不存在
|
||
}
|
||
|
||
// 密码盐
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
|
||
if (!empty($user['password_pay'])) {
|
||
if (empty($params['old_password_pay'])) {
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('pwd.oldPwdEmpty');//请输入原密码
|
||
}
|
||
$oldPassword = create_password($params['old_password_pay'], $passwordSalt);
|
||
if ($oldPassword != $user['password_pay']) {
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('pwd.oldPwdError');//原密码不正确
|
||
}
|
||
}
|
||
|
||
// 保存密码
|
||
$password = create_password($params['password_pay'], $passwordSalt);
|
||
$user->password_pay = $password;
|
||
$user->save();
|
||
|
||
$userAccountSafeCache->relieve();
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 修改支付密码
|
||
* @param $params
|
||
* @param $userId
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/20 19:13
|
||
*/
|
||
public static function setPayPassword(array $params, int $userId)
|
||
{
|
||
try {
|
||
$user = User::findOrEmpty($userId);
|
||
if ($user->isEmpty()) {
|
||
throw new \Exception('pwd.userNoExist');//用户不存在
|
||
}
|
||
|
||
$pwd_pay = ConfigService::get('login', 'password_pay');
|
||
// 密码盐
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
if ($user['password_pay'] !== create_password($pwd_pay, $passwordSalt)) {
|
||
throw new \Exception('network.parameterAbnormality');//已设置过不需要重复设置
|
||
}
|
||
if ($params['password_pay'] == $pwd_pay) {
|
||
throw new \Exception('pwd.pwdSimpleTips');//密码过于简单,请重新设置
|
||
}
|
||
|
||
|
||
// 保存密码
|
||
$password = create_password($params['password_pay'], $passwordSalt);
|
||
$user->password_pay = $password;
|
||
$user->save();
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 绑定手机号
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function bindMobile(array $params)
|
||
{
|
||
try {
|
||
// 变更手机号场景
|
||
$sceneId = NoticeEnum::CHANGE_MOBILE_CAPTCHA;
|
||
$where = [
|
||
['id', '=', $params['user_id']],
|
||
['mobile', '=', $params['mobile']]
|
||
];
|
||
|
||
// 绑定手机号场景
|
||
if ($params['type'] == 'bind') {
|
||
$sceneId = NoticeEnum::BIND_MOBILE_CAPTCHA;
|
||
$where = [
|
||
['mobile', '=', $params['mobile']]
|
||
];
|
||
}
|
||
|
||
// 校验短信
|
||
$checkSmsCode = (new SmsDriver())->verify($params['mobile'], $params['code'], $sceneId);
|
||
if (!$checkSmsCode) {
|
||
throw new \Exception('captcha.captchaError');//请输入正确的验证码
|
||
}
|
||
|
||
$user = User::where($where)->findOrEmpty();
|
||
if (!$user->isEmpty()) {
|
||
throw new \Exception('pwd.mobileExist');//该手机号已被使用
|
||
}
|
||
|
||
User::update([
|
||
'id' => $params['user_id'],
|
||
'mobile' => $params['mobile'],
|
||
]);
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 获取用户消息
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2023/9/20 19:45
|
||
*/
|
||
public static function notice($params)
|
||
{
|
||
//查询语言
|
||
$timeFormat = 'Y-m-d H:i:s';
|
||
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
|
||
if (!$language->isEmpty()) {
|
||
$timeFormat = $language['time_format'];
|
||
}
|
||
|
||
//系统消息
|
||
$notice1 = [
|
||
'unread' => 0,
|
||
'content' => '',
|
||
'time' => '',
|
||
];
|
||
$notice1['unread'] = UserNotice::where(['user_id' => $params['user_id']])
|
||
->where(['read' => 0,'notice_type' => 3])
|
||
->count();
|
||
$notice1_last = UserNotice::where(['user_id' => $params['user_id']])
|
||
->where(['notice_type' => 3])
|
||
->order(['read' => 'desc', 'id' => 'desc'])
|
||
->findOrEmpty();
|
||
if (!$notice1_last->isEmpty()) {
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($notice1_last['langs'],$params['lang']);
|
||
$data_text = '';
|
||
|
||
if(count($data) > 0){
|
||
$data_text = $data['text'];
|
||
}
|
||
|
||
$notice1['content'] = get_file_domain($data_text);
|
||
$notice1['time'] = date($timeFormat, strtotime($notice1_last['create_time']));
|
||
}
|
||
|
||
//任务消息
|
||
$notice2 = [
|
||
'unread' => 0,
|
||
'content' => '',
|
||
'time' => '',
|
||
];
|
||
$notice2['unread'] = UserNotice::where(['user_id' => $params['user_id']])
|
||
->where(['read' => 0,'notice_type' => 2])
|
||
->count();
|
||
$notice2_last = UserNotice::where(['user_id' => $params['user_id']])
|
||
->where(['notice_type' => 2])
|
||
->order(['read' => 'desc', 'id' => 'desc'])
|
||
->findOrEmpty();
|
||
if (!$notice2_last->isEmpty()) {
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($notice2_last['langs'],$params['lang']);
|
||
$data_text = '';
|
||
|
||
if(count($data) > 0){
|
||
$data_text = $data['text'];
|
||
}
|
||
$notice2['content'] = get_file_domain($data_text);
|
||
$notice2['time'] = date($timeFormat, strtotime($notice2_last['create_time']));
|
||
}
|
||
|
||
//站内信
|
||
$notice3 = [
|
||
'unread' => 0,
|
||
'content' => '',
|
||
'time' => '',
|
||
];
|
||
$notice3['unread'] = UserNotice::where(['user_id' => $params['user_id']])
|
||
->where(['read' => 0,'notice_type' => 1])
|
||
->count();
|
||
$notice3_last = UserNotice::where(['user_id' => $params['user_id']])
|
||
->where(['notice_type' => 1])
|
||
->order(['read' => 'desc', 'id' => 'desc'])
|
||
->findOrEmpty();
|
||
if (!$notice3_last->isEmpty()) {
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($notice3_last['langs'],$params['lang']);
|
||
$data_text = '';
|
||
|
||
if(count($data) > 0){
|
||
$data_text = $data['text'];
|
||
}
|
||
$notice3['content'] = get_file_domain($data_text);
|
||
$notice3['time'] = date($timeFormat, strtotime($notice3_last['create_time']));
|
||
}
|
||
|
||
return [
|
||
'notice1' => $notice1,
|
||
'notice2' => $notice2,
|
||
'notice3' => $notice3,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @notes 用户消息详情
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2023/9/20 17:09
|
||
*/
|
||
public static function noticeDetail($params)
|
||
{
|
||
$notice = UserNotice::field('id,title,content,read,langs,create_time')->where(['user_id' => $params['user_id'],'id' => $params['id']])->findOrEmpty();
|
||
if ($notice->isEmpty()) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
|
||
//查询语言
|
||
$timeFormat = 'Y-m-d H:i:s';
|
||
$language = Language::where(['symbol' => $params['lang']])->findOrEmpty();
|
||
if (!$language->isEmpty()) {
|
||
$timeFormat = $language['time_format'];
|
||
}
|
||
|
||
$notice['time'] = date($timeFormat, strtotime($notice['create_time']));
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($notice['langs'],$params['lang']);
|
||
$data_content = '';
|
||
$data_title = '';
|
||
|
||
if(count($data) > 0){
|
||
$data_content = $data['content'];
|
||
$data_title = $data['title'];
|
||
}
|
||
$notice['content'] = get_file_domain($data_content);
|
||
$notice['title'] = get_file_domain($data_title);
|
||
unset($notice['langs']);
|
||
unset($notice['create_time']);
|
||
|
||
if($notice['read'] == 0){
|
||
UserNotice::update([
|
||
'id' => $notice['id'],
|
||
'read' => 1
|
||
]
|
||
);
|
||
}
|
||
|
||
return ['data' => $notice];
|
||
}
|
||
|
||
/**
|
||
* @notes 奖励活动数据
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2023/9/20 17:09
|
||
*/
|
||
public static function activityIndex($params)
|
||
{
|
||
$reward = ConfigService::get('website', 'reward');
|
||
$activities = $reward['activity'];
|
||
foreach ($activities as $key => $activity) {
|
||
if($activities[$key]['is_show'] == 0){
|
||
unset($activities[$key]);
|
||
}
|
||
}
|
||
|
||
foreach ($activities as &$activity) {
|
||
$activity['count'] = UserRewardRecord::where(['user_id' => $params['user_id'],'top_id' => $activity['id']])->count();
|
||
}
|
||
$time = strtotime(date('Y-m-d 00:00:00'));
|
||
|
||
$report['today_income'] = UserFinance::where("create_time > $time")
|
||
->where(['user_id' => $params['user_id']])
|
||
->where(" change_type IN (14) ")
|
||
->sum('change_amount');
|
||
|
||
$report['total_income'] = UserFinance::where(['user_id' => $params['user_id']])
|
||
->where(" change_type IN (14) ")
|
||
->sum('change_amount');
|
||
|
||
return [
|
||
'activities' => $activities,
|
||
'report' => $report
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @notes 任务中心数据
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2023/9/20 17:09
|
||
*/
|
||
public static function missionIndex($params)
|
||
{
|
||
//更新任务领取记录
|
||
UtilsService::mission_reward_add($params['user_id']);
|
||
|
||
$reward = ConfigService::get('website', 'reward');
|
||
$list = $reward['tasks'];
|
||
foreach ($list as $key => $item) {
|
||
if($list[$key]['is_show'] == 0){
|
||
unset($list[$key]);
|
||
}
|
||
}
|
||
foreach ($list as &$item) {
|
||
$item['record_id'] = 0;
|
||
$item['status'] = 1;//0待领取1进行中2已完成
|
||
$rewardRecord = UserRewardRecord::where(['user_id' => $params['user_id'],'top_id' => $item['id'],'type' => 2])->order(['id' => 'asc'])->findOrEmpty();
|
||
|
||
//type 1升级VIP 2邀请下级首充 3累积邀请
|
||
switch ($item['type']) {
|
||
case 1:
|
||
$member = UserMember::where(['id' => $item['member_id']])->findOrEmpty();
|
||
$item['vip_name'] = $member['name'];
|
||
|
||
if(!$rewardRecord->isEmpty()) {
|
||
$item['record_id'] = $rewardRecord['id'];
|
||
//领取状态1已领取0未领取
|
||
switch ($rewardRecord['status']) {
|
||
case 0:
|
||
$item['status'] = 0;
|
||
|
||
break;
|
||
case 1:
|
||
$item['status'] = 2;
|
||
break;
|
||
}
|
||
|
||
}
|
||
break;
|
||
case 2:
|
||
|
||
$rewardRecordIng = UserRewardRecord::where(['user_id' => $params['user_id'],'top_id' => $item['id'],'type' => 2,'status' => 0])->order(['id' => 'asc'])->findOrEmpty();
|
||
if(!$rewardRecordIng->isEmpty()) {
|
||
$item['record_id'] = $rewardRecordIng['id'];
|
||
$item['status'] = 0;
|
||
}
|
||
break;
|
||
case 3:
|
||
//判断下级人数
|
||
$recharge_money = $item['recharge_money'];
|
||
|
||
$item['total_invite_num'] = UserRelation::alias('ur')
|
||
->join('user u', 'u.id = ur.user_id')
|
||
->where(['ur.parent_id' => $params['user_id'],'ur.level' => 1])
|
||
->where("u.total_recharge >= $recharge_money")
|
||
->count();
|
||
|
||
$item['percent'] = ($item['total_invite_num'] / $item['invite_num']) * 100;
|
||
if($item['percent'] >= 100){
|
||
$item['percent'] = 100;
|
||
}
|
||
//is_repeat 1可重复领取0不可重复
|
||
if($item['is_repeat'] == 0){
|
||
if(!$rewardRecord->isEmpty()) {
|
||
$item['record_id'] = $rewardRecord['id'];
|
||
//领取状态1已领取0未领取
|
||
switch ($rewardRecord['status']) {
|
||
case 0:
|
||
$item['status'] = 0;
|
||
|
||
break;
|
||
case 1:
|
||
$item['status'] = 2;
|
||
break;
|
||
}
|
||
}
|
||
|
||
}else{
|
||
|
||
$rewardRecordIng = UserRewardRecord::where(['user_id' => $params['user_id'],'top_id' => $item['id'],'type' => 2,'status' => 0])->order(['id' => 'asc'])->findOrEmpty();
|
||
if(!$rewardRecordIng->isEmpty()) {
|
||
$item['record_id'] = $rewardRecordIng['id'];
|
||
$item['percent'] = 100;
|
||
$item['status'] = 0;
|
||
}else{
|
||
|
||
//计算进度
|
||
$count = UserRewardRecord::where(['user_id' => $params['user_id'],'top_id' => $item['id'],'type' => 2])->count();
|
||
$used_invite_num = $count * $item['invite_num'];
|
||
|
||
$item['percent'] = (($item['total_invite_num'] - $used_invite_num) / $item['invite_num']) * 100;
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
|
||
}
|
||
|
||
return [
|
||
'list' => $list
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @notes 领取任务奖励
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function missionReward(array $params)
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$rewardRecord = UserRewardRecord::where(['id' => $params['id'],'user_id' => $params['user_id'],'status' => 0])->findOrEmpty();
|
||
if($rewardRecord->isEmpty()) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
UserRewardRecord::update([
|
||
'id' => $rewardRecord['id'],
|
||
'status' => 1
|
||
]
|
||
);
|
||
|
||
//记录日志
|
||
UtilsService::user_finance_add(
|
||
$params['user_id'],
|
||
15,
|
||
1,
|
||
$rewardRecord['reward'],
|
||
'',
|
||
'',
|
||
1 //冻结
|
||
);
|
||
|
||
//用户资金修改
|
||
UtilsService::user_money_change($params['user_id'], 1, $rewardRecord['reward'],'user_money');
|
||
|
||
UtilsService::user_money_change($params['user_id'], 1, $rewardRecord['reward'],'total_income_tasks');
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 任务中心数据
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2023/9/20 17:09
|
||
*/
|
||
public static function feedbackCate($params)
|
||
{
|
||
$field = ['id','name','langs'];
|
||
$cates = FeedbackCate::field($field)
|
||
->where(['is_show' => 1])
|
||
->order(['sort' => 'desc', 'id' => 'desc'])
|
||
->select()
|
||
->toArray();
|
||
foreach ($cates as &$cate) {
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($cate['langs'],$params['lang']);
|
||
$data_name = '';
|
||
|
||
if(count($data) > 0){
|
||
$data_name = $data['name'];
|
||
}
|
||
$cate['name'] = $data_name;
|
||
unset($cate['langs']);
|
||
}
|
||
|
||
return [
|
||
'cates' => $cates
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @notes 意见反馈
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function feedback(array $params)
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$cate = FeedbackCate::where(['id' => $params['cid']])->findOrEmpty();
|
||
if($cate->isEmpty()) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
$content = $params['content'];
|
||
if(strlen($content) < 1 || strlen($content) > 1024){
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
|
||
FeedbackRecord::create([
|
||
'user_id' => $params['user_id'],
|
||
'cid' => $params['cid'],
|
||
'content' => $content
|
||
]);
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes Google Authenticator校验
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function verifyGoogle(array $params)
|
||
{
|
||
try {
|
||
$userInfo = UserInfo::where(['user_id' => $params['user_id']])->findOrEmpty();
|
||
if($userInfo->isEmpty()) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
if($userInfo['auth_google'] != 0) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
|
||
$valid = UtilsService::get_google_verify($userInfo['google_key'],$params['code']);
|
||
if(!$valid) {
|
||
throw new \Exception('captcha.captchaError');//验证码错误
|
||
}
|
||
|
||
UserInfo::update([
|
||
'id' => $userInfo['id'],
|
||
'auth_google' => 1
|
||
]);
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 邮箱校验
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function verifyEmail(array $params)
|
||
{
|
||
try {
|
||
$userInfo = UserInfo::where(['user_id' => $params['user_id']])->findOrEmpty();
|
||
if($userInfo->isEmpty()) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
if($userInfo['auth_email'] != 0) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
if(!filter_var($params['email'], FILTER_VALIDATE_EMAIL)){
|
||
throw new \Exception('auth.emailError');//请输入正确的邮箱地址
|
||
}
|
||
|
||
$time = time() - 5*60;//5分钟内有效
|
||
|
||
$email = EmailRecord::where(['user_id' => $params['user_id'],'is_verify' => 0])->where("create_time > $time")->order('id desc')->findOrEmpty();
|
||
|
||
if($email->isEmpty()) {
|
||
throw new \Exception('captcha.captchaError');//验证码错误
|
||
}
|
||
if($email['code'] != $params['code']) {
|
||
throw new \Exception('captcha.captchaError');//验证码错误
|
||
}
|
||
|
||
//判断是否绑定过
|
||
$info_t = UserInfo::where(['email' => $params['email']])->findOrEmpty();
|
||
if(!$info_t->isEmpty()) {
|
||
throw new \Exception('auth.emailExisted');//该电子邮箱地址已存在
|
||
}
|
||
|
||
UserInfo::update([
|
||
'id' => $userInfo['id'],
|
||
'email' => $params['email'],
|
||
'auth_email' => 1
|
||
]);
|
||
|
||
EmailRecord::update([
|
||
'id' => $email['id'],
|
||
'is_verify' => 1
|
||
]);
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 实名认证
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function verifyRealname(array $params)
|
||
{
|
||
try {
|
||
$userInfo = UserInfo::where(['user_id' => $params['user_id']])->findOrEmpty();
|
||
UserInfo::update([
|
||
'id' => $userInfo['id'],
|
||
'card_name' => $params['card_name'],
|
||
'card_num' => $params['card_num'],
|
||
'card_img1' => $params['card_img1'],
|
||
'card_img2' => $params['card_img2'],
|
||
'card_img3' => $params['card_img3'],
|
||
'auth_card' => 2,
|
||
'card_time' => time()
|
||
]);
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 签到配置
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2024/02/22 10:54
|
||
*/
|
||
public static function signinConfig(array $params)
|
||
{
|
||
$format = 'Y-m-d';
|
||
$time = time();
|
||
//获取当前周几
|
||
$week = date('d', $time);
|
||
$datesArr = [];
|
||
|
||
for ($i=1; $i<=date('t'); $i++){
|
||
$date = date($format ,strtotime( '+' . $i-$week .' days', $time));
|
||
|
||
$datesArr[$i-1]['day'] = date('d', strtotime($date));
|
||
|
||
$datesArr[$i-1]['signined'] = false;
|
||
|
||
//查询当日是否签到
|
||
// 获取指定日期的0点时间戳
|
||
$startTime = strtotime($date);
|
||
$endTime = strtotime($date . " +1 day");
|
||
|
||
$now = time();
|
||
if($now > $startTime){
|
||
$record = UserSigninRecord::where(['user_id' => $params['user_id']])->where(" create_time >= $startTime AND create_time < $endTime ")->findOrEmpty();
|
||
if(!$record->isEmpty()) {
|
||
$datesArr[$i-1]['signined'] = true;
|
||
}
|
||
}
|
||
}
|
||
|
||
//判断是否签到
|
||
//查询会员等级
|
||
$member_id = UtilsService::get_user_member_id($params['user_id']);
|
||
$userMember = UserMember::where(['id' => $member_id])->findOrEmpty();
|
||
|
||
$signined = false;
|
||
$todayStart = strtotime("today midnight");
|
||
$sign_count = UserSigninRecord::where(['user_id' => $params['user_id']])->where("create_time > $todayStart")->count();
|
||
if($sign_count > 0) {
|
||
$signined = true;
|
||
}
|
||
|
||
$totalReward = UserSigninRecord::where(['user_id' => $params['user_id']])->sum('reward');
|
||
|
||
|
||
return [
|
||
'datesArr' => $datesArr,
|
||
'signined' => $signined,
|
||
'totalReward' => $totalReward,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @notes 签到
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function signin(array $params)
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$config = ConfigService::get('website', 'reward');
|
||
|
||
$record = UserSigninRecord::create([
|
||
'sn' => generate_sn(UserSigninRecord::class, 'sn'),
|
||
'user_id' => $params['user_id'],
|
||
'reward' => $config['signin_money'],
|
||
'point' => $config['signin_point'],
|
||
]);
|
||
|
||
if($config['signin_money'] > 0){
|
||
//记录日志
|
||
UtilsService::user_finance_add(
|
||
$record['user_id'],
|
||
21,
|
||
1,
|
||
$record['reward'],
|
||
$record['sn'],
|
||
'',
|
||
1//冻结
|
||
);
|
||
|
||
//用户资金修改
|
||
UtilsService::user_money_change($record['user_id'], 1, $record['reward'],'user_money');
|
||
}
|
||
|
||
//赠送积分
|
||
if($config['signin_point'] > 0){
|
||
UtilsService::user_money_change($record['user_id'], 1, $record['point'],'user_point');
|
||
}
|
||
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 挖矿 首页数据
|
||
* @param $params
|
||
* @return array
|
||
* @author BD
|
||
* @date 2024/02/22 10:54
|
||
*/
|
||
public static function getMineIndexData(array $params)
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
|
||
|
||
$userRes['avatar'] = $user['avatar'];
|
||
$userRes['mobile'] = $user['mobile'];
|
||
$userRes['sn'] = $user['sn'];
|
||
|
||
//查询会员等级
|
||
$member_id = UtilsService::get_user_member_id($params['user_id']);
|
||
$userMember = UserMember::where(['id' => $member_id])
|
||
->field('name,logo,mine_speed')
|
||
->findOrEmpty();
|
||
|
||
$userRes['member_id'] = $member_id;
|
||
$userRes['member_name'] = $userMember['name'];
|
||
|
||
$userRes['member_logo'] = FileService::getFileUrl($userMember['logo']);
|
||
$userRes['total_invest'] = ItemRecord::where(['user_id' => $params['user_id']])->sum('money');
|
||
$userRes['invite_count'] = UserRelation::where(['parent_id' => $params['user_id'],'level' => 1])->count();
|
||
$userRes['is_recive'] = false;
|
||
|
||
$field = 'id, name as text, logo, bg_img as image, text_color, money, level1_num,level1_vip_id, item_num, item_add_rate,mine_speed';
|
||
$members = UserMember::field($field)
|
||
->append(['vip_name'])
|
||
->where(['is_show' => 1])
|
||
->order(['money' => 'asc', 'id' => 'desc'])
|
||
->select()
|
||
->toArray();
|
||
|
||
$config = ConfigService::get('website', 'mine');
|
||
$precision = 6;
|
||
|
||
$config['precision2'] = $precision;
|
||
$config['endTime'] = 0;
|
||
$config['nowNum'] = 0;
|
||
$config['maxNum'] = number_format($config['cycle'] * $userMember['mine_speed'], $precision, '.', '');
|
||
$config['status'] = $user['mine_status'];//矿机状态0已暂停1挖矿中2待领取
|
||
$config['mine_speed_sec'] = $userMember['mine_speed']/3600;
|
||
$config['percent'] = 0;
|
||
|
||
//矿机状态0已暂停1挖矿中2待领取
|
||
switch($config['status']){
|
||
case 0:
|
||
if($config['is_auto']){
|
||
$startTime = time();
|
||
|
||
$config['endTime'] = ($config['cycle'] * 60 * 60) * 1000;
|
||
$config['nowNum'] = 0;
|
||
$config['status'] = 1;
|
||
|
||
User::update([
|
||
'id' => $user['id'],
|
||
'mine_status' => 1,
|
||
'mine_start_time' => $startTime,
|
||
]);
|
||
}
|
||
|
||
break;
|
||
case 1:
|
||
$startTime = $user['mine_start_time'];
|
||
$cycleTime = $config['cycle'] * 60 * 60;
|
||
|
||
$startingTime = time() - $startTime;
|
||
//启动时间达标
|
||
if($startingTime - $cycleTime >= 0){
|
||
//是否超时未领取
|
||
$outTime = $config['valid_days'] * 24 * 60 * 60;
|
||
if($startingTime - $outTime >= 0){
|
||
User::update([
|
||
'id' => $user['id'],
|
||
'mine_status' => 0
|
||
]);
|
||
}else{
|
||
$config['nowNum'] = number_format($config['cycle'] * $userMember['mine_speed'], $precision, '.', '');
|
||
$config['status'] = 2;
|
||
$config['percent'] = 100;
|
||
|
||
User::update([
|
||
'id' => $user['id'],
|
||
'mine_status' => 2
|
||
]);
|
||
}
|
||
}else{
|
||
$config['endTime'] = ($cycleTime - $startingTime) * 1000;
|
||
$config['nowNum'] = number_format($startingTime * $userMember['mine_speed'] / (60 * 60), $precision, '.', '');;
|
||
$config['status'] = 1;
|
||
$config['percent'] = round($startingTime/$cycleTime * 100,2);
|
||
|
||
$config['$startingTime'] = $startingTime;
|
||
$config['$cycleTime'] = $cycleTime;
|
||
}
|
||
|
||
break;
|
||
case 2:
|
||
//是否超时未领取
|
||
$startTime = $user['mine_start_time'];
|
||
$startingTime = time() - $startTime;
|
||
$outTime = $config['valid_days'] * 24 * 60 * 60;
|
||
if($startingTime - $outTime >= 0){
|
||
User::update([
|
||
'id' => $user['id'],
|
||
'mine_status' => 0
|
||
]);
|
||
}else{
|
||
$config['nowNum'] = $config['maxNum'];
|
||
$config['status'] = 2;
|
||
$config['percent'] = 100;
|
||
}
|
||
|
||
break;
|
||
}
|
||
|
||
$userRes['mine_speed'] = $userMember['mine_speed'].' '.$config['symbol'].' / H';
|
||
foreach ($members as &$member) {
|
||
$member['mine_speed'] = $member['mine_speed'].' '.$config['symbol'].' / H';
|
||
}
|
||
|
||
Db::commit();
|
||
return [
|
||
'config' => $config,
|
||
'user' => $userRes,
|
||
'members' => $members,
|
||
];
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 矿机 启动
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function mineStart(array $params)
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
User::update([
|
||
'id' => $params['user_id'],
|
||
'mine_status' => 1,
|
||
'mine_start_time' => time(),
|
||
]);
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 矿机 收益
|
||
* @param $params
|
||
* @return bool
|
||
* @author BD
|
||
* @date 2023/9/21 17:28
|
||
*/
|
||
public static function mineReceive(array $params)
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$user = User::where(['id' => $params['user_id']])->findOrEmpty();
|
||
|
||
//查询会员等级
|
||
$member_id = UtilsService::get_user_member_id($params['user_id']);
|
||
$userMember = UserMember::where(['id' => $member_id])->findOrEmpty();
|
||
|
||
$config = ConfigService::get('website', 'mine');
|
||
|
||
$amount_act = number_format($config['cycle'] * $userMember['mine_speed'], $config['precision'], '.', '');
|
||
|
||
$amount = round($amount_act / $config['rate'] , 2);
|
||
|
||
$record = UserMineRecord::create([
|
||
'sn' => generate_sn(UserMineRecord::class, 'sn'),
|
||
'user_id' => $params['user_id'],
|
||
'amount' => $amount,
|
||
'amount_act' => $amount_act,
|
||
'name' => $config['name'],
|
||
'symbol' => $config['symbol'],
|
||
'rate' => $config['rate'],
|
||
'precision' => $config['precision'],
|
||
'cycle' => $config['cycle'],
|
||
'start_time' => $user['mine_start_time'],
|
||
]);
|
||
|
||
if($amount > 0.01){
|
||
//记录日志
|
||
UtilsService::user_finance_add(
|
||
$record['user_id'],
|
||
24,
|
||
1,
|
||
$record['amount'],
|
||
$record['sn'],
|
||
'',
|
||
1//冻结
|
||
);
|
||
|
||
//用户资金修改
|
||
UtilsService::user_money_change($record['user_id'], 1, $record['amount'],'user_money');
|
||
//累积挖矿金额增加
|
||
UtilsService::user_money_change($record['user_id'], 1, $record['amount'],'total_income_mine');
|
||
}
|
||
|
||
User::update([
|
||
'id' => $params['user_id'],
|
||
'mine_status' => 0
|
||
]);
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
} |