socket.js 30 KB

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