socket.js 29 KB

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