index.js 11 KB

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