socket.js 29 KB

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