index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. import { homePage, appGetConfigImg } from '../../api/index'
  2. import { createQRcode } from "../../api/document"
  3. import { userLogin, bindMobile, bindBaseInfo } from '../../api/my'
  4. import { appointmentList } from '../../api/appointment'
  5. const app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. useNumber: 0, // 剩余预约次数
  12. documentVos: [], // 档案信息
  13. baseStr: 'data:image/jpg;base64,',
  14. QRCodeBase64: '',
  15. qrcodeDialog: false,
  16. qrUserName: '',
  17. phoneAuthShow: false,
  18. userAuthShow: false,
  19. avatarUrl: '',
  20. nickName: '',
  21. showTopBar: false,
  22. topBarObj: null,
  23. homepageImg: '',
  24. pageHasRequested: false // 页面数据是否已经请求过
  25. },
  26. /**
  27. * 生命周期函数--监听页面显示
  28. */
  29. onShow() {
  30. app.globalData.selectedInex = 0
  31. wx.setStorageSync('selectedInex', 0)
  32. this.handleToken()
  33. },
  34. handleToken () {
  35. const accessToken = wx.getStorageSync('accessToken')
  36. if (accessToken) {
  37. this.storageTokenLogin()
  38. } else {
  39. this.handleWXCodeLogin()
  40. }
  41. },
  42. storageTokenLogin () {
  43. this.handleUserLoginRes()
  44. },
  45. handleWXCodeLogin () {
  46. var that = this
  47. wx.login({
  48. success (loginCode) {
  49. if (loginCode.code) {
  50. that.userLoginFn(loginCode.code)
  51. }
  52. }
  53. })
  54. },
  55. userLoginFn (code) {
  56. var that = this
  57. var data = {
  58. code: code
  59. }
  60. wx.showLoading({
  61. title: '登录中...',
  62. mask: true
  63. })
  64. userLogin(data).then(res => {
  65. wx.hideLoading()
  66. that.setStorage(res.data)
  67. }).catch(e => {
  68. wx.hideLoading()
  69. wx.showModal({
  70. content: e,
  71. confirmColor: '#333',
  72. showCancel: false
  73. })
  74. })
  75. },
  76. handleUserLoginRes () {
  77. const that = this
  78. const _isNeedPhone = wx.getStorageSync('isNeedPhone')
  79. const _headImg = wx.getStorageSync('headImg')
  80. if (!_isNeedPhone && _headImg !== '') {
  81. app.globalData.userInfo.login = true
  82. wx.setStorageSync('login', true)
  83. this.initIndexData()
  84. } else {
  85. if (_isNeedPhone) {
  86. that.setData({
  87. phoneAuthShow: true
  88. })
  89. } else {
  90. if (_headImg == '') {
  91. that.setData({
  92. userAuthShow: true
  93. })
  94. } else {
  95. app.globalData.userInfo.login = true
  96. wx.setStorageSync('login', true)
  97. }
  98. }
  99. }
  100. },
  101. // 拉起手机号授权弹窗
  102. getPhoneNumber (e) {
  103. var phonePermission = e.detail.errMsg.split(':')[1]
  104. if (phonePermission == 'ok') {
  105. var phoneCode = e.detail.code
  106. this.bindMobileFn(phoneCode)
  107. } else {
  108. wx.setStorageSync('isNeedPhone', true)
  109. wx.showToast({
  110. title: '授权失败',
  111. icon: 'error'
  112. })
  113. }
  114. },
  115. // 绑定手机号
  116. bindMobileFn (phoneCode) {
  117. var that = this
  118. var data = {
  119. mobileCode: phoneCode
  120. }
  121. const _headImg = wx.getStorageSync('headImg')
  122. wx.showLoading({
  123. title: '加载中...',
  124. mask: true
  125. })
  126. bindMobile(data).then(res => {
  127. wx.hideLoading()
  128. const resPhoneNumber = res.data
  129. app.globalData.userInfo.phoneNumber = resPhoneNumber
  130. app.globalData.isNeedPhone = false
  131. wx.setStorageSync('isNeedPhone', false)
  132. wx.setStorageSync('phoneNumber', resPhoneNumber)
  133. that.setData({
  134. phoneAuthShow: false
  135. })
  136. if (!_headImg) {
  137. // 需要授权头像
  138. that.setData({
  139. userAuthShow: true
  140. })
  141. } else {
  142. app.globalData.userInfo.login = true
  143. wx.setStorageSync('login', true)
  144. }
  145. }).catch(e => {
  146. wx.hideLoading()
  147. wx.showModal({
  148. content: e,
  149. confirmColor: '#333',
  150. showCancel: false
  151. })
  152. })
  153. },
  154. // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
  155. getUserinfo () {
  156. var that = this
  157. wx.getUserProfile({
  158. desc: '用于获取用户权益'
  159. }).then(res => {
  160. const _avatarUrl = res.userInfo.avatarUrl
  161. const _nickName = res.userInfo.nickName
  162. that.setData({
  163. avatarUrl: _avatarUrl,
  164. nickname: _nickName
  165. })
  166. that.handleConfirmNickname()
  167. }).catch(e => {
  168. wx.showToast({
  169. title: '授权失败',
  170. icon: 'error'
  171. })
  172. })
  173. },
  174. // 提交用户头像和昵称到后台
  175. handleConfirmNickname () {
  176. var that = this
  177. var data = {
  178. avatarUrl: that.data.avatarUrl,
  179. nickName: that.data.nickname
  180. }
  181. wx.showLoading({
  182. title: '加载中...',
  183. mask: true
  184. })
  185. bindBaseInfo(data).then(res => {
  186. const _headImg = that.data.avatarUrl
  187. const _userName = that.data.nickname
  188. app.globalData.userInfo.login = true
  189. app.globalData.userInfo.headImg = _headImg
  190. app.globalData.userInfo.userName = _userName
  191. app.globalData.isNeedHeadImg = false
  192. wx.setStorageSync('login', true)
  193. wx.setStorageSync('headImg', _headImg)
  194. wx.setStorageSync('userName', _userName)
  195. wx.setStorageSync('isNeedHeadImg', false)
  196. that.setData({
  197. userAuthShow: false
  198. })
  199. that.initIndexData()
  200. wx.hideLoading()
  201. }).catch(e => {
  202. wx.hideLoading()
  203. wx.showModal({
  204. content: e,
  205. confirmColor: '#333',
  206. showCancel: false
  207. })
  208. })
  209. },
  210. async initIndexData () {
  211. wx.showNavigationBarLoading()
  212. await this.awaitHomePage()
  213. await this.awaitAppointmentList()
  214. await this.awaitAppGetConfigImg()
  215. wx.hideNavigationBarLoading()
  216. },
  217. // 获取首页档案信息 剩余次数
  218. awaitHomePage () {
  219. var that = this
  220. return new Promise((reslove,reject) => {
  221. homePage({}).then(hoemRes => {
  222. // wx.hideLoading()
  223. var response = hoemRes.data.documentVos || []
  224. response.map(item => {
  225. item.birthDay = item.birthday.split(' ')[0]
  226. })
  227. that.setData({
  228. useNumber: hoemRes.data.useNumber,
  229. documentVos: response
  230. })
  231. app.globalData.useNumber = hoemRes.data.useNumber
  232. reslove(hoemRes)
  233. }).catch(e => {
  234. // wx.hideLoading()
  235. wx.showModal({
  236. content: e,
  237. confirmColor: '#333',
  238. showCancel: false
  239. })
  240. })
  241. })
  242. },
  243. // 获取待预约次数
  244. awaitAppointmentList () {
  245. const that = this
  246. return new Promise((reslove,reject) => {
  247. appointmentList({
  248. status: 1,
  249. currentPage: 1
  250. }).then(res => {
  251. if (res.data.vos.length !== 0) {
  252. const response = res.data.vos || []
  253. response.map(item => {
  254. item.timeStr = item.appointmentTime.split(' ')[0]
  255. })
  256. that.setData({
  257. showTopBar: true,
  258. topBarObj: response[0]
  259. })
  260. app.globalData.hasAppointment = true
  261. } else {
  262. that.setData({
  263. showTopBar: false,
  264. topBarObj: null
  265. })
  266. }
  267. reslove(res)
  268. }).catch(e => {
  269. reject(e)
  270. })
  271. })
  272. },
  273. // 获取图片资源
  274. awaitAppGetConfigImg () {
  275. const that = this
  276. return new Promise((reslove,reject) => {
  277. appGetConfigImg({
  278. 'imgType': '5'
  279. }).then(configRes => {
  280. that.setData({
  281. homepageImg: configRes.data[0].imgUrl
  282. })
  283. reslove(configRes)
  284. }).catch(e => {
  285. reject(e)
  286. })
  287. })
  288. },
  289. /**
  290. * 处理套餐 购买 or 预约
  291. * useNumber: 0-购买 1-预约
  292. * **/
  293. handlePackage () {
  294. var type = this.data.useNumber > 0 ? 1 : 0
  295. switch (type) {
  296. case 0:
  297. wx.navigateTo({
  298. url: '/pages/buy/buy?from=index',
  299. })
  300. break;
  301. case 1:
  302. if (this.data.showTopBar) {
  303. this.stopAppointment()
  304. } else {
  305. this.appointmentAuth()
  306. }
  307. break;
  308. }
  309. },
  310. // 预约前判断是否存在一位检测人
  311. appointmentAuth () {
  312. if (this.data.documentVos.length == 0) {
  313. wx.showModal({
  314. content: '预约用户前需要先添加检测人员信息',
  315. cancelColor: '#666',
  316. confirmColor: '#333',
  317. success (res) {
  318. if (res.confirm) {
  319. wx.navigateTo({
  320. url: '/pages/createFile/createFile?from=index'
  321. })
  322. }
  323. }
  324. })
  325. } else {
  326. wx.navigateTo({
  327. url: '/pages/appointment/appointment?from=index',
  328. })
  329. }
  330. },
  331. // 点击全部档案
  332. handleAllFile () {
  333. wx.navigateTo({
  334. url: '/pages/myFile/myFile',
  335. })
  336. },
  337. // 点击查看报告
  338. handleRepoetDetail (e) {
  339. var reportid = e.currentTarget.dataset.reportid
  340. wx.navigateTo({
  341. url: '/pages/reportDetail/reportDetail?reportid=' + reportid
  342. })
  343. },
  344. // 添加检测人
  345. handleAddCheck () {
  346. wx.navigateTo({
  347. url: '/pages/createFile/createFile?form=index',
  348. })
  349. },
  350. /* 车子的图片 做跳转交互,跳转做判断,0次则跳出去充值弹框,充值弹窗有两个选择 :若激活卡,则跳转至实体卡兑换页面;若充值,则跳转至体验卡购买页面 */
  351. handlActions () {
  352. if (this.data.showTopBar) {
  353. this.stopAppointment()
  354. } else {
  355. if (this.data.useNumber == 0) {
  356. wx.showModal({
  357. title: '去充值',
  358. cancelColor: '#666',
  359. cancelText: '激活卡',
  360. confirmText: '去充值',
  361. confirmColor: '#333',
  362. success (res) {
  363. if (res.confirm) {
  364. wx.navigateTo({
  365. url: '/pages/buy/buy?from=index',
  366. })
  367. } else if (res.cancel) {
  368. wx.navigateTo({
  369. url: '/pages/exchange/exchange',
  370. })
  371. }
  372. }
  373. })
  374. } else {
  375. this.appointmentAuth()
  376. }
  377. }
  378. },
  379. stopAppointment () {
  380. wx.showModal({
  381. content: '您预约的健康筛查还未体验,请先体验',
  382. cancelColor: '#666',
  383. cancelText: '取消',
  384. confirmText: '我的预约',
  385. confirmColor: '#333',
  386. success (res) {
  387. if (res.confirm) {
  388. wx.navigateTo({
  389. url: '/pages/myAppointment/myAppointment',
  390. })
  391. }
  392. }
  393. })
  394. },
  395. handleShowQRCode (e) {
  396. var that = this
  397. var id = e.currentTarget.dataset.id
  398. var realname = e.currentTarget.dataset.realname
  399. this.setData({
  400. qrUserName: realname
  401. })
  402. wx.showLoading({
  403. title: '生成中...'
  404. })
  405. createQRcode({ documentId: id }).then(res => {
  406. wx.hideLoading()
  407. that.setData({
  408. QRCodeBase64: that.data.baseStr + res.data,
  409. qrcodeDialog: true
  410. })
  411. }).catch(e => {
  412. wx.hideLoading()
  413. wx.showModal({
  414. content: e,
  415. confirmColor: '#333',
  416. showCancel: false
  417. })
  418. })
  419. },
  420. handleCloseQRCode (e) {
  421. this.setData({
  422. qrcodeDialog: false,
  423. QRCodeBase64: ''
  424. })
  425. },
  426. onShareAppMessage () {
  427. return {
  428. title: '3分钟500+项健康指标',
  429. path: '/pages/index/index',
  430. imageUrl: '../../imaes/share.jpg'
  431. }
  432. },
  433. setStorage (response) {
  434. const accessToken = response.accessToken
  435. const isNeedPhone = response.isNeedPhone
  436. const isNeedHeadImg = response.headImg ? false : true
  437. const headImg = response.headImg
  438. const userName = response.userName
  439. const phoneNumber = response.phoneNumber
  440. app.globalData.accessToken = accessToken
  441. app.globalData.isNeedPhone = isNeedPhone
  442. app.globalData.isNeedHeadImg = isNeedHeadImg
  443. app.globalData.userInfo.headImg= headImg
  444. app.globalData.userInfo.userName = userName
  445. app.globalData.userInfo.phoneNumber = phoneNumber
  446. wx.setStorageSync('accessToken', accessToken)
  447. wx.setStorageSync('isNeedPhone', isNeedPhone)
  448. wx.setStorageSync('isNeedHeadImg', isNeedHeadImg)
  449. wx.setStorageSync('headImg', headImg)
  450. wx.setStorageSync('userName', userName)
  451. wx.setStorageSync('phoneNumber', phoneNumber)
  452. this.handleUserLoginRes()
  453. }
  454. })