- 框架初始化
 - 安装插件
 - 修复PHP8.4报错
This commit is contained in:
2025-04-19 17:21:20 +08:00
commit c6a4e1f5f6
5306 changed files with 967782 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
<?php
namespace addons\shopro\controller\user;
use addons\shopro\controller\Common;
use app\admin\model\shopro\user\Account as AccountModel;
class Account extends Common
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function index()
{
$params = $this->request->only([
'type'
]);
$user = auth_user();
$where = [
'user_id' => $user->id
];
if (!empty($params['type'])) {
$where['type'] = $params['type'];
}
$data = AccountModel::where($where)->order('updatetime desc')->find();
if (!$data) {
$this->error(__('No Results were found'));
}
$this->success('获取成功', $data);
}
public function save()
{
$user = auth_user();
$params = $this->request->only([
'type', 'account_name', 'account_header', 'account_no'
]);
if (!in_array($params['type'], ['wechat', 'alipay', 'bank'])) {
$this->error('请选择正确的账户类型');
}
if ($params['type'] === 'alipay') {
$params['account_header'] = '支付宝账户';
}
if ($params['type'] === 'wechat') {
$params['account_header'] = '微信账户';
$params['account_no'] = '-';
}
$this->svalidate($params, ".{$params['type']}");
$data = AccountModel::where(['user_id' => $user->id, 'type' => $params['type']])->find();
if (!$data) {
$data = AccountModel::create([
'user_id' => $user->id,
'type' => $params['type'],
'account_name' => $params['account_name'],
'account_header' => $params['account_header'],
'account_no' => $params['account_no'],
]);
} else {
$data->save($params);
}
$this->success('保存成功', $data);
}
}

View File

@@ -0,0 +1,179 @@
<?php
namespace addons\shopro\controller\user;
use think\Db;
use addons\shopro\controller\Common;
use app\admin\model\shopro\data\Area as AreaModel;
use app\admin\model\shopro\user\Address as UserAddressModel;
class Address extends Common
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function index()
{
$user = auth_user();
$userAddresses = UserAddressModel::where('user_id', $user->id)
->order('is_default', 'desc')
->order('id', 'desc')
->select();
$this->success('获取成功', $userAddresses);
}
/**
* 添加收货地址
*/
public function add()
{
$user = auth_user();
$params = $this->request->only([
'consignee', 'mobile', 'province_name', 'city_name', 'district_name', 'address', 'is_default'
]);
$params['user_id'] = $user->id;
$this->svalidate($params, ".add");
$params = $this->getAreaIdByName($params);
Db::transaction(function () use ($user, $params) {
$userAddress = new UserAddressModel();
$userAddress->save($params);
if ($userAddress->is_default) {
// 修改其他收货地址为非默认
UserAddressModel::where('id', '<>', $userAddress->id)
->where('user_id', $user->id)
->where('is_default', 1)
->update(['is_default' => 0]);
}
});
$this->success('保存成功');
}
/**
* 收货地址详情
*
* @param $id
* @return \think\Response
*/
public function detail()
{
$user = auth_user();
$id = $this->request->param('id');
$userAddress = UserAddressModel::where('user_id', $user->id)->where('id', $id)->find();
if (!$userAddress) {
$this->error(__('No Results were found'));
}
$this->success('获取成功', $userAddress);
}
/**
* 默认收货地址
*
* @return \think\Response
*/
public function default()
{
$user = auth_user();
$userAddress = UserAddressModel::default()->where('user_id', $user->id)->find();
$this->success('获取成功', $userAddress);
}
/**
* 编辑收货地址
*
* @return \think\Response
*/
public function edit()
{
$user = auth_user();
$id = $this->request->param('id');
$userAddress = UserAddressModel::where('user_id', $user->id)->where('id', $id)->find();
if (!$userAddress) {
$this->error(__('No Results were found'));
}
$params = $this->request->only([
'consignee', 'mobile', 'province_name', 'city_name', 'district_name', 'address', 'is_default'
]);
$this->svalidate($params, ".edit");
$params = $this->getAreaIdByName($params);
Db::transaction(function () use ($user, $params, $userAddress) {
$userAddress->save($params);
if ($userAddress->is_default) {
// 修改其他收货地址为非默认
UserAddressModel::where('id', '<>', $userAddress->id)
->where('user_id', $user->id)
->where('is_default', 1)
->update(['is_default' => 0]);
}
});
$this->success('保存成功');
}
/**
* 删除收货地址
*
* @param string $id 要删除的收货地址
* @return void
*/
public function delete()
{
$user = auth_user();
$id = $this->request->param('id');
$userAddress = UserAddressModel::where('user_id', $user->id)->where('id', $id)->find();
if (!$userAddress) {
$this->error(__('No Results were found'));
}
$userAddress->delete();
$this->success('删除成功');
}
private function getAreaIdByName($params)
{
$province = AreaModel::where([
'name' => $params['province_name'],
'level' => 'province'
])->find();
if (!$province) $this->error('请选择正确的行政区');
$params['province_id'] = $province->id;
$city = AreaModel::where([
'name' => $params['city_name'],
'level' => 'city',
'pid' => $province->id
])->find();
if (!$city) $this->error('请选择正确的行政区');
$params['city_id'] = $city->id;
$district = AreaModel::where([
'name' => $params['district_name'],
'level' => 'district',
'pid' => $city->id
])->find();
if (!$district) $this->error('请选择正确的行政区');
$params['district_id'] = $district->id;
return $params;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace addons\shopro\controller\user;
use think\helper\Str;
use addons\shopro\controller\Common;
use app\admin\model\shopro\user\Coupon as UserCouponModel;
class Coupon extends Common
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function index()
{
$user = auth_user();
$type = $this->request->param('type', 'can_use'); // 优惠券类型geted=已领取can_use=可用cannot_use=暂不可用used=已使用expired=已过期,invalid=已失效(包含已使用和已过期)
$userCoupons = UserCouponModel::with('coupon')->where('user_id', $user->id);
if (in_array($type, ['geted', 'can_use', 'cannot_use', 'used', 'expired', 'invalid'])) {
$userCoupons = $userCoupons->{Str::camel($type)}();
}
$userCoupons = $userCoupons->order('id', 'desc')->paginate($this->request->param('list_rows', 10));
$this->success('获取成功', $userCoupons);
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace addons\shopro\controller\user;
use addons\shopro\controller\Common;
use app\admin\model\shopro\user\GoodsLog as UserGoodsLogModel;
use app\admin\model\shopro\goods\Goods;
class GoodsLog extends Common
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function index()
{
$user = auth_user();
$type = $this->request->param('type');
// 首先删除商品不存在的记录
UserGoodsLogModel::whereNotExists(function ($query) {
$goodsTableName = (new Goods())->getQuery()->getTable();
$tableName = (new UserGoodsLogModel())->getQuery()->getTable();
$query = $query->table($goodsTableName)->where($goodsTableName . '.id=' . $tableName . '.goods_id')->whereNull($goodsTableName . '.deletetime'); // 不查软删除的商品
return $query;
})->where('user_id', $user->id)->delete();
$logs = UserGoodsLogModel::with('goods')->{$type}()->where('user_id', $user->id);
$logs = $logs->order('updatetime', 'desc')->paginate($this->request->param('list_rows', 10)); // 按照更新时间排序
$this->success('获取成功', $logs);
}
/**
* 收藏/取消收藏
*
* @param Request $request
* @return void
*/
public function favorite()
{
$user = auth_user();
$goods_id = $this->request->param('goods_id');
$goods_ids = $this->request->param('goods_ids');
if (!$goods_id && !$goods_ids) {
$this->error('缺少参数');
}
if ($goods_ids) {
// 个人中心批量取消收藏
$log = UserGoodsLogModel::favorite()->whereIn('goods_id', $goods_ids)
->where('user_id', $user->id)->delete();
$this->success('取消收藏成功');
}
$log = UserGoodsLogModel::favorite()->where('goods_id', $goods_id)
->where('user_id', $user->id)->find();
$favorite = false; // 取消收藏
if ($log) {
// 取消收藏
$log->delete();
} else {
$favorite = true; // 收藏
$log = new UserGoodsLogModel();
$log->goods_id = $goods_id;
$log->user_id = $user->id;
$log->type = 'favorite';
$log->save();
}
$this->success($favorite ? '收藏成功' : '取消收藏');
}
public function viewDel()
{
$goods_id = $this->request->param('goods_id'); // 支持 逗号分开
$user = auth_user();
UserGoodsLogModel::views()->whereIn('goods_id', $goods_id)
->where('user_id', $user->id)->delete();
$this->success('删除成功');
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace addons\shopro\controller\user;
use addons\shopro\controller\Common;
use app\admin\model\shopro\user\Invoice as UserInvoiceModel;
use think\Db;
class Invoice extends Common
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function index()
{
$user = auth_user();
$userInvoices = UserInvoiceModel::where('user_id', $user->id)
->order('id', 'asc')
->select();
$this->success('获取成功', $userInvoices);
}
/**
* 添加发票抬头
*
* @return \think\Response
*/
public function add()
{
$user = auth_user();
$params = $this->request->only([
'type', 'name', 'tax_no', 'address', 'mobile', 'bank_name', 'bank_no'
]);
$params['user_id'] = $user->id;
$this->svalidate($params, ".add");
Db::transaction(function () use ($user, $params) {
$userInvoice = new UserInvoiceModel();
$userInvoice->save($params);
});
$this->success('保存成功');
}
/**
* 发票详情
*
* @param $id
* @return \think\Response
*/
public function detail()
{
$user = auth_user();
$id = $this->request->param('id');
$userInvoice = UserInvoiceModel::where('user_id', $user->id)->where('id', $id)->find();
if (!$userInvoice) {
$this->error(__('No Results were found'));
}
$this->success('获取成功', $userInvoice);
}
/**
* 编辑发票
*
* @return \think\Response
*/
public function edit()
{
$user = auth_user();
$id = $this->request->param('id');
$params = $this->request->only([
'type', 'name', 'tax_no', 'address', 'mobile', 'bank_name', 'bank_no'
]);
$this->svalidate($params, ".edit");
$userInvoice = UserInvoiceModel::where('user_id', $user->id)->where('id', $id)->find();
if (!$userInvoice) {
$this->error(__('No Results were found'));
}
Db::transaction(function () use ($params, $userInvoice) {
$userInvoice->save($params);
});
$this->success('保存成功');
}
/**
* 删除发票
*
* @param string $id 要删除的发票
* @return void
*/
public function delete()
{
$user = auth_user();
$id = $this->request->param('id');
$userInvoice = UserInvoiceModel::where('user_id', $user->id)->where('id', $id)->find();
if (!$userInvoice) {
$this->error(__('No Results were found'));
}
$userInvoice->delete();
$this->success('删除成功');
}
}

View File

@@ -0,0 +1,305 @@
<?php
namespace addons\shopro\controller\user;
use app\common\library\Sms;
use addons\shopro\controller\Common;
use addons\shopro\service\user\UserAuth;
use app\admin\model\shopro\user\User as UserModel;
use app\admin\model\shopro\user\Coupon as UserCouponModel;
use app\admin\model\shopro\order\Order as OrderModel;
use app\admin\model\shopro\order\Aftersale as AftersaleModel;
use app\admin\model\shopro\ThirdOauth;
class User extends Common
{
protected $noNeedLogin = ['smsRegister', 'accountLogin', 'smsLogin', 'resetPassword'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
\think\Lang::load(APP_PATH . 'api/lang/zh-cn/user.php'); // 加载语言包
}
/**
* 用户数据
*/
public function data()
{
$user = auth_user();
// 查询用户优惠券数量
$data['coupons_num'] = UserCouponModel::geted()->where('user_id', $user->id)->count();
// 订单数量
$orderNum = [];
$orderNum['unpaid'] = OrderModel::where('user_id', $user->id)->unpaid()->count();
$orderNum['nosend'] = OrderModel::where('user_id', $user->id)->pretendPaid()->nosend()->count();
$orderNum['noget'] = OrderModel::where('user_id', $user->id)->pretendPaid()->noget()->count();
$orderNum['nocomment'] = OrderModel::where('user_id', $user->id)->paid()->nocomment()->count();
$orderNum['aftersale'] = AftersaleModel::where('user_id', $user->id)->needOper()->count();
$data['order_num'] = $orderNum;
$this->success('用户数据', $data);
}
/**
* 第三方授权信息
*/
public function thirdOauth()
{
$user = auth_user();
$provider = $this->request->param('provider', '');
$platform = $this->request->param('platform', '');
if (!in_array($platform, ['miniProgram', 'officialAccount', 'openPlatform'])) {
$this->error(__('Invalid parameters'));
}
$where = [
'platform' => $platform,
'user_id' => $user->id
];
if ($provider !== '') {
$where['provider'] = $provider;
}
$oauth = ThirdOauth::where($where)->field('nickname, avatar, platform, provider')->find();
$this->success('', $oauth);
}
/**
* 用户信息
*/
public function profile()
{
//TODO @ldh: 1.账号被禁用 2.连表查group
$user = auth_user(true);
$user = UserModel::with(['parent_user', 'third_oauth'])->where('id', $user->id)->find();
$user->hidden(['password', 'salt', 'createtime', 'updatetime', 'deletetime', 'remember_token', 'login_fail', 'login_ip', 'login_time']);
$this->success('个人详情', $user);
}
/**
* 更新用户资料
*/
public function update()
{
$user = auth_user();
$params = $this->request->only(['avatar', 'nickname', 'gender']);
$this->svalidate($params);
$user->save($params);
$user->hidden(['password', 'salt', 'createtime', 'updatetime', 'deletetime', 'remember_token', 'login_fail', 'login_ip', 'login_time']);
$this->success('更新成功', $user);
}
/**
* 账号密码登录
*/
public function accountLogin()
{
$user = auth_user();
if ($user) {
$this->error('您已登录,不需要重新登录');
}
$params = $this->request->only(['account', 'password']);
$this->svalidate($params, '.accountLogin');
$ret = $this->auth->login($params['account'], $params['password']);
if ($ret) {
set_token_in_header($this->auth->getToken());
$this->success(__('Logged in successful'));
} else {
$this->error($this->auth->getError() ?: '注册失败');
}
}
/**
* 短信验证码登陆
*/
public function smsLogin()
{
$user = auth_user();
if ($user) {
$this->error('您已登录,不需要重新登录');
}
$params = $this->request->only(['mobile', 'code']);
$this->svalidate($params, '.smsLogin');
if (!Sms::check($params['mobile'], $params['code'], 'mobilelogin')) {
$this->error(__('Captcha is incorrect'));
}
$user = UserModel::getByMobile($params['mobile']);
if ($user) {
if ($user->status != 'normal') {
$this->error(__('Account is locked'));
}
//如果已经有账号则直接登录
$ret = $this->auth->direct($user->id);
}else {
$this->error('该手机号暂未注册');
}
if (isset($ret) && $ret) {
Sms::flush($params['mobile'], 'mobilelogin');
set_token_in_header($this->auth->getToken());
$this->success(__('Logged in successful'));
} else {
$this->error($this->auth->getError() ?: '登录失败');
}
}
/**
* 短信验证码注册
*/
public function smsRegister()
{
$user = auth_user();
if ($user) {
$this->error('您已登录,请先退出登录');
}
$params = $this->request->only(['mobile', 'code', 'password']);
$this->svalidate($params, '.smsRegister');
$ret = Sms::check($params['mobile'], $params['code'], 'register');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
// 注册
$userAuth = new UserAuth();
$auth = $userAuth->register($params);
set_token_in_header($auth->getToken());
$this->success(__('Sign up successful'));
}
/**
* 修改密码
*/
public function changePassword()
{
$user = auth_user();
$params = $this->request->only(['oldPassword', 'newPassword']);
$this->svalidate($params, '.changePassword');
$userAuth = new UserAuth();
$userAuth->changePassword($params['newPassword'], $params['oldPassword']);
$this->auth->direct($user->id);
set_token_in_header($this->auth->getToken());
$this->success(__('Change password successful'));
}
/**
* 重置/忘记密码
*/
public function resetPassword()
{
$params = $this->request->only(['mobile', 'code', 'password']);
$this->svalidate($params, '.resetPassword');
$ret = Sms::check($params['mobile'], $params['code'], 'resetpwd');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
$userAuth = new UserAuth();
$userAuth->resetPassword($params);
$this->success(__('Reset password successful'));
}
/**
* 更换手机号
*/
public function changeMobile()
{
$params = $this->request->only(['mobile', 'code']);
$this->svalidate($params, '.changeMobile');
$ret = Sms::check($params['mobile'], $params['code'], 'changemobile');
if (!$ret) {
$this->error(__('Captcha is incorrect'));
}
$userAuth = new UserAuth();
$userAuth->changeMobile($params);
$this->success('绑定成功');
}
/**
* 修改用户名
*/
public function changeUsername()
{
$user = auth_user(true);
$params = $this->request->only(['username']);
$this->svalidate($params, '.changeUsername');
$userAuth = new UserAuth();
$userAuth->changeUsername($params);
$this->success('绑定成功');
}
/**
* 更新小程序头像和昵称
*/
public function updateMpUserInfo()
{
$user = auth_user(true);
$params = $this->request->only(['avatar', 'nickname']);
$this->svalidate($params, '.updateMpUserInfo');
$user->save($params);
$thirdOauth = \app\admin\model\shopro\ThirdOauth::where('user_id', $user->id)->where([
'provider' => 'wechat',
'platform' => 'miniProgram'
])->find();
$thirdOauth->save($params);
$this->success('绑定成功');
}
/**
* 登出
*/
public function logout()
{
$userAuth = new UserAuth();
$userAuth->logout();
$this->success(__('Logout successful'));
}
/**
* 用户注销
*/
public function logoff()
{
$userAuth = new UserAuth();
$userAuth->logoff();
$this->success('注销成功');
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace addons\shopro\controller\user;
use addons\shopro\controller\Common;
use app\admin\model\shopro\user\WalletLog as UserWalletLogModel;
class WalletLog extends Common
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
public function index()
{
$type = $this->request->param('type', 'money');
$tab = $this->request->param('tab', 'all');
$list_rows = $this->request->param('list_rows', 10);
$date = $this->request->param('date/a');
$user = auth_user();
$where['user_id'] = $user->id;
switch ($tab) {
case 'income':
$where['amount'] = ['>', 0];
break;
case 'expense':
$where['amount'] = ['<', 0];
break;
}
$income = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '>', 0)->whereTime('createtime', 'between', $date)->sum('amount');
$expense = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '<', 0)->whereTime('createtime', 'between', $date)->sum('amount');
$logs = UserWalletLogModel::where($where)->{$type}()->whereTime('createtime', 'between', $date)->order('createtime', 'desc')->paginate($list_rows);
$this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]);
}
}