Ai_GirlFriend/xunifriend_RaeeC/application/api/controller/UserBasic.php
2026-01-31 19:15:41 +08:00

316 lines
10 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\User;
use app\common\model\UserBondConfig;
use app\common\model\UserBondLog;
use app\common\model\UserLevel;
use think\Config;
use think\Db;
use think\Validate;
/**
* 会员接口
*/
class UserBasic extends Api
{
protected $noNeedLogin = [];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
if (!Config::get('fastadmin.usercenter')) {
$this->error(__('User center already closed'));
}
}
/**
* 获取用户基础数据
*/
public function get_user_basic(){
$info = $this->auth->getUserinfo();
$info['hobbies'] = empty($info['hobbies'])?[]: explode(',',$info['hobbies']);
//查询下一个级别
$next_level = UserLevel::where('level',$info['level']+1)->find();
if(empty($next_level)){
$next_level_intimacy = $info['intimacy'];
}else{
$next_level_intimacy = $next_level['intimacy'];
}
$info['next_level_intimacy'] = $next_level_intimacy;
$info['intimacy_percent'] = round($info['intimacy']/$next_level_intimacy,2)*100;
//每日牵绊数据
$UserBondLog = new UserBondLog();
$UserBondConfig = new UserBondConfig();
$info['bond_today_all'] = $UserBondConfig->sum('upper');
$info['bond_today'] = $UserBondLog->where([
'user_id'=>$this->auth->id,
'createdate'=>date('Y-m-d')
])->sum('intimacy');
$info['bond_today_percent'] = round($info['bond_today']/$info['bond_today_all'],2)*100;
if($info['vip_endtime'] < time()){
$info['vip'] = 0;
}else{
$info['vip'] = 1;
}
$info['vip_endtime'] = $info['vip_endtime'] ? date('Y-m-d H:i:s',$info['vip_endtime']) : '';
$info['money'] = (int)$info['money'];
$info['wxapp_openid'] = Db::name('third')->where(['user_id'=>$this->auth->id,'platform'=>'wxapp'])->value('openid');
$this->success('获取成功',$info);
}
/**
* 修改用户数据
*/
public function set_user_basic(){
$platform = $this->request->post();
if(empty($platform['gender']) || empty($platform['nickname']) || empty($platform['birthday'])
|| empty($platform['hobbies']) || empty($platform['city'])){
$this->error('请填写完整信息');
}
if(count($platform['hobbies'])>3){
$this->error('最多选择3个兴趣标签');
}
$res = $this->sanfang_check_text($platform['nickname']);
if($res['code'] != 200){
$this->error($res['msg']);
}
$platform['hobbies'] = implode(',',$platform['hobbies']);
$platform['reg_step'] = 2;
Db::name('user')->where('id',$this->auth->id)->update($platform);
$this->success('修改成功');
}
/**
* 修改用户数据
*/
public function set_user_basic_single(){
$platform = $this->request->post();
$data = [];
if(!empty($platform['nickname'])){
$res = $this->sanfang_check_text($platform['nickname']);
if($res['code'] != 200){
$this->error($res['msg']);
}
$data['nickname'] = $platform['nickname'];
}
if(!empty($platform['hobbies'])){
if(count($platform['hobbies'])>3){
$this->error('最多选择3个兴趣标签');
}
$data['hobbies'] = implode(',',$platform['hobbies']);
}
if(!empty($platform['avatar'])){
$data['avatar'] = $platform['avatar'];
}
if(empty($data)){
$this->error('请填写信息');
}
Db::name('user')->where('id',$this->auth->id)->update($data);
$this->success('修改成功');
}
/**
* 获取兴趣标签
*/
public function get_hobbies(){
$hobbies = Db::name('hobbies')->where([
'user_id'=>0,
'deletetime'=>null
])->order(['weigh'=>'desc'])->select();
$user_hobbies = Db::name('hobbies')->where([
'deletetime'=>null,
'user_id'=>$this->auth->id
])->order(['createtime'=>'asc'])->select();
//合并数组
$hobbies = array_merge($hobbies,$user_hobbies);
$this->success('获取成功',$hobbies);
}
/**
* 自定义兴趣标签
*/
public function add_hobbies(){
$hobbies = $this->request->post();
if(empty($hobbies['name'])){
$this->error('请填写标签');
}
//验证是否存在
$is_exist = Db::name('hobbies')->where([
'user_id'=>0,
'deletetime'=>null,
'name'=>$hobbies['name']
])->order(['weigh'=>'desc'])->select();
$is_exist_user = Db::name('hobbies')->where([
'deletetime'=>null,
'user_id'=>$this->auth->id,
'name'=>$hobbies['name']
])->order(['createtime'=>'asc'])->select();
if(!empty($is_exist) || !empty($is_exist_user)){
$this->error('该标签已存在');
}
//走微信接口验证是否可用
$res = $this->sanfang_check_text($hobbies['name']);
if($res['code'] != 200){
$this->error($res['msg']);
}
$hobbies['user_id'] = $this->auth->id;
$hobbies['createtime'] = time();
$hobbies['updatetime'] = time();
$hobbies['weigh'] = 0;
$hobbies['deletetime'] = null;
$res = Db::name('hobbies')->insert($hobbies);
if($res){
$this->success('添加成功');
}else{
$this->error('添加失败');
}
}
/**
* 会员等级
*/
public function get_level_list(){
$user_level = new UserLevel();
$info = $this->auth->getUserinfo();
$list = $user_level->order(['weigh'=>'desc'])->select();
foreach ($list as $k=>$v){
if($v['level'] > $info['level']){
$list[$k]['buttton'] = false;
}else{
$list[$k]['buttton'] = true;
}
}
$this->success('获取成功',$list);
}
/**
* 获取每日牵绊
*/
public function get_bond(){
$UserBondConfig = new UserBondConfig();
$UserBondLog = new UserBondLog();
$list = $UserBondConfig->order(['weigh'=>'desc'])->select();
foreach($list as $k=>$v){
$list[$k]['intimacy_day'] = $UserBondLog->where([
'user_id'=>$this->auth->id,
'type'=>$v['type'],
'createdate'=>date('Y-m-d')
])->sum('intimacy');
$list[$k]['intimacy_percent'] = round($list[$k]['intimacy_day']/$v['upper'],2)*100;
}
$this->success('获取成功',$list);
}
/**
* 增加亲密度,并判断是否达到每日上限,是否升级会员等级
*/
public function add_bond(){
$post_data = $this->request->post();
if(empty($post_data['type'])){
$this->error('参数错误');
}
if(empty($post_data['num'])){
$this->error('参数错误');
}
//查询每日牵绊数据
$UserBondConfig = new UserBondConfig();
$info = $UserBondConfig->where('type',$post_data['type'])->find();
if(empty($info)){
$this->error('参数错误');
}
//增加数量
$intimacy = $post_data['num']*$info['single_exp'];
$UserBondLog = new UserBondLog();
$user_info = $this->auth->getUserinfo();
$result = $UserBondLog->add_log($post_data['type'],$intimacy,$user_info);
$this->success('操作成功',$result);
}
/**
* 余额记录
*/
public function money_log(){
$post_data = $this->request->post();
$page = isset($post_data['page']) ? $post_data['page'] : 1;
$limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
$where = [];
if($post_data['type'] == 1){
//充值
$where['money'] = ['>',0];
}else{
//消费
$where['money'] = ['<',0];
}
$where['user_id'] = $this->auth->id;
$list = Db::name('user_money_log')->where($where)->order('id desc')->page($page,$limit)->select();
foreach($list as $key=>$val){
$list[$key]['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
}
$this->success('操作成功',$list);
}
/**
* 换装套餐列表
*/
public function get_count_package(){
$list = Db::name('count_package')->order(['weigh'=>'desc'])->select();
$this->success('操作成功',$list);
}
/**
* 兑换换装次数
*/
public function exchange_count(){
$post_data = $this->request->post();
$package_id = $post_data['count_package_id'];
if(empty($package_id)){
$this->error('请选择套餐');
}
$package = Db::name('count_package')->where('id',$package_id)->find();
if(!$package){
$this->error('套餐不存在');
}
$user = User::get($this->auth->id);
if($user['money'] < $package['jinbi']){
$this->error('金币不足');
}
Db::startTrans();
try {
//扣除金币
User::money($package['jinbi']*-1, $this->auth->id, '兑换换装次数');
//扣除金币
User::clothes_num($package['num'], $this->auth->id, '金币兑换换装次数');
Db::commit();
}catch (\Exception $e){
Db::rollback();
$this->error($e->getMessage());
}
$this->success('兑换成功');
}
/**
* 充值套餐列表
*/
public function get_recharge_package(){
$list = Db::name('recharge_package')->order(['weigh'=>'desc'])->select();
$this->success('操作成功',$list);
}
/**
* VIP套餐列表
*/
public function get_vip_package(){
$list = Db::name('vip_package')->order(['weigh'=>'desc'])->select();
$this->success('操作成功',$list);
}
}