socket.js 29 KB

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