socket.js 29 KB

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