index.js 12 KB

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