index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. that.setData({
  291. showTopBar: true,
  292. topBarObj: res.data.vos[0]
  293. })
  294. app.globalData.hasAppointment = true
  295. } else {
  296. that.setData({
  297. showTopBar: false,
  298. topBarObj: null
  299. })
  300. }
  301. })
  302. // 获取图片资源
  303. appGetConfigImg({
  304. 'imgType': '5'
  305. }).then(configRes => {
  306. console.log(configRes.data[0].imgUrl, 'configRes')
  307. that.setData({
  308. homepageImg: configRes.data[0].imgUrl
  309. })
  310. })
  311. },
  312. /**
  313. * 处理套餐 购买 or 预约
  314. * useNumber: 0-购买 1-预约
  315. * **/
  316. handlePackage () {
  317. var type = this.data.useNumber > 0 ? 1 : 0
  318. switch (type) {
  319. case 0:
  320. wx.navigateTo({
  321. url: '/pages/buy/buy?from=index',
  322. })
  323. break;
  324. case 1:
  325. if (this.data.showTopBar) {
  326. this.stopAppointment()
  327. } else {
  328. this.appointmentAuth()
  329. }
  330. break;
  331. }
  332. },
  333. // 预约前判断是否存在一位检测人
  334. appointmentAuth () {
  335. if (this.data.documentVos.length == 0) {
  336. wx.showModal({
  337. content: '预约用户前需要先添加检测人员信息',
  338. cancelColor: '#666',
  339. confirmColor: '#333',
  340. success (res) {
  341. if (res.confirm) {
  342. wx.navigateTo({
  343. url: '/pages/createFile/createFile?from=index'
  344. })
  345. }
  346. }
  347. })
  348. } else {
  349. wx.navigateTo({
  350. url: '/pages/appointment/appointment?from=index',
  351. })
  352. }
  353. },
  354. // 点击全部档案
  355. handleAllFile () {
  356. wx.navigateTo({
  357. url: '/pages/myFile/myFile',
  358. })
  359. },
  360. // 点击查看报告
  361. handleRepoetDetail (e) {
  362. var reportid = e.currentTarget.dataset.reportid
  363. wx.navigateTo({
  364. url: '/pages/reportDetail/reportDetail?reportid=' + reportid
  365. })
  366. },
  367. // 添加检测人
  368. handleAddCheck () {
  369. wx.navigateTo({
  370. url: '/pages/createFile/createFile?form=index',
  371. })
  372. },
  373. /* 车子的图片 做跳转交互,跳转做判断,0次则跳出去充值弹框,充值弹窗有两个选择 :若激活卡,则跳转至实体卡兑换页面;若充值,则跳转至体验卡购买页面 */
  374. handlActions () {
  375. if (this.data.showTopBar) {
  376. this.stopAppointment()
  377. } else {
  378. if (this.data.useNumber == 0) {
  379. wx.showModal({
  380. title: '去充值',
  381. cancelColor: '#666',
  382. cancelText: '激活卡',
  383. confirmText: '去充值',
  384. confirmColor: '#333',
  385. success (res) {
  386. if (res.confirm) {
  387. wx.navigateTo({
  388. url: '/pages/buy/buy?from=index',
  389. })
  390. } else if (res.cancel) {
  391. wx.navigateTo({
  392. url: '/pages/exchange/exchange',
  393. })
  394. }
  395. }
  396. })
  397. } else {
  398. this.appointmentAuth()
  399. }
  400. }
  401. },
  402. stopAppointment () {
  403. wx.showModal({
  404. content: '您预约的健康筛查还未体验,请先体验',
  405. cancelColor: '#666',
  406. cancelText: '取消',
  407. confirmText: '我的预约',
  408. confirmColor: '#333',
  409. success (res) {
  410. if (res.confirm) {
  411. wx.navigateTo({
  412. url: '/pages/myAppointment/myAppointment',
  413. })
  414. }
  415. }
  416. })
  417. },
  418. handleShowQRCode (e) {
  419. var that = this
  420. var id = e.currentTarget.dataset.id
  421. var realname = e.currentTarget.dataset.realname
  422. this.setData({
  423. qrUserName: realname
  424. })
  425. wx.showLoading({
  426. title: '生成中...'
  427. })
  428. createQRcode({ documentId: id }).then(res => {
  429. wx.hideLoading()
  430. that.setData({
  431. QRCodeBase64: that.data.baseStr + res.data,
  432. qrcodeDialog: true
  433. })
  434. }).catch(e => {
  435. wx.hideLoading()
  436. wx.showModal({
  437. content: e,
  438. confirmColor: '#333',
  439. showCancel: false
  440. })
  441. })
  442. },
  443. handleCloseQRCode (e) {
  444. this.setData({
  445. qrcodeDialog: false,
  446. QRCodeBase64: ''
  447. })
  448. }
  449. })