socket.js 28 KB

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