socket.js 27 KB

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