socket.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. const {
  2. io
  3. } = require('./socket.io-v4.msgpack.js');
  4. var user = require('./services/user.js');
  5. const api = require('/config/api.js');
  6. const util = require('/utils/util.js');
  7. const UNLOGIN = 'NO_LOGIN'
  8. const btoa = require('./utils/btoa');
  9. const manyCount = 50
  10. import remote from './config.js'
  11. var app = getApp();
  12. var isIos = false
  13. wx.getSystemInfo({
  14. success: function (res) {
  15. isIos = res.platform == "ios"
  16. }
  17. })
  18. const debounce = (fn, wait) => {
  19. let callback = fn;
  20. let timerId = null;
  21. function debounced() {
  22. let context = this;
  23. let args = arguments;
  24. clearTimeout(timerId);
  25. timerId = setTimeout(function () {
  26. callback.apply(context, args);
  27. }, wait);
  28. }
  29. return debounced;
  30. }
  31. let urlToJson = (url = window.location.href) => { // 箭头函数默认传值为当前页面url
  32. let obj = {},
  33. index = url.indexOf('?'), // 看url有没有参数
  34. params = url.substr(index + 1); // 截取url参数部分 id = 1 & type = 2
  35. if (index != -1) { // 有参数时
  36. let parr = params.split('&'); // 将参数分割成数组 ["id = 1 ", " type = 2"]
  37. for (let i of parr) { // 遍历数组
  38. let arr = i.split('='); // 1) i id = 1 arr = [id, 1] 2)i type = 2 arr = [type, 2]
  39. obj[arr[0]] = arr[1]; // obj[arr[0]] = id, obj.id = 1 obj[arr[0]] = type, obj.type = 2
  40. }
  41. }
  42. return obj;
  43. }
  44. export default {
  45. joinUrl() {
  46. // 去除test-shop,转回shop
  47. // const url = true ? this.data.url.replace('shop.html', 'test-shop.html') : this.data.url;
  48. const url = this.data.url;
  49. let options = {
  50. API_BASE_URL: api.API_BASE_URL,
  51. "url": url,
  52. // "url": 'http://192.168.0.112:8080',
  53. "reload": this.data.reload,
  54. "token": wx.getStorageSync('token'),
  55. "code": this.mcode,
  56. "brandId": this.options.id,
  57. "open": this.data.showCommodity,
  58. "pauseVideo": this.pauseVideo,
  59. "bottom": this.data.bottom || 0,
  60. socket: {
  61. socketHost: remote.socketHost,
  62. path: '/new-zfb',
  63. options: {
  64. ...this.data.socketOptions,
  65. // nickname: encodeURI(encodeURI(this.data.socketOptions.nickname))
  66. nickname: encodeURIComponent(encodeURIComponent(this.data.socketOptions.nickname))
  67. }
  68. }
  69. }
  70. // let base = 'http://127.0.0.1:5500/index.html'
  71. // let base = remote.viewHost + '/shop-container/shop.html'
  72. let sponsor = !!this.data.canShow
  73. if (this.data.join && !this.options.join) {
  74. sponsor = false
  75. }
  76. // 33是从我的房间出来的
  77. if (Number(this.data.type) === 33) {
  78. sponsor = true;
  79. }
  80. // debugger
  81. // remote.viewHost
  82. let hostUrl
  83. if (options.url.indexOf('www.4dkankan.com') != -1) {
  84. hostUrl = 'https://www.4dkankan.com/shop-container-zfb/'
  85. } else if (options.url.indexOf('test.4dkankan.com') != -1) {
  86. hostUrl = 'https://test.4dkankan.com/shop-container-zfb/'
  87. } else {
  88. // hostUrl = 'https://zfb.4dkankan.com/shop-container/'
  89. // hostUrl = remote.viewHost + '/shop-container/'
  90. hostUrl = remote.viewHost + '/shop-container-v4/'
  91. }
  92. // let base = remote.viewHost + '/shop-container/fashilong.html?env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
  93. let base = hostUrl + 'fashilong.html?time=' + Date.now() + '&env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
  94. // let base = remote.viewHost + '/shop.html'
  95. this.data.reload = false
  96. this.data.showCommodity = false
  97. //上线前隐藏Vlog
  98. // options.url = options.url + '&vlog';
  99. if (!this.data.webviewUrl) {
  100. console.log(base)
  101. this.setData({
  102. 'webviewUrl': base + '#' + JSON.stringify(options)
  103. })
  104. } else {
  105. this.socketSendMessage('clientSyncAction', {
  106. sender: 'h5',
  107. type: 'hashChange',
  108. data: options
  109. })
  110. }
  111. },
  112. onShow() {
  113. this.setData({
  114. isIos,
  115. showComtypesAllTab: false
  116. })
  117. // wx.showToast({
  118. // title: '测试--socket.connected' + this.socket.connected,
  119. // })
  120. const onlineAction = () => {
  121. this.pauseVideo = false
  122. this.joinUrl()
  123. // debugger
  124. this.socketSendMessage('changeOnlineStatus', {
  125. status: 1
  126. })
  127. }
  128. console.warn("socketInstance-1", this.socketInstance);
  129. if (this.socketInstance) {
  130. if (!this.socketInstance.connected) {
  131. // wx.showToast({
  132. // title: 'socketInstance-2',
  133. // icon: 'none',
  134. // duration: 5000
  135. // })
  136. // this.socketInstance.connect();
  137. // wx.showModal({
  138. // title: '提示',
  139. // content: '欧克',
  140. // showCancel: false,
  141. // confirmColor: '#0075DC',
  142. // success: function (res) {}
  143. // })
  144. setTimeout(onlineAction, 300)
  145. } else {
  146. // wx.showToast({
  147. // title: 'socketInstance-3',
  148. // icon: 'none',
  149. // duration: 5000
  150. // })
  151. onlineAction();
  152. }
  153. }
  154. // if (this.socketSendMessage) {
  155. // this.pauseVideo = false
  156. // this.joinUrl()
  157. // // debugger
  158. // this.socketSendMessage('changeOnlineStatus', {
  159. // status: 1
  160. // })
  161. // }
  162. },
  163. changeShowComtypesAllTab(ev) {
  164. this.setData({
  165. showCommodity: false
  166. })
  167. setTimeout(() => {
  168. this.setData({
  169. showComtypesAllTab: ev.currentTarget.dataset.show,
  170. showCommodity: true
  171. })
  172. }, 100)
  173. },
  174. async authorizeRecord() {
  175. let isAuth = await new Promise((r, j) => {
  176. wx.authorize({
  177. scope: 'scope.record',
  178. success: () => r(true),
  179. fail: () => r(false)
  180. })
  181. })
  182. if (isAuth) return true
  183. let res = await new Promise(r => {
  184. wx.showModal({
  185. title: '提示',
  186. content: '您未授权录音,说话功能将无法使用',
  187. showCancel: true,
  188. confirmText: "授权",
  189. confirmColor: "#52a2d8",
  190. success: res => r(res),
  191. fail: () => r(false)
  192. })
  193. })
  194. if (!res || res.cancel) return;
  195. isAuth = await new Promise((r) => {
  196. wx.openSetting({
  197. success: res => r(res.authSetting['scope.record']),
  198. fail: () => r(false)
  199. })
  200. })
  201. return isAuth
  202. },
  203. // 获取录音权限状态
  204. async getAuthorizeRecordStatus() {
  205. const isAuth = await new Promise((r, j) => {
  206. wx.authorize({
  207. scope: 'scope.record',
  208. success: () => r(true),
  209. fail: () => r(false)
  210. })
  211. })
  212. return Promise.resolve(isAuth)
  213. },
  214. async agetUserInfo() {
  215. const res = await util.request(api.UserInfo)
  216. if (res.errno === 401) {
  217. return {
  218. userId: UNLOGIN,
  219. avatar: ''
  220. }
  221. } else {
  222. const data = res.data
  223. data.region = data.city ? data.city.split(',') : []
  224. data.birthday = data.birthday || '1990-01-01'
  225. return data
  226. }
  227. },
  228. async getUserInfo() {
  229. let userInfo = wx.getStorageSync('userInfo');
  230. let token = wx.getStorageSync('token');
  231. if (userInfo && userInfo.userId && token) {
  232. let info = await this.agetUserInfo()
  233. return {
  234. ...userInfo,
  235. ...info,
  236. avatarUrl: info.avatar
  237. };
  238. } else {
  239. return {
  240. userId: UNLOGIN,
  241. avatar: ''
  242. }
  243. }
  244. // let detail
  245. // let isAuth = await new Promise((r, j) => {
  246. // wx.authorize({
  247. // scope: 'scope.userInfo',
  248. // success: () => r(true),
  249. // fail: () => r(false)
  250. // })
  251. // })
  252. // if (!isAuth) {
  253. // this.setData({userAuth: true})
  254. // detail = await new Promise(r => {
  255. // this.bindGetUserInfo = (e) => {
  256. // if (e.detail.userInfo) {
  257. // this.setData({userAuth: false})
  258. // console.log('gei', e.detail)
  259. // r(e.detail)
  260. // }
  261. // }
  262. // })
  263. // } else {
  264. // detail = await new Promise(r => {
  265. // wx.getUserInfo({
  266. // success: res => r(res),
  267. // fail: () => r(false)
  268. // })
  269. // })
  270. // }
  271. // try {
  272. // let res = await user.loginByWeixin(detail)
  273. // app.globalData.userInfo = res.data.userInfo;
  274. // app.globalData.token = res.data.token;
  275. // return res.data.userInfo
  276. // } catch(e) {
  277. // return false
  278. // }
  279. },
  280. login() {
  281. getApp().setLoginProps(false)
  282. },
  283. async getSocketOptions(sceneId, roomId) {
  284. //TODO
  285. console.log('this.data.type', this.data.type)
  286. // debugger;
  287. let result
  288. if (Number(this.data.type) === 33) {
  289. result = await util.request(api.enterRoom, {
  290. businessId: roomId
  291. }, 'POST', 'application/json')
  292. if (result.code !== 200) {
  293. wx.showModal({
  294. content: result.error,
  295. complete: () => {
  296. if (result.message && result.message.isAnchor == 0) {
  297. wx.switchTab({
  298. url: '/pages/index/index',
  299. })
  300. } else {
  301. wx.redirectTo({
  302. url: '/pages/roomManger/roomManger',
  303. });
  304. }
  305. }
  306. })
  307. return
  308. }
  309. }
  310. const capacities = !!result ? result.message.capacities : 50 // 房间限制人数
  311. const {
  312. isAnchor,
  313. assistant,
  314. } = !!result ? result.message : {}
  315. let userInfo = await this.getUserInfo()
  316. // console.log('---', userInfo)
  317. // this.setData({
  318. // userInfoa: userInfo.nickname.split('').join(' ')
  319. // })
  320. userInfo.nickname = userInfo.nickname.replace(/[^\u4E00-\u9FA5A-Za-z0-9]/g, '')
  321. if (userInfo.nickname == "") {
  322. userInfo.nickname = "口"
  323. }
  324. // this.role !== 'leader'
  325. // let roomType
  326. // if ((!this.data.canShow && !this.data.join) || (this.data.join && !this.options.join)) {
  327. // // roomType = '1v1'
  328. // if (this.options.roomId) {
  329. // this.role = 'leader'
  330. // }
  331. // console.log('**************')
  332. // console.log(this.options)
  333. // }
  334. let isAllowMic // 真正MIC权, 房主与 授权一个 要在房间isAllowMic开启
  335. if (Number(isAnchor) === 1) {
  336. this.role = "leader"
  337. isAllowMic = 1
  338. } else {
  339. this.role = 'customer'
  340. isAllowMic = 0
  341. }
  342. // 助手改用isAssistant作为flag, role因为V3不能新增角色,只支持leader/customer。
  343. let isAssistant
  344. if (assistant && assistant.userId && assistant.userId == userInfo.userId) {
  345. isAssistant = true;
  346. } else {
  347. isAssistant = false
  348. }
  349. console.log('进入房间角色, 是否助手 %s', this.role, isAssistant);
  350. const isAuthMic = await this.getAuthorizeRecordStatus();
  351. // console.log('当前用户录音权限状态', isAuthMic)
  352. this.setData({
  353. isAllowMic,
  354. isAuthMic
  355. })
  356. const assistantId = (assistant && assistant.userId) ? assistant.userId : '';
  357. if (capacities) {
  358. this.setData({
  359. peopleCount: capacities
  360. })
  361. } else {
  362. this.setData({
  363. peopleCount: manyCount
  364. })
  365. }
  366. return {
  367. role: this.role,
  368. userId: userInfo.userId,
  369. // roomType,
  370. avatar: userInfo.avatarUrl,
  371. nickname: userInfo.nickname,
  372. voiceStatus: getApp().globalData.voiceProps.noMute ? 0 : 2,
  373. isAuthMic: isAuthMic ? 1 : 0,
  374. isAllowMic: isAllowMic,
  375. roomId: roomId,
  376. sceneNumber: sceneId,
  377. onlineStatus: 1,
  378. assistantId: assistantId,
  379. isAssistant: isAssistant ? 1 : 0,
  380. oid: userInfo.weixin_openid,
  381. userLimitNum: capacities || 50
  382. }
  383. },
  384. async socketStart({
  385. sceneId,
  386. roomId,
  387. options
  388. }) {
  389. if (!options) {
  390. options = await this.getSocketOptions(sceneId, roomId)
  391. }
  392. console.log('小程序参数', options)
  393. if (!options.roomId) {
  394. return Promise.resolve(false)
  395. }
  396. // 真正进入统计
  397. if (options.roomId !== '888888') {
  398. util.request(api.trackRoom, {
  399. roomId: options.roomId,
  400. type: 0
  401. }, 'POST', 'application/json')
  402. }
  403. let userInfo = await this.getUserInfo()
  404. let socket = io(remote.socketHost, {
  405. path: '/new-zfb',
  406. transport: ['websocket'],
  407. extraHeaders: {
  408. "oid": userInfo.weixin_openid
  409. },
  410. query: {
  411. ...options,
  412. isClient: true,
  413. from: 2
  414. }
  415. });
  416. this.socketInstance = socket
  417. console.error('新建socket Room', options.roomId)
  418. this.setData({
  419. socketStatus: 0,
  420. })
  421. socket.on('connect', () => this.setData({
  422. socketStatus: 1
  423. }))
  424. socket.on('connect_error', () => this.setData({
  425. socketStatus: -1
  426. }))
  427. socket.on('connect_timeout', () => this.setData({
  428. socketStatus: -1
  429. }))
  430. socket.on('disconnect', () => this.setData({
  431. socketStatus: -1
  432. }))
  433. socket.on('reconnect', () => {
  434. // wx.showToast({
  435. // title: '重连',
  436. // })
  437. this.setData({
  438. socketStatus: this.data.socketStatus
  439. })
  440. let noMute = getApp().globalData.voiceProps.noMute
  441. this.socketSendMessage('changeVoiceStatus', {
  442. status: noMute ? 0 : 2
  443. })
  444. this.socketSendMessage('changeOnlineStatus', {
  445. status: 1
  446. })
  447. })
  448. socket.on('reconnect_failed', () => this.setData({
  449. socketStatus: -1
  450. }))
  451. socket.on('error', () => this.setData({
  452. socketStatus: -1
  453. }))
  454. socket.on('roomIn', config => {
  455. let enableTalk = config.roomsConfig.enableTalk !== false
  456. let noMute = getApp().globalData.voiceProps.noMute
  457. getApp().globalData.voiceProps.force = enableTalk
  458. if (!enableTalk && !noMute) {
  459. if (this.role !== 'leader') {
  460. // this.mic()
  461. }
  462. }
  463. })
  464. this.socketSendMessage = (event, obj) => {
  465. console.error('发送 socket Room', options.roomId, event, obj)
  466. socket.emit(event, obj)
  467. }
  468. socket.on('clientSyncAction', (data) => {
  469. console.log('调用', data.type, '方法', data)
  470. if (this[data.type]) {
  471. this[data.type](data)
  472. } else if (data.type == 'wx-subscribe') {
  473. this.getUrlCode(data.data)
  474. } else {
  475. console.error('没有', data.type, '方法')
  476. }
  477. })
  478. socket.on('action', (data) => {
  479. if (data.type === 'navigateToGoods') {
  480. this.navigateToGoodsAction(data.data)
  481. }
  482. })
  483. socket.on('changeRoomEnableTalk', config => {
  484. if (this.role !== 'leader') {
  485. this.changeRoomEnableTalk(config)
  486. }
  487. })
  488. socket.on('startCall', this.startCall.bind(this))
  489. socket.on('stopCall', (data) => {
  490. console.log('on stopCall')
  491. this.stopCall(data)
  492. })
  493. this.handleSomeOneInRoom = this.handleSomeOneInRoom.bind(this)
  494. // socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
  495. socket.on('someOneInRoom', debounce(this.handleSomeOneInRoom, 100))
  496. socket.on('someOneLeaveRoom', (user, data) => {
  497. this.handleSomeOneLeave(user)
  498. })
  499. socket.on('roomClose', (data) => {
  500. console.log('on roomClose')
  501. this.stopCall(data)
  502. })
  503. socket.on('autoReJoin', (data) => {
  504. console.log('on autoReJoin')
  505. if ('roomId' in data) {
  506. options.roomId = Number(data.roomId)
  507. }
  508. })
  509. // 有MIC通知,主要是其他用户禁MIC 移到中间层处理。
  510. // socket.on('beHasMic', (data) => {
  511. // const socketOptions = this.data.socketOptions
  512. // if (data.user) {
  513. // const isOther = (socketOptions.role !== 'leader' && (Number(socketOptions.userId) !== Number(data.user.userId)));
  514. // if (isOther) {
  515. // this.closeMic();
  516. // // wx.showToast({
  517. // // title: '关mic' + isOther
  518. // // })
  519. // }
  520. // }
  521. // })
  522. socket.on("beKicked", data => {
  523. const that = this
  524. if (data.userId && data.roomId) {
  525. const socketOptions = this.data.socketOptions
  526. const userId = data.userId
  527. const roomId = data.roomId
  528. // debugger
  529. if (socketOptions.userId == userId && this.options.roomId == roomId) {
  530. wx.showToast({
  531. title: '您已被踢出房间!',
  532. icon: 'none',
  533. complete: () => {}
  534. })
  535. setTimeout(() => {
  536. that.socketStop();
  537. wx.switchTab({
  538. url: '/pages/index/index',
  539. })
  540. }, 1000)
  541. }
  542. }
  543. });
  544. socket.on("roomMaximum", () => {
  545. this.setData({
  546. roomMaximum: true
  547. })
  548. });
  549. //全员退出
  550. socket.on('roomDisMiss', () => {
  551. // wx.showToast({
  552. // title: '全员退出',
  553. // });
  554. this.setData({
  555. roomDisMiss: true
  556. })
  557. // this.exitRoom();
  558. })
  559. //服务器未知错误重进
  560. socket.on('unKnowError', () => {
  561. this.setData({
  562. unKnowError: true
  563. })
  564. })
  565. //被动通知开关MIC 要3秒后
  566. socket.on('serverOnMic', ({
  567. voiceStatus
  568. }) => {
  569. setTimeout(() => {
  570. if (Number(voiceStatus) === 2) {
  571. // wx.showToast({
  572. // title: '开MIC',
  573. // icon:'none'
  574. // })
  575. this.openMic();
  576. }
  577. if (Number(voiceStatus) === 0) {
  578. // wx.showToast({
  579. // title: '关MIC',
  580. // icon:'none'
  581. // })
  582. this.closeMic();
  583. }
  584. }, 3000)
  585. });
  586. this.socketStop = () => {
  587. if (socket) {
  588. socket.close()
  589. console.error('断开 并滞空 socket Room', options.roomId)
  590. this.setData({
  591. socketStatus: 2
  592. })
  593. socket = null
  594. }
  595. }
  596. return options
  597. },
  598. getUrlCode(url) {
  599. this.socketSendMessage('clientSyncAction', {
  600. sender: 'wx',
  601. type: 'wx-subscribe-result',
  602. data: 3020
  603. })
  604. // wx.request({
  605. // url: url, //仅为示例,并非真实的接口地址
  606. // method: 'get',
  607. // success: (res) => {
  608. // let code = -1
  609. // if (typeof res.data.code != 'undefined') {
  610. // code = res.data.code
  611. // }
  612. // this.socketSendMessage('clientSyncAction', {
  613. // sender: 'wx',
  614. // type: 'wx-subscribe-result',
  615. // data: code
  616. // })
  617. // },
  618. // fail: (err) => {
  619. // console.log(err)
  620. // }
  621. // })
  622. },
  623. changeRoomEnableTalk(data) {
  624. console.log(data)
  625. let noMute = getApp().globalData.voiceProps.noMute
  626. getApp().globalData.voiceProps.force = data.enableTalk
  627. // noMute true 静音
  628. // enableTalk false 静音
  629. if (!!data.enableTalk === !!noMute) {
  630. this.mic()
  631. }
  632. },
  633. navigateToGoods({
  634. data
  635. }) {
  636. // wx.showToast({
  637. // title: JSON.stringify(data).substr(40)
  638. // })
  639. this.navigateToGoodsAction(data)
  640. },
  641. navigateToGoodsAction(id) {
  642. wx.navigateTo({
  643. url: '/pages/goods/goods?id=' + id,
  644. })
  645. },
  646. getUrl(url, socketOptions, isJoin) {
  647. url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
  648. if (isJoin) {
  649. url += '&role=' + this.role + '&shopping'
  650. } else {
  651. url += '&role=' + this.role
  652. }
  653. console.error(url)
  654. console.log(isJoin)
  655. return url
  656. },
  657. navigateToMiniProgram(data) {
  658. wx.showModal({
  659. title: '温馨提示',
  660. content: '即将跳到其他小程序,是否继续?',
  661. showCancel: true, //是否显示取消按钮
  662. cancelText: "取消", //默认是“取消”
  663. confirmText: "确定", //默认是“确定”
  664. success: function (res) {
  665. if (res.cancel) {
  666. //点击取消,wx.navigateBack
  667. } else {
  668. wx.navigateToMiniProgram(data.data)
  669. }
  670. },
  671. fail: function (res) {
  672. //接口调用失败的回调函数,wx.navigateBack
  673. },
  674. complete: function (res) {
  675. //接口调用结束的回调函数(调用成功、失败都会执行)
  676. },
  677. })
  678. },
  679. async handleSomeOneInRoom(data) {
  680. if (data && data.user) {
  681. console.log('handleSomeOneInRoom', data)
  682. this.startCall(data)
  683. }
  684. },
  685. async startCall(data) {
  686. //TODO 触发三次
  687. console.log('startCall-data', data)
  688. // if( this.role =='leader'){
  689. this.setData({
  690. shareStatus: 1
  691. })
  692. if (!data) return;
  693. this.setData({
  694. surplus: this.data.peopleCount - data.roomsPerson.length
  695. })
  696. //undefined是未授权,状态为3
  697. let voiceStatus
  698. if (!this.isAuthorizeRecord) {
  699. const unAuth = await this.authorizeRecord();
  700. if (typeof unAuth === 'undefined') {
  701. // debugger
  702. voiceStatus = 3
  703. } else {
  704. voiceStatus = Number(unAuth)
  705. }
  706. }
  707. //限制只有主持人才可以开麦
  708. // if (this.role == 'leader') {
  709. // if (!this.isAuthorizeRecord) {
  710. // const voiceStatus = Number(await this.authorizeRecord())
  711. // this.isAuthorizeRecord = true
  712. // // getApp().setVoiceProps({
  713. // // noMute: !voiceStatus
  714. // // })
  715. // // console.log(getApp().globalData.voiceProps.noMute)
  716. // // this.socketSendMessage('changeVoiceStatus', {
  717. // // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  718. // // })
  719. // // this.data.socketOptions.voiceStatus = 1
  720. // // this.socketSendMessage('changeVoiceStatus', {status: noMute ? 0 : 2})
  721. // }
  722. // }
  723. const socketOptions = this.data.socketOptions
  724. getApp().globalData.roomId = socketOptions.roomId
  725. const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
  726. if (!user) {
  727. return
  728. }
  729. //屏蔽有人进来才开麦克风
  730. // if (data.roomsPerson.length <= 1) {
  731. // return
  732. // }
  733. user.noMute = getApp().globalData.voiceProps.noMute
  734. getApp().setVoiceProps({
  735. ...user,
  736. action: 'startCall'
  737. })
  738. // this.socketSendMessage('changeVoiceStatus', {
  739. // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  740. // })
  741. // }
  742. },
  743. stopCall() {
  744. console.error('stopCall')
  745. this.setData({
  746. shareStatus: 0
  747. })
  748. getApp().setVoiceProps({
  749. noMute: false,
  750. action: 'stopCall'
  751. })
  752. if (this.runManager) {
  753. // this.recorderManager.stop()
  754. this.runManager = false
  755. }
  756. },
  757. handleSomeOneLeave(data) {
  758. if (data.roomsPerson.length <= 1) {
  759. // this.stopCall()
  760. }
  761. this.setData({
  762. surplus: this.data.peopleCount - data.roomsPerson.length
  763. })
  764. },
  765. //deprecated
  766. async newRoomBk(data) {
  767. if (data.roomId) return;
  768. this.stopCall()
  769. getApp().globalData.rtcParams = []
  770. getApp().globalData.pusher = ''
  771. if (this.data.join && !this.options.join) {
  772. wx.switchTab({
  773. url: '/pages/index/index',
  774. })
  775. return;
  776. }
  777. this.role = this.data.canShow ? 'leader' : 'customer'
  778. let options = await this.getSocketOptions(this.mcode)
  779. this.socketSendMessage('clientSyncAction', {
  780. type: 'newRoom',
  781. data: options
  782. })
  783. setTimeout(async () => {
  784. this.wssSuccess = false
  785. this.socketStop && this.socketStop()
  786. this.data.many = !!this.data.canShow
  787. // this.setData({
  788. // // peopleCount: this.data.many ? manyCount : 5
  789. // peopleCount: manyCount
  790. // })
  791. let base = this.base
  792. let socketOptions = await this.socketStart({
  793. options
  794. })
  795. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  796. this.base = base
  797. this.setData({
  798. url,
  799. socketOptions,
  800. })
  801. this.joinUrl()
  802. this.setData({
  803. socketOptions
  804. })
  805. this.loadConponSuccess = true
  806. this.readySendCouponCtrl()
  807. }, 300)
  808. },
  809. // 真正退出房间
  810. async exitRoom() {
  811. const roomId = this.data.socketOptions.roomId;
  812. const role = this.role;
  813. const result = await util.request(api.exitRoom, {
  814. businessId: roomId
  815. }, 'POST', 'application/json');
  816. this.socketSendMessage('stopCall', {
  817. from: 2
  818. })
  819. this.stopCall();
  820. this.socketStop();
  821. if (role === 'leader') {
  822. wx.redirectTo({
  823. url: '/pages/roomManger/roomManger',
  824. });
  825. } else {
  826. wx.switchTab({
  827. url: '/pages/index/index'
  828. });
  829. }
  830. },
  831. async exit() {
  832. // this.stopCall()
  833. getApp().globalData.rtcParams = []
  834. getApp().globalData.pusher = ''
  835. this.socketStop && this.socketStop()
  836. this.role = 'leader'
  837. let base = this.base
  838. let socketOptions = await this.socketStart({
  839. sceneId: this.mcode
  840. })
  841. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  842. this.base = base
  843. wx.nextTick(() => {
  844. setTimeout(() => {
  845. this.setData({
  846. url,
  847. loadUrl: true,
  848. socketOptions,
  849. showCommodityCtrl: false,
  850. hideWebView: false,
  851. reload: true
  852. })
  853. this.joinUrl()
  854. }, 500)
  855. })
  856. },
  857. clearDebuger() {
  858. this.setData({
  859. debugerInfo: ''
  860. })
  861. },
  862. async mic({
  863. data
  864. }) {
  865. if (Number(data.user.isAllowMic) === 1) {
  866. let noMute = getApp().globalData.voiceProps.noMute
  867. // debugger
  868. // noMute true 静音
  869. // enableTalk false 静音
  870. // if (!!getApp().globalData.voiceProps.force === !!noMute)
  871. // return
  872. // if (!getApp().globalData.voiceProps.force && (!this.data.socketOptions.voiceStatus || noMute)) return;
  873. if (!this.data.socketOptions.voiceStatus) {
  874. let voiceStatus = await this.authorizeRecord()
  875. if (voiceStatus) {
  876. this.data.socketOptions.voiceStatus = 1
  877. noMute = false
  878. } else {
  879. noMute = true
  880. }
  881. } else {
  882. noMute = !noMute
  883. }
  884. getApp().globalData.voiceProps.noMute = noMute
  885. this.socketSendMessage('changeVoiceStatus', {
  886. status: noMute ? 0 : 2,
  887. user: data.user
  888. })
  889. getApp().setVoiceProps({
  890. noMute
  891. })
  892. wx.showToast({
  893. title: `已${noMute ? '关闭' : '开启'}麦克风`,
  894. })
  895. }
  896. },
  897. closeMic() {
  898. getApp().globalData.voiceProps.noMute = true
  899. this.socketSendMessage('changeVoiceStatus', {
  900. status: 0,
  901. })
  902. getApp().setVoiceProps({
  903. noMute: true
  904. })
  905. },
  906. openMic() {
  907. getApp().globalData.voiceProps.noMute = false
  908. this.socketSendMessage('changeVoiceStatus', {
  909. status: 2,
  910. })
  911. getApp().setVoiceProps({
  912. noMute: false
  913. })
  914. },
  915. callPhone() {
  916. wx.makePhoneCall({
  917. phoneNumber: this.data.contractPhone,
  918. })
  919. this.setData({
  920. showContact: false
  921. })
  922. },
  923. /**
  924. * 用户点击右上角分享
  925. */
  926. onShareAppMessage: function (res) {
  927. let {
  928. id,
  929. newPicUrl
  930. } = this.data
  931. if (res.from === 'button') {
  932. this.setData({
  933. sendShare: false
  934. })
  935. return {
  936. title: '【好友推荐】一起来云逛吧',
  937. imageUrl: newPicUrl,
  938. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=true&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`,
  939. }
  940. } else {
  941. return {
  942. imageUrl: newPicUrl,
  943. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=false`,
  944. }
  945. }
  946. },
  947. /**
  948. * 生命周期函数--监听页面卸载
  949. */
  950. onUnload: function () {
  951. console.log('on onUnload')
  952. // this.socketSendMessage('stopCall', {})
  953. // this.stopCall()
  954. this.socketStop && this.socketStop()
  955. getApp().globalData.pusher = ''
  956. },
  957. cart(data) {
  958. this.setData({
  959. showCommodityCtrl: data.data
  960. })
  961. },
  962. share() {
  963. console.log('share-debug')
  964. const companyName = `指房宝(杭州)科技有限公司`
  965. const vrLink = `/pages/webview/index`
  966. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  967. const shareImg = img_url
  968. this.count = this.count || 0
  969. console.log('share-debug many:%s shareStatus: %s', !!this.data.many, this.data.shareStatus);
  970. if (this.data.many && this.data.shareStatus == 1) {
  971. //开启一起逛时候的分享
  972. console.log(`share-debug: /pages/shareRoom/shareRoom?img_url=${btoa(img_url)}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`)
  973. console.log('share-debug', this.data.socketOptions)
  974. wx.navigateTo({
  975. url: `/pages/shareRoom/shareRoom?img_url=${btoa(img_url)}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`,
  976. })
  977. } else {
  978. console.log(`share-debug: /pages/shared/shared?img_url=${btoa(img_url)}&shareImg=${btoa(shareImg)}&companyName=${companyName}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}`);
  979. wx.navigateTo({
  980. url: `/pages/shared/shared?img_url=${btoa(img_url)}&shareImg=${btoa(shareImg)}&companyName=${companyName}&vrLink=${btoa(vrLink)}&id=${this.data.id}&type=${this.data.type}`,
  981. })
  982. }
  983. },
  984. back(data) {
  985. if (data.sender !== 'h5') return;
  986. wx.switchTab({
  987. url: '/pages/index/index'
  988. })
  989. this.setData({
  990. showCommodityCtrl: false
  991. })
  992. },
  993. service() {
  994. this.setData({
  995. showContact: true,
  996. showCommodity: false,
  997. showCoupon: false
  998. })
  999. },
  1000. invite(data) {
  1001. if (data.sender !== 'h5') return;
  1002. // 分享房间进入统计
  1003. if (this.data.socketOptions.roomId !== '888888') {
  1004. util.request(api.trackRoom, {
  1005. roomId: this.data.socketOptions.roomId,
  1006. type: 1,
  1007. }, 'POST', 'application/json')
  1008. }
  1009. this.setData({
  1010. sendShare: true,
  1011. count: ++this.data.count
  1012. })
  1013. },
  1014. coupon(data) {
  1015. if (data.sender !== 'h5') return;
  1016. this.setData({
  1017. showContact: false,
  1018. showCommodity: false,
  1019. showCoupon: true
  1020. })
  1021. },
  1022. liveGotoGood(ev) {
  1023. let id = ev.currentTarget.dataset.item.goodsId
  1024. wx.navigateTo({
  1025. url: '/pages/goods/goods?id=' + id,
  1026. })
  1027. },
  1028. gotoGoodsDOM(event) {
  1029. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  1030. },
  1031. gotoGoodsSocket(data) {
  1032. this.gotoGoods(data.data)
  1033. },
  1034. gotoGoods(id) {
  1035. console.log('---', id)
  1036. this.socketSendMessage('clientSyncAction', {
  1037. type: 'openTag',
  1038. data: id
  1039. })
  1040. this.setData({
  1041. showCommodity: false
  1042. })
  1043. this.joinUrl()
  1044. },
  1045. addCard(event) {
  1046. wx.navigateTo({
  1047. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  1048. })
  1049. },
  1050. buyGoods(event) {
  1051. wx.navigateTo({
  1052. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  1053. })
  1054. },
  1055. showCommodityFn() {
  1056. this.setData({
  1057. showCommodity: true,
  1058. showContact: false,
  1059. showCoupon: false
  1060. })
  1061. this.joinUrl()
  1062. },
  1063. hideComodity() {
  1064. this.setData({
  1065. showCommodity: false
  1066. })
  1067. this.joinUrl()
  1068. },
  1069. hideCoupon() {
  1070. this.setData({
  1071. showCoupon: !this.data.showCoupon
  1072. })
  1073. },
  1074. async receive(ev) {
  1075. let item = ev.target.dataset.item
  1076. try {
  1077. // wx.showToast({
  1078. // title: '领取优惠卷',
  1079. // })
  1080. // return;
  1081. if (item.hasReceived || item.number <= item.receiveNumber) return;
  1082. let res = await util.request(api.CouponExchange, {
  1083. couponId: item.id
  1084. })
  1085. if (res.code === 0) {
  1086. wx.showToast({
  1087. title: '已成功领取',
  1088. success: () => {
  1089. this.setData({
  1090. showCoupon: false
  1091. })
  1092. wx.nextTick(() => {
  1093. this.setData({
  1094. coupons: this.data.coupons.map(citem => {
  1095. return {
  1096. ...citem,
  1097. hasReceived: citem.id === item.id ? true : citem.hasReceived
  1098. }
  1099. }),
  1100. showCoupon: true
  1101. })
  1102. })
  1103. }
  1104. })
  1105. } else if (res.errno === 401) {
  1106. getApp().setLoginProps(false)
  1107. } else {
  1108. wx.showToast({
  1109. title: res.msg,
  1110. })
  1111. }
  1112. } catch (e) {
  1113. console.error(e)
  1114. wx.showToast({
  1115. icon: 'none',
  1116. title: '领取失败',
  1117. })
  1118. }
  1119. },
  1120. async getCouponList(id) {
  1121. const success = (res) => {
  1122. this.setData({
  1123. coupons: res.data.list.map(item => {
  1124. item.typeMoney = item.typeMoney.toString()
  1125. item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
  1126. item.typeMoney.length === 4 ? '70rpx' : '130rpx'
  1127. return item
  1128. })
  1129. })
  1130. this.loadConponSuccess = true
  1131. this.readySendCouponCtrl()
  1132. }
  1133. let res = await util.request(api.BrandCouponList, {
  1134. brandId: id,
  1135. pageNum: 1,
  1136. pageSize: 10000
  1137. }, 'GET')
  1138. console.log(res)
  1139. if (res.code === 0) {
  1140. success(res)
  1141. } else {
  1142. let res = await util.request(api.UNBrandCouponList, {
  1143. brandId: id,
  1144. pageNum: 1,
  1145. pageSize: 10000
  1146. }, 'GET')
  1147. success(res)
  1148. }
  1149. },
  1150. ready() {
  1151. this.wssSuccess = true
  1152. this.readySendCouponCtrl()
  1153. },
  1154. readySendCouponCtrl() {
  1155. if (this.wssSuccess && this.loadConponSuccess) {
  1156. this.loadConponSuccess = false
  1157. this.socketSendMessage('clientSyncAction', {
  1158. type: 'showCoupon',
  1159. data: this.data.coupons.length > 0
  1160. })
  1161. }
  1162. },
  1163. getBrand: function (id, code) {
  1164. this.getGoodsCount(code, id)
  1165. return;
  1166. },
  1167. getGoodsCount(code, id) {
  1168. util.request(api.GoodsNumCount, {
  1169. isDelete: 0,
  1170. isOnSale: 1,
  1171. brandId: id
  1172. }, 'GET')
  1173. .then(res => {
  1174. if (res.errno === 0) {
  1175. this.setData({
  1176. goodsCount: res.data
  1177. })
  1178. }
  1179. this.getCouponList(id)
  1180. })
  1181. },
  1182. getGoodsList(id, category_id) {
  1183. var that = this;
  1184. if (!(this.data.navList && this.data.navList.length)) {
  1185. that.navDatas = {}
  1186. let navDatas = this.data.navList = this.data.comtypes
  1187. // util.request(api.GoodsCategory, { id: category_id })
  1188. // .then(function (res) {
  1189. // if (res.errno == 0) {
  1190. // let navDatas = res.data.brotherCategory
  1191. // that.setData({
  1192. // navList: navDatas,
  1193. // currTypeId: category_id
  1194. // });
  1195. that.navDatas = {}
  1196. navDatas.forEach(item => {
  1197. util.request(api.GoodsList, {
  1198. brandId: id,
  1199. categoryId: item.category_id,
  1200. page: that.data.page,
  1201. size: that.data.size
  1202. })
  1203. .then(res => {
  1204. if (res.errno === 0) {
  1205. that.navDatas[item.category_id] = res.data.goodsList
  1206. }
  1207. })
  1208. })
  1209. // }
  1210. // })
  1211. }
  1212. if (that.navDatas[category_id]) {
  1213. if (!isIos) {
  1214. let showCommodity = that.data.showCommodity
  1215. that.setData({
  1216. showCommodity: false
  1217. })
  1218. setTimeout(() => {
  1219. wx.nextTick(() => {
  1220. that.setData({
  1221. goodsList: that.navDatas[category_id],
  1222. currTypeId: category_id,
  1223. showCommodity: showCommodity
  1224. });
  1225. })
  1226. }, 500)
  1227. } else {
  1228. that.setData({
  1229. goodsList: that.navDatas[category_id],
  1230. currTypeId: category_id,
  1231. });
  1232. }
  1233. } else {
  1234. console.error('诱惑去啦')
  1235. util.request(api.GoodsList, {
  1236. brandId: id,
  1237. categoryId: category_id,
  1238. page: that.data.page,
  1239. size: that.data.size
  1240. })
  1241. .then(function (res) {
  1242. if (res.errno === 0) {
  1243. that.setData({
  1244. goodsList: res.data.goodsList,
  1245. currTypeId: category_id
  1246. });
  1247. // this.data.navList
  1248. }
  1249. });
  1250. }
  1251. },
  1252. getBrandDetail: function (id, type, cb) {
  1253. console.log('getBrandDetail-params', id, type)
  1254. util.request(api.BrandDetail, {
  1255. id: id,
  1256. type: type,
  1257. }).then((res) => {
  1258. console.log('getBrandDetail', res)
  1259. let base = res.data.brand.sceneUrl
  1260. // let base = 'http://192.168.0.112:8080/shop.html?m=t-7Uqj9Fq&origin=fashilong'
  1261. if (res.errno === 0) {
  1262. let url = base + "&sid=" + id
  1263. // debugger
  1264. this.setData({
  1265. id: id,
  1266. newPicUrl: res.data.brand.appListPicUrl,
  1267. sceneNum: res.data.brand.sceneNum,
  1268. canShow: res.data.brand.canShow,
  1269. contractPhone: res.data.brand.contractPhone,
  1270. contactInfo: {
  1271. contactPhone: res.data.brand.contactPhone || '',
  1272. contactHead: res.data.brand.contactHead || '',
  1273. contactCompanyName: res.data.brand.contactCompanyName || '',
  1274. contactNickName: res.data.brand.contactNickName || '',
  1275. brands: res.data.brand.brands || []
  1276. }
  1277. })
  1278. if (this.data.many === void 0) {
  1279. this.data.many = !!res.data.brand.canShow
  1280. }
  1281. this.setData({
  1282. // peopleCount: this.data.many ? manyCount : 5,
  1283. peopleCount: manyCount
  1284. })
  1285. if (!res.data.brand.canShow) {
  1286. this.role = 'customer'
  1287. } else if (!this.options.join) {
  1288. this.role = 'leader'
  1289. }
  1290. cb(url, urlToJson(url).m, )
  1291. }
  1292. });
  1293. },
  1294. sendContactInfo() {
  1295. // this.socketSendMessage('getContactInfo', {
  1296. // contactInfo: this.data.contactInfo,
  1297. // })
  1298. this.socketSendMessage('clientSyncAction', {
  1299. type: 'getContactInfo',
  1300. contactInfo: this.data.contactInfo
  1301. })
  1302. },
  1303. goToCase({
  1304. data
  1305. }) {
  1306. console.log('data', data)
  1307. wx.showModal({
  1308. title: '提示',
  1309. content: `是否跳转到新的场景?`,
  1310. showCancel: true,
  1311. confirmColor: "#52a2d8",
  1312. success: res => {
  1313. wx.navigateTo({
  1314. url: `/pages/webview/index?id=${data.id}&type=32`,
  1315. })
  1316. },
  1317. fail: () => {
  1318. }
  1319. })
  1320. },
  1321. selectType(ev) {
  1322. this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
  1323. },
  1324. hideCS() {
  1325. this.setData({
  1326. showCommodity: false,
  1327. showCoupon: false,
  1328. showContact: false
  1329. })
  1330. },
  1331. hideContact() {
  1332. this.setData({
  1333. showContact: false
  1334. })
  1335. },
  1336. calcShare() {
  1337. // this.exit()
  1338. this.setData({
  1339. sendShare: false
  1340. })
  1341. },
  1342. contactKf() {
  1343. let keys = Object.keys(this.navDatas)
  1344. let goodsId = this.navDatas[keys[0]][0].id
  1345. let user = wx.getStorageSync('userinfoDetail')
  1346. util.request(api.AddTalkCount, {
  1347. goodsId,
  1348. viewId: user && user.userId || '',
  1349. sceneNum: this.data.sceneNum
  1350. }, 'get')
  1351. this.hideAlert && this.hideAlert()
  1352. this.hideContact && this.hideContact()
  1353. },
  1354. onHide() {
  1355. this.socketSendMessage('changeOnlineStatus', {
  1356. status: 0
  1357. })
  1358. this.pauseVideo = true
  1359. this.joinUrl()
  1360. }
  1361. }