index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. wx.request({
  145. url: 'https://aipush.aidsleep.cn/savehotelnew',
  146. method: 'POST',
  147. data: {
  148. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  149. unionid: wx.getStorageSync('unionid'),
  150. longandlat: that.data.longitude + "|" + that.data.latitude
  151. },
  152. success: (res) => {
  153. },
  154. fail: () => {
  155. }
  156. });
  157. },
  158. onCloseTipsDialog() {
  159. this.setData({
  160. showTipsA: false,
  161. showTipsB: false
  162. })
  163. },
  164. onMenuClick(e) {
  165. console.log(e.currentTarget.dataset);
  166. const { index } = e.currentTarget.dataset;
  167. var hasAuth = wx.getStorageSync("hasAuth");
  168. switch (index - 0) {
  169. case 0:
  170. // need Login
  171. if (hasAuth) {
  172. this.toPage(index);
  173. } else {
  174. this.showDialog();
  175. }
  176. break;
  177. default:
  178. this.toPage(index);
  179. break;
  180. }
  181. },
  182. toPage(index) {
  183. const type = this.data.menuList[index].type;
  184. const url = this.data.menuList[index].url;
  185. if (!url) {
  186. return
  187. }
  188. if (type == 'switchTab') {
  189. wx.switchTab({
  190. url: url
  191. })
  192. } else if (type == 'navigate') {
  193. wx.navigateTo({
  194. url: url
  195. })
  196. } else if (type == 'miniProgram') {
  197. //develop开发版;trial体验版;release正式版
  198. const appId = this.data.menuCardList[index].appId;
  199. wx.navigateToMiniProgram({
  200. appId: appId,
  201. path: url,
  202. extraData: {
  203. },
  204. envVersion: 'develop',
  205. success(res) {
  206. // 打开成功
  207. }
  208. })
  209. }
  210. },
  211. onMenuCardClick(e) {
  212. console.log(e.currentTarget.dataset);
  213. const { index } = e.currentTarget.dataset;
  214. var hasAuth = wx.getStorageSync("hasAuth");
  215. switch (index - 0) {
  216. case 0:
  217. case 1:
  218. case 2:
  219. case 3:
  220. // need Login
  221. if (hasAuth) {
  222. if (index == 0) {
  223. var hasHotelAuth = wx.getStorageSync("hasHotelAuth");
  224. if (!hasHotelAuth) {
  225. this.setData({
  226. showTipsB: true
  227. })
  228. return
  229. }
  230. }
  231. if (index == 2) {
  232. var hasReportAuth = wx.getStorageSync("hasReportAuth");
  233. var unionid = wx.getStorageSync("unionid");
  234. if (!unionid) {
  235. this.setData({
  236. showTipsA: true
  237. })
  238. return
  239. }
  240. if (!hasReportAuth) {
  241. wx.showModal({
  242. title: '提示',
  243. content: '没有睡眠报告',
  244. showCancel: false
  245. });
  246. return
  247. }
  248. }
  249. this.toCardPage(index);
  250. } else {
  251. this.showDialog();
  252. }
  253. break;
  254. default:
  255. this.toCardPage(index);
  256. break;
  257. }
  258. },
  259. toCardPage(index) {
  260. const type = this.data.menuCardList[index].type;
  261. const url = this.data.menuCardList[index].url;
  262. if (!url) {
  263. return
  264. }
  265. if (type == 'switchTab') {
  266. wx.switchTab({
  267. url: url
  268. })
  269. } else if (type == 'navigate') {
  270. wx.navigateTo({
  271. url: url
  272. })
  273. } else if (type == 'miniProgram') {
  274. //develop开发版;trial体验版;release正式版
  275. const appId = this.data.menuCardList[index].appId;
  276. wx.navigateToMiniProgram({
  277. appId: appId,
  278. path: url,
  279. extraData: {
  280. },
  281. envVersion: 'develop',
  282. success(res) {
  283. // 打开成功
  284. }
  285. })
  286. }
  287. },
  288. /**
  289. * 生命周期函数--监听页面加载
  290. */
  291. onLoad(options) {
  292. wx.hideTabBar()
  293. },
  294. /**
  295. * 生命周期函数--监听页面初次渲染完成
  296. */
  297. onReady() {
  298. wx.hideTabBar()
  299. this.getLocation();
  300. },
  301. /**
  302. * 生命周期函数--监听页面显示
  303. */
  304. onShow() {
  305. wx.hideTabBar()
  306. this.checkAuth()
  307. },
  308. showDialog() {
  309. var hasAuth = wx.getStorageSync("hasAuth");
  310. if (hasAuth) {
  311. return
  312. }
  313. this.setData({
  314. showAuthorizedDialog: true
  315. })
  316. },
  317. checkAuth() {
  318. var hasAuth = wx.getStorageSync("hasAuth");
  319. this.setData({
  320. hasAuth: hasAuth
  321. })
  322. if (hasAuth) {
  323. var userInfo = wx.getStorageSync("userInfo");
  324. this.setData({
  325. nickName: userInfo.nickName || "用户登录",
  326. avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  327. phoneNumber: userInfo.phoneNumber || ""
  328. })
  329. // 判断扫码控制权限
  330. wx.request({
  331. url: `${homeApi_empower}/wxstatus`,
  332. data: {
  333. unionid: wx.getStorageSync('unionid'),
  334. openid: wx.getStorageSync('openid'),
  335. token: wx.getStorageSync('token'),
  336. },
  337. method: 'POST',
  338. success: function (intervalRes) {
  339. console.log('intervalRes=' + JSON.stringify(intervalRes));
  340. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  341. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  342. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  343. wx.setStorageSync('hasHotelAuth', true);
  344. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  345. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  346. wx.setStorageSync('hname', intervalRes.data.hname);
  347. wx.setStorageSync('rname', intervalRes.data.rname);
  348. } else {
  349. wx.setStorageSync('hasHotelAuth', false);
  350. }
  351. },
  352. fail: function (error) {
  353. }
  354. });
  355. }
  356. return hasAuth;
  357. },
  358. authorizationSuccessful() {
  359. console.log("授权成功");
  360. this.setData({ showAuthorizedDialog: false })
  361. this.checkAuth();
  362. },
  363. /**
  364. * 生命周期函数--监听页面隐藏
  365. */
  366. onHide() {
  367. },
  368. /**
  369. * 生命周期函数--监听页面卸载
  370. */
  371. onUnload() {
  372. },
  373. /**
  374. * 页面相关事件处理函数--监听用户下拉动作
  375. */
  376. onPullDownRefresh() {
  377. },
  378. /**
  379. * 页面上拉触底事件的处理函数
  380. */
  381. onReachBottom() {
  382. },
  383. /**
  384. * 用户点击右上角分享
  385. */
  386. onShareAppMessage() {
  387. }
  388. })