...
|
...
|
@@ -13,11 +13,66 @@ use think\Validate; |
|
|
|
|
|
class PublicController extends RestBaseController
|
|
|
{
|
|
|
// 微信小程序用户登录 TODO 增加最后登录信息记录,如 ip
|
|
|
/**
|
|
|
* @title 获取sessionKey
|
|
|
* @description 小程序登录注册
|
|
|
* @author Tiger Yang
|
|
|
* @url /wxapp/public/getSessionKey
|
|
|
* @method POST
|
|
|
*
|
|
|
* @param name:code type:string require:1 other: desc:code
|
|
|
*
|
|
|
* @return session_key:session_key
|
|
|
*/
|
|
|
public function getSessionKey(){
|
|
|
$validate = new Validate([
|
|
|
'code' => 'require',
|
|
|
]);
|
|
|
|
|
|
$validate->message([
|
|
|
'code.require' => '缺少参数code!',
|
|
|
]);
|
|
|
|
|
|
$data = $this->request->param();
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
|
|
|
}
|
|
|
|
|
|
$code = $data['code'];
|
|
|
$appId = config('app_id');
|
|
|
$appSecret = config('app_secret');
|
|
|
|
|
|
$response = cmf_curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
|
|
|
|
|
|
$response = json_decode($response, true);
|
|
|
if (!empty($response['errcode'])) {
|
|
|
$this->error(['code'=>'41001','msg'=>'操作失败:'.$response['errcode']]);
|
|
|
}
|
|
|
$this->success('获取成功',$response);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @title 小程序登录注册
|
|
|
* @description 小程序登录注册
|
|
|
* @author Tiger Yang
|
|
|
* @url /wxapp/public/login
|
|
|
* @method POST
|
|
|
*
|
|
|
* @param name:openid type:string require:1 other: desc:openid
|
|
|
* @param name:session_key type:string require:1 other: desc:session_key
|
|
|
* @param name:encrypted_data type:string require:1 other: desc:encrypted_data
|
|
|
* @param name:iv type:string require:1 other: desc:iv
|
|
|
* @param name:raw_data type:string require:1 other: desc:raw_data
|
|
|
* @param name:signature type:string require:1 other: desc:signature
|
|
|
*
|
|
|
* @return token:登录唯一标识
|
|
|
* @return user_type:用户类型[2-用户,3-员工,4-技师]
|
|
|
*/
|
|
|
public function login()
|
|
|
{
|
|
|
$validate = new Validate([
|
|
|
'code' => 'require',
|
|
|
'openid' => 'require',
|
|
|
'session_key' => 'require',
|
|
|
'encrypted_data' => 'require',
|
|
|
'iv' => 'require',
|
|
|
'raw_data' => 'require',
|
...
|
...
|
@@ -25,7 +80,8 @@ class PublicController extends RestBaseController |
|
|
]);
|
|
|
|
|
|
$validate->message([
|
|
|
'code.require' => '缺少参数code!',
|
|
|
'openid.require' => '缺少参数openid!',
|
|
|
'session_key.require' => '缺少参数session_key!',
|
|
|
'encrypted_data.require' => '缺少参数encrypted_data!',
|
|
|
'iv.require' => '缺少参数iv!',
|
|
|
'raw_data.require' => '缺少参数raw_data!',
|
...
|
...
|
@@ -34,29 +90,19 @@ class PublicController extends RestBaseController |
|
|
|
|
|
$data = $this->request->param();
|
|
|
if (!$validate->check($data)) {
|
|
|
$this->error($validate->getError());
|
|
|
$this->error(['code'=>'40003','msg'=>$validate->getError()]);
|
|
|
}
|
|
|
|
|
|
//TODO 真实逻辑实现
|
|
|
$code = $data['code'];
|
|
|
$appId = '你的 appid';
|
|
|
$appSecret = '你的 secket';
|
|
|
|
|
|
$response = cmf_curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
|
|
|
|
|
|
$response = json_decode($response, true);
|
|
|
if (!empty($response['errcode'])) {
|
|
|
$this->error('操作失败!');
|
|
|
}
|
|
|
$appId = config('app_id');
|
|
|
|
|
|
$openid = $response['openid'];
|
|
|
$sessionKey = $response['session_key'];
|
|
|
$openid = $data['openid'];
|
|
|
$sessionKey = $data['session_key'];
|
|
|
|
|
|
$pc = new WXBizDataCrypt($appId, $sessionKey);
|
|
|
$errCode = $pc->decryptData($data['encrypted_data'], $data['iv'], $wxUserData);
|
|
|
|
|
|
if ($errCode != 0) {
|
|
|
$this->error('操作失败!');
|
|
|
$this->error(['code'=>'41002','msg'=>'检验数据失败!'],['errCode'=>$errCode,'param'=>$data]);
|
|
|
}
|
|
|
|
|
|
$findThirdPartyUser = Db::name("third_party_user")
|
...
|
...
|
@@ -71,7 +117,19 @@ class PublicController extends RestBaseController |
|
|
unset($wxUserData['watermark']);
|
|
|
|
|
|
if ($findThirdPartyUser) {
|
|
|
$user_type=Db::name('user')->where(['id'=>$findThirdPartyUser['user_id']])->value('user_type');
|
|
|
|
|
|
switch ($user_type){
|
|
|
case 3:
|
|
|
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'staff');
|
|
|
break;
|
|
|
case 4:
|
|
|
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'technician');
|
|
|
break;
|
|
|
default :
|
|
|
$token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'wxapp');
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
$userData = [
|
|
|
'last_login_ip' => $ip,
|
...
|
...
|
@@ -88,10 +146,10 @@ class PublicController extends RestBaseController |
|
|
->where('openid', $openid)
|
|
|
->where('app_id', $appId)
|
|
|
->update($userData);
|
|
|
|
|
|
$this->success("登录成功!", ['token' => $token,'user_type'=>$user_type]);
|
|
|
} else {
|
|
|
|
|
|
//TODO 使用事务做用户注册
|
|
|
Db::startTrans();
|
|
|
$userId = Db::name("user")->insertGetId([
|
|
|
'create_time' => $currentTime,
|
|
|
'user_status' => 1,
|
...
|
...
|
@@ -100,10 +158,10 @@ class PublicController extends RestBaseController |
|
|
'user_nickname' => $wxUserData['nickName'],
|
|
|
'avatar' => $wxUserData['avatarUrl'],
|
|
|
'last_login_ip' => $ip,
|
|
|
'last_login_time' => $currentTime,
|
|
|
'last_login_time' => $currentTime
|
|
|
]);
|
|
|
|
|
|
Db::name("third_party_user")->insert([
|
|
|
$row=Db::name("third_party_user")->insert([
|
|
|
'openid' => $openid,
|
|
|
'user_id' => $userId,
|
|
|
'third_party' => 'wxapp',
|
...
|
...
|
@@ -117,12 +175,16 @@ class PublicController extends RestBaseController |
|
|
'more' => json_encode($wxUserData)
|
|
|
]);
|
|
|
|
|
|
if($userId && $row){
|
|
|
Db::commit();
|
|
|
$token = cmf_generate_user_token($userId, 'wxapp');
|
|
|
|
|
|
$this->success("登录成功!", ['token' => $token,'user_type'=>2]);
|
|
|
}else{
|
|
|
Db::rollback();
|
|
|
$this->error(['code'=>'40004','msg'=>'登录失败']);
|
|
|
}
|
|
|
|
|
|
$this->success("登录成功!", ['token' => $token]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
...
|
...
|
|