index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 (pullDownRefresh = false) {
  210. var that = this
  211. wx.showLoading({
  212. title: '加载中...',
  213. mask: true
  214. })
  215. // 获取首页档案信息 剩余次数
  216. homePage({}).then(hoemRes => {
  217. wx.hideLoading()
  218. if (pullDownRefresh) {
  219. wx.stopPullDownRefresh()
  220. }
  221. var response = hoemRes.data.documentVos || []
  222. response.map(item => {
  223. item.birthDay = item.birthday.split(' ')[0]
  224. })
  225. that.setData({
  226. useNumber: hoemRes.data.useNumber,
  227. documentVos: response,
  228. isRefreshPage: true
  229. })
  230. app.globalData.useNumber = hoemRes.data.useNumber
  231. }).catch(e => {
  232. wx.hideLoading()
  233. wx.showModal({
  234. content: e,
  235. confirmColor: '#333',
  236. showCancel: false
  237. })
  238. })
  239. await that.awaitAppointmentList()
  240. await that.awaitAppGetConfigImg()
  241. },
  242. // 获取待预约次数
  243. awaitAppointmentList () {
  244. const that = this
  245. wx.showNavigationBarLoading()
  246. return new Promise((reslove,reject) => {
  247. appointmentList({
  248. status: 1,
  249. currentPage: 1
  250. }).then(res => {
  251. reslove(res)
  252. wx.hideNavigationBarLoading()
  253. if (res.data.vos.length !== 0) {
  254. const response = res.data.vos || []
  255. response.map(item => {
  256. item.timeStr = item.appointmentTime.split(' ')[0]
  257. })
  258. that.setData({
  259. showTopBar: true,
  260. topBarObj: response[0]
  261. })
  262. app.globalData.hasAppointment = true
  263. } else {
  264. that.setData({
  265. showTopBar: false,
  266. topBarObj: null
  267. })
  268. }
  269. }).catch(e => {
  270. reject(e)
  271. wx.hideNavigationBarLoading()
  272. })
  273. })
  274. },
  275. // 获取图片资源
  276. awaitAppGetConfigImg () {
  277. const that = this
  278. wx.showNavigationBarLoading()
  279. return new Promise((reslove,reject) => {
  280. appGetConfigImg({
  281. 'imgType': '5'
  282. }).then(configRes => {
  283. reslove(configRes)
  284. wx.hideNavigationBarLoading()
  285. that.setData({
  286. homepageImg: configRes.data[0].imgUrl
  287. })
  288. }).catch(e => {
  289. reject(e)
  290. wx.hideNavigationBarLoading()
  291. })
  292. })
  293. },
  294. /**
  295. * 处理套餐 购买 or 预约
  296. * useNumber: 0-购买 1-预约
  297. * **/
  298. handlePackage () {
  299. var type = this.data.useNumber > 0 ? 1 : 0
  300. switch (type) {
  301. case 0:
  302. wx.navigateTo({
  303. url: '/pages/buy/buy?from=index',
  304. })
  305. break;
  306. case 1:
  307. if (this.data.showTopBar) {
  308. this.stopAppointment()
  309. } else {
  310. this.appointmentAuth()
  311. }
  312. break;
  313. }
  314. },
  315. // 预约前判断是否存在一位检测人
  316. appointmentAuth () {
  317. if (this.data.documentVos.length == 0) {
  318. wx.showModal({
  319. content: '预约用户前需要先添加检测人员信息',
  320. cancelColor: '#666',
  321. confirmColor: '#333',
  322. success (res) {
  323. if (res.confirm) {
  324. wx.navigateTo({
  325. url: '/pages/createFile/createFile?from=index'
  326. })
  327. }
  328. }
  329. })
  330. } else {
  331. wx.navigateTo({
  332. url: '/pages/appointment/appointment?from=index',
  333. })
  334. }
  335. },
  336. // 点击全部档案
  337. handleAllFile () {
  338. wx.navigateTo({
  339. url: '/pages/myFile/myFile',
  340. })
  341. },
  342. // 点击查看报告
  343. handleRepoetDetail (e) {
  344. var reportid = e.currentTarget.dataset.reportid
  345. wx.navigateTo({
  346. url: '/pages/reportDetail/reportDetail?reportid=' + reportid
  347. })
  348. },
  349. // 添加检测人
  350. handleAddCheck () {
  351. wx.navigateTo({
  352. url: '/pages/createFile/createFile?form=index',
  353. })
  354. },
  355. /* 车子的图片 做跳转交互,跳转做判断,0次则跳出去充值弹框,充值弹窗有两个选择 :若激活卡,则跳转至实体卡兑换页面;若充值,则跳转至体验卡购买页面 */
  356. handlActions () {
  357. if (this.data.showTopBar) {
  358. this.stopAppointment()
  359. } else {
  360. if (this.data.useNumber == 0) {
  361. wx.showModal({
  362. title: '去充值',
  363. cancelColor: '#666',
  364. cancelText: '激活卡',
  365. confirmText: '去充值',
  366. confirmColor: '#333',
  367. success (res) {
  368. if (res.confirm) {
  369. wx.navigateTo({
  370. url: '/pages/buy/buy?from=index',
  371. })
  372. } else if (res.cancel) {
  373. wx.navigateTo({
  374. url: '/pages/exchange/exchange',
  375. })
  376. }
  377. }
  378. })
  379. } else {
  380. this.appointmentAuth()
  381. }
  382. }
  383. },
  384. stopAppointment () {
  385. wx.showModal({
  386. content: '您预约的健康筛查还未体验,请先体验',
  387. cancelColor: '#666',
  388. cancelText: '取消',
  389. confirmText: '我的预约',
  390. confirmColor: '#333',
  391. success (res) {
  392. if (res.confirm) {
  393. wx.navigateTo({
  394. url: '/pages/myAppointment/myAppointment',
  395. })
  396. }
  397. }
  398. })
  399. },
  400. handleShowQRCode (e) {
  401. var that = this
  402. var id = e.currentTarget.dataset.id
  403. var realname = e.currentTarget.dataset.realname
  404. this.setData({
  405. qrUserName: realname
  406. })
  407. wx.showLoading({
  408. title: '生成中...'
  409. })
  410. createQRcode({ documentId: id }).then(res => {
  411. wx.hideLoading()
  412. that.setData({
  413. QRCodeBase64: that.data.baseStr + res.data,
  414. qrcodeDialog: true
  415. })
  416. }).catch(e => {
  417. wx.hideLoading()
  418. wx.showModal({
  419. content: e,
  420. confirmColor: '#333',
  421. showCancel: false
  422. })
  423. })
  424. },
  425. handleCloseQRCode (e) {
  426. this.setData({
  427. qrcodeDialog: false,
  428. QRCodeBase64: ''
  429. })
  430. },
  431. onShareAppMessage () {
  432. return {
  433. title: '3分钟500+项健康指标',
  434. path: '/pages/index/index',
  435. imageUrl: '../../imaes/share.jpg'
  436. }
  437. },
  438. setStorage (response) {
  439. const accessToken = response.accessToken
  440. const isNeedPhone = response.isNeedPhone
  441. const isNeedHeadImg = response.headImg ? false : true
  442. const headImg = response.headImg
  443. const userName = response.userName
  444. const phoneNumber = response.phoneNumber
  445. app.globalData.accessToken = accessToken
  446. app.globalData.isNeedPhone = isNeedPhone
  447. app.globalData.isNeedHeadImg = isNeedHeadImg
  448. app.globalData.userInfo.headImg= headImg
  449. app.globalData.userInfo.userName = userName
  450. app.globalData.userInfo.phoneNumber = phoneNumber
  451. wx.setStorageSync('accessToken', accessToken)
  452. wx.setStorageSync('isNeedPhone', isNeedPhone)
  453. wx.setStorageSync('isNeedHeadImg', isNeedHeadImg)
  454. wx.setStorageSync('headImg', headImg)
  455. wx.setStorageSync('userName', userName)
  456. wx.setStorageSync('phoneNumber', phoneNumber)
  457. this.handleUserLoginRes()
  458. }
  459. })