socket.js 26 KB

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