diff --git a/README.md b/README.md index beb62da..6ac9165 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ ```php $permissions = [ - 'users' => ['name' => '用户管理', 'curd' => true, 'children' => ['balance' => '变更余额']], + 'users' => ['name' => '用户管理', 'curd' => true, 'children' => ['balance' => '变更余额', 'password' => '重置密码']], 'user-balance' => ['name' => '余额流水', 'curd' => ['index', 'show']], ]; ``` diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 8080578..ed71408 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -41,6 +41,7 @@ class UserFactory extends Factory return [ 'username' => $username, + 'gender' => $faker->randomElement(['男', '女', '未知']), // 123456 'password' => '$2y$10$QAGGjfTDjmgDFrX8LkFZ4e0A4MG.doRc8xoq1Cixf6IbHq7RPRqtq', 'name' => $faker->name(), diff --git a/database/migrations/2022_08_11_110611_create_users_table.php b/database/migrations/2022_08_11_110611_create_users_table.php index 8f0782a..d79b734 100644 --- a/database/migrations/2022_08_11_110611_create_users_table.php +++ b/database/migrations/2022_08_11_110611_create_users_table.php @@ -19,6 +19,7 @@ return new class extends Migration $table->string('password')->nullable(); $table->string('phone')->nullable(); $table->string('name')->nullable(); + $table->string('gender')->nullable(); $table->string('avatar')->nullable(); $table->decimal('balance', 12, 2)->default(0)->comment('余额'); $table->string('invite_code')->comment('邀请码'); @@ -44,6 +45,7 @@ return new class extends Migration Schema::create('user_balance_logs', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('user_id'); + $table->string('user_name')->comment('用户名'); $table->string('cate')->comment('类别'); $table->string('description')->comment('描述'); $table->decimal('amount', 12, 2)->comment('变动数量, 正数为增加, 负数为减少'); diff --git a/lang/zh_CN/user-balance.php b/lang/zh_CN/user-balance.php index 9c3ed07..da0bd27 100644 --- a/lang/zh_CN/user-balance.php +++ b/lang/zh_CN/user-balance.php @@ -7,9 +7,7 @@ return [ ], 'fields' => [ 'user_id' => '用户', - 'user' => [ - 'phone' => '用户', - ], + 'user_name' => '用户', 'cate' => '类别', 'amount' => '金额', 'description' => '描述', diff --git a/lang/zh_CN/user.php b/lang/zh_CN/user.php index 0a79a2e..ca4d5cf 100644 --- a/lang/zh_CN/user.php +++ b/lang/zh_CN/user.php @@ -13,7 +13,6 @@ return [ 'phone' => '手机号', 'avatar' => '头像', 'balance' => '余额', - 'profit' => 'e品额', 'inviter_id' => '邀请人', 'inviter' => [ 'name' => '邀请人', diff --git a/routes/api.php b/routes/api.php index fd82249..4592592 100644 --- a/routes/api.php +++ b/routes/api.php @@ -18,7 +18,6 @@ Route::group([ Route::post('wx-bind-phone', [AuthController::class, 'wxbindPhone']); Route::post('reset', [AuthController::class, 'reset']); - Route::post('reset-pwd', [AuthController::class, 'resetPwd']); }); Route::group(['prefix' => 'user', 'middleware' => ['auth:api']], function () { diff --git a/src/Action/ShowPassword.php b/src/Action/ShowPassword.php new file mode 100644 index 0000000..c19df11 --- /dev/null +++ b/src/Action/ShowPassword.php @@ -0,0 +1,26 @@ +parent->model(); + $form = PasswordForm::make()->payload(['id' => $model->id]); + return Modal::make()->lg()->title($this->title)->body($form)->button(''); + } + + protected function authorize($user): bool + { + return $user->can('dcat.admin.users.password'); + } +} diff --git a/src/Form/BalanceForm.php b/src/Form/BalanceForm.php index 2a629bc..d067926 100644 --- a/src/Form/BalanceForm.php +++ b/src/Form/BalanceForm.php @@ -31,6 +31,7 @@ class BalanceForm extends Form implements LazyRenderable $admin = Admin::user(); $user->balanceLogs()->create([ + 'user_name' => $user->phone, 'amount' => $amount, 'balance' => $user->balance, 'cate' => $input['cate'], diff --git a/src/Form/PasswordForm.php b/src/Form/PasswordForm.php new file mode 100644 index 0000000..b7239a3 --- /dev/null +++ b/src/Form/PasswordForm.php @@ -0,0 +1,33 @@ + false, 'submit' => true]; + + public function handle(array $input) + { + if ($input['password'] !== $input['confirm_password']) { + return $this->response()->error('两次密码不一致'); + } + $info = User::findOrFail($this->payload['id']); + $info->update(['password' => Hash::make($input['password'])]); + + return $this->response()->success('操作成功')->refresh(); + } + + public function form() + { + $this->password('password', '新密码'); + $this->password('confirm_password', '确认密码'); + } +} diff --git a/src/Http/Admin/UserBalanceController.php b/src/Http/Admin/UserBalanceController.php index 42588c4..ae7a34f 100644 --- a/src/Http/Admin/UserBalanceController.php +++ b/src/Http/Admin/UserBalanceController.php @@ -23,7 +23,7 @@ class UserBalanceController extends AdminController $grid->disableRowSelector(); - $grid->column('user.phone')->link(fn() => admin_url('user-balance?user_id=' . $this->user_id), '_self'); + $grid->column('user_name')->if(fn() => !!$this->user)->link(fn() => admin_url('user-balance?user_id=' . $this->user_id), '_self'); $grid->column('cate'); $grid->column('description'); $grid->column('amount'); @@ -58,8 +58,8 @@ class UserBalanceController extends AdminController protected function detail($id) { - return Show::make($id, UserBalance::with(['user']), function (Show $show) { - $show->field('user.phone'); + return Show::make($id, UserBalance::with([]), function (Show $show) { + $show->field('user_name'); $show->field('cate'); $show->field('description'); $show->field('amount'); diff --git a/src/Http/Admin/UserController.php b/src/Http/Admin/UserController.php index e182eb4..8345a5d 100644 --- a/src/Http/Admin/UserController.php +++ b/src/Http/Admin/UserController.php @@ -20,6 +20,7 @@ use Peidikeji\User\Models\User; use Peidikeji\User\Models\UserSocialite; use Illuminate\Support\Str; use Peidikeji\User\Action\ShowBalance; +use Peidikeji\User\Action\ShowPassword; class UserController extends AdminController { @@ -144,12 +145,14 @@ class UserController extends AdminController $show->field('inviter.phone'); $show->field('invite_code'); $show->field('balance'); - $show->field('profit'); $show->field('created_at'); $show->tools(function (Tools $tools) { $tools->disableList(); + $tools->disableDelete(); + $tools->disableEdit(); $tools->append(new ShowBalance()); + $tools->append(new ShowPassword()); }); $tab = new Tab(); diff --git a/src/Http/Api/AuthController.php b/src/Http/Api/AuthController.php index c1d07d0..78eab94 100644 --- a/src/Http/Api/AuthController.php +++ b/src/Http/Api/AuthController.php @@ -2,9 +2,7 @@ namespace Peidikeji\User\Http\Api; -use App\Exceptions\BizException; use App\Http\Controllers\Controller; -use App\Models\Sms; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; use Illuminate\Validation\Rules\Password; @@ -42,10 +40,10 @@ class AuthController extends Controller ]); $phone = $request->input('phone'); - $result = Sms::checkCode('login', $phone, $request->input('code')); - if (!$result) { - return $this->error('验证码不正确或已过期'); - } + // $result = Sms::checkCode('login', $phone, $request->input('code')); + // if (!$result) { + // return $this->error('验证码不正确或已过期'); + // } $user = User::where('phone', $phone)->first(); if (!$user) { @@ -141,10 +139,10 @@ class AuthController extends Controller return $this->error('用户已经注册'); } - $result = Sms::checkCode('register', $phone, $request->input('code')); - if (!$result) { - return $this->error('验证码不正确或已过期'); - } + // $result = Sms::checkCode('register', $phone, $request->input('code')); + // if (!$result) { + // return $this->error('验证码不正确或已过期'); + // } $user = $this->createUser(['phone' => $phone], $request->input('invite_code')); @@ -160,10 +158,10 @@ class AuthController extends Controller ]); $phone = $request->input('phone'); - $result = Sms::checkCode('reset', $phone, $request->input('code')); - if (!$result) { - return $this->error('验证码不正确或已过期'); - } + // $result = Sms::checkCode('reset', $phone, $request->input('code')); + // if (!$result) { + // return $this->error('验证码不正确或已过期'); + // } $user = User::where('phone', $phone)->first(); if (!$user) { @@ -187,10 +185,9 @@ class AuthController extends Controller { if ($invite_code) { $inviterId = User::where('invite_code', $invite_code)->value('id'); - if (!$inviterId) { - throw new BizException('邀请码错误'); + if ($inviterId) { + $attributes['inviter_id'] = $inviterId; } - $attributes['inviter_id'] = $inviterId; } $user = User::create($attributes); @@ -198,24 +195,4 @@ class AuthController extends Controller return $user; } - - public function resetPwd(Request $request) - { - $input = $request->validate([ - 'password' => 'required|current_password:api', - 'new_password' => 'required', - ], [ - 'password.current_password' => '密码错误', - ]); - $user = auth('api')->user(); - if (!$user || !Hash::check($input['password'], $user->password)) { - throw new BizException('密码错误'); - } - - $user->password = bcrypt($input['new_password']); - $user->save(); - - $user->tokens()->delete(); - return $this->success('修改成功'); - } } diff --git a/src/Http/Resources/UserResource.php b/src/Http/Resources/UserResource.php index 9ac7c8c..1c8858a 100644 --- a/src/Http/Resources/UserResource.php +++ b/src/Http/Resources/UserResource.php @@ -3,30 +3,15 @@ namespace Peidikeji\User\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Http\Response; class UserResource extends JsonResource { public function toArray($request) { - return [ - 'id' => $this->id, - 'name' => $this->name, - 'username' => $this->username, - 'avatar' => $this->avatar, + return array_merge(UserTinyResource::make($this), [ 'balance' => $this->balance, - 'profit' => $this->profit, - 'invite_code' => $this->invite_code, 'inviter_id' => $this->inviter_id, 'phone' => $this->phone, - 'gender' => $this->gender, - 'vip_expired_at' => $this->vip_expired_at?->timestamp, - 'created_at' => $this->created_at?->timestamp, - ]; - } - - public function with($request) - { - return ['code' => Response::HTTP_OK, 'message' => '']; + ]); } } diff --git a/src/Http/Resources/UserTinyResource.php b/src/Http/Resources/UserTinyResource.php index f9c09ee..edfb0cf 100644 --- a/src/Http/Resources/UserTinyResource.php +++ b/src/Http/Resources/UserTinyResource.php @@ -3,7 +3,6 @@ namespace Peidikeji\User\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Http\Response; class UserTinyResource extends JsonResource { @@ -12,19 +11,13 @@ class UserTinyResource extends JsonResource return [ 'id' => $this->id, 'name' => $this->name, + 'gender' => $this->gender, 'username' => $this->username, 'avatar' => $this->avatar, 'invite_code' => $this->invite_code, - 'inviter_id' => $this->inviter_id, + 'phone' => $this->phone ? substr_replace($this->phone, '****', 3, 4) : $this->phone, 'created_at' => $this->created_at?->timestamp, - 'is_vip' => $this->isVip(), - 'vip_expired_at' => $this->vip_expired_at?->timestamp, ]; } - - public function with($request) - { - return ['code' => Response::HTTP_OK, 'message' => '']; - } } diff --git a/src/Models/User.php b/src/Models/User.php index 6649327..572b278 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -15,7 +15,7 @@ class User extends Authenticatable use HasDateTimeFormatter; use Filterable; - protected $fillable = ['username', 'password', 'avatar', 'balance', 'invite_code', 'inviter_id', 'inviter_path', 'name', 'phone']; + protected $fillable = ['username', 'password', 'avatar', 'balance', 'invite_code', 'inviter_id', 'inviter_path', 'name', 'gender', 'phone']; protected static function booted() { diff --git a/src/Models/UserBalance.php b/src/Models/UserBalance.php index 01652d6..04f1cd4 100644 --- a/src/Models/UserBalance.php +++ b/src/Models/UserBalance.php @@ -11,7 +11,7 @@ class UserBalance extends Model protected $table = 'user_balance_logs'; - protected $fillable = ['amount', 'balance', 'cate', 'description', 'remarks', 'source_id', 'source_type', 'user_id']; + protected $fillable = ['amount', 'balance', 'cate', 'description', 'remarks', 'source_id', 'source_type', 'user_id', 'user_name']; public function user() {