socket.js 27 KB

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