From 479631d244f5d26d0bdd27e3367ca36c90171ae4 Mon Sep 17 00:00:00 2001 From: qinzexin <“731344816@qq.com”> Date: Thu, 15 May 2025 16:25:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8E=E5=A4=9A=E6=A0=B7=E9=9D=92=E6=98=A5?= =?UTF-8?q?=E6=90=AD+=E8=B4=A6=E5=90=8E=E6=8E=88=E6=9D=83=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=89=93=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/school/classes/ClassesLib.php | 2 +- application/api/controller/Index.php | 2 + application/api/controller/User.php | 56 ++++++++++- application/common/model/User.php | 94 +++++++++++++++++++ .../model/school/classes/ClassesLib.php | 8 +- application/index/controller/Index.php | 2 + public/assets/js/backend/user/user.js | 2 +- 7 files changed, 159 insertions(+), 7 deletions(-) diff --git a/application/admin/controller/school/classes/ClassesLib.php b/application/admin/controller/school/classes/ClassesLib.php index 71c6601..5b6f523 100644 --- a/application/admin/controller/school/classes/ClassesLib.php +++ b/application/admin/controller/school/classes/ClassesLib.php @@ -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; diff --git a/application/api/controller/Index.php b/application/api/controller/Index.php index ff46ef6..1c141d4 100644 --- a/application/api/controller/Index.php +++ b/application/api/controller/Index.php @@ -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), diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 3511448..010e96e 100644 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -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信息) diff --git a/application/common/model/User.php b/application/common/model/User.php index a174cf6..68581c4 100644 --- a/application/common/model/User.php +++ b/application/common/model/User.php @@ -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; + } + } diff --git a/application/common/model/school/classes/ClassesLib.php b/application/common/model/school/classes/ClassesLib.php index b8ea257..2a2f5ec 100644 --- a/application/common/model/school/classes/ClassesLib.php +++ b/application/common/model/school/classes/ClassesLib.php @@ -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; diff --git a/application/index/controller/Index.php b/application/index/controller/Index.php index 452ff98..1fe08a1 100644 --- a/application/index/controller/Index.php +++ b/application/index/controller/Index.php @@ -13,6 +13,8 @@ class Index extends Frontend public function index() { + + //rtu return $this->view->fetch(); } diff --git a/public/assets/js/backend/user/user.js b/public/assets/js/backend/user/user.js index 7788a9d..6b7f6fa 100644 --- a/public/assets/js/backend/user/user.js +++ b/public/assets/js/backend/user/user.js @@ -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; }