index.js 11 KB

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