useRoom.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import browser from '/@/utils/browser';
  2. import axios, { AxiosResponse } from 'axios';
  3. import { ref, computed, unref } from 'vue';
  4. import { useRtcStore } from '../store/modules/rtc';
  5. // import { useRtcSdk } from './useTRTC';
  6. const roomParam = browser.getURLParam('roomId');
  7. interface roomDataType extends SceneItemType {
  8. sceneData: SceneItemType[];
  9. }
  10. export interface SceneItemType {
  11. id: number;
  12. roomId: number;
  13. buildObjStatus: number;
  14. createTime: string;
  15. name: string;
  16. num: string;
  17. payStatus: number;
  18. sceneName: string;
  19. snCode: string;
  20. status: number;
  21. thumb: string;
  22. title: string;
  23. viewCount: number;
  24. isLaser: boolean;
  25. type: number;
  26. phone: Nullable<string>;
  27. bind: boolean;
  28. takeLookLock: number;
  29. maxMan: number;
  30. roomInfo: string;
  31. useEndTime: string;
  32. useStartTime: string;
  33. video?: string[];
  34. image?: string[];
  35. head?: string;
  36. }
  37. export const room = ref<Nullable<roomDataType>>(null);
  38. export const roomId = roomParam;
  39. export const sceneList = computed<SceneItemType[]>(() => unref(room)?.sceneData || []);
  40. export const isLeader = browser.getURLParam('role') === 'leader';
  41. export const currentNum = browser.getURLParam('m');
  42. export const firstNum = computed(() => unref(sceneList)[0].num);
  43. export const currentSceneIndex = computed(() =>
  44. unref(sceneList).findIndex((i) => i.num === browser.getURLParam('m')),
  45. );
  46. interface roomParamsType {
  47. name: string;
  48. role: string;
  49. vruserId: string;
  50. isTour: number;
  51. roomId: string;
  52. avatar: string;
  53. }
  54. interface getSignType {
  55. expire: number;
  56. sdkAppId: number;
  57. sign: string;
  58. operatorType?: number;
  59. }
  60. function createNewURL(params: roomParamsType): string {
  61. let tempUrl = window.location.href;
  62. // ['mode', 'name', 'role', 'vruserId']
  63. Object.keys(params).forEach((item) => {
  64. tempUrl = browser.replaceQueryString(tempUrl, item, params[item]);
  65. console.log('tempUrl', tempUrl);
  66. });
  67. return tempUrl;
  68. }
  69. function createNewURLEntry(params: roomParamsType) {
  70. const tempUrl = createNewURL(params);
  71. history.replaceState(null, '', tempUrl);
  72. }
  73. export function useRoom() {
  74. return {
  75. room,
  76. sceneList,
  77. currentScene,
  78. changeScene,
  79. initialRoom,
  80. enterRoom,
  81. leaveRoom,
  82. createNewURLEntry,
  83. createNewURL,
  84. getSign,
  85. validPassRoom,
  86. shareRoom,
  87. currentSceneIndex,
  88. currentNum,
  89. firstNum,
  90. };
  91. }
  92. export const currentScene = computed(() => {
  93. const num = browser.getURLParam('m');
  94. return sceneList.value.find((scene) => scene.num === num);
  95. });
  96. export const changeScene = (scene: SceneItemType) => {
  97. const rtcStore = useRtcStore();
  98. if (currentScene.value?.num !== scene.num && scene?.num.length) {
  99. console.log(scene, currentScene.value);
  100. const params = new URLSearchParams(location.search);
  101. params.set('m', scene.num);
  102. const url = new URL(location.href);
  103. url.search = `?` + params.toString();
  104. // if (browser.isMobile()) {
  105. // if (browser.detectIOS()) {
  106. // location.replace(url);
  107. // } else {
  108. // console.warn('安卓跳转减少内存');
  109. // history.replaceState(null, document.title, url);
  110. // history.go(0);
  111. // }
  112. // } else {
  113. // location.replace(url);
  114. // }
  115. location.replace(url);
  116. rtcStore.clearMemberList();
  117. } else {
  118. // location.reload();
  119. }
  120. };
  121. const shopAxios = axios.create({
  122. // baseURL: !import.meta.env.DEV ? import.meta.env.VITE_APP_APIS_URL : '',
  123. baseURL: import.meta.env.VITE_APP_APIS_URL,
  124. });
  125. const wxToken = browser.getURLParam('wxToken') || '';
  126. if (wxToken) {
  127. shopAxios.defaults.headers['wxToken'] = wxToken;
  128. }
  129. export const initialRoom = async () => {
  130. shopAxios.defaults.headers.platform = browser.getURLParam('platform') || '';
  131. const res = await shopAxios.get('/takelook/roomInfo', { params: { roomId } });
  132. room.value = res.data.data;
  133. };
  134. export const enterRoom = async () => {
  135. // if (!isLeader) return;
  136. const rtcStore = useRtcStore();
  137. const userID = rtcStore.userId || browser.getURLParam('vruserId');
  138. const role = rtcStore.role || browser.getURLParam('role');
  139. shopAxios.defaults.headers.platform = browser.getURLParam('platform') || '';
  140. // overRide测试人数
  141. if (Number(import.meta.env.VITE_ROOM_MEMBER_DEBUG) === 1) {
  142. if (room.value) {
  143. room.value.maxMan = Number(import.meta.env.VITE_ROOM_MEMBER);
  144. console.warn('测试设置人数: ' + Number(import.meta.env.VITE_ROOM_MEMBER));
  145. }
  146. }
  147. await shopAxios.get('/takelook/inOrOutRoom', {
  148. params: {
  149. type: 0,
  150. role: role,
  151. roomId,
  152. userId: userID,
  153. },
  154. });
  155. };
  156. export const leaveRoom = async () => {
  157. // if (!isLeader) return;
  158. const rtcStore = useRtcStore();
  159. const userID = rtcStore.userId || browser.getURLParam('vruserId');
  160. const role = rtcStore.role || browser.getURLParam('role');
  161. shopAxios.defaults.headers.platform = browser.getURLParam('platform') || '';
  162. await shopAxios.get('/takelook/inOrOutRoom', {
  163. params: {
  164. type: 1,
  165. role: role,
  166. roomId,
  167. userId: userID,
  168. },
  169. });
  170. };
  171. export const shareRoom = async (roomId: string, userId: string) => {
  172. // if (!isLeader) return;
  173. shopAxios.defaults.headers.platform = browser.getURLParam('platform') || '';
  174. return shopAxios.get('/takelook/shareRoom', {
  175. params: {
  176. userId,
  177. roomId,
  178. },
  179. });
  180. };
  181. // if (roomId) {
  182. // shopAxios.get('/takelook/roomAddView', { params: { roomId } });
  183. // }
  184. export const GET_COMMON_SIG = '/takelook/rtcMedia/getToken';
  185. export const getSign = async (
  186. userId: string,
  187. channelName: string,
  188. roleId: string,
  189. platform?: string,
  190. ): Promise<getSignType> => {
  191. let role = 1;
  192. if (roleId === 'leader') {
  193. role = 1;
  194. }
  195. const res = await shopAxios.get<AxiosResponse>(GET_COMMON_SIG, {
  196. params: {
  197. userId,
  198. roleId: role,
  199. channelName,
  200. platform,
  201. },
  202. });
  203. return res.data.data;
  204. };
  205. export const validPassRoom = async (roomId: string, password: string) => {
  206. shopAxios.defaults.headers.platform = browser.getURLParam('platform') || '';
  207. const res = await shopAxios.post('/takelook/checkRoomVisitPassword', {
  208. roomId: roomId,
  209. password: password,
  210. // wxToken: wxToken,
  211. });
  212. return res.data?.data || false;
  213. };
  214. //h5端模拟
  215. export const webLogin = async (nickName: string) => {
  216. shopAxios.defaults.headers.platform = browser.getURLParam('platform') || '';
  217. const res = await shopAxios.post('/takelook/webApi/webLogin', {
  218. nickName: nickName,
  219. });
  220. return res.data?.data || false;
  221. };