socket.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // const io = require('./socket.io-mp');
  2. const {
  3. io
  4. } = require('./socket.io-v4')
  5. var user = require('./services/user.js');
  6. const api = require('/config/api.js');
  7. const util = require('/utils/util.js');
  8. const UNLOGIN = 'NO_LOGIN'
  9. const btoa = require('./utils/btoa')
  10. const manyCount = 50
  11. import remote from './config.js'
  12. var app = getApp();
  13. var isIos = false
  14. wx.getSystemInfo({
  15. success: function (res) {
  16. isIos = res.platform == "ios"
  17. }
  18. })
  19. let urlToJson = (url = window.location.href) => { // 箭头函数默认传值为当前页面url
  20. let obj = {},
  21. index = url.indexOf('?'), // 看url有没有参数
  22. params = url.substr(index + 1); // 截取url参数部分 id = 1 & type = 2
  23. if (index != -1) { // 有参数时
  24. let parr = params.split('&'); // 将参数分割成数组 ["id = 1 ", " type = 2"]
  25. for (let i of parr) { // 遍历数组
  26. let arr = i.split('='); // 1) i id = 1 arr = [id, 1] 2)i type = 2 arr = [type, 2]
  27. obj[arr[0]] = arr[1]; // obj[arr[0]] = id, obj.id = 1 obj[arr[0]] = type, obj.type = 2
  28. }
  29. }
  30. return obj;
  31. }
  32. export default {
  33. joinUrl() {
  34. let options = {
  35. API_BASE_URL: api.API_BASE_URL,
  36. "url": this.data.url,
  37. // "url": 'http://192.168.0.112:8080',
  38. "reload": this.data.reload,
  39. "token": wx.getStorageSync('token'),
  40. "code": this.mcode,
  41. "brandId": this.options.id,
  42. "open": this.data.showCommodity,
  43. "pauseVideo": this.pauseVideo,
  44. "bottom": this.data.bottom || 0,
  45. socket: {
  46. socketHost: remote.socketHost,
  47. path: '/fsl-node',
  48. options: {
  49. ...this.data.socketOptions,
  50. // nickname: encodeURI(encodeURI(this.data.socketOptions.nickname))
  51. nickname: encodeURIComponent(encodeURIComponent(this.data.socketOptions.nickname))
  52. }
  53. }
  54. }
  55. // let base = 'http://127.0.0.1:5500/index.html'
  56. // let base = remote.viewHost + '/shop-container/shop.html'
  57. let sponsor = !!this.data.canShow
  58. if (this.data.join && !this.options.join) {
  59. sponsor = false
  60. }
  61. // remote.viewHost
  62. let hostUrl
  63. if (options.url.indexOf('www.4dkankan.com') != -1) {
  64. hostUrl = 'https://www.4dkankan.com/shop-container-zfb/'
  65. } else if (options.url.indexOf('test.4dkankan.com') != -1) {
  66. hostUrl = 'https://test.4dkankan.com/shop-container-zfb/'
  67. } else {
  68. // hostUrl = 'https://zfb.4dkankan.com/shop-container/'
  69. hostUrl = remote.viewHost + '/shop-container-v4/';
  70. // hostUrl = remote.viewHost + '/test-gemer/';
  71. }
  72. // let base = remote.viewHost + '/shop-container/fashilong.html?env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
  73. let base = hostUrl + 'fashilong.html?time=' + Date.now() + '&env=' + remote.env + '&sponsor=' + sponsor + '&many=' + this.data.many
  74. // let base = remote.viewHost + '/shop.html'
  75. this.data.reload = false
  76. this.data.showCommodity = false
  77. // options.url = options.url+'&vlog';
  78. if (!this.data.webviewUrl) {
  79. console.log(base)
  80. this.setData({
  81. 'webviewUrl': base + '#' + JSON.stringify(options)
  82. })
  83. } else {
  84. this.socketSendMessage('clientSyncAction', {
  85. sender: 'h5',
  86. type: 'hashChange',
  87. data: options
  88. })
  89. }
  90. },
  91. onShow() {
  92. this.setData({
  93. isIos,
  94. showComtypesAllTab: false
  95. })
  96. if (this.socketSendMessage) {
  97. this.pauseVideo = false
  98. this.joinUrl()
  99. this.socketSendMessage('changeOnlineStatus', {
  100. status: true
  101. })
  102. }
  103. },
  104. changeShowComtypesAllTab(ev) {
  105. this.setData({
  106. showCommodity: false
  107. })
  108. setTimeout(() => {
  109. this.setData({
  110. showComtypesAllTab: ev.currentTarget.dataset.show,
  111. showCommodity: true
  112. })
  113. }, 100)
  114. },
  115. async authorizeRecord() {
  116. let isAuth = await new Promise((r, j) => {
  117. wx.authorize({
  118. scope: 'scope.record',
  119. success: () => r(true),
  120. fail: () => r(false)
  121. })
  122. })
  123. if (isAuth) return true
  124. let res = await new Promise(r => {
  125. wx.showModal({
  126. title: '提示',
  127. content: '您未授权录音,说话功能将无法使用',
  128. showCancel: true,
  129. confirmText: "授权",
  130. confirmColor: "#52a2d8",
  131. success: res => r(res),
  132. fail: () => r(false)
  133. })
  134. })
  135. if (!res || res.cancel) return;
  136. isAuth = await new Promise((r) => {
  137. wx.openSetting({
  138. success: res => r(res.authSetting['scope.record']),
  139. fail: () => r(false)
  140. })
  141. })
  142. return isAuth
  143. },
  144. async agetUserInfo() {
  145. const res = await util.request(api.UserInfo)
  146. if (res.code === 401) {
  147. return {
  148. userId: UNLOGIN,
  149. avatar: ''
  150. }
  151. } else {
  152. const data = res.data
  153. data.region = data.city ? data.city.split(',') : []
  154. data.birthday = data.birthday || '1990-01-01'
  155. return data
  156. }
  157. },
  158. async getUserInfo() {
  159. let userInfo = wx.getStorageSync('userInfo');
  160. let token = wx.getStorageSync('token');
  161. if (userInfo && userInfo.userId && token) {
  162. let info = await this.agetUserInfo()
  163. return {
  164. ...userInfo,
  165. ...info,
  166. avatarUrl: info.avatar
  167. };
  168. } else {
  169. return {
  170. userId: UNLOGIN,
  171. avatar: ''
  172. }
  173. }
  174. // let detail
  175. // let isAuth = await new Promise((r, j) => {
  176. // wx.authorize({
  177. // scope: 'scope.userInfo',
  178. // success: () => r(true),
  179. // fail: () => r(false)
  180. // })
  181. // })
  182. // if (!isAuth) {
  183. // this.setData({userAuth: true})
  184. // detail = await new Promise(r => {
  185. // this.bindGetUserInfo = (e) => {
  186. // if (e.detail.userInfo) {
  187. // this.setData({userAuth: false})
  188. // console.log('gei', e.detail)
  189. // r(e.detail)
  190. // }
  191. // }
  192. // })
  193. // } else {
  194. // detail = await new Promise(r => {
  195. // wx.getUserInfo({
  196. // success: res => r(res),
  197. // fail: () => r(false)
  198. // })
  199. // })
  200. // }
  201. // try {
  202. // let res = await user.loginByWeixin(detail)
  203. // app.globalData.userInfo = res.data.userInfo;
  204. // app.globalData.token = res.data.token;
  205. // return res.data.userInfo
  206. // } catch(e) {
  207. // return false
  208. // }
  209. },
  210. login() {
  211. getApp().setLoginProps(false)
  212. },
  213. async getSocketOptions(sceneId, roomId) {
  214. let room = roomId || (Number(Date.now().toString().slice(4)) + parseInt((Math.random() * 1000)))
  215. // let room = '147852'
  216. let userInfo = await this.getUserInfo()
  217. // console.log('---', userInfo)
  218. // this.setData({
  219. // userInfoa: userInfo.nickname.split('').join(' ')
  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. transport:['websocket'],
  260. query: {
  261. ...options,
  262. isClient: true,
  263. from: 2
  264. }
  265. })
  266. console.error('新建socket Room', options.roomId)
  267. this.setData({
  268. socketStatus: 0
  269. })
  270. socket.on('connect', () => this.setData({
  271. socketStatus: 1
  272. }))
  273. socket.on('connect_error', () => this.setData({
  274. socketStatus: -1
  275. }))
  276. socket.on('connect_timeout', () => this.setData({
  277. socketStatus: -1
  278. }))
  279. socket.on('disconnect', () => this.setData({
  280. socketStatus: -1
  281. }))
  282. socket.on('reconnect', () => {
  283. // wx.showToast({
  284. // title: '重连',
  285. // })
  286. this.setData({
  287. socketStatus: this.data.socketStatus
  288. })
  289. let noMute = getApp().globalData.voiceProps.noMute
  290. this.socketSendMessage('changeVoiceStatus', {
  291. status: noMute ? 0 : 2
  292. })
  293. this.socketSendMessage('changeOnlineStatus', {
  294. status: true
  295. })
  296. })
  297. socket.on('reconnect_failed', () => this.setData({
  298. socketStatus: -1
  299. }))
  300. socket.on('error', () => this.setData({
  301. socketStatus: -1
  302. }))
  303. socket.on('roomIn', config => {
  304. let enableTalk = config.roomsConfig.enableTalk !== false
  305. let noMute = getApp().globalData.voiceProps.noMute
  306. getApp().globalData.voiceProps.force = enableTalk
  307. if (!enableTalk && !noMute) {
  308. if (this.role !== 'leader') {
  309. // this.mic()
  310. }
  311. }
  312. })
  313. this.socketSendMessage = (event, obj) => {
  314. console.error('发送 socket Room', options.roomId, event, obj)
  315. socket.emit(event, obj)
  316. }
  317. socket.on('clientSyncAction', (data) => {
  318. console.log('调用', data.type, '方法', data)
  319. if (this[data.type]) {
  320. this[data.type](data)
  321. } else if (data.type == 'wx-subscribe') {
  322. this.getUrlCode(data.data)
  323. } else {
  324. console.error('没有', data.type, '方法')
  325. }
  326. })
  327. socket.on('action', (data) => {
  328. if (data.type === 'navigateToGoods') {
  329. this.navigateToGoodsAction(data.data)
  330. }
  331. })
  332. socket.on('changeRoomEnableTalk', config => {
  333. if (this.role !== 'leader') {
  334. this.changeRoomEnableTalk(config)
  335. }
  336. })
  337. socket.on('startCall', this.startCall.bind(this))
  338. socket.on('stopCall', (data) => {
  339. console.log('on stopCall')
  340. this.stopCall(data)
  341. })
  342. socket.on('someOneInRoom', this.startCall.bind(this))
  343. socket.on('someOneLeaveRoom', (user, data) => {
  344. this.handleSomeOneLeave(user)
  345. })
  346. socket.on('roomClose', (data) => {
  347. console.log('on roomClose')
  348. this.stopCall(data)
  349. })
  350. this.socketStop = () => {
  351. socket.close()
  352. console.error('断开 并滞空 socket Room', options.roomId)
  353. this.setData({
  354. socketStatus: 2
  355. })
  356. socket = null
  357. }
  358. return options
  359. },
  360. getUrlCode(url) {
  361. this.socketSendMessage('clientSyncAction', {
  362. sender: 'wx',
  363. type: 'wx-subscribe-result',
  364. data: 3020
  365. })
  366. // wx.request({
  367. // url: url, //仅为示例,并非真实的接口地址
  368. // method: 'get',
  369. // success: (res) => {
  370. // let code = -1
  371. // if (typeof res.data.code != 'undefined') {
  372. // code = res.data.code
  373. // }
  374. // this.socketSendMessage('clientSyncAction', {
  375. // sender: 'wx',
  376. // type: 'wx-subscribe-result',
  377. // data: code
  378. // })
  379. // },
  380. // fail: (err) => {
  381. // console.log(err)
  382. // }
  383. // })
  384. },
  385. changeRoomEnableTalk(data) {
  386. console.log(data)
  387. let noMute = getApp().globalData.voiceProps.noMute
  388. getApp().globalData.voiceProps.force = data.enableTalk
  389. // noMute true 静音
  390. // enableTalk false 静音
  391. if (!!data.enableTalk === !!noMute) {
  392. this.mic()
  393. }
  394. },
  395. navigateToGoods({
  396. data
  397. }) {
  398. // wx.showToast({
  399. // title: JSON.stringify(data).substr(40)
  400. // })
  401. this.navigateToGoodsAction(data)
  402. },
  403. navigateToGoodsAction(id) {
  404. wx.navigateTo({
  405. url: '/pages/goods/goods?id=' + id,
  406. })
  407. },
  408. getUrl(url, socketOptions, isJoin) {
  409. url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
  410. if (isJoin) {
  411. url += '&role=' + this.role + '&shopping'
  412. } else {
  413. url += '&role=' + this.role
  414. }
  415. console.error(url)
  416. console.log(isJoin)
  417. return url
  418. },
  419. navigateToMiniProgram(data) {
  420. wx.showModal({
  421. title: '温馨提示',
  422. content: '即将跳到其他小程序,是否继续?',
  423. showCancel: true, //是否显示取消按钮
  424. cancelText: "取消", //默认是“取消”
  425. confirmText: "确定", //默认是“确定”
  426. success: function (res) {
  427. if (res.cancel) {
  428. //点击取消,wx.navigateBack
  429. } else {
  430. wx.navigateToMiniProgram(data.data)
  431. }
  432. },
  433. fail: function (res) {
  434. //接口调用失败的回调函数,wx.navigateBack
  435. },
  436. complete: function (res) {
  437. //接口调用结束的回调函数(调用成功、失败都会执行)
  438. },
  439. })
  440. },
  441. async startCall(data) {
  442. // if( this.role =='leader'){
  443. this.setData({
  444. shareStatus: 1
  445. })
  446. if (!data) return;
  447. this.setData({
  448. surplus: this.data.peopleCount - data.roomsPerson.length
  449. })
  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. const socketOptions = this.data.socketOptions
  464. getApp().globalData.roomId = socketOptions.roomId
  465. const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
  466. if (!user) {
  467. return
  468. }
  469. //屏蔽有人进来才开麦克风
  470. // if (data.roomsPerson.length <= 1) {
  471. // return
  472. // }
  473. user.noMute = getApp().globalData.voiceProps.noMute
  474. getApp().setVoiceProps({
  475. ...user,
  476. action: 'startCall'
  477. })
  478. // this.socketSendMessage('changeVoiceStatus', {
  479. // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  480. // })
  481. // }
  482. },
  483. stopCall() {
  484. console.error('stopCall')
  485. this.setData({
  486. shareStatus: 0
  487. })
  488. getApp().setVoiceProps({
  489. noMute: false,
  490. action: 'stopCall'
  491. })
  492. if (this.runManager) {
  493. // this.recorderManager.stop()
  494. this.runManager = false
  495. }
  496. },
  497. handleSomeOneLeave(data) {
  498. if (data.roomsPerson.length <= 1) {
  499. // this.stopCall()
  500. }
  501. this.setData({
  502. surplus: this.data.peopleCount - data.roomsPerson.length
  503. })
  504. },
  505. async newRoom(data) {
  506. if (data.roomId) return;
  507. this.stopCall()
  508. getApp().globalData.rtcParams = []
  509. getApp().globalData.pusher = ''
  510. if (this.data.join && !this.options.join) {
  511. wx.switchTab({
  512. url: '/pages/index/index',
  513. })
  514. return;
  515. }
  516. this.role = this.data.canShow ? 'leader' : 'customer'
  517. let options = await this.getSocketOptions(this.mcode)
  518. this.socketSendMessage('clientSyncAction', {
  519. type: 'newRoom',
  520. data: options
  521. })
  522. setTimeout(async () => {
  523. this.wssSuccess = false
  524. this.socketStop && this.socketStop()
  525. this.data.many = !!this.data.canShow
  526. this.setData({
  527. // peopleCount: this.data.many ? manyCount : 5
  528. peopleCount: this.data.many ? manyCount : 2
  529. })
  530. let base = this.base
  531. let socketOptions = await this.socketStart({
  532. options
  533. })
  534. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  535. this.base = base
  536. this.setData({
  537. url,
  538. socketOptions,
  539. })
  540. this.joinUrl()
  541. this.setData({
  542. socketOptions
  543. })
  544. this.loadConponSuccess = true
  545. this.readySendCouponCtrl()
  546. }, 300)
  547. },
  548. async exit() {
  549. this.stopCall()
  550. getApp().globalData.rtcParams = []
  551. getApp().globalData.pusher = ''
  552. this.socketStop && this.socketStop()
  553. this.role = 'leader'
  554. let base = this.base
  555. let socketOptions = await this.socketStart({
  556. sceneId: this.mcode
  557. })
  558. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  559. this.base = base
  560. wx.nextTick(() => {
  561. setTimeout(() => {
  562. this.setData({
  563. url,
  564. loadUrl: true,
  565. socketOptions,
  566. showCommodityCtrl: false,
  567. hideWebView: false,
  568. reload: true
  569. })
  570. this.joinUrl()
  571. }, 500)
  572. })
  573. },
  574. clearDebuger() {
  575. this.setData({
  576. debugerInfo: ''
  577. })
  578. },
  579. async mic() {
  580. let noMute = getApp().globalData.voiceProps.noMute
  581. // noMute true 静音
  582. // enableTalk false 静音
  583. // if (!!getApp().globalData.voiceProps.force === !!noMute)
  584. // return
  585. // if (!getApp().globalData.voiceProps.force && (!this.data.socketOptions.voiceStatus || noMute)) return;
  586. if (!this.data.socketOptions.voiceStatus) {
  587. let voiceStatus = await this.authorizeRecord()
  588. if (voiceStatus) {
  589. this.data.socketOptions.voiceStatus = 1
  590. noMute = false
  591. } else {
  592. noMute = true
  593. }
  594. } else {
  595. noMute = !noMute
  596. }
  597. getApp().globalData.voiceProps.noMute = noMute
  598. this.socketSendMessage('changeVoiceStatus', {
  599. status: noMute ? 0 : 2
  600. })
  601. getApp().setVoiceProps({
  602. noMute
  603. })
  604. wx.showToast({
  605. title: `已${noMute ? '关闭' : '开启'}麦克风`,
  606. })
  607. },
  608. callPhone() {
  609. wx.makePhoneCall({
  610. phoneNumber: this.data.contractPhone,
  611. })
  612. this.setData({
  613. showContact: false
  614. })
  615. },
  616. /**
  617. * 用户点击右上角分享
  618. */
  619. onShareAppMessage: function (res) {
  620. let {
  621. id,
  622. newPicUrl
  623. } = this.data
  624. if (res.from === 'button') {
  625. this.setData({
  626. sendShare: false
  627. })
  628. return {
  629. title: '【好友推荐】一起来云逛吧',
  630. imageUrl: newPicUrl,
  631. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=true&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`,
  632. }
  633. } else {
  634. return {
  635. imageUrl: newPicUrl,
  636. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=false`,
  637. }
  638. }
  639. },
  640. /**
  641. * 生命周期函数--监听页面卸载
  642. */
  643. onUnload: function () {
  644. console.log('on onUnload')
  645. this.socketSendMessage('stopCall', {})
  646. this.stopCall()
  647. this.socketStop()
  648. getApp().globalData.pusher = ''
  649. },
  650. cart(data) {
  651. this.setData({
  652. showCommodityCtrl: data.data
  653. })
  654. },
  655. share() {
  656. console.log('**********')
  657. // console.log(!!this.data.mamy)
  658. const companyName = `指房宝(杭州)科技有限公司`
  659. const vrLink = `/pages/webview/index`
  660. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  661. const shareImg = img_url
  662. this.count = this.count || 0
  663. if (this.data.many && this.data.shareStatus == 1) {
  664. //开启一起逛时候的分享
  665. 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}`)
  666. console.log(this.data.socketOptions)
  667. wx.navigateTo({
  668. 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}`,
  669. })
  670. } else {
  671. 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}`);
  672. wx.navigateTo({
  673. 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}`,
  674. })
  675. }
  676. },
  677. back(data) {
  678. if (data.sender !== 'h5') return;
  679. wx.switchTab({
  680. url: '/pages/index/index'
  681. })
  682. this.setData({
  683. showCommodityCtrl: false
  684. })
  685. },
  686. service() {
  687. this.setData({
  688. showContact: true,
  689. showCommodity: false,
  690. showCoupon: false
  691. })
  692. },
  693. invite(data) {
  694. if (data.sender !== 'h5') return;
  695. this.setData({
  696. sendShare: true,
  697. count: ++this.data.count
  698. })
  699. },
  700. coupon(data) {
  701. if (data.sender !== 'h5') return;
  702. this.setData({
  703. showContact: false,
  704. showCommodity: false,
  705. showCoupon: true
  706. })
  707. },
  708. liveGotoGood(ev) {
  709. let id = ev.currentTarget.dataset.item.goodsId
  710. wx.navigateTo({
  711. url: '/pages/goods/goods?id=' + id,
  712. })
  713. },
  714. gotoGoodsDOM(event) {
  715. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  716. },
  717. gotoGoodsSocket(data) {
  718. this.gotoGoods(data.data)
  719. },
  720. gotoGoods(id) {
  721. console.log('---', id)
  722. this.socketSendMessage('clientSyncAction', {
  723. type: 'openTag',
  724. data: id
  725. })
  726. this.setData({
  727. showCommodity: false
  728. })
  729. this.joinUrl()
  730. },
  731. addCard(event) {
  732. wx.navigateTo({
  733. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  734. })
  735. },
  736. buyGoods(event) {
  737. wx.navigateTo({
  738. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  739. })
  740. },
  741. showCommodityFn() {
  742. this.setData({
  743. showCommodity: true,
  744. showContact: false,
  745. showCoupon: false
  746. })
  747. this.joinUrl()
  748. },
  749. hideComodity() {
  750. this.setData({
  751. showCommodity: false
  752. })
  753. this.joinUrl()
  754. },
  755. hideCoupon() {
  756. this.setData({
  757. showCoupon: !this.data.showCoupon
  758. })
  759. },
  760. async receive(ev) {
  761. let item = ev.target.dataset.item
  762. try {
  763. // wx.showToast({
  764. // title: '领取优惠卷',
  765. // })
  766. // return;
  767. if (item.hasReceived || item.number <= item.receiveNumber) return;
  768. let res = await util.request(api.CouponExchange, {
  769. couponId: item.id
  770. })
  771. if (res.code === 0) {
  772. wx.showToast({
  773. title: '已成功领取',
  774. success: () => {
  775. this.setData({
  776. showCoupon: false
  777. })
  778. wx.nextTick(() => {
  779. this.setData({
  780. coupons: this.data.coupons.map(citem => {
  781. return {
  782. ...citem,
  783. hasReceived: citem.id === item.id ? true : citem.hasReceived
  784. }
  785. }),
  786. showCoupon: true
  787. })
  788. })
  789. }
  790. })
  791. } else if (res.errno === 401) {
  792. getApp().setLoginProps(false)
  793. } else {
  794. wx.showToast({
  795. title: res.msg,
  796. })
  797. }
  798. } catch (e) {
  799. console.error(e)
  800. wx.showToast({
  801. icon: 'none',
  802. title: '领取失败',
  803. })
  804. }
  805. },
  806. async getCouponList(id) {
  807. const success = (res) => {
  808. const data = res.data || res.message
  809. this.setData({
  810. coupons: 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: this.data.many ? manyCount : 2,
  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. }