index.js 10 KB

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