6 changed files with 181 additions and 0 deletions
-
1.gitignore
-
31app/Controller/UserController.php
-
45app/Request/UserUnionidRequest.php
-
63app/Service/UserService.php
-
18app/Service/UserServiceInterface.php
-
23config/routes.php
@ -0,0 +1,31 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
|
|||
namespace App\Controller; |
|||
|
|||
use Hyperf\Di\Annotation\Inject; |
|||
use App\Service\UserServiceInterface; |
|||
use App\Request\UserUnionidRequest; |
|||
|
|||
class UserController extends BaseController |
|||
{ |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var UserServiceInterface |
|||
*/ |
|||
protected $userService; |
|||
|
|||
/** |
|||
* 根据用户的openid更新unionid信息 |
|||
*/ |
|||
public function saveUserUnionid(UserUnionidRequest $request) |
|||
{ |
|||
$openid = $this->request->input('openid',''); |
|||
$unionid = $this->request->input('unionid',''); |
|||
|
|||
return $this->success($this->userService->saveUserUnionid($openid,$unionid)); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
|
|||
namespace App\Request; |
|||
|
|||
use Hyperf\Validation\Request\FormRequest; |
|||
|
|||
class UserUnionidRequest extends FormRequest |
|||
{ |
|||
/** |
|||
* Determine if the user is authorized to make this request. |
|||
*/ |
|||
public function authorize(): bool |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
/** |
|||
* Get the validation rules that apply to the request. |
|||
*/ |
|||
public function rules(): array |
|||
{ |
|||
return [ |
|||
'openid' => 'required|nonempty', |
|||
'unionid' => 'required|nonempty', |
|||
]; |
|||
} |
|||
|
|||
public function messages(): array |
|||
{ |
|||
return [ |
|||
'openid.*' => ':attribute必须', |
|||
'unionid.*' => ':attribute必须', |
|||
]; |
|||
} |
|||
|
|||
public function attributes(): array |
|||
{ |
|||
return [ |
|||
'openid' => '用户openid', |
|||
'unionid' => '用户unionid', |
|||
]; |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
<?php |
|||
|
|||
namespace App\Service; |
|||
|
|||
use App\Model\OrderMain; |
|||
use App\Model\Users; |
|||
|
|||
class UserService implements UserServiceInterface |
|||
{ |
|||
|
|||
/** |
|||
* 是否平台新用户 |
|||
* 在很多奖励的地方会需要用到这个查询 |
|||
* 判定条件: |
|||
* 没有在平台下过单(包括线上和线下) |
|||
* @param $user_id |
|||
* @return mixed|void |
|||
*/ |
|||
public function isStageNewUser($user_id): bool |
|||
{ |
|||
$exist = OrderMain::query() |
|||
->where(['user_id' => $user_id]) |
|||
->where(function ($query){ |
|||
$query->where('state', 'in', [4,5,10]) |
|||
->orWhere('dm_state', 'in', [2,3]); |
|||
}) |
|||
->exists(); |
|||
|
|||
return !$exist; |
|||
} |
|||
|
|||
/** |
|||
* 根据用户的openid更新unionid信息 |
|||
* 如果没有找到用户,则不做任何处理 |
|||
* @param $openid |
|||
* @param $unionid |
|||
* @return array |
|||
*/ |
|||
public function saveUserUnionid($openid,$unionid) |
|||
{ |
|||
$result = [ |
|||
'status' => false, |
|||
'msg' => '用户不存在或者已存在相同unionid' |
|||
]; |
|||
|
|||
// 查询用户是否存在
|
|||
$userinfo = Users::select('id','unionid')->where('openid',$openid)->first(); |
|||
if($userinfo && $userinfo->unionid != $unionid){ |
|||
$userinfo->unionid = $unionid; |
|||
if($res = $userinfo->save()){ |
|||
$result['status'] = true; |
|||
$result['msg'] = '更改用户unionid信息成功'; |
|||
$result['res'] = $res; |
|||
}else{ |
|||
$result['msg'] = '更改用户unionid信息失败'; |
|||
} |
|||
} |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Service; |
|||
|
|||
|
|||
interface UserServiceInterface |
|||
{ |
|||
/** |
|||
* 是否平台新用户 |
|||
* @param $user_id |
|||
* @return mixed |
|||
*/ |
|||
public function isStageNewUser($user_id): bool; |
|||
|
|||
public function saveUserUnionid($openid,$unionid); |
|||
|
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue