socket.js 29 KB

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