index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // pages/mine/mine.js
  2. const defaultAvatarUrl = "../../static/images/no-login.png"
  3. const homeApi_empower = "https://aipush.aidsleep.cn";
  4. import api from '../../utils/api';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. showAuthorizedDialog: false,
  11. showTipsA: false,
  12. showTipsB: false,
  13. show1:false,
  14. aid:null,
  15. hasAuth: false,
  16. seeAicoList: "/subpagesTwo/seeAisoList/seeAicoList",
  17. avatarUrl: defaultAvatarUrl,
  18. nickName: "用户登录",
  19. phoneNumber: "",
  20. latitude: null,
  21. longitude: null,
  22. menuCardList: [],
  23. },
  24. getLocation() {
  25. const that = this;
  26. var hasAuth = wx.getStorageSync("hasAuth");
  27. if (!hasAuth) {
  28. return
  29. }
  30. wx.getSetting({
  31. success(res) {
  32. if (res.authSetting['scope.userLocation']) {
  33. // 用户已经授权
  34. wx.getLocation({
  35. type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
  36. success(locationRes) {
  37. that.setData({
  38. latitude: locationRes.latitude,
  39. longitude: locationRes.longitude,
  40. });
  41. console.log('纬度:', locationRes.latitude);
  42. console.log('经度:', locationRes.longitude);
  43. that.savehotelnew();
  44. },
  45. fail(err) {
  46. that.setData({
  47. latitude: null,
  48. longitude: null,
  49. error: err.message
  50. });
  51. console.error('获取位置失败:', err.message);
  52. }
  53. });
  54. } else {
  55. // 用户未授权
  56. // wx.authorize({
  57. // scope: 'scope.userLocation',
  58. // success() {
  59. // // 用户同意授权后再次获取位置
  60. // that.savehotelnew();
  61. // },
  62. // fail() {
  63. // // 用户拒绝授权
  64. // that.setData({
  65. // latitude: null,
  66. // longitude: null,
  67. // error: '用户拒绝授权获取地理位置'
  68. // });
  69. // console.error('用户拒绝授权获取地理位置');
  70. // }
  71. // });
  72. }
  73. }
  74. });
  75. },
  76. savehotelnew() {
  77. const that = this;
  78. console.log({
  79. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  80. unionid: wx.getStorageSync('unionid'),
  81. longandlat: that.data.longitude + "|" + that.data.latitude
  82. });
  83. wx.request({
  84. url: 'https://aipush.aidsleep.cn/savehotelnew',
  85. method: 'POST',
  86. data: {
  87. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  88. unionid: wx.getStorageSync('unionid'),
  89. longandlat: that.data.longitude + "|" + that.data.latitude
  90. },
  91. success: (res) => {
  92. },
  93. fail: () => {
  94. }
  95. });
  96. },
  97. onCloseTipsDialog() {
  98. this.setData({
  99. showTipsA: false,
  100. showTipsB: false
  101. })
  102. },
  103. onMenuClick(e) {
  104. console.log(e.currentTarget.dataset);
  105. const { index } = e.currentTarget.dataset;
  106. var hasAuth = wx.getStorageSync("hasAuth");
  107. switch (index - 0) {
  108. case 0:
  109. // need Login
  110. if (hasAuth) {
  111. this.toPage(index);
  112. } else {
  113. this.showDialog();
  114. }
  115. break;
  116. default:
  117. this.toPage(index);
  118. break;
  119. }
  120. },
  121. bindAccount(){
  122. // 调用 /data/api.data/bind
  123. api.bind({
  124. params: { aid: this.data.aid ||10011},
  125. headers: { unionid: wx.getStorageSync("unionid")},
  126. })
  127. .then(data =>{
  128. if (data.code === 1) {
  129. wx.showToast({
  130. icon: 'none',
  131. title: data.info,
  132. });
  133. }else{
  134. wx.showToast({
  135. icon: 'none',
  136. title: data.info,
  137. });
  138. }
  139. this.isManager()
  140. })
  141. .catch(err => console.error('Bind Error:', err));
  142. },
  143. isManager(){
  144. // 调用 /data/api.data/isManager
  145. api.isManager({
  146. params: {},
  147. headers: { unionid: wx.getStorageSync("unionid")},
  148. })
  149. .then(data =>{
  150. if (data.code === 1) {
  151. wx.setStorageSync('isManager', data.data);
  152. }else{
  153. wx.setStorageSync('isManager', null);
  154. }
  155. this.initMenuCardList()
  156. })
  157. .catch(err => console.error('IsManager Error:', err));
  158. },
  159. toPage(index) {
  160. const type = this.data.menuList[index].type;
  161. const url = this.data.menuList[index].url;
  162. if (!url) {
  163. return
  164. }
  165. if (type == 'switchTab') {
  166. wx.switchTab({
  167. url: url
  168. })
  169. } else if (type == 'navigate') {
  170. wx.navigateTo({
  171. url: url
  172. })
  173. } else if (type == 'miniProgram') {
  174. //develop开发版;trial体验版;release正式版
  175. const appId = this.data.menuCardList[index].appId;
  176. wx.navigateToMiniProgram({
  177. appId: appId,
  178. path: url,
  179. extraData: {
  180. },
  181. envVersion: 'develop',
  182. success(res) {
  183. // 打开成功
  184. }
  185. })
  186. }
  187. },
  188. onMenuCardClick(e) {
  189. console.log(e.currentTarget.dataset);
  190. const { index } = e.currentTarget.dataset;
  191. var hasAuth = wx.getStorageSync("hasAuth");
  192. switch (index - 0) {
  193. case 0:
  194. case 1:
  195. case 2:
  196. case 3:
  197. case 4:
  198. // need Login
  199. if (hasAuth) {
  200. if (index == 0) {
  201. let that=this
  202. // 判断扫码控制权限
  203. wx.request({
  204. url: `${homeApi_empower}/wxstatus`,
  205. data: {
  206. unionid: wx.getStorageSync('unionid'),
  207. openid: wx.getStorageSync('openid'),
  208. token: wx.getStorageSync('token'),
  209. },
  210. method: 'POST',
  211. success: function (intervalRes) {
  212. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  213. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  214. wx.setStorageSync('hasHotelAuth', true);
  215. } else {
  216. wx.setStorageSync('hasHotelAuth', false);
  217. }
  218. try {
  219. wx.setStorageSync('hotelEmpower', intervalRes?.data?.hotel);
  220. wx.setStorageSync('roomEmpower', intervalRes?.data?.room);
  221. wx.setStorageSync('hname', intervalRes?.data?.hname);
  222. wx.setStorageSync('rname', intervalRes?.data?.rname);
  223. }catch ( e){
  224. console.log(e)
  225. }
  226. var hasHotelAuth = wx.getStorageSync("hasHotelAuth");
  227. if (!hasHotelAuth) {
  228. that.setData({
  229. showTipsB: true
  230. })
  231. return
  232. }
  233. that.toCardPage(index);
  234. },
  235. fail: function (error) {
  236. }
  237. });
  238. return;
  239. }
  240. if (index == 2) {
  241. var hasReportAuth = wx.getStorageSync("hasReportAuth");
  242. var unionid = wx.getStorageSync("unionid");
  243. if (!unionid) {
  244. this.setData({
  245. showTipsA: true
  246. })
  247. return
  248. }
  249. if (!hasReportAuth) {
  250. wx.showModal({
  251. title: '提示',
  252. content: '没有睡眠报告',
  253. showCancel: false
  254. });
  255. return
  256. }
  257. }
  258. this.toCardPage(index);
  259. } else {
  260. if(index == 1){
  261. this.toCardPage(index);
  262. break;
  263. }
  264. this.showDialog();
  265. }
  266. break;
  267. default:
  268. this.toCardPage(index);
  269. break;
  270. }
  271. },
  272. toCardPage(index) {
  273. const type = this.data.menuCardList[index].type;
  274. const url = this.data.menuCardList[index].url;
  275. if (!url) {
  276. return
  277. }
  278. if (type == 'switchTab') {
  279. wx.switchTab({
  280. url: url
  281. })
  282. } else if (type == 'navigate') {
  283. wx.navigateTo({
  284. url: url
  285. })
  286. } else if (type == 'miniProgram') {
  287. //develop开发版;trial体验版;release正式版
  288. const appId = this.data.menuCardList[index].appId;
  289. wx.navigateToMiniProgram({
  290. appId: appId,
  291. path: url,
  292. extraData: {
  293. },
  294. envVersion: 'develop',
  295. success(res) {
  296. // 打开成功
  297. }
  298. })
  299. }
  300. },
  301. toSubscriptions() {
  302. const tmplIds = 'k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw';
  303. // 这里是获取下发权限地方,根据官方文档,可以根据 wx.getSetting() 的 withSubscriptions 这个参数获取用户是否打开订阅消息总开关。后面我们需要获取用户是否同意总是同意消息推送。所以这里要给它设置为true 。
  304. wx.getSetting({
  305. withSubscriptions: true, // 这里设置为true,下面才会返回mainSwitch
  306. success: function (res) {
  307. // 调起授权界面弹窗
  308. if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
  309. if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  310. let moIdState = res.subscriptionsSetting.itemSettings[tmplIds]; // 用户同意的消息模板id
  311. if (moIdState === 'accept') {
  312. console.log('接受了消息推送');
  313. } else if (moIdState === 'reject') {
  314. console.log("拒绝消息推送");
  315. } else if (moIdState === 'ban') {
  316. console.log("已被后台封禁");
  317. }
  318. } else {
  319. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  320. wx.showModal({
  321. title: '提示',
  322. content: '请授权开通服务通知',
  323. showCancel: true,
  324. success: function (ress) {
  325. if (ress.confirm) {
  326. wx.requestSubscribeMessage({ // 调起消息订阅界面
  327. tmplIds: ['k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw', 'JSM4TwUMy5w5AHvdmJv7_NQ0h5FstGqtc12fqPOoPQU','rO9DCTKcSCxEIVp6GsV56hCLPwFKhvVytXGRkJxtTO4'],
  328. success(res) {
  329. console.log('订阅消息 成功 ');
  330. console.log(res);
  331. },
  332. fail(er) {
  333. console.log("订阅消息 失败 ");
  334. console.log(er);
  335. }
  336. })
  337. }
  338. }
  339. })
  340. }
  341. } else {
  342. console.log('订阅消息未开启')
  343. }
  344. },
  345. fail: function (error) {
  346. console.log(error);
  347. },
  348. })
  349. },
  350. /**
  351. * 生命周期函数--监听页面加载
  352. */
  353. onLoad(options) {
  354. wx.hideTabBar()
  355. if (options.aid){
  356. this.setData({
  357. aid:options.aid
  358. })
  359. }
  360. this.initMenuCardList()
  361. // this.toSubscriptions();
  362. },
  363. /**
  364. * 生命周期函数--监听页面初次渲染完成
  365. */
  366. onReady() {
  367. wx.hideTabBar()
  368. this.getLocation();
  369. },
  370. bindAccountPopup() {
  371. this.bindAccount()
  372. this.setData({
  373. show1: false,
  374. aid:null
  375. });
  376. },
  377. onClose() {
  378. this.setData({
  379. show1: false,
  380. });
  381. },
  382. /**
  383. * 生命周期函数--监听页面显示
  384. */
  385. onShow() {
  386. wx.hideTabBar()
  387. this.checkAuth()
  388. },
  389. showDialog() {
  390. var hasAuth = wx.getStorageSync("hasAuth");
  391. if (hasAuth) {
  392. return
  393. }
  394. this.setData({
  395. showAuthorizedDialog: true
  396. })
  397. },
  398. checkAuth() {
  399. var hasAuth = wx.getStorageSync("hasAuth");
  400. this.setData({
  401. hasAuth: hasAuth
  402. })
  403. if (hasAuth) {
  404. var userInfo = wx.getStorageSync("userInfo");
  405. this.setData({
  406. nickName: userInfo.nickName || "用户登录",
  407. avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  408. phoneNumber: userInfo.phoneNumber || ""
  409. })
  410. this.initMenuCardList()
  411. var storageSync = wx.getStorageSync("unionid");
  412. if (storageSync &&this.data.aid) {
  413. this.setData({
  414. show1: true,
  415. })
  416. }
  417. // 判断扫码控制权限
  418. wx.request({
  419. url: `${homeApi_empower}/wxstatus`,
  420. data: {
  421. unionid: wx.getStorageSync('unionid'),
  422. openid: wx.getStorageSync('openid'),
  423. token: wx.getStorageSync('token'),
  424. },
  425. method: 'POST',
  426. success: function (intervalRes) {
  427. console.log('intervalRes=55555555' + JSON.stringify(intervalRes));
  428. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  429. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  430. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  431. wx.setStorageSync('hasHotelAuth', true);
  432. } else {
  433. wx.setStorageSync('hasHotelAuth', false);
  434. }
  435. try {
  436. wx.setStorageSync('hotelEmpower', intervalRes?.data?.hotel);
  437. wx.setStorageSync('roomEmpower', intervalRes?.data?.room);
  438. wx.setStorageSync('hname', intervalRes?.data?.hname);
  439. wx.setStorageSync('rname', intervalRes?.data?.rname);
  440. }catch ( e){
  441. console.log(e)
  442. }
  443. },
  444. fail: function (error) {
  445. }
  446. });
  447. }
  448. return hasAuth;
  449. },
  450. initMenuCardList(){
  451. this.setData({menuCardList:[{
  452. icon: "../../static/index/icon/ic_fjkz.png",
  453. bgIcon: "../../static/index/bg/bg_fjkz.png",
  454. title: "房间控制",
  455. type: "navigate",
  456. color: "#3C84D9",
  457. bgColor: "#EEF4FF",
  458. url: "/subpages/main/main"
  459. }, {
  460. icon: "../../static/index/icon/ic_sm.png",
  461. bgIcon: "../../static/index/bg/bg_sm.png",
  462. title: "扫码",
  463. type: "navigate",
  464. color: "#10B09C",
  465. bgColor: "#E6FFFC",
  466. url: "/subpages/scan/scan"
  467. }, {
  468. icon: "../../static/index/icon/ic_jrsmbg.png",
  469. bgIcon: "../../static/index/bg/bg_jrsmbg.png",
  470. title: "最新睡眠报告",
  471. type: "navigate",
  472. color: "#8658B9",
  473. bgColor: "#F6F3FF",
  474. url: "/subpages/data/data"
  475. }, {
  476. icon: "../../static/index/icon/ic_lssmbg.png",
  477. bgIcon: "../../static/index/bg/bg_lssmbg.png",
  478. title: "历史睡眠报告",
  479. type: "navigate",
  480. color: "#BB6268",
  481. bgColor: "#FFF6F7",
  482. url: "/subpages/sleepReport/sleepReport"
  483. },
  484. /* {
  485. icon: "../../static/index/icon/ic_wdcd.png",
  486. bgIcon: "../../static/index/bg/bg_wdcd.png",
  487. title: wx.getStorageSync("isManager")?"巡检房间" : "我的床垫",
  488. type: "navigate",
  489. color: "#317A9B",
  490. bgColor: "#E6FFFC",
  491. url: wx.getStorageSync("isManager")?this.data.seeAicoList+'?hotel_id='+wx.getStorageSync("isManager")[0] : "/subpages/myMattress/myMattress"
  492. },*/
  493. {
  494. icon: "../../static/index/icon/ic_wdd.png",
  495. bgIcon: "../../static/index/bg/ic_wdd_bg.png",
  496. title: wx.getStorageSync("isManager")?"巡检房间" : "我的设备",
  497. type: "navigate",
  498. color: "#317A9B",
  499. bgColor: "#E6FFFC",
  500. url: wx.getStorageSync("isManager")?this.data.seeAicoList+'?hotel_id='+wx.getStorageSync("isManager")[0] : "/subpagesTwo/search/search"
  501. },
  502. {
  503. icon: "../../static/index/icon/ic_zxsc.png",
  504. bgIcon: "../../static/index/bg/bg_zxsc.png",
  505. title: "在线商城",
  506. type: "miniProgram",
  507. color: "#C2714B",
  508. bgColor: "#FFF8F4",
  509. appId: "wx63f79b3d61ba632b" || "gh_78da4ac5d0cc",
  510. url: "pages/home/dashboard/index"
  511. }],
  512. menuList: [{
  513. icon: "../../static/mine/dcwj.png",
  514. title: "健康检测",
  515. type: "navigate",
  516. url: "/subpages/testSAS/testSAS"
  517. }, {
  518. icon: "../../static/mine/zxkf.png",
  519. title: "在线客服",
  520. type: "contact",
  521. url: ""
  522. }, {
  523. icon: "../../static/mine/bzzx.png",
  524. title: "帮助中心",
  525. type: "navigate",
  526. url: "/subpages/helpList/helpList"
  527. }, {
  528. icon: "../../static/mine/gywm.png",
  529. title: "关于我们",
  530. type: "navigate",
  531. url: ""
  532. }]})
  533. },
  534. authorizationSuccessful() {
  535. console.log("授权成功");
  536. this.setData({ showAuthorizedDialog: false })
  537. this.checkAuth();
  538. },
  539. /**
  540. * 生命周期函数--监听页面隐藏
  541. */
  542. onHide() {
  543. },
  544. /**
  545. * 生命周期函数--监听页面卸载
  546. */
  547. onUnload() {
  548. },
  549. /**
  550. * 页面相关事件处理函数--监听用户下拉动作
  551. */
  552. onPullDownRefresh() {
  553. },
  554. /**
  555. * 页面上拉触底事件的处理函数
  556. */
  557. onReachBottom() {
  558. },
  559. /**
  560. * 用户点击右上角分享
  561. */
  562. onShareAppMessage() {
  563. }
  564. })