index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. }).catch(e => {
  276. wx.hideLoading()
  277. wx.showModal({
  278. content: e,
  279. confirmColor: '#333',
  280. showCancel: false
  281. })
  282. })
  283. appointmentList({
  284. status: 1,
  285. currentPage: 1
  286. }).then(res => {
  287. console.log(res, 'appointmentList');
  288. if (res.data.vos.length !== 0) {
  289. that.setData({
  290. showTopBar: true,
  291. topBarObj: res.data.vos[0]
  292. })
  293. } else {
  294. that.setData({
  295. showTopBar: false,
  296. topBarObj: null
  297. })
  298. }
  299. }).catch(e => {
  300. })
  301. },
  302. /**
  303. * 处理套餐 购买 or 预约
  304. * useNumber: 0-购买 1-预约
  305. * **/
  306. handlePackage () {
  307. var type = this.data.useNumber > 0 ? 1 : 0
  308. switch (type) {
  309. case 0:
  310. wx.navigateTo({
  311. url: '/pages/buy/buy?from=index',
  312. })
  313. break;
  314. case 1:
  315. this.appointmentAuth()
  316. break;
  317. }
  318. },
  319. // 预约前判断是否存在一位检测人
  320. appointmentAuth () {
  321. if (this.data.documentVos.length == 0) {
  322. wx.showModal({
  323. content: '预约用户前需要先添加检测人员信息',
  324. cancelColor: '#666',
  325. confirmColor: '#333',
  326. success (res) {
  327. if (res.confirm) {
  328. wx.navigateTo({
  329. url: '/pages/createFile/createFile?from=index'
  330. })
  331. }
  332. }
  333. })
  334. } else {
  335. wx.navigateTo({
  336. url: '/pages/appointment/appointment?from=index',
  337. })
  338. }
  339. },
  340. // 点击全部档案
  341. handleAllFile () {
  342. wx.navigateTo({
  343. url: '/pages/myFile/myFile',
  344. })
  345. },
  346. // 点击查看报告
  347. handleRepoetDetail (e) {
  348. var reportid = e.currentTarget.dataset.reportid
  349. wx.navigateTo({
  350. url: '/pages/reportDetail/reportDetail?reportid=' + reportid
  351. })
  352. },
  353. // 添加检测人
  354. handleAddCheck () {
  355. wx.navigateTo({
  356. url: '/pages/createFile/createFile?form=index',
  357. })
  358. },
  359. /* 车子的图片 做跳转交互,跳转做判断,0次则跳出去充值弹框,充值弹窗有两个选择 :若激活卡,则跳转至实体卡兑换页面;若充值,则跳转至体验卡购买页面 */
  360. handlActions () {
  361. if (this.data.useNumber == 0) {
  362. wx.showModal({
  363. title: '去充值',
  364. cancelColor: '#666',
  365. cancelText: '激活卡',
  366. confirmText: '去充值',
  367. confirmColor: '#333',
  368. success (res) {
  369. if (res.confirm) {
  370. wx.navigateTo({
  371. url: '/pages/buy/buy?from=index',
  372. })
  373. } else if (res.cancel) {
  374. wx.navigateTo({
  375. url: '/pages/exchange/exchange',
  376. })
  377. }
  378. }
  379. })
  380. } else {
  381. this.appointmentAuth()
  382. }
  383. },
  384. handleShowQRCode (e) {
  385. var that = this
  386. var id = e.currentTarget.dataset.id
  387. var realname = e.currentTarget.dataset.realname
  388. this.setData({
  389. qrUserName: realname
  390. })
  391. wx.showLoading({
  392. title: '生成中...'
  393. })
  394. createQRcode({ documentId: id }).then(res => {
  395. wx.hideLoading()
  396. that.setData({
  397. QRCodeBase64: that.data.baseStr + res.data,
  398. qrcodeDialog: true
  399. })
  400. }).catch(e => {
  401. wx.hideLoading()
  402. wx.showModal({
  403. content: e,
  404. confirmColor: '#333',
  405. showCancel: false
  406. })
  407. })
  408. },
  409. handleCloseQRCode (e) {
  410. this.setData({
  411. qrcodeDialog: false,
  412. QRCodeBase64: ''
  413. })
  414. }
  415. })