socket copy.js 29 KB

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