index.js 10 KB

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