【环信uniapp uikit】手把手教你uniapp uikit运行到鸿蒙

饭宝  论坛元老 | 2025-1-21 16:52:50 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1010|帖子 1010|积分 3030

写在前面:

好消息好消息,环信uniapp出uikit啦~
更好的消息,环信uniapp sdk也支持鸿蒙系统啦!!!!那么我们一起来看看uniapp uikit如何运行到鸿蒙系统~~let’s go
   uniapp uikit以及支持鸿蒙系统的uniapp sdk版本都是4.11.0
  
准备工作

1. HBuilderX 4.36

2. DevEco-Studio 5.0.5.310

3. sass:sass-loader 10.1.1 及之前版本

4. node:12.13.0 - 17.0.0,推荐 LTS 版本 16.17.0

5. npm:版本需与 Node.js 版本匹配

6. 已经在环信即时通讯云控制台创建了有效的环信即时通讯 IM 开发者账号,并获取了 App Key

7. 相识uniapp创建运行鸿蒙系统

8. 相识uniapp UIkit各功能以及api调用


开始集成:

第一步:创建一个uniapp+vue3项目进度10%


第二步:安装依赖进度15%

  1. npm init -y
  2. npm i easemob-websdk@4.11.0 pinyin-pro@3.26.0 mobx@6.13.4 --save
复制代码
第三步:下载uniapp uikit源码进度20%

  1. git clone https://github.com/easemob/easemob-uikit-uniapp.git
复制代码
第四步:拷贝uikit组件进度50%

  1.   mkdir -p ./ChatUIKit
  2.   # macOS
  3.   mv ${组件项目路径}/ChatUIKit/* ./ChatUIKit
  4.   
  5.   # windows
  6.   move ${组件项目路径}/ChatUIKit/* .\ChatUIKit
复制代码

第五步:替换pages/index/index.vue文件进度70%

  1. <template>
  2.   <view class="index">
  3.     <view class="login-form">
  4.       <input
  5.         class="input-field"
  6.         type="text"
  7.         v-model="userId"
  8.         placeholder="请输入用户ID"
  9.       />
  10.       <input
  11.         class="input-field"
  12.         type="password"
  13.         v-model="password"
  14.         placeholder="请输入密码"
  15.       />
  16.       <view class="login-button" @click="handleLogin">登录</view>
  17.     </view>
  18.   </view>
  19. </template>
  20. <script lang="ts" setup>
  21. import { ref } from "vue";
  22. const userId = ref("");
  23. const password = ref("");
  24. const id = ""; // 用户 ID 或者群组 ID
  25. const type = ""; // 会话类型:单聊 singleChat,群聊为 groupChat
  26. const handleLogin = () => {
  27.   if (!userId.value || !password.value) {
  28.     uni.showToast({
  29.       title: "请输入用户ID和密码",
  30.       icon: "none"
  31.     });
  32.     return;
  33.   }
  34.   // 登录
  35.   uni.$UIKit.chatStore
  36.     .login({
  37.       user: userId.value,
  38.       pwd: password.value
  39.     })
  40.     .then(() => {
  41.       // 登录成功跳转聊天页面
  42.       uni.navigateTo({
  43.         url: `/ChatUIKit/modules/Chat/index?id=${id}&type=${type}`
  44.       });
  45.     })
  46.     .catch((error) => {
  47.       console.error("登录失败", error);
  48.     });
  49. };
  50. // 退出登录
  51. const logout = () => {
  52.     uni.$UIKit.chatStore.logout();
  53. };
  54. </script>
  55. <style lang="scss" scoped>
  56. .index {
  57.   height: 100%;
  58.   display: flex;
  59.   justify-content: center;
  60.   align-items: center;
  61.   background-color: #f5f5f5;
  62.   .login-form {
  63.     width: 80%;
  64.     max-width: 300px;
  65.     padding: 20px;
  66.     background-color: #fff;
  67.     border-radius: 8px;
  68.     box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  69.     .input-field {
  70.       width: 100%;
  71.       height: 40px;
  72.       margin-bottom: 15px;
  73.       padding: 0 15px;
  74.       border: 1px solid #dcdfe6;
  75.       border-radius: 4px;
  76.       font-size: 14px;
  77.       box-sizing: border-box;
  78.       &:focus {
  79.         border-color: #006eff;
  80.       }
  81.     }
  82.     .login-button {
  83.       width: 100%;
  84.       padding: 12px 0;
  85.       color: #fff;
  86.       background-color: #006eff;
  87.       font-size: 16px;
  88.       border-radius: 4px;
  89.       text-align: center;
  90.       &:active {
  91.         opacity: 0.8;
  92.       }
  93.     }
  94.   }
  95. }
  96. </style>
复制代码
第六步:替换app.vue文件进度80%

  1. <script lang="ts">
  2. import { ChatUIKit } from "./ChatUIKit";
  3. import websdk from "easemob-websdk/uniApp/Easemob-chat";
  4. import { EasemobChatStatic } from "easemob-websdk/Easemob-chat";
  5. // 创建 IM 实例
  6. const chat = new (websdk as unknown as EasemobChatStatic).connection({
  7.   appKey: '', // 应用的 App Key
  8.   isHttpDNS: false,
  9.   url: '', // 环信 websocket URL
  10.   apiUrl: '', // 环信 Restful API URL
  11.   delivery: true // 是否开启消息已送达回执
  12. });
  13. // 初始化 ChatUIKit
  14. ChatUIKit.init({
  15.   chat, // 传入 IM 实例
  16.   config: {
  17.     theme: {
  18.                         // 头像形状:圆形(circle)和方形(square)
  19.       avatarShape: "square"
  20.     },
  21.     isDebug: true // 是否开启调试模式
  22.   }
  23. });
  24. uni.$UIKit = ChatUIKit;
  25. export default {
  26.   onLaunch: function () {
  27.     console.log("App Launch");
  28.   },
  29.   onShow: function () {
  30.     console.log("App Show");
  31.     // 在 onShow 中调用 ChatUIKit.onShow() 方法,主动监测 IM 连接状态
  32.     ChatUIKit.onShow();
  33.   },
  34.   onHide: function () {
  35.     console.log("App Hide");
  36.   }
  37. };
  38. </script>
  39. <style>
  40. /* 通用样式 */
  41. html,body,page {
  42.   height: 100%;
  43.   width: 100%;
  44. }
  45. </style>
复制代码
第七步:在pages.json设置路由进度90%

  1. {
  2.   "pages": [
  3.         {
  4.                         "path": "pages/index/index",
  5.                         "style": {
  6.                                 "navigationBarTitleText": "uni-app"
  7.                         }
  8.                 },
  9.                 {
  10.                         "path": "ChatUIKit/modules/Chat/index",
  11.                         "style": {
  12.                                 "navigationStyle": "custom",
  13.                                 // #ifdef MP-WEIXIN
  14.                                 "disableScroll": true,
  15.                                 // #endif
  16.                                 "app-plus": {
  17.                                         "bounce": "none",
  18.                                         "softinputNavBar": "none"
  19.                                 }
  20.                         }
  21.                 },
  22.                 {
  23.                         "path": "ChatUIKit/modules/VideoPreview/index",
  24.                         "style": {
  25.                                 "navigationBarTitleText": "Video Preview",
  26.                                 "app-plus": {
  27.                                         "bounce": "none"
  28.                                 }
  29.                         }
  30.                 }
  31.   ]
  32. }
复制代码
第八步:运行到chrome浏览器看下结果进度90%



第九步:运行到鸿蒙并发送一条消息进度100%




遇到的标题:

标题1:

具体报错信息如下
hvigor ERROR: Invalid product for target ‘default’.
Detail: Check the target applyToProducts field for ‘default’: [ ‘default’, ‘release’ ].
at /Users/admin/Desktop/ouyeel_worksheet/unpackage/debug/app-harmony-2f573459/build-profile.json5
办理方案:

   在harmony-configs/build-profile.json5文件,复制default设置,将default改为relese,参考
  

标题2:

登录报错

办理方案:

   在/harmony-configs/entry/src/main/module.json5文件添加以下代码
  1.     "requestPermissions": [
  2.               {"name": "ohos.permission.GET_NETWORK_INFO"},
  3.               {
  4.                 "name": "ohos.permission.INTERNET"},
  5.             ],
复制代码

标题3:

HBuilderX无限重连

办理方案:

   看看sdk 是不是最新版,无限重连的标题已经在 4.12.0版本的sdk修复~
  
总结:

初步运行到鸿蒙的话标题会比较多,大家可以善用百度大法办理掉它!!!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

饭宝

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表