socket.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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. // wx.showToast({
  276. // title: '重连',
  277. // })
  278. this.setData({
  279. socketStatus: this.data.socketStatus
  280. })
  281. let noMute = getApp().globalData.voiceProps.noMute
  282. this.socketSendMessage('changeVoiceStatus', {
  283. status: noMute ? 0 : 2
  284. })
  285. this.socketSendMessage('changeOnlineStatus', {
  286. status: true
  287. })
  288. })
  289. socket.on('reconnect_failed', () => this.setData({
  290. socketStatus: -1
  291. }))
  292. socket.on('error', () => this.setData({
  293. socketStatus: -1
  294. }))
  295. socket.on('roomIn', config => {
  296. let enableTalk = config.roomsConfig.enableTalk !== false
  297. let noMute = getApp().globalData.voiceProps.noMute
  298. getApp().globalData.voiceProps.force = enableTalk
  299. if (!enableTalk && !noMute) {
  300. this.mic()
  301. }
  302. })
  303. this.socketSendMessage = (event, obj) => {
  304. console.error('发送 socket Room', options.roomId, event, obj)
  305. socket.emit(event, obj)
  306. }
  307. socket.on('clientSyncAction', (data) => {
  308. console.log('调用', data.type, '方法', data)
  309. if (this[data.type]) {
  310. this[data.type](data)
  311. } else if (data.type == 'wx-subscribe') {
  312. this.getUrlCode(data.data)
  313. } else {
  314. console.error('没有', data.type, '方法')
  315. }
  316. })
  317. socket.on('action', (data) => {
  318. if (data.type === 'navigateToGoods') {
  319. this.navigateToGoodsAction(data.data)
  320. }
  321. })
  322. socket.on('changeRoomEnableTalk', config => {
  323. if (this.role !== 'leader') {
  324. this.changeRoomEnableTalk(config)
  325. }
  326. })
  327. socket.on('startCall', this.startCall.bind(this))
  328. socket.on('stopCall', (data) => {
  329. console.log('on stopCall')
  330. this.stopCall(data)
  331. })
  332. socket.on('someOneInRoom', this.startCall.bind(this))
  333. socket.on('someOneLeaveRoom', (user, data) => {
  334. this.handleSomeOneLeave(user)
  335. })
  336. socket.on('roomClose', (data) => {
  337. console.log('on roomClose')
  338. this.stopCall(data)
  339. })
  340. this.socketStop = () => {
  341. socket.close()
  342. console.error('断开 并滞空 socket Room', options.roomId)
  343. this.setData({
  344. socketStatus: 2
  345. })
  346. socket = null
  347. }
  348. return options
  349. },
  350. getUrlCode(url) {
  351. this.socketSendMessage('clientSyncAction', {
  352. sender: 'wx',
  353. type: 'wx-subscribe-result',
  354. data: 3020
  355. })
  356. // wx.request({
  357. // url: url, //仅为示例,并非真实的接口地址
  358. // method: 'get',
  359. // success: (res) => {
  360. // let code = -1
  361. // if (typeof res.data.code != 'undefined') {
  362. // code = res.data.code
  363. // }
  364. // this.socketSendMessage('clientSyncAction', {
  365. // sender: 'wx',
  366. // type: 'wx-subscribe-result',
  367. // data: code
  368. // })
  369. // },
  370. // fail: (err) => {
  371. // console.log(err)
  372. // }
  373. // })
  374. },
  375. changeRoomEnableTalk(data) {
  376. console.log(data)
  377. let noMute = getApp().globalData.voiceProps.noMute
  378. getApp().globalData.voiceProps.force = data.enableTalk
  379. // noMute true 静音
  380. // enableTalk false 静音
  381. if (!!data.enableTalk === !!noMute) {
  382. this.mic()
  383. }
  384. },
  385. navigateToGoods({
  386. data
  387. }) {
  388. // wx.showToast({
  389. // title: JSON.stringify(data).substr(40)
  390. // })
  391. this.navigateToGoodsAction(data)
  392. },
  393. navigateToGoodsAction(id) {
  394. wx.navigateTo({
  395. url: '/pages/goods/goods?id=' + id,
  396. })
  397. },
  398. getUrl(url, socketOptions, isJoin) {
  399. url += '&room_id=' + socketOptions.roomId + '&user_id=' + socketOptions.userId + '&origin=fashilong'
  400. if (isJoin) {
  401. url += '&role=' + this.role + '&shopping'
  402. } else {
  403. url += '&role=' + this.role
  404. }
  405. console.error(url)
  406. console.log(isJoin)
  407. return url
  408. },
  409. navigateToMiniProgram(data) {
  410. wx.showModal({
  411. title: '温馨提示',
  412. content: '即将跳到其他小程序,是否继续?',
  413. showCancel: true, //是否显示取消按钮
  414. cancelText: "取消", //默认是“取消”
  415. confirmText: "确定", //默认是“确定”
  416. success: function (res) {
  417. if (res.cancel) {
  418. //点击取消,wx.navigateBack
  419. } else {
  420. wx.navigateToMiniProgram(data.data)
  421. }
  422. },
  423. fail: function (res) {
  424. //接口调用失败的回调函数,wx.navigateBack
  425. },
  426. complete: function (res) {
  427. //接口调用结束的回调函数(调用成功、失败都会执行)
  428. },
  429. })
  430. },
  431. async startCall(data) {
  432. this.setData({
  433. shareStatus: 1
  434. })
  435. if (!data) return;
  436. this.setData({
  437. surplus: this.data.peopleCount - data.roomsPerson.length
  438. })
  439. if (!this.isAuthorizeRecord) {
  440. const voiceStatus = Number(await this.authorizeRecord())
  441. this.isAuthorizeRecord = true
  442. // getApp().setVoiceProps({
  443. // noMute: !voiceStatus
  444. // })
  445. // console.log(getApp().globalData.voiceProps.noMute)
  446. // this.socketSendMessage('changeVoiceStatus', {
  447. // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  448. // })
  449. // this.data.socketOptions.voiceStatus = 1
  450. // this.socketSendMessage('changeVoiceStatus', {status: noMute ? 0 : 2})
  451. }
  452. const socketOptions = this.data.socketOptions
  453. getApp().globalData.roomId = socketOptions.roomId
  454. const user = data.roomsPerson.find(user => user.userId == socketOptions.userId)
  455. if (!user) {
  456. return
  457. }
  458. if (data.roomsPerson.length <= 1) {
  459. return
  460. }
  461. user.noMute = getApp().globalData.voiceProps.noMute
  462. getApp().setVoiceProps({
  463. ...user,
  464. action: 'startCall'
  465. })
  466. // this.socketSendMessage('changeVoiceStatus', {
  467. // status: getApp().globalData.voiceProps.noMute ? 0 : 2
  468. // })
  469. },
  470. stopCall() {
  471. console.error('stopCall')
  472. this.setData({
  473. shareStatus: 0
  474. })
  475. getApp().setVoiceProps({
  476. noMute: false,
  477. action: 'stopCall'
  478. })
  479. if (this.runManager) {
  480. // this.recorderManager.stop()
  481. this.runManager = false
  482. }
  483. },
  484. handleSomeOneLeave(data) {
  485. if (data.roomsPerson.length <= 1) {
  486. // this.stopCall()
  487. }
  488. },
  489. async newRoom(data) {
  490. if (data.roomId) return;
  491. this.stopCall()
  492. getApp().globalData.rtcParams = []
  493. getApp().globalData.pusher = ''
  494. if (this.data.join && !this.options.join) {
  495. wx.switchTab({
  496. url: '/pages/index/index',
  497. })
  498. return;
  499. }
  500. this.role = this.data.canShow ? 'leader' : 'customer'
  501. let options = await this.getSocketOptions(this.mcode)
  502. this.socketSendMessage('clientSyncAction', {
  503. type: 'newRoom',
  504. data: options
  505. })
  506. setTimeout(async () => {
  507. this.wssSuccess = false
  508. this.socketStop && this.socketStop()
  509. this.data.many = !!this.data.canShow
  510. this.setData({
  511. // peopleCount: this.data.many ? manyCount : 5
  512. peopleCount: this.data.many ? manyCount : 2
  513. })
  514. let base = this.base
  515. let socketOptions = await this.socketStart({
  516. options
  517. })
  518. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  519. this.base = base
  520. this.setData({
  521. url,
  522. socketOptions,
  523. })
  524. this.joinUrl()
  525. this.setData({
  526. socketOptions
  527. })
  528. this.loadConponSuccess = true
  529. this.readySendCouponCtrl()
  530. }, 300)
  531. },
  532. async exit() {
  533. this.stopCall()
  534. getApp().globalData.rtcParams = []
  535. getApp().globalData.pusher = ''
  536. this.socketStop && this.socketStop()
  537. this.role = 'leader'
  538. let base = this.base
  539. let socketOptions = await this.socketStart({
  540. sceneId: this.mcode
  541. })
  542. let url = this.getUrl(base, socketOptions, false) + (this.urlPj || '')
  543. this.base = base
  544. wx.nextTick(() => {
  545. setTimeout(() => {
  546. this.setData({
  547. url,
  548. loadUrl: true,
  549. socketOptions,
  550. showCommodityCtrl: false,
  551. hideWebView: false,
  552. reload: true
  553. })
  554. this.joinUrl()
  555. }, 500)
  556. })
  557. },
  558. clearDebuger() {
  559. this.setData({
  560. debugerInfo: ''
  561. })
  562. },
  563. async mic() {
  564. let noMute = getApp().globalData.voiceProps.noMute
  565. // noMute true 静音
  566. // enableTalk false 静音
  567. // if (!!getApp().globalData.voiceProps.force === !!noMute)
  568. // return
  569. // if (!getApp().globalData.voiceProps.force && (!this.data.socketOptions.voiceStatus || noMute)) return;
  570. if (!this.data.socketOptions.voiceStatus) {
  571. let voiceStatus = await this.authorizeRecord()
  572. if (voiceStatus) {
  573. this.data.socketOptions.voiceStatus = 1
  574. noMute = false
  575. } else {
  576. noMute = true
  577. }
  578. } else {
  579. noMute = !noMute
  580. }
  581. getApp().globalData.voiceProps.noMute = noMute
  582. this.socketSendMessage('changeVoiceStatus', {
  583. status: noMute ? 0 : 2
  584. })
  585. getApp().setVoiceProps({
  586. noMute
  587. })
  588. wx.showToast({
  589. title: `已${noMute ? '关闭' : '开启'}麦克风`,
  590. })
  591. },
  592. callPhone() {
  593. wx.makePhoneCall({
  594. phoneNumber: this.data.contractPhone,
  595. })
  596. this.setData({
  597. showContact: false
  598. })
  599. },
  600. /**
  601. * 用户点击右上角分享
  602. */
  603. onShareAppMessage: function (res) {
  604. let {
  605. id,
  606. newPicUrl
  607. } = this.data
  608. if (res.from === 'button') {
  609. this.setData({
  610. sendShare: false
  611. })
  612. return {
  613. title: '【好友推荐】一起来云逛吧',
  614. imageUrl: newPicUrl,
  615. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=true&roomId=${this.data.socketOptions.roomId}&many=${!!this.data.many}`,
  616. }
  617. } else {
  618. return {
  619. imageUrl: newPicUrl,
  620. path: `/pages/webview/index?id=${id}&type=${this.data.type}&join=false`,
  621. }
  622. }
  623. },
  624. /**
  625. * 生命周期函数--监听页面卸载
  626. */
  627. onUnload: function () {
  628. console.log('on onUnload')
  629. this.socketSendMessage('stopCall', {})
  630. this.stopCall()
  631. this.socketStop()
  632. getApp().globalData.pusher = ''
  633. },
  634. cart(data) {
  635. this.setData({
  636. showCommodityCtrl: data.data
  637. })
  638. },
  639. share() {
  640. console.log('**********')
  641. // console.log(!!this.data.mamy)
  642. const companyName = `指房宝(杭州)科技有限公司`
  643. const vrLink = `/pages/webview/index`
  644. const img_url = this.data.newPicUrl || 'http://video.cgaii.com/new4dage/images/images/home_2_a.jpg'
  645. const shareImg = img_url
  646. this.count = this.count || 0
  647. if (this.data.many && this.data.shareStatus == 1) {
  648. //开启一起逛时候的分享
  649. 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}`)
  650. console.log(this.data.socketOptions)
  651. wx.navigateTo({
  652. 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}`,
  653. })
  654. } else {
  655. 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}`);
  656. wx.navigateTo({
  657. 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}`,
  658. })
  659. }
  660. },
  661. back(data) {
  662. if (data.sender !== 'h5') return;
  663. wx.switchTab({
  664. url: '/pages/index/index'
  665. })
  666. this.setData({
  667. showCommodityCtrl: false
  668. })
  669. },
  670. service() {
  671. this.setData({
  672. showContact: true,
  673. showCommodity: false,
  674. showCoupon: false
  675. })
  676. },
  677. invite(data) {
  678. if (data.sender !== 'h5') return;
  679. this.setData({
  680. sendShare: true,
  681. count: ++this.data.count
  682. })
  683. },
  684. coupon(data) {
  685. if (data.sender !== 'h5') return;
  686. this.setData({
  687. showContact: false,
  688. showCommodity: false,
  689. showCoupon: true
  690. })
  691. },
  692. liveGotoGood(ev) {
  693. let id = ev.currentTarget.dataset.item.goodsId
  694. wx.navigateTo({
  695. url: '/pages/goods/goods?id=' + id,
  696. })
  697. },
  698. gotoGoodsDOM(event) {
  699. this.gotoGoods(event.currentTarget.dataset.item.hotIdList[0])
  700. },
  701. gotoGoodsSocket(data) {
  702. this.gotoGoods(data.data)
  703. },
  704. gotoGoods(id) {
  705. console.log('---', id)
  706. this.socketSendMessage('clientSyncAction', {
  707. type: 'openTag',
  708. data: id
  709. })
  710. this.setData({
  711. showCommodity: false
  712. })
  713. this.joinUrl()
  714. },
  715. addCard(event) {
  716. wx.navigateTo({
  717. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=addCard',
  718. })
  719. },
  720. buyGoods(event) {
  721. wx.navigateTo({
  722. url: '/pages/goods/goods?id=' + event.currentTarget.dataset.id + '&oper=buyGoods',
  723. })
  724. },
  725. showCommodityFn() {
  726. this.setData({
  727. showCommodity: true,
  728. showContact: false,
  729. showCoupon: false
  730. })
  731. this.joinUrl()
  732. },
  733. hideComodity() {
  734. this.setData({
  735. showCommodity: false
  736. })
  737. this.joinUrl()
  738. },
  739. hideCoupon() {
  740. this.setData({
  741. showCoupon: !this.data.showCoupon
  742. })
  743. },
  744. async receive(ev) {
  745. let item = ev.target.dataset.item
  746. try {
  747. // wx.showToast({
  748. // title: '领取优惠卷',
  749. // })
  750. // return;
  751. if (item.hasReceived || item.number <= item.receiveNumber) return;
  752. let res = await util.request(api.CouponExchange, {
  753. couponId: item.id
  754. })
  755. if (res.code === 0) {
  756. wx.showToast({
  757. title: '已成功领取',
  758. success: () => {
  759. this.setData({
  760. showCoupon: false
  761. })
  762. wx.nextTick(() => {
  763. this.setData({
  764. coupons: this.data.coupons.map(citem => {
  765. return {
  766. ...citem,
  767. hasReceived: citem.id === item.id ? true : citem.hasReceived
  768. }
  769. }),
  770. showCoupon: true
  771. })
  772. })
  773. }
  774. })
  775. } else if (res.errno === 401) {
  776. getApp().setLoginProps(false)
  777. } else {
  778. wx.showToast({
  779. title: res.msg,
  780. })
  781. }
  782. } catch (e) {
  783. console.error(e)
  784. wx.showToast({
  785. icon: 'none',
  786. title: '领取失败',
  787. })
  788. }
  789. },
  790. async getCouponList(id) {
  791. const success = (res) => {
  792. this.setData({
  793. coupons: res.data.list.map(item => {
  794. item.typeMoney = item.typeMoney.toString()
  795. item.fontSize = item.typeMoney.length === 3 ? '90rpx' :
  796. item.typeMoney.length === 4 ? '70rpx' : '130rpx'
  797. return item
  798. })
  799. })
  800. this.loadConponSuccess = true
  801. this.readySendCouponCtrl()
  802. }
  803. let res = await util.request(api.BrandCouponList, {
  804. brandId: id,
  805. pageNum: 1,
  806. pageSize: 10000
  807. }, 'GET')
  808. console.log(res)
  809. if (res.code === 0) {
  810. success(res)
  811. } else {
  812. let res = await util.request(api.UNBrandCouponList, {
  813. brandId: id,
  814. pageNum: 1,
  815. pageSize: 10000
  816. }, 'GET')
  817. success(res)
  818. }
  819. },
  820. ready() {
  821. this.wssSuccess = true
  822. this.readySendCouponCtrl()
  823. },
  824. readySendCouponCtrl() {
  825. if (this.wssSuccess && this.loadConponSuccess) {
  826. this.loadConponSuccess = false
  827. this.socketSendMessage('clientSyncAction', {
  828. type: 'showCoupon',
  829. data: this.data.coupons.length > 0
  830. })
  831. }
  832. },
  833. getBrand: function (id, code) {
  834. this.getGoodsCount(code, id)
  835. return;
  836. let that = this;
  837. util.request(api.SueneCategory, {
  838. sceneNum: code
  839. }, 'GET').then(function (res) {
  840. if (res.code === 0) {
  841. const comtypes = res.list.map(item => {
  842. item.width = (item.name.length + (item.num.toString().length / 2) + 2) * 16
  843. return {
  844. ...item
  845. }
  846. })
  847. that.setData({
  848. comWidth: comtypes.reduce((a, b) => a + b.width + 10, 0),
  849. comtypes,
  850. thumComtypes: (!isIos && comtypes.length > 3) ? comtypes.slice(0, 3) : null,
  851. currTypeId: comtypes.length > 0 && comtypes[0].category_id
  852. });
  853. wx.showToast({
  854. title: 'currTypeId' + that.data.currTypeId.length,
  855. })
  856. that.data.currTypeId && that.getGoodsList(id, that.data.currTypeId);
  857. }
  858. });
  859. },
  860. getGoodsCount(code, id) {
  861. util.request(api.GoodsNumCount, {
  862. isDelete: 0,
  863. isOnSale: 1,
  864. brandId: id
  865. }, 'GET')
  866. .then(res => {
  867. if (res.code === 0) {
  868. this.setData({
  869. goodsCount: res.data
  870. })
  871. }
  872. this.getCouponList(id)
  873. })
  874. },
  875. getGoodsList(id, category_id) {
  876. var that = this;
  877. if (!(this.data.navList && this.data.navList.length)) {
  878. that.navDatas = {}
  879. let navDatas = this.data.navList = this.data.comtypes
  880. // util.request(api.GoodsCategory, { id: category_id })
  881. // .then(function (res) {
  882. // if (res.errno == 0) {
  883. // let navDatas = res.data.brotherCategory
  884. // that.setData({
  885. // navList: navDatas,
  886. // currTypeId: category_id
  887. // });
  888. that.navDatas = {}
  889. navDatas.forEach(item => {
  890. util.request(api.GoodsList, {
  891. brandId: id,
  892. categoryId: item.category_id,
  893. page: that.data.page,
  894. size: that.data.size
  895. })
  896. .then(res => {
  897. if (res.errno === 0) {
  898. that.navDatas[item.category_id] = res.data.goodsList
  899. }
  900. })
  901. })
  902. // }
  903. // })
  904. }
  905. if (that.navDatas[category_id]) {
  906. if (!isIos) {
  907. let showCommodity = that.data.showCommodity
  908. that.setData({
  909. showCommodity: false
  910. })
  911. setTimeout(() => {
  912. wx.nextTick(() => {
  913. that.setData({
  914. goodsList: that.navDatas[category_id],
  915. currTypeId: category_id,
  916. showCommodity: showCommodity
  917. });
  918. })
  919. }, 500)
  920. } else {
  921. that.setData({
  922. goodsList: that.navDatas[category_id],
  923. currTypeId: category_id,
  924. });
  925. }
  926. } else {
  927. console.error('诱惑去啦')
  928. util.request(api.GoodsList, {
  929. brandId: id,
  930. categoryId: category_id,
  931. page: that.data.page,
  932. size: that.data.size
  933. })
  934. .then(function (res) {
  935. if (res.errno === 0) {
  936. that.setData({
  937. goodsList: res.data.goodsList,
  938. currTypeId: category_id
  939. });
  940. // this.data.navList
  941. }
  942. });
  943. }
  944. },
  945. getBrandDetail: function (id, type, cb) {
  946. util.request(api.BrandDetail, {
  947. id: id,
  948. type: type,
  949. }).then((res) => {
  950. let base = res.data.brand.sceneUrl
  951. // let base = 'http://192.168.0.112:8080/shop.html?m=t-7Uqj9Fq&origin=fashilong'
  952. if (res.errno === 0) {
  953. let url = base + "&sid=" + id
  954. this.setData({
  955. id: id,
  956. newPicUrl: res.data.brand.appListPicUrl,
  957. sceneNum: res.data.brand.sceneNum,
  958. canShow: res.data.brand.canShow,
  959. contractPhone: res.data.brand.contractPhone
  960. })
  961. if (this.data.many === void 0) {
  962. this.data.many = !!res.data.brand.canShow
  963. }
  964. this.setData({
  965. // peopleCount: this.data.many ? manyCount : 5,
  966. peopleCount: this.data.many ? manyCount : 2,
  967. })
  968. if (!res.data.brand.canShow) {
  969. this.role = 'customer'
  970. } else if (!this.options.join) {
  971. this.role = 'leader'
  972. }
  973. cb(url, urlToJson(url).m, )
  974. }
  975. });
  976. },
  977. selectType(ev) {
  978. this.getGoodsList(this.options.id, ev.target.dataset.item.category_id)
  979. },
  980. hideCS() {
  981. this.setData({
  982. showCommodity: false,
  983. showCoupon: false,
  984. showContact: false
  985. })
  986. },
  987. hideContact() {
  988. this.setData({
  989. showContact: false
  990. })
  991. },
  992. calcShare() {
  993. // this.exit()
  994. this.setData({
  995. sendShare: false
  996. })
  997. },
  998. contactKf() {
  999. let keys = Object.keys(this.navDatas)
  1000. let goodsId = this.navDatas[keys[0]][0].id
  1001. let user = wx.getStorageSync('userinfoDetail')
  1002. util.request(api.AddTalkCount, {
  1003. goodsId,
  1004. viewId: user && user.userId || '',
  1005. sceneNum: this.data.sceneNum
  1006. }, 'get')
  1007. this.hideAlert && this.hideAlert()
  1008. this.hideContact && this.hideContact()
  1009. },
  1010. onHide() {
  1011. this.socketSendMessage('changeOnlineStatus', {
  1012. status: false
  1013. })
  1014. this.pauseVideo = true
  1015. this.joinUrl()
  1016. }
  1017. }