socket.js 29 KB

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