|
|
@@ -168,23 +168,32 @@ class Login extends AuthController
|
|
|
}
|
|
|
$userService = app()->make(UserServices::class);
|
|
|
$userInfo = $userService->getUserScore($unionid);
|
|
|
- $info = array('unionid' => $unionid,'integral' => $userInfo['integral']);
|
|
|
- if(!$userInfo['uid']){
|
|
|
- $info['code'] = 0;
|
|
|
+ if(empty($userInfo)){
|
|
|
+ $params = [$unionid];
|
|
|
+ $score = Db::query('select sum(score) score from eb_score_record where uniond_id = ? and `status` = 0', $params);
|
|
|
+ if(empty($score)){
|
|
|
+ $info = array('unionid' => $unionid,'integral' => 0);
|
|
|
+ }else{
|
|
|
+ $integral = $score[0]['score'];
|
|
|
+ if(empty($integral)){
|
|
|
+ $integral = 0;
|
|
|
+ }
|
|
|
+ $info = array('unionid' => $unionid,'integral' => $integral);
|
|
|
+ }
|
|
|
}else{
|
|
|
- $info['code'] = 1;
|
|
|
+ $info = array('unionid' => $unionid,'integral' => $userInfo['integral']);
|
|
|
}
|
|
|
return app('json')->success($info);
|
|
|
}
|
|
|
|
|
|
|
|
|
- private function checkLock($orderId, $unionId, $score){
|
|
|
+ private function checkLock($orderId, $unionId, $score,$title,$mark,$integration_status,$status){
|
|
|
// 2. 准备 INSERT IGNORE SQL 语句
|
|
|
// 使用 IGNORE 关键字,如果 order_id 主键冲突,则忽略本次插入
|
|
|
- $sql = "INSERT IGNORE INTO `eb_score_record` (`order_id`, `create_time`, `uniond_id`, `score`) VALUES (?, NOW(), ?, ?)";
|
|
|
+ $sql = "INSERT IGNORE INTO `eb_score_record` (`order_id`, `create_time`, `uniond_id`, `score`,`title`,`mark`,`integration_status`,`status`) VALUES (?, NOW(), ?, ?,?,?,?,?)";
|
|
|
|
|
|
// 3. 准备绑定的参数,防止SQL注入
|
|
|
- $params = [$orderId, $unionId, $score];
|
|
|
+ $params = [$orderId, $unionId, $score,$title,$mark,$integration_status,$status];
|
|
|
|
|
|
try {
|
|
|
// 4. 执行 SQL
|
|
|
@@ -217,26 +226,45 @@ class Login extends AuthController
|
|
|
if(!$isRight){
|
|
|
return app('json')->fail("无权限");
|
|
|
}
|
|
|
- $canAdd = $this->checkLock($order_id,$unionid,$score);
|
|
|
- if(!$canAdd){
|
|
|
- return app('json')->fail("流水号已经存在");
|
|
|
+ $isLock = CacheService::setMutex($order_id,5);
|
|
|
+ if(!$isLock){
|
|
|
+ return app('json')->fail("同一订单号请勿提交频繁");
|
|
|
}
|
|
|
$userService = app()->make(UserServices::class);
|
|
|
$userInfo = $userService->getUserScore($unionid);
|
|
|
+ if(empty($userInfo)){//代表用户不存在
|
|
|
+ $canAdd = $this->checkLock($order_id,$unionid,$score,$title,$mark,$integration_status,0);
|
|
|
+ if(!$canAdd){
|
|
|
+ return app('json')->fail("流水号已经存在");
|
|
|
+ }
|
|
|
+ $code = 2;//代表暂存
|
|
|
+ }else{
|
|
|
+ $uid = $userInfo['uid'];//用户存在的话
|
|
|
+ $canAdd = $this->checkLock($order_id,$unionid,$score,$title,$mark,$integration_status,1);
|
|
|
+ if(!$canAdd){
|
|
|
+ return app('json')->fail("流水号已经存在。");
|
|
|
+ }
|
|
|
+ $result = $this->doAddScore($uid,$score,$integration_status,$title,$mark);
|
|
|
+ if($result){
|
|
|
+ $code = 1;
|
|
|
+ $params = [$order_id];
|
|
|
+ Db::execute('update `eb_score_record` set `status` = 0 where `order_id` = ?', $params);
|
|
|
+ }else{
|
|
|
+ $code = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $info = array('unionid' => $unionid);
|
|
|
+ $info['code'] = $code;
|
|
|
+ return app('json')->success($info);
|
|
|
+ }
|
|
|
|
|
|
- $uid = $userInfo['uid'];
|
|
|
+ private function doAddScore($uid,$score,$integration_status,$title,$mark){
|
|
|
+ $userService = app()->make(UserServices::class);
|
|
|
$data = array('integration' => $score,'integration_status'=>$integration_status);
|
|
|
$data['title'] = $title;
|
|
|
$data['mark'] = $mark;
|
|
|
$data['is_other'] = true;
|
|
|
- Log::error($data);
|
|
|
$result = $userService->addScore($uid,$data);
|
|
|
- $info = array('unionid' => $unionid);
|
|
|
- if($result){
|
|
|
- $info['code'] = 1;
|
|
|
- }else{
|
|
|
- $info['code'] = 0;
|
|
|
- }
|
|
|
- return app('json')->success($info);
|
|
|
+ return $result;
|
|
|
}
|
|
|
}
|