socket.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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. },
  496. async newRoom(data) {
  497. if (data.roomId) return;
  498. this.stopCall()
  499. getApp().globalData.rtcParams = []
  500. getApp().globalData.pusher = ''
  501. if (this.data.join && !this.options.join) {
  502. wx.switchTab({
  503. url: '/pages/index/index',
  504. })
  505. return;
  506. }
  507. this.role = this.data.canShow ? 'leader' : 'customer'
  508. let options = await this.getSocketOptions(this.mcode)
  509. this.socketSendMessage('clientSyncAction', {
  510. type: 'newRoom',
  511. data: options
  512. })
  513. setTimeout(async () => {
  514. this.wssSuccess = false
  515. this.socketStop && this.socketStop()
  516. this.data.many = !!this.data.canShow
  517. this.setData({
  518. // peopleCount: this.data.many ? manyCount : 5
  519. peopleCount: this.data.many ? manyCount : 2
  520. })
  521. let base = this.base
  522. let socketOptions = await this.socketStart({
  523. options
  524. })
  525. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  526. this.base = base
  527. this.setData({
  528. url,
  529. socketOptions,
  530. })
  531. this.joinUrl()
  532. this.setData({
  533. socketOptions
  534. })
  535. this.loadConponSuccess = true
  536. this.readySendCouponCtrl()
  537. }, 300)
  538. },
  539. async exit() {
  540. this.stopCall()
  541. getApp().globalData.rtcParams = []
  542. getApp().globalData.pusher = ''
  543. this.socketStop && this.socketStop()
  544. this.role = 'leader'
  545. let base = this.base
  546. let socketOptions = await this.socketStart({
  547. sceneId: this.mcode
  548. })
  549. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  550. this.base = base
  551. wx.nextTick(() => {
  552. setTimeout(() => {
  553. this.setData({
  554. url,
  555. loadUrl: true,
  556. socketOptions,
  557. showCommodityCtrl: false,
  558. hideWebView: false,
  559. reload: true
  560. })
  561. this.joinUrl()
  562. }, 500)
  563. })
  564. },
  565. clearDebuger() {
  566. this.setData({
  567. debugerInfo: ''
  568. })
  569. },
  570. async mic() {
  571. let noMute = getApp().globalData.voiceProps.noMute
  572. // noMute true 静音
  573. // enableTalk false 静音
  574. // if (!!getApp().globalData.voiceProps.force === !!noMute)
  575. // return
  576. // if (!getApp().globalData.voiceProps.force && (!this.data.socketOptions.voiceStatus || noMute)) return;
  577. if (!this.data.socketOptions.voiceStatus) {
  578. let voiceStatus = await this.authorizeRecord()
  579. if (voiceStatus) {
  580. this.data.socketOptions.voiceStatus = 1
  581. noMute = false
  582. } else {
  583. noMute = true
  584. }
  585. } else {
  586. noMute = !noMute
  587. }
  588. getApp().globalData.voiceProps.noMute = noMute
  589. this.socketSendMessage('changeVoiceStatus', {
  590. status: noMute ? 0 : 2
  591. })
  592. getApp().setVoiceProps({
  593. noMute
  594. })
  595. wx.showToast({
  596. title: `已${noMute ? '关闭' : '开启'}麦克风`,
  597. })
  598. },
  599. callPhone() {
  600. wx.makePhoneCall({
  601. phoneNumber: this.data.contractPhone,
  602. })
  603. this.setData({
  604. showContact: false
  605. })
  606. },
  607. /**
  608. * 用户点击右上角分享
  609. */
  610. onShareAppMessage: function (res) {
  611. let {
  612. id,
  613. newPicUrl
  614. } = this.data
  615. if (res.from === 'button') {
  616. this.setData({
  617. sendShare: false
  618. })
  619. return {
  620. title: '【好友推荐】一起来云逛吧',
  621. imageUrl: newPicUrl,
  622. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=true&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`,
  623. }
  624. } else {
  625. return {
  626. imageUrl: newPicUrl,
  627. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=false`,
  628. }
  629. }
  630. },
  631. /**
  632. * 生命周期函数--监听页面卸载
  633. */
  634. onUnload: function () {
  635. console.log('on onUnload')
  636. this.socketSendMessage('stopCall', {})
  637. this.stopCall()
  638. this.socketStop()
  639. getApp().globalData.pusher = ''
  640. },
  641. cart(data) {
  642. this.setData({
  643. showCommodityCtrl: data.data
  644. })
  645. },
  646. share() {
  647. console.log('**********')
  648. // console.log(!!this.data.mamy)
  649. const companyName = `指房宝(杭州)科技有限公司`
  650. const vrLink = `/pages/webview/index`
  651. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  652. const shareImg = img_url
  653. this.count = this.count || 0
  654. if (this.data.many && this.data.shareStatus == 1) {
  655. //开启一起逛时候的分享
  656. 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}`)
  657. console.log(this.data.socketOptions)
  658. wx.navigateTo({
  659. 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}`,
  660. })
  661. } else {
  662. 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}`);
  663. wx.navigateTo({
  664. 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}`,
  665. })
  666. }
  667. },
  668. back(data) {
  669. if (data.sender !== 'h5') return;
  670. wx.switchTab({
  671. url: '/pages/index/index'
  672. })
  673. this.setData({
  674. showCommodityCtrl: false
  675. })
  676. },
  677. service() {
  678. this.setData({
  679. showContact: true,
  680. showCommodity: false,
  681. showCoupon: false
  682. })
  683. },
  684. invite(data) {
  685. if (data.sender !== 'h5') return;
  686. this.setData({
  687. sendShare: true,
  688. count: ++this.data.count
  689. })
  690. },
  691. coupon(data) {
  692. if (data.sender !== 'h5') return;
  693. this.setData({
  694. showContact: false,
  695. showCommodity: false,
  696. showCoupon: true
  697. })
  698. },
  699. liveGotoGood(ev) {
  700. let id = ev.currentTarget.dataset.item.goodsId
  701. wx.navigateTo({
  702. url: '/pages/goods/goods?id=' + id,
  703. })
  704. },
  705. gotoGoodsDOM(event) {
  706. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  707. },
  708. gotoGoodsSocket(data) {
  709. this.gotoGoods(data.data)
  710. },
  711. gotoGoods(id) {
  712. console.log('---', id)
  713. this.socketSendMessage('clientSyncAction', {
  714. type: 'openTag',
  715. data: id
  716. })
  717. this.setData({
  718. showCommodity: false
  719. })
  720. this.joinUrl()
  721. },
  722. addCard(event) {
  723. wx.navigateTo({
  724. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  725. })
  726. },
  727. buyGoods(event) {
  728. wx.navigateTo({
  729. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  730. })
  731. },
  732. showCommodityFn() {
  733. this.setData({
  734. showCommodity: true,
  735. showContact: false,
  736. showCoupon: false
  737. })
  738. this.joinUrl()
  739. },
  740. hideComodity() {
  741. this.setData({
  742. showCommodity: false
  743. })
  744. this.joinUrl()
  745. },
  746. hideCoupon() {
  747. this.setData({
  748. showCoupon: !this.data.showCoupon
  749. })
  750. },
  751. async receive(ev) {
  752. let item = ev.target.dataset.item
  753. try {
  754. // wx.showToast({
  755. // title: '领取优惠卷',
  756. // })
  757. // return;
  758. if (item.hasReceived || item.number <= item.receiveNumber) return;
  759. let res = await util.request(api.CouponExchange, {
  760. couponId: item.id
  761. })
  762. if (res.code === 0) {
  763. wx.showToast({
  764. title: '已成功领取',
  765. success: () => {
  766. this.setData({
  767. showCoupon: false
  768. })
  769. wx.nextTick(() => {
  770. this.setData({
  771. coupons: this.data.coupons.map(citem => {
  772. return {
  773. ...citem,
  774. hasReceived: citem.id === item.id ? true : citem.hasReceived
  775. }
  776. }),
  777. showCoupon: true
  778. })
  779. })
  780. }
  781. })
  782. } else if (res.errno === 401) {
  783. getApp().setLoginProps(false)
  784. } else {
  785. wx.showToast({
  786. title: res.msg,
  787. })
  788. }
  789. } catch (e) {
  790. console.error(e)
  791. wx.showToast({
  792. icon: 'none',
  793. title: '领取失败',
  794. })
  795. }
  796. },
  797. async getCouponList(id) {
  798. const success = (res) => {
  799. this.setData({
  800. coupons: res.data.list.map(item => {
  801. item.typeMoney = item.typeMoney.toString()
  802. item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
  803. item.typeMoney.length === 4 ? '70rpx' : '130rpx'
  804. return item
  805. })
  806. })
  807. this.loadConponSuccess = true
  808. this.readySendCouponCtrl()
  809. }
  810. let res = await util.request(api.BrandCouponList, {
  811. brandId: id,
  812. pageNum: 1,
  813. pageSize: 10000
  814. }, 'GET')
  815. console.log(res)
  816. if (res.code === 0) {
  817. success(res)
  818. } else {
  819. let res = await util.request(api.UNBrandCouponList, {
  820. brandId: id,
  821. pageNum: 1,
  822. pageSize: 10000
  823. }, 'GET')
  824. success(res)
  825. }
  826. },
  827. ready() {
  828. this.wssSuccess = true
  829. this.readySendCouponCtrl()
  830. },
  831. readySendCouponCtrl() {
  832. if (this.wssSuccess && this.loadConponSuccess) {
  833. this.loadConponSuccess = false
  834. this.socketSendMessage('clientSyncAction', {
  835. type: 'showCoupon',
  836. data: this.data.coupons.length > 0
  837. })
  838. }
  839. },
  840. getBrand: function (id, code) {
  841. this.getGoodsCount(code, id)
  842. return;
  843. let that = this;
  844. util.request(api.SueneCategory, {
  845. sceneNum: code
  846. }, 'GET').then(function (res) {
  847. if (res.code === 0) {
  848. const comtypes = res.list.map(item => {
  849. item.width = (item.name.length + (item.num.toString().length / 2) + 2) * 16
  850. return {
  851. ...item
  852. }
  853. })
  854. that.setData({
  855. comWidth: comtypes.reduce((a, b) => a + b.width + 10, 0),
  856. comtypes,
  857. thumComtypes: (!isIos && comtypes.length > 3) ? comtypes.slice(0, 3) : null,
  858. currTypeId: comtypes.length > 0 && comtypes[0].category_id
  859. });
  860. wx.showToast({
  861. title: 'currTypeId' + that.data.currTypeId.length,
  862. })
  863. that.data.currTypeId && that.getGoodsList(id, that.data.currTypeId);
  864. }
  865. });
  866. },
  867. getGoodsCount(code, id) {
  868. util.request(api.GoodsNumCount, {
  869. isDelete: 0,
  870. isOnSale: 1,
  871. brandId: id
  872. }, 'GET')
  873. .then(res => {
  874. if (res.code === 0) {
  875. this.setData({
  876. goodsCount: res.data
  877. })
  878. }
  879. this.getCouponList(id)
  880. })
  881. },
  882. getGoodsList(id, category_id) {
  883. var that = this;
  884. if (!(this.data.navList && this.data.navList.length)) {
  885. that.navDatas = {}
  886. let navDatas = this.data.navList = this.data.comtypes
  887. // util.request(api.GoodsCategory, { id: category_id })
  888. // .then(function (res) {
  889. // if (res.errno == 0) {
  890. // let navDatas = res.data.brotherCategory
  891. // that.setData({
  892. // navList: navDatas,
  893. // currTypeId: category_id
  894. // });
  895. that.navDatas = {}
  896. navDatas.forEach(item => {
  897. util.request(api.GoodsList, {
  898. brandId: id,
  899. categoryId: item.category_id,
  900. page: that.data.page,
  901. size: that.data.size
  902. })
  903. .then(res => {
  904. if (res.errno === 0) {
  905. that.navDatas[item.category_id] = res.data.goodsList
  906. }
  907. })
  908. })
  909. // }
  910. // })
  911. }
  912. if (that.navDatas[category_id]) {
  913. if (!isIos) {
  914. let showCommodity = that.data.showCommodity
  915. that.setData({
  916. showCommodity: false
  917. })
  918. setTimeout(() => {
  919. wx.nextTick(() => {
  920. that.setData({
  921. goodsList: that.navDatas[category_id],
  922. currTypeId: category_id,
  923. showCommodity: showCommodity
  924. });
  925. })
  926. }, 500)
  927. } else {
  928. that.setData({
  929. goodsList: that.navDatas[category_id],
  930. currTypeId: category_id,
  931. });
  932. }
  933. } else {
  934. console.error('诱惑去啦')
  935. util.request(api.GoodsList, {
  936. brandId: id,
  937. categoryId: category_id,
  938. page: that.data.page,
  939. size: that.data.size
  940. })
  941. .then(function (res) {
  942. if (res.errno === 0) {
  943. that.setData({
  944. goodsList: res.data.goodsList,
  945. currTypeId: category_id
  946. });
  947. // this.data.navList
  948. }
  949. });
  950. }
  951. },
  952. getBrandDetail: function (id, type, cb) {
  953. util.request(api.BrandDetail, {
  954. id: id,
  955. type: type,
  956. }).then((res) => {
  957. let base = res.data.brand.sceneUrl
  958. // let base = 'http://192.168.0.112:8080/shop.html?m=t-7Uqj9Fq&origin=fashilong'
  959. if (res.errno === 0) {
  960. let url = base + "&sid=" + id
  961. this.setData({
  962. id: id,
  963. newPicUrl: res.data.brand.appListPicUrl,
  964. sceneNum: res.data.brand.sceneNum,
  965. canShow: res.data.brand.canShow,
  966. contractPhone: res.data.brand.contractPhone
  967. })
  968. if (this.data.many === void 0) {
  969. this.data.many = !!res.data.brand.canShow
  970. }
  971. this.setData({
  972. // peopleCount: this.data.many ? manyCount : 5,
  973. peopleCount: this.data.many ? manyCount : 2,
  974. })
  975. if (!res.data.brand.canShow) {
  976. this.role = 'customer'
  977. } else if (!this.options.join) {
  978. this.role = 'leader'
  979. }
  980. cb(url, urlToJson(url).m, )
  981. }
  982. });
  983. },
  984. selectType(ev) {
  985. this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
  986. },
  987. hideCS() {
  988. this.setData({
  989. showCommodity: false,
  990. showCoupon: false,
  991. showContact: false
  992. })
  993. },
  994. hideContact() {
  995. this.setData({
  996. showContact: false
  997. })
  998. },
  999. calcShare() {
  1000. // this.exit()
  1001. this.setData({
  1002. sendShare: false
  1003. })
  1004. },
  1005. contactKf() {
  1006. let keys = Object.keys(this.navDatas)
  1007. let goodsId = this.navDatas[keys[0]][0].id
  1008. let user = wx.getStorageSync('userinfoDetail')
  1009. util.request(api.AddTalkCount, {
  1010. goodsId,
  1011. viewId: user && user.userId || '',
  1012. sceneNum: this.data.sceneNum
  1013. }, 'get')
  1014. this.hideAlert && this.hideAlert()
  1015. this.hideContact && this.hideContact()
  1016. },
  1017. onHide() {
  1018. this.socketSendMessage('changeOnlineStatus', {
  1019. status: false
  1020. })
  1021. this.pauseVideo = true
  1022. this.joinUrl()
  1023. }
  1024. }