index.js 12 KB

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