index.js 10 KB

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