socket.js 33 KB

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