index.js 13 KB

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