以前我写过一篇如何少写PHP "烂"代码 https://segmentfault.com/a/11...
感受不少新人对此不太理解。今天以打卡功能为例,去讲解其中的奥秘。那篇文章讲过代码开发的过程当中分几种类型。segmentfault
Route -> Controller -> Service -> Action
Route -> Controller -> Service -> Repository
通过屡次实际开发验证后,发现Repository彻底是屡次一举。因此在这里更正下,取消Repository。api
Route -> Controller -> Service
需求是这样的,用户天天打卡得到积分,积分计入用户帐户,而且需记录用户积分的获取及消费状况。如图所示,请求到控制器后,经过控制器去调用服务,服务又调用建立用户打卡模块完成打开,在用户打卡过程当中对用户帐户积分进行变动及记录用户积分获取记录。架构
CREATE TABLE `member_attendance` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `member_id` int(11) NOT NULL COMMENT '用户编码', `status` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0' COMMENT '1签到成功', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `wallet` ( `user_id` bigint(20) NOT NULL COMMENT '用户标示', `balance` decimal(12,2) NOT NULL COMMENT '钱包余额', `integral` decimal(12,2) NOT NULL DEFAULT '0', `add_time` int(11) NOT NULL COMMENT '添加时间', `update_time` int(11) NOT NULL COMMENT '更改时间', UNIQUE KEY `wallet_user_id_unique` (`user_id`), KEY `wallet_user_id_index` (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `member_integral_detail` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `member_id` int(11) NOT NULL COMMENT '用户编码', `title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '来源或消费', `integral` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '积分数', `type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '类型 0收入 -1支出', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
$api->post ('user/attendance','UserController@attendance');
public function attendance() { $result = $this->userService->attendance ($this->request); if ($result) { return $this->response->array (Response::return (200, '打卡成功')); } return $this->response->array (Response::return (0, "打卡失败或已打卡")); }
public function attendance($request) { return (new CreateUserAttendance())->execute ($request); }
public function issetToday($userId) { $result = MemberAttendance::where ([ ['member_id', '=', $userId], ]) ->whereDate ('created_at', date ('Y-m-d', time ())) ->exists (); return $result; } // -------------------- 上述是下方issetToday方法,写在MemberModel中 class CreateUserAttendance { public function execute($data) { if ((new MemberAttendance())->issetToday ($data->user_id)) { return false; } $models = new MemberAttendance(); $models->member_id = $data->user_id; $models->status = (string)"1"; $result = $models->save (); if ($result) { (new CreateUserIntegralDetail())->execute ($data->user_id, '打卡', 10, 0); return true; } return false; } }
interface integralDetail { public function execute($userId, $title, $integral, $type); } class CreateUserIntegralDetail extends UpdateUserWalletIntegral implements integralDetail { public function execute($userId, $title, $integral, $type) { parent::exec ($userId, $integral, $type); $models = new MemberIntegralDetail(); $models->member_id = $userId; $models->title = $title; $models->integral = $integral; $models->type = $type; return $models->save (); } }
上述代码继承了更新用户积分的动做,在每次打卡成功后,咱们调用父类方法直接更新用户积分。post
class UpdateUserWalletIntegral { public function exec($userId, $integral, $type) { if ($type == 0) { Wallet::where (['user_id', '=', $userId])->increment ('integral', $integral); } else { Wallet::where (['user_id', '=', $userId])->decrement ('integral', $integral); } } }
感谢你看到这里,但愿本篇文章能够帮到你。有什么问题可在下方评论区留言。谢谢🙏this