socket.js 35 KB

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