socket.js 33 KB

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