Zory 19 hodín pred
rodič
commit
3fcc397e9c

+ 20 - 6
app/controller/api/Auth.php

@@ -36,13 +36,23 @@ class Auth extends Base
     {
         try {
             $param = $this->_valid([
-                "login.require"     => trans("empty.require"),
-                "code.require"      => trans("empty.require"),
+                "login.require"     => '获取登录参数失败',
+                "code.default"      => '',
+                "encryptedData.default"      => '',
+                "iv.default"        => '',
             ],$request->method());
             if (!is_array($param)) return error($param);
             $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['login']);
             if (empty($sessionKey)) return error("授权登录失败");
-            $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
+            if (!empty($param['code'])) {
+                $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
+                echo  "sdasd\n";
+                print_r($mobileStr);
+            } else {
+                $data = $this->decrypt($param['encryptedData'],$sessionKey['session_key'],$param['iv']);
+                $mobileStr = $data['purePhoneNumber'];
+            }
+            print_r($mobileStr);
             if (empty($mobileStr)) return error("授权信息获取失败");
             return $this->encode("success",['mobile' => $mobileStr,'openid' => $sessionKey['openid']]);
         } catch (\Throwable $throwable) {
@@ -69,7 +79,11 @@ class Auth extends Base
             $mobile = [];
             if (!empty($param['mobile'])) {
                 if (!isset($mobileData['openid'])) return error("授权失败");
-                $mobile = $this->decrypt2code(sConf('wechat.min_private_key'), $mobileData['mobile']);
+                if (isValidMobile($mobileData['mobile'])) {
+                    $mobile = ["purePhoneNumber" => $mobileData['mobile']];
+                } else {
+                    $mobile = $this->decrypt2code(sConf('wechat.min_private_key'), $mobileData['mobile']);
+                }
             }
             if (empty($mobile)) return error("授权登录失败");
             $userInfo = [
@@ -175,8 +189,8 @@ class Auth extends Base
         $key = base64_decode($session_key);
         $iv_decoded = base64_decode($iv);
         // 使用 AES-256-CBC 模式解密
-        $decrypted = openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
+        $decrypted = openssl_decrypt($data, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
 
-        return $decrypted;
+        return json_decode($decrypted,true);
     }
 }

+ 19 - 2
app/controller/api/Service.php

@@ -167,7 +167,23 @@ class Service extends Base
                 "poi_id"    => $param['groupId'],
                 "service_id" => $param['sendId'],
             ]);
-            (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => $param['sendId']])->save(['last_at' => getDateFull()]);
+            $chatStore = (new SaasChatStore)->where(['openid' => $request->user['openid'],"service_id" => $param['sendId']])->with(['user' => function($query){
+                $query->field("openid,nickname,avatar,mobile,create_at");
+            }])->append(['last','avatar'])->withAttr(['last' => function($data,$resp) use($param){
+                $last = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['sendId'],"openid" => $resp['openid']])->order("create_at desc")->field("content,create_at,type")->findOrEmpty();
+                if ($last->isEmpty()) {
+                    return ['type' => "text","content"=>"无",'num' => 0,'time' => time(),"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
+                }
+                $total = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['sendId'],"openid" => $resp['openid'],'is_read' => 0])->count();
+                return ["content"=> str_cut_ellipsis($last['content']),'num' => ($total),"create_at"=> formatTime($last['create_at']),"time"=> strtotime($last['create_at']),'type' => $last['type']];
+            },'avatar' => function(){
+                return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
+            }])->findOrEmpty();
+            if (!$chatStore->isEmpty())
+            {
+                $chatStore->save(['last_at' => getDateFull()]);
+            }
+            $chatStore['last_at'] = getDateFull();
             $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
             $api->trigger("service-{$param['sendId']}","message",[
                 "type"  =>  $param['type'],
@@ -184,7 +200,8 @@ class Service extends Base
                 "user"  => [
                     "nickname"  => $request->user['nickname'],
                     "avatar"    => $request->user['avatar']
-                ]
+                ],
+                "item"  => $chatStore->toArray()
             ]);
             return successTrans("success.data");
         } catch (\Throwable $throwable) {

+ 5 - 5
app/controller/service/Service.php

@@ -48,16 +48,15 @@ class Service extends Base
             $param['field'] = "last_at";
             $data = $this->service->setModel()->getList($param,['user' => function($query){
                 $query->field("openid,nickname,avatar,mobile,create_at");
-            }],true,['last','avatar','last_time'],['last' => function($data,$resp) use($param){
+            }],true,['last','avatar'],['last' => function($data,$resp) use($param){
                 $last = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['service_id'],"openid" => $resp['openid']])->order("create_at desc")->field("content,create_at,type")->findOrEmpty();
                 if ($last->isEmpty()) {
                     return ['type' => "text","content"=>"无",'num' => 0,'time' => time(),"create_at"=> formatTime(date("Y-m-d H:i:s",time()))];
                 }
-                return ["content"=> str_cut_ellipsis($last['content']),'num' => 0,"create_at"=> formatTime($last['create_at']),"time"=> strtotime($last['create_at']),'type' => $last['type']];
+                $total = (new SaasChatMsg)->where(["poi_id" => $resp['poi_id'],"service_id" => $param['service_id'],"openid" => $resp['openid'],'is_read' => 0])->count();
+                return ["content"=> str_cut_ellipsis($last['content']),'num' => $total,"create_at"=> formatTime($last['create_at']),"time"=> strtotime($last['create_at']),'type' => $last['type']];
             },'avatar' => function(){
                 return "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/20260511/23627b2cb8f73a8a90b95c31d726e6ecad0cbb9a.png";
-            },'last_time' => function($data,$resp){
-                return $resp['last_at']?date('Y-m-d',strtotime($resp['last_at'])):'-';
             }]);
             return successTrans(100010,pageFormat($data),200);
         } catch (\Throwable $th) {
@@ -138,6 +137,7 @@ class Service extends Base
                 "poi_id"    => $param['groupId'],
                 "service_id" => $request->user['id'],
             ]);
+            (new SaasChatMsg)->where(['openid' => $param['openid'],'service_id' => $request->user['id']])->save(['is_read' => 1]);
             $api = new Api('http://127.0.0.1:3232', config('plugin.webman.push.app.app_key'),config('plugin.webman.push.app.app_secret'));
             $api->trigger("user-{$param['openid']}","message",[
                 "type"  =>  $param['type'],
@@ -149,7 +149,7 @@ class Service extends Base
                 "poi_id"    => $param['groupId'],
                 "openid"  => $param['openid'],
                 "service_id"  => $request->user['id'],
-                "create_at"  => formatTime(time()),
+                "create_at"  => formatTime(time())
             ]);
             return successTrans("success.data",['time' => formatTime(time())]);
         } catch (\Throwable $th) {

+ 17 - 0
app/functions.php

@@ -41,6 +41,23 @@ if (!function_exists("str2date"))
     }
 }
 
+if (!function_exists("isValidMobile"))
+{
+    /**
+     * 验证是否为有效的中国大陆手机号码
+     * @param string $mobile
+     * @return bool
+     */
+    function isValidMobile(string $mobile): bool
+    {
+        // 去除空格、换行等
+        $mobile = trim($mobile);
+        // 正则:1 开头,第二位 3-9,后面 9 位数字,总共 11 位
+        $pattern = '/^1[3-9]\d{9}$/';
+        return preg_match($pattern, $mobile) === 1;
+    }
+}
+
 if(!function_exists("hide_mobile")){
 
     /**