useRoom.ts 5.6 KB

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