index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // pages/mine/mine.js
  2. import {getCheckInInfo, getDrawImg} from "./getPosterObj";
  3. const defaultAvatarUrl = "../../static/images/no-login.png"
  4. const homeApi_empower = "https://aipush.aidsleep.cn";
  5. import api from '../../utils/api';
  6. Page({
  7. imagePath: '',
  8. history: [],
  9. future: [],
  10. isSave: false,
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. showAuthorizedDialog: false,
  16. showTipsA: false,
  17. showTipsB: false,
  18. show1:false,
  19. aid:null,
  20. hasAuth: false,
  21. seeAicoList: "/subpagesTwo/seeAisoList/seeAicoList",
  22. avatarUrl: defaultAvatarUrl,
  23. nickName: "用户登录",
  24. phoneNumber: "",
  25. latitude: null,
  26. longitude: null,
  27. menuCardList: [],
  28. customActionStyle: {
  29. border: {
  30. borderColor: '#1A7AF8',
  31. },
  32. scale: {
  33. textIcon: '/palette/switch.png',
  34. imageIcon: '/palette/scale.png',
  35. },
  36. delete: {
  37. icon: '/palette/close.png',
  38. },
  39. },
  40. paintPallette: {},
  41. template: {},
  42. showImg:false
  43. },
  44. onClickShow() {
  45. this.setData({ showImg: true });
  46. },
  47. onClickHide() {
  48. this.setData({ showImg: false });
  49. },
  50. handleSureDownload() {
  51. this.isSave = false;
  52. api.painter({
  53. params: {score: 69},
  54. }).then(data => {
  55. if (data.code === 1) {
  56. var DrawImg = getDrawImg(data?.data?.pic||'/static/painter/hua.png', 0);
  57. var CheckInInfo = getCheckInInfo(data?.data?.talk|| "亲爱的,哪怕只有一分钟的平静闭眼,也是身体在努力修复的信号。新的一天,新的开始!", DrawImg.height);
  58. // 定义 palette 对象
  59. const palette = {
  60. "width": "750rpx",
  61. "height": CheckInInfo.height + "rpx",
  62. "background": "transparent",
  63. "views": [
  64. ...DrawImg.obj,
  65. ...CheckInInfo.obj,
  66. ]
  67. };
  68. // 更新数据到页面
  69. this.isSave = true;
  70. this.setData({
  71. paintPallette: palette
  72. });
  73. } else {
  74. }
  75. }).catch(err => {
  76. });
  77. },
  78. saveImage() {
  79. console.log("saveImage", this.imagePath)
  80. if (this.imagePath && typeof this.imagePath === 'string') {
  81. this.isSave = false;
  82. // 存入系统相册
  83. wx.saveImageToPhotosAlbum({
  84. filePath: this.imagePath || '',
  85. success: res => {
  86. this.onClickHide()
  87. },
  88. fail: res => {
  89. this.onClickHide()
  90. }
  91. })
  92. }
  93. },
  94. // 生成海报点击事件
  95. onImgOK(e) {
  96. this.imagePath = e.detail.path;
  97. this.setData({
  98. image: this.imagePath,
  99. });
  100. if (this.isSave) {
  101. this.onClickShow()
  102. }
  103. },
  104. touchEnd({detail}) {
  105. let needRefresh = detail.index >= 0 && detail.index <= this.data.template.views.length;
  106. if (needRefresh) {
  107. this.history.push({
  108. ...detail,
  109. });
  110. if (this.data.template.views[detail.index].id === detail.view.id) {
  111. this.data.template.views.splice(detail.index, 1);
  112. } else {
  113. this.data.template.views.splice(detail.index, 0, detail.view);
  114. }
  115. } else {
  116. if (!this.data.template || !this.data.template.views) {
  117. return;
  118. }
  119. for (let view of this.data.template.views) {
  120. if (view.id === detail.view.id) {
  121. this.history.push({
  122. view: {
  123. ...detail.view,
  124. ...view,
  125. },
  126. });
  127. view.css = detail.view.css;
  128. break;
  129. }
  130. }
  131. }
  132. this.future.length = 0;
  133. const props = {
  134. paintPallette: this.data.template,
  135. };
  136. if (needRefresh) {
  137. props.template = this.data.template;
  138. }
  139. this.setData(props);
  140. },
  141. getLocation() {
  142. const that = this;
  143. var hasAuth = wx.getStorageSync("hasAuth");
  144. if (!hasAuth) {
  145. return
  146. }
  147. wx.getSetting({
  148. success(res) {
  149. if (res.authSetting['scope.userLocation']) {
  150. // 用户已经授权
  151. wx.getLocation({
  152. type: 'gcj02', // 返回可以用于wx.openLocation的经纬度
  153. success(locationRes) {
  154. that.setData({
  155. latitude: locationRes.latitude,
  156. longitude: locationRes.longitude,
  157. });
  158. console.log('纬度:', locationRes.latitude);
  159. console.log('经度:', locationRes.longitude);
  160. that.savehotelnew();
  161. },
  162. fail(err) {
  163. that.setData({
  164. latitude: null,
  165. longitude: null,
  166. error: err.message
  167. });
  168. console.error('获取位置失败:', err.message);
  169. }
  170. });
  171. } else {
  172. // 用户未授权
  173. // wx.authorize({
  174. // scope: 'scope.userLocation',
  175. // success() {
  176. // // 用户同意授权后再次获取位置
  177. // that.savehotelnew();
  178. // },
  179. // fail() {
  180. // // 用户拒绝授权
  181. // that.setData({
  182. // latitude: null,
  183. // longitude: null,
  184. // error: '用户拒绝授权获取地理位置'
  185. // });
  186. // console.error('用户拒绝授权获取地理位置');
  187. // }
  188. // });
  189. }
  190. }
  191. });
  192. },
  193. savehotelnew() {
  194. const that = this;
  195. console.log({
  196. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  197. unionid: wx.getStorageSync('unionid'),
  198. longandlat: that.data.longitude + "|" + that.data.latitude
  199. });
  200. wx.request({
  201. url: 'https://aipush.aidsleep.cn/savehotelnew',
  202. method: 'POST',
  203. data: {
  204. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  205. unionid: wx.getStorageSync('unionid'),
  206. longandlat: that.data.longitude + "|" + that.data.latitude
  207. },
  208. success: (res) => {
  209. },
  210. fail: () => {
  211. }
  212. });
  213. },
  214. onCloseTipsDialog() {
  215. this.setData({
  216. showTipsA: false,
  217. showTipsB: false
  218. })
  219. },
  220. onMenuClick(e) {
  221. console.log(e.currentTarget.dataset);
  222. const { index } = e.currentTarget.dataset;
  223. var hasAuth = wx.getStorageSync("hasAuth");
  224. switch (index - 0) {
  225. case 0:
  226. // need Login
  227. if (hasAuth) {
  228. this.toPage(index);
  229. } else {
  230. this.showDialog();
  231. }
  232. break;
  233. default:
  234. this.toPage(index);
  235. break;
  236. }
  237. },
  238. bindAccount(){
  239. // 调用 /data/api.data/bind
  240. api.bind({
  241. params: { aid: this.data.aid ||10011},
  242. headers: { unionid: wx.getStorageSync("unionid")},
  243. })
  244. .then(data =>{
  245. if (data.code === 1) {
  246. wx.showToast({
  247. icon: 'none',
  248. title: data.info,
  249. });
  250. }else{
  251. wx.showToast({
  252. icon: 'none',
  253. title: data.info,
  254. });
  255. }
  256. this.isManager()
  257. })
  258. .catch(err => console.error('Bind Error:', err));
  259. },
  260. isManager(){
  261. // 调用 /data/api.data/isManager
  262. api.isManager({
  263. params: {},
  264. headers: { unionid: wx.getStorageSync("unionid")},
  265. })
  266. .then(data =>{
  267. if (data.code === 1) {
  268. wx.setStorageSync('isManager', data.data);
  269. }else{
  270. wx.setStorageSync('isManager', null);
  271. }
  272. this.initMenuCardList()
  273. })
  274. .catch(err => console.error('IsManager Error:', err));
  275. },
  276. toPage(index) {
  277. const type = this.data.menuList[index].type;
  278. const url = this.data.menuList[index].url;
  279. if (!url) {
  280. this.handleSureDownload()
  281. return
  282. }
  283. if (type == 'switchTab') {
  284. wx.switchTab({
  285. url: url
  286. })
  287. } else if (type == 'navigate') {
  288. wx.navigateTo({
  289. url: url
  290. })
  291. } else if (type == 'miniProgram') {
  292. //develop开发版;trial体验版;release正式版
  293. const appId = this.data.menuCardList[index].appId;
  294. wx.navigateToMiniProgram({
  295. appId: appId,
  296. path: url,
  297. extraData: {
  298. },
  299. envVersion: 'develop',
  300. success(res) {
  301. // 打开成功
  302. }
  303. })
  304. }
  305. },
  306. onMenuCardClick(e) {
  307. console.log(e.currentTarget.dataset);
  308. const { index } = e.currentTarget.dataset;
  309. var hasAuth = wx.getStorageSync("hasAuth");
  310. switch (index - 0) {
  311. case 0:
  312. case 1:
  313. case 2:
  314. case 3:
  315. case 4:
  316. // need Login
  317. if (hasAuth) {
  318. if (index == 0) {
  319. let that=this
  320. // 判断扫码控制权限
  321. wx.request({
  322. url: `${homeApi_empower}/wxstatus`,
  323. data: {
  324. unionid: wx.getStorageSync('unionid'),
  325. openid: wx.getStorageSync('openid'),
  326. token: wx.getStorageSync('token'),
  327. },
  328. method: 'POST',
  329. success: function (intervalRes) {
  330. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  331. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  332. wx.setStorageSync('hasHotelAuth', true);
  333. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  334. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  335. wx.setStorageSync('hname', intervalRes.data.hname);
  336. wx.setStorageSync('rname', intervalRes.data.rname);
  337. } else {
  338. wx.setStorageSync('hasHotelAuth', false);
  339. }
  340. var hasHotelAuth = wx.getStorageSync("hasHotelAuth");
  341. if (!hasHotelAuth) {
  342. that.setData({
  343. showTipsB: true
  344. })
  345. return
  346. }
  347. that.toCardPage(index);
  348. },
  349. fail: function (error) {
  350. }
  351. });
  352. return;
  353. }
  354. if (index == 2) {
  355. var hasReportAuth = wx.getStorageSync("hasReportAuth");
  356. var unionid = wx.getStorageSync("unionid");
  357. if (!unionid) {
  358. this.setData({
  359. showTipsA: true
  360. })
  361. return
  362. }
  363. if (!hasReportAuth) {
  364. wx.showModal({
  365. title: '提示',
  366. content: '没有睡眠报告',
  367. showCancel: false
  368. });
  369. return
  370. }
  371. }
  372. this.toCardPage(index);
  373. } else {
  374. this.showDialog();
  375. }
  376. break;
  377. default:
  378. this.toCardPage(index);
  379. break;
  380. }
  381. },
  382. toCardPage(index) {
  383. const type = this.data.menuCardList[index].type;
  384. const url = this.data.menuCardList[index].url;
  385. if (!url) {
  386. return
  387. }
  388. if (type == 'switchTab') {
  389. wx.switchTab({
  390. url: url
  391. })
  392. } else if (type == 'navigate') {
  393. wx.navigateTo({
  394. url: url
  395. })
  396. } else if (type == 'miniProgram') {
  397. //develop开发版;trial体验版;release正式版
  398. const appId = this.data.menuCardList[index].appId;
  399. wx.navigateToMiniProgram({
  400. appId: appId,
  401. path: url,
  402. extraData: {
  403. },
  404. envVersion: 'develop',
  405. success(res) {
  406. // 打开成功
  407. }
  408. })
  409. }
  410. },
  411. toSubscriptions() {
  412. const tmplIds = 'k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw';
  413. // 这里是获取下发权限地方,根据官方文档,可以根据 wx.getSetting() 的 withSubscriptions 这个参数获取用户是否打开订阅消息总开关。后面我们需要获取用户是否同意总是同意消息推送。所以这里要给它设置为true 。
  414. wx.getSetting({
  415. withSubscriptions: true, // 这里设置为true,下面才会返回mainSwitch
  416. success: function (res) {
  417. // 调起授权界面弹窗
  418. if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
  419. if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  420. let moIdState = res.subscriptionsSetting.itemSettings[tmplIds]; // 用户同意的消息模板id
  421. if (moIdState === 'accept') {
  422. console.log('接受了消息推送');
  423. } else if (moIdState === 'reject') {
  424. console.log("拒绝消息推送");
  425. } else if (moIdState === 'ban') {
  426. console.log("已被后台封禁");
  427. }
  428. } else {
  429. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  430. wx.showModal({
  431. title: '提示',
  432. content: '请授权开通服务通知',
  433. showCancel: true,
  434. success: function (ress) {
  435. if (ress.confirm) {
  436. wx.requestSubscribeMessage({ // 调起消息订阅界面
  437. tmplIds: ['k7OQFUKk8elm3ca3EoIfMFe81u0DCIuDSk300a2obxw', 'JSM4TwUMy5w5AHvdmJv7_NQ0h5FstGqtc12fqPOoPQU','rO9DCTKcSCxEIVp6GsV56hCLPwFKhvVytXGRkJxtTO4'],
  438. success(res) {
  439. console.log('订阅消息 成功 ');
  440. console.log(res);
  441. },
  442. fail(er) {
  443. console.log("订阅消息 失败 ");
  444. console.log(er);
  445. }
  446. })
  447. }
  448. }
  449. })
  450. }
  451. } else {
  452. console.log('订阅消息未开启')
  453. }
  454. },
  455. fail: function (error) {
  456. console.log(error);
  457. },
  458. })
  459. },
  460. /**
  461. * 生命周期函数--监听页面加载
  462. */
  463. onLoad(options) {
  464. wx.hideTabBar()
  465. if (options.aid){
  466. this.setData({
  467. aid:options.aid
  468. })
  469. }
  470. this.initMenuCardList()
  471. // this.toSubscriptions();
  472. },
  473. /**
  474. * 生命周期函数--监听页面初次渲染完成
  475. */
  476. onReady() {
  477. wx.hideTabBar()
  478. this.getLocation();
  479. },
  480. bindAccountPopup() {
  481. this.bindAccount()
  482. this.setData({
  483. show1: false,
  484. aid:null
  485. });
  486. },
  487. onClose() {
  488. this.setData({
  489. show1: false,
  490. });
  491. },
  492. /**
  493. * 生命周期函数--监听页面显示
  494. */
  495. onShow() {
  496. wx.hideTabBar()
  497. this.checkAuth()
  498. },
  499. showDialog() {
  500. var hasAuth = wx.getStorageSync("hasAuth");
  501. if (hasAuth) {
  502. return
  503. }
  504. this.setData({
  505. showAuthorizedDialog: true
  506. })
  507. },
  508. checkAuth() {
  509. var hasAuth = wx.getStorageSync("hasAuth");
  510. this.setData({
  511. hasAuth: hasAuth
  512. })
  513. if (hasAuth) {
  514. var userInfo = wx.getStorageSync("userInfo");
  515. this.setData({
  516. nickName: userInfo.nickName || "用户登录",
  517. avatarUrl: userInfo.avatarUrl || defaultAvatarUrl,
  518. phoneNumber: userInfo.phoneNumber || ""
  519. })
  520. this.initMenuCardList()
  521. var storageSync = wx.getStorageSync("unionid");
  522. if (storageSync &&this.data.aid) {
  523. this.setData({
  524. show1: true,
  525. })
  526. }
  527. // 判断扫码控制权限
  528. wx.request({
  529. url: `${homeApi_empower}/wxstatus`,
  530. data: {
  531. unionid: wx.getStorageSync('unionid'),
  532. openid: wx.getStorageSync('openid'),
  533. token: wx.getStorageSync('token'),
  534. },
  535. method: 'POST',
  536. success: function (intervalRes) {
  537. console.log('intervalRes=' + JSON.stringify(intervalRes));
  538. console.log('intervalRes.data=' + JSON.stringify(intervalRes.data));
  539. //empower.js? [sm]:152 intervalRes.data={"st":"success","status":"0"}
  540. if (intervalRes.data && intervalRes.data.st && intervalRes.data.status && intervalRes.data.st == 'success' && intervalRes.data.status == '1') {
  541. wx.setStorageSync('hasHotelAuth', true);
  542. wx.setStorageSync('hotelEmpower', intervalRes.data.hotel);
  543. wx.setStorageSync('roomEmpower', intervalRes.data.room);
  544. wx.setStorageSync('hname', intervalRes.data.hname);
  545. wx.setStorageSync('rname', intervalRes.data.rname);
  546. } else {
  547. wx.setStorageSync('hasHotelAuth', false);
  548. }
  549. },
  550. fail: function (error) {
  551. }
  552. });
  553. }
  554. return hasAuth;
  555. },
  556. initMenuCardList(){
  557. this.setData({menuCardList:[{
  558. icon: "../../static/index/icon/ic_fjkz.png",
  559. bgIcon: "../../static/index/bg/bg_fjkz.png",
  560. title: "房间控制",
  561. type: "navigate",
  562. color: "#3C84D9",
  563. bgColor: "#EEF4FF",
  564. url: "/subpages/main/main"
  565. }, {
  566. icon: "../../static/index/icon/ic_sm.png",
  567. bgIcon: "../../static/index/bg/bg_sm.png",
  568. title: "扫码",
  569. type: "navigate",
  570. color: "#10B09C",
  571. bgColor: "#E6FFFC",
  572. url: "/subpages/scan/scan"
  573. }, {
  574. icon: "../../static/index/icon/ic_jrsmbg.png",
  575. bgIcon: "../../static/index/bg/bg_jrsmbg.png",
  576. title: "最新睡眠报告",
  577. type: "navigate",
  578. color: "#8658B9",
  579. bgColor: "#F6F3FF",
  580. url: "/subpages/data/data"
  581. }, {
  582. icon: "../../static/index/icon/ic_lssmbg.png",
  583. bgIcon: "../../static/index/bg/bg_lssmbg.png",
  584. title: "历史睡眠报告",
  585. type: "navigate",
  586. color: "#BB6268",
  587. bgColor: "#FFF6F7",
  588. url: "/subpages/sleepReport/sleepReport"
  589. }, {
  590. icon: "../../static/index/icon/ic_wdcd.png",
  591. bgIcon: "../../static/index/bg/bg_wdcd.png",
  592. title: wx.getStorageSync("isManager")?"巡检房间" : "我的床垫",
  593. type: "navigate",
  594. color: "#317A9B",
  595. bgColor: "#E6FFFC",
  596. url: wx.getStorageSync("isManager")?this.data.seeAicoList+'?hotel_id='+wx.getStorageSync("isManager")[0] : "/subpages/myMattress/myMattress"
  597. }, {
  598. icon: "../../static/index/icon/ic_zxsc.png",
  599. bgIcon: "../../static/index/bg/bg_zxsc.png",
  600. title: "在线商城",
  601. type: "miniProgram",
  602. color: "#C2714B",
  603. bgColor: "#FFF8F4",
  604. appId: "wx63f79b3d61ba632b" || "gh_78da4ac5d0cc",
  605. url: "pages/home/dashboard/index"
  606. }],
  607. menuList: [{
  608. icon: "../../static/mine/dcwj.png",
  609. title: "健康检测",
  610. type: "navigate",
  611. url: "/subpages/testSAS/testSAS"
  612. }, {
  613. icon: "../../static/mine/zxkf.png",
  614. title: "在线客服",
  615. type: "contact",
  616. url: ""
  617. }, {
  618. icon: "../../static/mine/bzzx.png",
  619. title: "帮助中心",
  620. type: "navigate",
  621. url: "/subpages/helpList/helpList"
  622. }, {
  623. icon: "../../static/mine/gywm.png",
  624. title: "关于我们",
  625. type: "navigate",
  626. url: ""
  627. }]})
  628. },
  629. authorizationSuccessful() {
  630. console.log("授权成功");
  631. this.setData({ showAuthorizedDialog: false })
  632. this.checkAuth();
  633. },
  634. /**
  635. * 生命周期函数--监听页面隐藏
  636. */
  637. onHide() {
  638. },
  639. /**
  640. * 生命周期函数--监听页面卸载
  641. */
  642. onUnload() {
  643. },
  644. /**
  645. * 页面相关事件处理函数--监听用户下拉动作
  646. */
  647. onPullDownRefresh() {
  648. },
  649. /**
  650. * 页面上拉触底事件的处理函数
  651. */
  652. onReachBottom() {
  653. },
  654. /**
  655. * 用户点击右上角分享
  656. */
  657. onShareAppMessage() {
  658. }
  659. })