index.js 12 KB

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