index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. let that=this
  230. // 判断扫码控制权限
  231. wx.request({
  232. url: `${homeApi_empower}/wxstatus`,
  233. data: {
  234. unionid: wx.getStorageSync('unionid'),
  235. openid: wx.getStorageSync('openid'),
  236. token: wx.getStorageSync('token'),
  237. },
  238. method: 'POST',
  239. success: function (intervalRes) {
  240. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  241. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  242. wx.setStorageSync('hasHotelAuth', true);
  243. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  244. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  245. wx.setStorageSync('hname', intervalRes.data.hname);
  246. wx.setStorageSync('rname', intervalRes.data.rname);
  247. } else {
  248. wx.setStorageSync('hasHotelAuth', false);
  249. }
  250. var hasHotelAuth = wx.getStorageSync("hasHotelAuth");
  251. if (!hasHotelAuth) {
  252. that.setData({
  253. showTipsB: true
  254. })
  255. return
  256. }
  257. that.toCardPage(index);
  258. },
  259. fail: function (error) {
  260. }
  261. });
  262. return;
  263. }
  264. if (index == 2) {
  265. var hasReportAuth = wx.getStorageSync("hasReportAuth");
  266. var unionid = wx.getStorageSync("unionid");
  267. if (!unionid) {
  268. this.setData({
  269. showTipsA: true
  270. })
  271. return
  272. }
  273. if (!hasReportAuth) {
  274. wx.showModal({
  275. title: '提示',
  276. content: '没有睡眠报告',
  277. showCancel: false
  278. });
  279. return
  280. }
  281. }
  282. this.toCardPage(index);
  283. } else {
  284. this.showDialog();
  285. }
  286. break;
  287. default:
  288. this.toCardPage(index);
  289. break;
  290. }
  291. },
  292. toCardPage(index) {
  293. const type = this.data.menuCardList[index].type;
  294. const url = this.data.menuCardList[index].url;
  295. if (!url) {
  296. return
  297. }
  298. if (type == 'switchTab') {
  299. wx.switchTab({
  300. url: url
  301. })
  302. } else if (type == 'navigate') {
  303. wx.navigateTo({
  304. url: url
  305. })
  306. } else if (type == 'miniProgram') {
  307. //develop开发版;trial体验版;release正式版
  308. const appId = this.data.menuCardList[index].appId;
  309. wx.navigateToMiniProgram({
  310. appId: appId,
  311. path: url,
  312. extraData: {
  313. },
  314. envVersion: 'develop',
  315. success(res) {
  316. // 打开成功
  317. }
  318. })
  319. }
  320. },
  321. toSubscriptions() {
  322. const tmplIds = 'k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw';
  323. // 这里是获取下发权限地方,根据官方文档,可以根据 wx.getSetting() 的 withSubscriptions 这个参数获取用户是否打开订阅消息总开关。后面我们需要获取用户是否同意总是同意消息推送。所以这里要给它设置为true 。
  324. wx.getSetting({
  325. withSubscriptions: true, // 这里设置为true,下面才会返回mainSwitch
  326. success: function (res) {
  327. // 调起授权界面弹窗
  328. if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
  329. if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  330. let moIdState = res.subscriptionsSetting.itemSettings[tmplIds]; // 用户同意的消息模板id
  331. if (moIdState === 'accept') {
  332. console.log('接受了消息推送');
  333. } else if (moIdState === 'reject') {
  334. console.log("拒绝消息推送");
  335. } else if (moIdState === 'ban') {
  336. console.log("已被后台封禁");
  337. }
  338. } else {
  339. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  340. wx.showModal({
  341. title: '提示',
  342. content: '请授权开通服务通知',
  343. showCancel: true,
  344. success: function (ress) {
  345. if (ress.confirm) {
  346. wx.requestSubscribeMessage({ // 调起消息订阅界面
  347. tmplIds: ['k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw', 'JSM4TwUMy5w5AHvdmJv7_NQ0h5FstGqtc12fqPOoPQU','rO9DCTKcSCxEIVp6GsV56hCLPwFKhvVytXGRkJxtTO4'],
  348. success(res) {
  349. console.log('订阅消息 成功 ');
  350. console.log(res);
  351. },
  352. fail(er) {
  353. console.log("订阅消息 失败 ");
  354. console.log(er);
  355. }
  356. })
  357. }
  358. }
  359. })
  360. }
  361. } else {
  362. console.log('订阅消息未开启')
  363. }
  364. },
  365. fail: function (error) {
  366. console.log(error);
  367. },
  368. })
  369. },
  370. /**
  371. * 生命周期函数--监听页面加载
  372. */
  373. onLoad(options) {
  374. wx.hideTabBar()
  375. // this.toSubscriptions();
  376. },
  377. /**
  378. * 生命周期函数--监听页面初次渲染完成
  379. */
  380. onReady() {
  381. wx.hideTabBar()
  382. this.getLocation();
  383. },
  384. /**
  385. * 生命周期函数--监听页面显示
  386. */
  387. onShow() {
  388. wx.hideTabBar()
  389. this.checkAuth()
  390. },
  391. showDialog() {
  392. var hasAuth = wx.getStorageSync("hasAuth");
  393. if (hasAuth) {
  394. return
  395. }
  396. this.setData({
  397. showAuthorizedDialog: true
  398. })
  399. },
  400. checkAuth() {
  401. var hasAuth = wx.getStorageSync("hasAuth");
  402. this.setData({
  403. hasAuth: hasAuth
  404. })
  405. if (hasAuth) {
  406. var userInfo = wx.getStorageSync("userInfo");
  407. this.setData({
  408. nickName: userInfo.nickName || "用户登录",
  409. avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  410. phoneNumber: userInfo.phoneNumber || ""
  411. })
  412. // 判断扫码控制权限
  413. wx.request({
  414. url: `${homeApi_empower}/wxstatus`,
  415. data: {
  416. unionid: wx.getStorageSync('unionid'),
  417. openid: wx.getStorageSync('openid'),
  418. token: wx.getStorageSync('token'),
  419. },
  420. method: 'POST',
  421. success: function (intervalRes) {
  422. console.log('intervalRes=' + JSON.stringify(intervalRes));
  423. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  424. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  425. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  426. wx.setStorageSync('hasHotelAuth', true);
  427. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  428. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  429. wx.setStorageSync('hname', intervalRes.data.hname);
  430. wx.setStorageSync('rname', intervalRes.data.rname);
  431. } else {
  432. wx.setStorageSync('hasHotelAuth', false);
  433. }
  434. },
  435. fail: function (error) {
  436. }
  437. });
  438. }
  439. return hasAuth;
  440. },
  441. authorizationSuccessful() {
  442. console.log("授权成功");
  443. this.setData({ showAuthorizedDialog: false })
  444. this.checkAuth();
  445. },
  446. /**
  447. * 生命周期函数--监听页面隐藏
  448. */
  449. onHide() {
  450. },
  451. /**
  452. * 生命周期函数--监听页面卸载
  453. */
  454. onUnload() {
  455. },
  456. /**
  457. * 页面相关事件处理函数--监听用户下拉动作
  458. */
  459. onPullDownRefresh() {
  460. },
  461. /**
  462. * 页面上拉触底事件的处理函数
  463. */
  464. onReachBottom() {
  465. },
  466. /**
  467. * 用户点击右上角分享
  468. */
  469. onShareAppMessage() {
  470. }
  471. })