socket.js 26 KB

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