IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    UNIAPP 打开 QQ 群聊窗口

    obaby发表于 2024-02-05 05:38:51
    love 0

    为了方便反馈问题以及沟通,在 app 的版本界面添加了一个 QQ 群,现在的问题是QQ 群号有了,但是要发消息得先复制群号,打开 QQ 加群,才能发送消息。

    网上搜索了一下 uniapp 打开 qq 聊天窗口的方法,网上的代码不少,但是基本都是抄来抄去,多数代码应该只在安卓手机上测试过,在苹果手机上可能没有测试,最后找到这么一篇算是比较有价值的:https://blog.csdn.net/m0_61447532/article/details/127958946

    针对 安卓和 ios 需要单独处理:

    let qq = '123456'
           if (plus.os.name == "Android") {
             var main = plus.android.runtimeMainActivity();
             var Intent = plus.android.importClass('android.content.Intent');
             var Uri = plus.android.importClass('android.net.Uri');
             var intent = new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + qq));
             main.startActivity(intent);
           }
           if (plus.os.name == "iOS") {
             plus.runtime.launchApplication({
               action: "mqq://im/chat?chat_type=wpa&uin=" + qq + "&version=1&src_type=web"
             }, function(e) {
               plus.nativeUI.confirm("检查到您未安装qq,请先到appstore搜索下载?", function(i) {
                 if (i.index == 0) {
                   iosAppstore("itunes.apple.com/cn/app/mqq/");
                 }
               });
             });
           }

    不过上面的代码是针对 qq 聊天的,如果要打开 qq 群聊天窗口需要将 qq 群的号码转换成密钥:

    登录:https://qun.qq.com/ 点击加群组件,选择 android:

    对应代码为:

    /****************
    *
    * 发起添加群流程。群号:闺蜜圈App(323089499) 的 key 为: 9x-uQPZ
    * 调用 joinQQGroup(9x-uQPZ) 即可发起手Q客户端申请加群 闺蜜圈App(323089499)
    *
    * @param key 由官网生成的key
    * @return 返回true表示呼起手Q成功,返回false表示呼起失败
    ******************/
    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26jump_from%3Dwebapi%26k%3D" + key));
       // 此Flag可根据具体产品需要自定义,如设置,则在加群界面按返回,返回手Q主界面,不设置,按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }

    对应代码为安卓java 原生,这里只需要知道 uri 是怎么拼接的就 ok 了,换成 uni 代码:

    if (plus.os.name == "Android") {
    
                        plus.runtime.openURL('mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26jump_from%3Dwebapi%26k%3D' +'9x-uQPZ')
                    }

    ios 代码:

    - (BOOL)joinGroup:(NSString *)groupUin key:(NSString *)key{
    NSString *urlStr = [NSString stringWithFormat:@"mqqapi://card/show_pslcard?src_type=internal&version=1&uin=%@&authSig=%@&card_type=group&source=external&jump_from=webapi", @"323089499",@"0P27iJ"];
    NSURL *url = [NSURL URLWithString:urlStr];
    if([[UIApplication sharedApplication] canOpenURL:url]){
    [[UIApplication sharedApplication] openURL:url];
    return YES;
    }
    else return NO;
    }

    用同样的方法把 uri 拼起来:

    if (plus.os.name == "iOS") {
                        plus.runtime.launchApplication({
                            // action: "mqq://im/chat?chat_type=wpa&uin=" + qq + "&version=1&src_type=web"
                            action: "mqqapi://card/show_pslcard?src_type=internal&version=1&uin=323089499&authSig=96utdUKhGUwrEV0Mi9PI8TCupddqg6Trri&card_type=group&source=external&jump_from=webapi"
                        }, function(e) {
                            plus.nativeUI.confirm("检查到您未安装qq,请先到appstore搜索下载?", function(i) {
                                if (i.index == 0) {
                                    iosAppstore("itunes.apple.com/cn/app/mqq/");
                                }
                            });
                        });

    然后就可以正常打开 qq 群聊窗口了:

    

    参考链接:

    https://www.dandelioncloud.cn/article/details/1478993088000643073

    https://blog.csdn.net/m0_61447532/article/details/127958946

    https://qun.qq.com/

    The post UNIAPP 打开 QQ 群聊窗口 first appeared on obaby@mars.



沪ICP备19023445号-2号
友情链接