index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  216. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  217. wx.setStorageSync('hname', intervalRes.data.hname);
  218. wx.setStorageSync('rname', intervalRes.data.rname);
  219. } else {
  220. wx.setStorageSync('hasHotelAuth', false);
  221. }
  222. var hasHotelAuth = wx.getStorageSync("hasHotelAuth");
  223. if (!hasHotelAuth) {
  224. that.setData({
  225. showTipsB: true
  226. })
  227. return
  228. }
  229. that.toCardPage(index);
  230. },
  231. fail: function (error) {
  232. }
  233. });
  234. return;
  235. }
  236. if (index == 2) {
  237. var hasReportAuth = wx.getStorageSync("hasReportAuth");
  238. var unionid = wx.getStorageSync("unionid");
  239. if (!unionid) {
  240. this.setData({
  241. showTipsA: true
  242. })
  243. return
  244. }
  245. if (!hasReportAuth) {
  246. wx.showModal({
  247. title: '提示',
  248. content: '没有睡眠报告',
  249. showCancel: false
  250. });
  251. return
  252. }
  253. }
  254. this.toCardPage(index);
  255. } else {
  256. this.showDialog();
  257. }
  258. break;
  259. default:
  260. this.toCardPage(index);
  261. break;
  262. }
  263. },
  264. toCardPage(index) {
  265. const type = this.data.menuCardList[index].type;
  266. const url = this.data.menuCardList[index].url;
  267. if (!url) {
  268. return
  269. }
  270. if (type == 'switchTab') {
  271. wx.switchTab({
  272. url: url
  273. })
  274. } else if (type == 'navigate') {
  275. wx.navigateTo({
  276. url: url
  277. })
  278. } else if (type == 'miniProgram') {
  279. //develop开发版;trial体验版;release正式版
  280. const appId = this.data.menuCardList[index].appId;
  281. wx.navigateToMiniProgram({
  282. appId: appId,
  283. path: url,
  284. extraData: {
  285. },
  286. envVersion: 'develop',
  287. success(res) {
  288. // 打开成功
  289. }
  290. })
  291. }
  292. },
  293. toSubscriptions() {
  294. const tmplIds = 'k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw';
  295. // 这里是获取下发权限地方,根据官方文档,可以根据 wx.getSetting() 的 withSubscriptions 这个参数获取用户是否打开订阅消息总开关。后面我们需要获取用户是否同意总是同意消息推送。所以这里要给它设置为true 。
  296. wx.getSetting({
  297. withSubscriptions: true, // 这里设置为true,下面才会返回mainSwitch
  298. success: function (res) {
  299. // 调起授权界面弹窗
  300. if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
  301. if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  302. let moIdState = res.subscriptionsSetting.itemSettings[tmplIds]; // 用户同意的消息模板id
  303. if (moIdState === 'accept') {
  304. console.log('接受了消息推送');
  305. } else if (moIdState === 'reject') {
  306. console.log("拒绝消息推送");
  307. } else if (moIdState === 'ban') {
  308. console.log("已被后台封禁");
  309. }
  310. } else {
  311. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  312. wx.showModal({
  313. title: '提示',
  314. content: '请授权开通服务通知',
  315. showCancel: true,
  316. success: function (ress) {
  317. if (ress.confirm) {
  318. wx.requestSubscribeMessage({ // 调起消息订阅界面
  319. tmplIds: ['k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw', 'JSM4TwUMy5w5AHvdmJv7_NQ0h5FstGqtc12fqPOoPQU','rO9DCTKcSCxEIVp6GsV56hCLPwFKhvVytXGRkJxtTO4'],
  320. success(res) {
  321. console.log('订阅消息 成功 ');
  322. console.log(res);
  323. },
  324. fail(er) {
  325. console.log("订阅消息 失败 ");
  326. console.log(er);
  327. }
  328. })
  329. }
  330. }
  331. })
  332. }
  333. } else {
  334. console.log('订阅消息未开启')
  335. }
  336. },
  337. fail: function (error) {
  338. console.log(error);
  339. },
  340. })
  341. },
  342. /**
  343. * 生命周期函数--监听页面加载
  344. */
  345. onLoad(options) {
  346. wx.hideTabBar()
  347. if (options.aid){
  348. this.setData({
  349. aid:options.aid
  350. })
  351. }
  352. this.initMenuCardList()
  353. // this.toSubscriptions();
  354. },
  355. /**
  356. * 生命周期函数--监听页面初次渲染完成
  357. */
  358. onReady() {
  359. wx.hideTabBar()
  360. this.getLocation();
  361. },
  362. bindAccountPopup() {
  363. this.bindAccount()
  364. this.setData({
  365. show1: false,
  366. aid:null
  367. });
  368. },
  369. onClose() {
  370. this.setData({
  371. show1: false,
  372. });
  373. },
  374. /**
  375. * 生命周期函数--监听页面显示
  376. */
  377. onShow() {
  378. wx.hideTabBar()
  379. this.checkAuth()
  380. },
  381. showDialog() {
  382. var hasAuth = wx.getStorageSync("hasAuth");
  383. if (hasAuth) {
  384. return
  385. }
  386. this.setData({
  387. showAuthorizedDialog: true
  388. })
  389. },
  390. checkAuth() {
  391. var hasAuth = wx.getStorageSync("hasAuth");
  392. this.setData({
  393. hasAuth: hasAuth
  394. })
  395. if (hasAuth) {
  396. var userInfo = wx.getStorageSync("userInfo");
  397. this.setData({
  398. nickName: userInfo.nickName || "用户登录",
  399. avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  400. phoneNumber: userInfo.phoneNumber || ""
  401. })
  402. this.initMenuCardList()
  403. var storageSync = wx.getStorageSync("unionid");
  404. if (storageSync &&this.data.aid) {
  405. this.setData({
  406. show1: true,
  407. })
  408. }
  409. // 判断扫码控制权限
  410. wx.request({
  411. url: `${homeApi_empower}/wxstatus`,
  412. data: {
  413. unionid: wx.getStorageSync('unionid'),
  414. openid: wx.getStorageSync('openid'),
  415. token: wx.getStorageSync('token'),
  416. },
  417. method: 'POST',
  418. success: function (intervalRes) {
  419. console.log('intervalRes=' + JSON.stringify(intervalRes));
  420. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  421. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  422. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  423. wx.setStorageSync('hasHotelAuth', true);
  424. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  425. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  426. wx.setStorageSync('hname', intervalRes.data.hname);
  427. wx.setStorageSync('rname', intervalRes.data.rname);
  428. } else {
  429. wx.setStorageSync('hasHotelAuth', false);
  430. }
  431. },
  432. fail: function (error) {
  433. }
  434. });
  435. }
  436. return hasAuth;
  437. },
  438. initMenuCardList(){
  439. this.setData({menuCardList:[{
  440. icon: "../../static/index/icon/ic_fjkz.png",
  441. bgIcon: "../../static/index/bg/bg_fjkz.png",
  442. title: "房间控制",
  443. type: "navigate",
  444. color: "#3C84D9",
  445. bgColor: "#EEF4FF",
  446. url: "/subpages/main/main"
  447. }, {
  448. icon: "../../static/index/icon/ic_sm.png",
  449. bgIcon: "../../static/index/bg/bg_sm.png",
  450. title: "扫码",
  451. type: "navigate",
  452. color: "#10B09C",
  453. bgColor: "#E6FFFC",
  454. url: "/subpages/scan/scan"
  455. }, {
  456. icon: "../../static/index/icon/ic_jrsmbg.png",
  457. bgIcon: "../../static/index/bg/bg_jrsmbg.png",
  458. title: "最新睡眠报告",
  459. type: "navigate",
  460. color: "#8658B9",
  461. bgColor: "#F6F3FF",
  462. url: "/subpages/data/data"
  463. }, {
  464. icon: "../../static/index/icon/ic_lssmbg.png",
  465. bgIcon: "../../static/index/bg/bg_lssmbg.png",
  466. title: "历史睡眠报告",
  467. type: "navigate",
  468. color: "#BB6268",
  469. bgColor: "#FFF6F7",
  470. url: "/subpages/sleepReport/sleepReport"
  471. }, {
  472. icon: "../../static/index/icon/ic_wdcd.png",
  473. bgIcon: "../../static/index/bg/bg_wdcd.png",
  474. title: wx.getStorageSync("isManager")?"巡检房间" : "我的床垫",
  475. type: "navigate",
  476. color: "#317A9B",
  477. bgColor: "#E6FFFC",
  478. url: wx.getStorageSync("isManager")?this.data.seeAicoList+'?hotel_id='+wx.getStorageSync("isManager")[0] : "/subpages/myMattress/myMattress"
  479. }, {
  480. icon: "../../static/index/icon/ic_zxsc.png",
  481. bgIcon: "../../static/index/bg/bg_zxsc.png",
  482. title: "在线商城",
  483. type: "miniProgram",
  484. color: "#C2714B",
  485. bgColor: "#FFF8F4",
  486. appId: "wx63f79b3d61ba632b" || "gh_78da4ac5d0cc",
  487. url: "pages/home/dashboard/index"
  488. }],
  489. menuList: [{
  490. icon: "../../static/mine/dcwj.png",
  491. title: "健康检测",
  492. type: "navigate",
  493. url: "/subpages/testSAS/testSAS"
  494. }, {
  495. icon: "../../static/mine/zxkf.png",
  496. title: "在线客服",
  497. type: "contact",
  498. url: ""
  499. }, {
  500. icon: "../../static/mine/bzzx.png",
  501. title: "帮助中心",
  502. type: "navigate",
  503. url: "/subpages/helpList/helpList"
  504. }, {
  505. icon: "../../static/mine/gywm.png",
  506. title: "关于我们",
  507. type: "navigate",
  508. url: ""
  509. }]})
  510. },
  511. authorizationSuccessful() {
  512. console.log("授权成功");
  513. this.setData({ showAuthorizedDialog: false })
  514. this.checkAuth();
  515. },
  516. /**
  517. * 生命周期函数--监听页面隐藏
  518. */
  519. onHide() {
  520. },
  521. /**
  522. * 生命周期函数--监听页面卸载
  523. */
  524. onUnload() {
  525. },
  526. /**
  527. * 页面相关事件处理函数--监听用户下拉动作
  528. */
  529. onPullDownRefresh() {
  530. },
  531. /**
  532. * 页面上拉触底事件的处理函数
  533. */
  534. onReachBottom() {
  535. },
  536. /**
  537. * 用户点击右上角分享
  538. */
  539. onShareAppMessage() {
  540. }
  541. })