index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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: ""
  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. // need Login
  226. if (hasAuth) {
  227. if (index == 0) {
  228. var hasHotelAuth = wx.getStorageSync("hasHotelAuth");
  229. if (!hasHotelAuth) {
  230. this.setData({
  231. showTipsB: true
  232. })
  233. return
  234. }
  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. /**
  294. * 生命周期函数--监听页面加载
  295. */
  296. onLoad(options) {
  297. wx.hideTabBar()
  298. },
  299. /**
  300. * 生命周期函数--监听页面初次渲染完成
  301. */
  302. onReady() {
  303. wx.hideTabBar()
  304. this.getLocation();
  305. },
  306. /**
  307. * 生命周期函数--监听页面显示
  308. */
  309. onShow() {
  310. wx.hideTabBar()
  311. this.checkAuth()
  312. },
  313. showDialog() {
  314. var hasAuth = wx.getStorageSync("hasAuth");
  315. if (hasAuth) {
  316. return
  317. }
  318. this.setData({
  319. showAuthorizedDialog: true
  320. })
  321. },
  322. checkAuth() {
  323. var hasAuth = wx.getStorageSync("hasAuth");
  324. this.setData({
  325. hasAuth: hasAuth
  326. })
  327. if (hasAuth) {
  328. var userInfo = wx.getStorageSync("userInfo");
  329. this.setData({
  330. nickName: userInfo.nickName || "用户登录",
  331. avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  332. phoneNumber: userInfo.phoneNumber || ""
  333. })
  334. // 判断扫码控制权限
  335. wx.request({
  336. url: `${homeApi_empower}/wxstatus`,
  337. data: {
  338. unionid: wx.getStorageSync('unionid'),
  339. openid: wx.getStorageSync('openid'),
  340. token: wx.getStorageSync('token'),
  341. },
  342. method: 'POST',
  343. success: function (intervalRes) {
  344. console.log('intervalRes=' + JSON.stringify(intervalRes));
  345. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  346. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  347. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  348. wx.setStorageSync('hasHotelAuth', true);
  349. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  350. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  351. wx.setStorageSync('hname', intervalRes.data.hname);
  352. wx.setStorageSync('rname', intervalRes.data.rname);
  353. } else {
  354. wx.setStorageSync('hasHotelAuth', false);
  355. }
  356. },
  357. fail: function (error) {
  358. }
  359. });
  360. }
  361. return hasAuth;
  362. },
  363. authorizationSuccessful() {
  364. console.log("授权成功");
  365. this.setData({ showAuthorizedDialog: false })
  366. this.checkAuth();
  367. },
  368. /**
  369. * 生命周期函数--监听页面隐藏
  370. */
  371. onHide() {
  372. },
  373. /**
  374. * 生命周期函数--监听页面卸载
  375. */
  376. onUnload() {
  377. },
  378. /**
  379. * 页面相关事件处理函数--监听用户下拉动作
  380. */
  381. onPullDownRefresh() {
  382. },
  383. /**
  384. * 页面上拉触底事件的处理函数
  385. */
  386. onReachBottom() {
  387. },
  388. /**
  389. * 用户点击右上角分享
  390. */
  391. onShareAppMessage() {
  392. }
  393. })