index.js 12 KB

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