index.js 12 KB

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