socket.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. console.log('**********')
  636. // console.log(!!this.data.mamy)
  637. const companyName = `杭州天门科技有限公司`
  638. const vrLink = `/pages/webview/index`
  639. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  640. const shareImg = img_url
  641. this.count = this.count || 0
  642. if (this.data.many && this.data.shareStatus == 1) {
  643. //开启一起逛时候的分享
  644. 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}`)
  645. console.log(this.data.socketOptions)
  646. wx.navigateTo({
  647. 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}`,
  648. })
  649. } else {
  650. 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}`);
  651. wx.navigateTo({
  652. 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}`,
  653. })
  654. }
  655. },
  656. back(data) {
  657. if (data.sender !== 'h5') return;
  658. wx.switchTab({
  659. url: '/pages/index/index'
  660. })
  661. this.setData({
  662. showCommodityCtrl: false
  663. })
  664. },
  665. service() {
  666. this.setData({
  667. showContact: true,
  668. showCommodity: false,
  669. showCoupon: false
  670. })
  671. },
  672. invite(data) {
  673. if (data.sender !== 'h5') return;
  674. this.setData({
  675. sendShare: true,
  676. count: ++this.data.count
  677. })
  678. },
  679. coupon(data) {
  680. if (data.sender !== 'h5') return;
  681. this.setData({
  682. showContact: false,
  683. showCommodity: false,
  684. showCoupon: true
  685. })
  686. },
  687. liveGotoGood(ev) {
  688. let id = ev.currentTarget.dataset.item.goodsId
  689. wx.navigateTo({
  690. url: '/pages/goods/goods?id=' + id,
  691. })
  692. },
  693. gotoGoodsDOM(event) {
  694. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  695. },
  696. gotoGoodsSocket(data) {
  697. this.gotoGoods(data.data)
  698. },
  699. gotoGoods(id) {
  700. console.log('---', id)
  701. this.socketSendMessage('clientSyncAction', {
  702. type: 'openTag',
  703. data: id
  704. })
  705. this.setData({
  706. showCommodity: false
  707. })
  708. this.joinUrl()
  709. },
  710. addCard(event) {
  711. wx.navigateTo({
  712. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  713. })
  714. },
  715. buyGoods(event) {
  716. wx.navigateTo({
  717. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  718. })
  719. },
  720. showCommodityFn() {
  721. this.setData({
  722. showCommodity: true,
  723. showContact: false,
  724. showCoupon: false
  725. })
  726. this.joinUrl()
  727. },
  728. hideComodity() {
  729. this.setData({
  730. showCommodity: false
  731. })
  732. this.joinUrl()
  733. },
  734. hideCoupon() {
  735. this.setData({
  736. showCoupon: !this.data.showCoupon
  737. })
  738. },
  739. async receive(ev) {
  740. let item = ev.target.dataset.item
  741. try {
  742. // wx.showToast({
  743. // title: '领取优惠卷',
  744. // })
  745. // return;
  746. if (item.hasReceived || item.number <= item.receiveNumber) return;
  747. let res = await util.request(api.CouponExchange, {
  748. couponId: item.id
  749. })
  750. if (res.code === 0) {
  751. wx.showToast({
  752. title: '已成功领取',
  753. success: () => {
  754. this.setData({
  755. showCoupon: false
  756. })
  757. wx.nextTick(() => {
  758. this.setData({
  759. coupons: this.data.coupons.map(citem => {
  760. return {
  761. ...citem,
  762. hasReceived: citem.id === item.id ? true : citem.hasReceived
  763. }
  764. }),
  765. showCoupon: true
  766. })
  767. })
  768. }
  769. })
  770. } else if (res.errno === 401) {
  771. getApp().setLoginProps(false)
  772. } else {
  773. wx.showToast({
  774. title: res.msg,
  775. })
  776. }
  777. } catch (e) {
  778. console.error(e)
  779. wx.showToast({
  780. icon: 'none',
  781. title: '领取失败',
  782. })
  783. }
  784. },
  785. async getCouponList(id) {
  786. const success = (res) => {
  787. this.setData({
  788. coupons: res.data.list.map(item => {
  789. item.typeMoney = item.typeMoney.toString()
  790. item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
  791. item.typeMoney.length === 4 ? '70rpx' : '130rpx'
  792. return item
  793. })
  794. })
  795. this.loadConponSuccess = true
  796. this.readySendCouponCtrl()
  797. }
  798. let res = await util.request(api.BrandCouponList, {
  799. brandId: id,
  800. pageNum: 1,
  801. pageSize: 10000
  802. }, 'GET')
  803. console.log(res)
  804. if (res.code === 0) {
  805. success(res)
  806. } else {
  807. let res = await util.request(api.UNBrandCouponList, {
  808. brandId: id,
  809. pageNum: 1,
  810. pageSize: 10000
  811. }, 'GET')
  812. success(res)
  813. }
  814. },
  815. ready() {
  816. this.wssSuccess = true
  817. this.readySendCouponCtrl()
  818. },
  819. readySendCouponCtrl() {
  820. if (this.wssSuccess && this.loadConponSuccess) {
  821. this.loadConponSuccess = false
  822. this.socketSendMessage('clientSyncAction', {
  823. type: 'showCoupon',
  824. data: this.data.coupons.length > 0
  825. })
  826. }
  827. },
  828. getBrand: function (id, code) {
  829. this.getGoodsCount(code, id)
  830. return;
  831. let that = this;
  832. util.request(api.SueneCategory, {
  833. sceneNum: code
  834. }, 'GET').then(function (res) {
  835. if (res.code === 0) {
  836. const comtypes = res.list.map(item => {
  837. item.width = (item.name.length + (item.num.toString().length / 2) + 2) * 16
  838. return {
  839. ...item
  840. }
  841. })
  842. that.setData({
  843. comWidth: comtypes.reduce((a, b) => a + b.width + 10, 0),
  844. comtypes,
  845. thumComtypes: (!isIos && comtypes.length > 3) ? comtypes.slice(0, 3) : null,
  846. currTypeId: comtypes.length > 0 && comtypes[0].category_id
  847. });
  848. wx.showToast({
  849. title: 'currTypeId' + that.data.currTypeId.length,
  850. })
  851. that.data.currTypeId && that.getGoodsList(id, that.data.currTypeId);
  852. }
  853. });
  854. },
  855. getGoodsCount(code, id) {
  856. util.request(api.GoodsNumCount, {
  857. isDelete: 0,
  858. isOnSale: 1,
  859. brandId: id
  860. }, 'GET')
  861. .then(res => {
  862. if (res.code === 0) {
  863. this.setData({
  864. goodsCount: res.data
  865. })
  866. }
  867. this.getCouponList(id)
  868. })
  869. },
  870. getGoodsList(id, category_id) {
  871. var that = this;
  872. if (!(this.data.navList && this.data.navList.length)) {
  873. that.navDatas = {}
  874. let navDatas = this.data.navList = this.data.comtypes
  875. // util.request(api.GoodsCategory, { id: category_id })
  876. // .then(function (res) {
  877. // if (res.errno == 0) {
  878. // let navDatas = res.data.brotherCategory
  879. // that.setData({
  880. // navList: navDatas,
  881. // currTypeId: category_id
  882. // });
  883. that.navDatas = {}
  884. navDatas.forEach(item => {
  885. util.request(api.GoodsList, {
  886. brandId: id,
  887. categoryId: item.category_id,
  888. page: that.data.page,
  889. size: that.data.size
  890. })
  891. .then(res => {
  892. if (res.errno === 0) {
  893. that.navDatas[item.category_id] = res.data.goodsList
  894. }
  895. })
  896. })
  897. // }
  898. // })
  899. }
  900. if (that.navDatas[category_id]) {
  901. if (!isIos) {
  902. let showCommodity = that.data.showCommodity
  903. that.setData({
  904. showCommodity: false
  905. })
  906. setTimeout(() => {
  907. wx.nextTick(() => {
  908. that.setData({
  909. goodsList: that.navDatas[category_id],
  910. currTypeId: category_id,
  911. showCommodity: showCommodity
  912. });
  913. })
  914. }, 500)
  915. } else {
  916. that.setData({
  917. goodsList: that.navDatas[category_id],
  918. currTypeId: category_id,
  919. });
  920. }
  921. } else {
  922. console.error('诱惑去啦')
  923. util.request(api.GoodsList, {
  924. brandId: id,
  925. categoryId: category_id,
  926. page: that.data.page,
  927. size: that.data.size
  928. })
  929. .then(function (res) {
  930. if (res.errno === 0) {
  931. that.setData({
  932. goodsList: res.data.goodsList,
  933. currTypeId: category_id
  934. });
  935. // this.data.navList
  936. }
  937. });
  938. }
  939. },
  940. getBrandDetail: function (id, type, cb) {
  941. util.request(api.BrandDetail, {
  942. id: id,
  943. type: type,
  944. }).then((res) => {
  945. let base = res.data.brand.sceneUrl
  946. // let base = 'http://192.168.0.112:8080/shop.html?m=t-7Uqj9Fq&origin=fashilong'
  947. if (res.errno === 0) {
  948. let url = base + "&sid=" + id
  949. this.setData({
  950. id: id,
  951. newPicUrl: res.data.brand.appListPicUrl,
  952. sceneNum: res.data.brand.sceneNum,
  953. canShow: res.data.brand.canShow,
  954. contractPhone: res.data.brand.contractPhone
  955. })
  956. if (this.data.many === void 0) {
  957. this.data.many = !!res.data.brand.canShow
  958. }
  959. this.setData({
  960. // peopleCount: this.data.many ? manyCount : 5,
  961. peopleCount: this.data.many ? manyCount : 2,
  962. })
  963. if (!res.data.brand.canShow) {
  964. this.role = 'customer'
  965. } else if (!this.options.join) {
  966. this.role = 'leader'
  967. }
  968. cb(url, urlToJson(url).m, )
  969. }
  970. });
  971. },
  972. selectType(ev) {
  973. this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
  974. },
  975. hideCS() {
  976. this.setData({
  977. showCommodity: false,
  978. showCoupon: false,
  979. showContact: false
  980. })
  981. },
  982. hideContact() {
  983. this.setData({
  984. showContact: false
  985. })
  986. },
  987. calcShare() {
  988. // this.exit()
  989. this.setData({
  990. sendShare: false
  991. })
  992. },
  993. contactKf() {
  994. let keys = Object.keys(this.navDatas)
  995. let goodsId = this.navDatas[keys[0]][0].id
  996. let user = wx.getStorageSync('userinfoDetail')
  997. util.request(api.AddTalkCount, {
  998. goodsId,
  999. viewId: user && user.userId || '',
  1000. sceneNum: this.data.sceneNum
  1001. }, 'get')
  1002. this.hideAlert && this.hideAlert()
  1003. this.hideContact && this.hideContact()
  1004. },
  1005. onHide() {
  1006. this.socketSendMessage('changeOnlineStatus', {
  1007. status: false
  1008. })
  1009. this.pauseVideo = true
  1010. this.joinUrl()
  1011. }
  1012. }