socket.js 28 KB

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