与多样青春搭+账后授权登录打通

This commit is contained in:
qinzexin 2025-05-15 16:25:33 +08:00
parent 6e879728ff
commit 479631d244
7 changed files with 159 additions and 7 deletions

View File

@ -43,7 +43,7 @@ class ClassesLib extends Backend
protected $model = null;
//不用审核允许修改的字段
protected $no_auth_fields = ["title","user_id","teacher_id","classes_type","classes_cate_ids","classes_label_ids","self_label_tag",'headimage','images','notice','content',"virtual_num","virtual_collect","underline_price"];
protected $no_auth_fields = ["weigh","title","user_id","teacher_id","classes_type","classes_cate_ids","classes_label_ids","self_label_tag",'headimage','images','notice','content',"virtual_num","virtual_collect","underline_price"];
//更新数据是否需要触发审核开关
protected $need_auth = false;

View File

@ -98,6 +98,8 @@ class Index extends Api
"site_city"=> Virtual::getNowCity(),
"site_timezone"=>config('site.timezone'),
"wx_miniapp_version"=>config("site.wx_miniapp_version"),
];
$customer_service = [
"image"=>cdnurl(config('site.customer_service_image'),true),

View File

@ -21,7 +21,7 @@ use app\admin\library\Wechat;
*/
class User extends Api
{
protected $noNeedLogin = ["registerLogin",'getOpenid','decodeData','login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
protected $noNeedLogin = ["activityMiniLogin","registerLogin",'getOpenid','decodeData','login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
protected $noNeedRight = '*';
protected $miniConfig;
@ -37,6 +37,60 @@ class User extends Api
/**
* 基于多样青春搭+小程序发起的自动登录
*
* @ApiMethod (POST)
* @ApiParams (name="token", type="string", required=false, description="非加密token加密则忽略")
* @ApiParams (name="time", type="string", required=false, description="非加密时间戳,加密则忽略")
* @ApiParams (name="encryption_data", type="string", required=false, description="加密则必传参数")
*/
public function activityMiniLogin()
{
$token = $this->request->post('token');
$time = $this->request->post('time');
$encryption_data = $this->request->post('encryption_data');
if(!$token && !$time && !$encryption_data) $this->error("确缺少必备传参!");
if(!$encryption_data){
if(!$token || !$time) $this->error("确缺少必备传参!");
}else{
//解密获取 token 和 time
//获取私钥
$activity_private_key = config("site.activity_private_key");
// 私钥解密
$decrypted = '';
if (!openssl_private_decrypt(base64_decode($encryption_data), $decrypted, $activity_private_key)) {
$this->error("私钥解密失败: " . openssl_error_string());
}
//解json
$decrypted = json_decode($decrypted, true);
$token = $decrypted['token'];
$time = $decrypted['time'];
//时间超2分钟则失效
if(time() - $time > 120){
$this->error("登录授权超时!");
}
}
$user = \app\common\model\User::loginByActivityToken($token,true);
// var_dump($user);
if ($user->status != 'normal') {
$this->error(__('Account is locked'));
}
//如果已经有账号则直接登录
$ret = $this->auth->direct($user->id);
if ($ret) {
$data = ['userinfo' => $this->auth->getUserinfo()];
$this->success(__('Logged in successful'), $data);
} else {
$this->error($this->auth->getError());
}
}
/**
* @ApiTitle(获取小程序openid信息)

View File

@ -407,6 +407,100 @@ class User extends BaseModel
return (new self)->allList($page, $limit,array_merge($where_params,$params));
}
public static function getSign(){
$timestamp = time();
$data = [
"timestamp"=>$timestamp,
];
return self::encryptedData($data);
}
public static function encryptedData($data){
//私钥
$activity_private_key = config("site.activity_private_key");
//转成json保留格式
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
// 私钥加密(数字签名)
$signed = '';
if (!openssl_private_encrypt($data, $signed, $activity_private_key)) {
throw new \Exception("私钥加密失败: " . openssl_error_string());
}
return base64_encode($signed);
}
public static function getUserInfoByActivityToken($token){
//用token换取用户信息
//获取自签名
$sign = self::getSign();
//加密参数数据
$encryption_data = self::encryptedData(["token"=>$token]);
$key = "hschool";
$activity_da_domain = config("site.activity_da_domain");
$curl_url = "{$activity_da_domain}/api/openapi.user/index";
$post_data = [
"sign"=>$sign,
"encryption_data"=>$encryption_data,
"key"=>$key,
];
//执行curl的post请求php原生写法无封装 content_type走json
//content_type走json
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result,true);
if($result['code']==1){
$data = $result['data'];
$data['token'] = $data;
return $data;
}
return [];
}
/** 根据活动微信小程序的的登录token 登录或注册本小程序用户
* @param $token
* @return void
*/
public static function loginByActivityToken($token,$trans=false){
//用token换取用户信息
$user_info = self::getUserInfoByActivityToken($token);
if(!$user_info) throw new \Exception("登录失败!");
if($trans){
self::beginTrans();
}
$res = true;
try{
//凭此用户信息注册或登录本小程序用户
$user = self::where("mobile",$user_info["mobile"])->find();
//检测更新教练下单学员账号创建状态 2022/8/27 new
if(!$user)$user = (new self)->addUserByMobile($user_info["mobile"],$user_info["nickname"]);
// $user['nickname'] = $people_name;
// $user->save();
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
}
return $user;
}
}

View File

@ -962,7 +962,7 @@ $user_unpaid_order = $user_paid_order =null;
//所有课时加起来
$classes_lib->limit_num = ClassesSpec::where("classes_lib_id",$classes_lib_id)->sum( "limit_num");
$classes_lib->limit_num = ClassesSpec::where("classes_lib_id",$classes_lib_id)->where("status","=","1")->sum( "limit_num");
//更新虚拟用户数据
//得到课程所有虚拟参与者数量
$virtual_people = VirtualUser::where("jointype",'1')->where("classes_lib_id",$classes_lib_id)->count();
@ -976,8 +976,8 @@ $user_unpaid_order = $user_paid_order =null;
//更新课程信息开始和结束时间信息
//课程开始和结束时间等于所有课时的最早开始和最晚结束时间
$classes_lib->start_time = ClassesSpec::where("classes_lib_id",$classes_lib_id)->min("start_time");
$classes_lib->end_time = ClassesSpec::where("classes_lib_id",$classes_lib_id)->max("end_time");
$classes_lib->start_time = ClassesSpec::where("classes_lib_id",$classes_lib_id)->where("status","=","1")->min("start_time");
$classes_lib->end_time = ClassesSpec::where("classes_lib_id",$classes_lib_id)->where("status","=","1")->max("end_time");
//设置课程收藏
$classes_lib->collect = Collect::where("classes_lib_id",$classes_lib_id)->count();
@ -1126,7 +1126,7 @@ $user_unpaid_order = $user_paid_order =null;
//不用审核允许修改的字段
public $no_auth_fields = ["user_id","teacher_id","classes_type","classes_cate_ids","classes_label_ids","self_label_tag",'headimage','images','notice','content',"virtual_num","virtual_collect","underline_price"];
public $no_auth_fields = ["title","weigh","user_id","teacher_id","classes_type","classes_cate_ids","classes_label_ids","self_label_tag",'headimage','images','notice','content',"virtual_num","virtual_collect","underline_price"];
public $need_auth = false;

View File

@ -13,6 +13,8 @@ class Index extends Frontend
public function index()
{
//rtu
return $this->view->fetch();
}

View File

@ -306,7 +306,7 @@ define(['jquery', 'bootstrap', 'backend', 'csmtable', 'form'], function ($, unde
}
var activity_order_url = function (row,dom) {
return 'school/classes/activity/order/order/index?user_id='+row.shop_id;
return 'school/classes/activity/order/order/index?user_id='+row.id;
}