my.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // pages/my/my.js
  2. import itt from '../../utils/util'
  3. import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
  4. import { getAccountNmber } from '../../api/appointment'
  5. import { userUploadHeadImg } from '../../api/upload'
  6. import { documentList } from "../../api/document"
  7. const app = getApp()
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. overshow: false,
  14. pageLogin: false,
  15. isRelogin: false, // 是否为设置退出登录重新登录
  16. showPhoneAuthBtn: true,
  17. showAvaNick: false,
  18. isNeedPhoneBtn: false, // 是否需要显示手机号授权
  19. isNeedHeadImgBtn: false,// 是否需要显示用户头像 昵称 授权
  20. isRegister: false,
  21. avatarUrl: '',
  22. confirmAvatarUrl: '',
  23. confirmNickname: '',
  24. nickname: '',
  25. phone: '',
  26. count: 0,
  27. gridList: [
  28. {
  29. id: 0,
  30. title: '我的地址',
  31. url: '/pages/address/address?form=my&back=0',
  32. imgUrl: '../../imaes/dizhi@2x.png'
  33. },
  34. {
  35. id: 1,
  36. title: '我的档案',
  37. url: '/pages/myFile/myFile?form=my',
  38. imgUrl: '../../imaes/dangan@2x.png'
  39. },
  40. {
  41. id: 2,
  42. title: '我的预约',
  43. url: '/pages/myAppointment/myAppointment?form=my',
  44. imgUrl: '../../imaes/yuyue@2x.png'
  45. },
  46. {
  47. id: 7,
  48. title: '实体卡兑换',
  49. url: '/pages/exchange/exchange?form=my',
  50. imgUrl: '../../imaes/chongzhi.png'
  51. },
  52. {
  53. id: 3,
  54. title: '我的记录',
  55. url: '/pages/myRecord/myRecord?form=my',
  56. imgUrl: '../../imaes/jilu@2x.png'
  57. },
  58. {
  59. id: 4,
  60. title: '我的报告',
  61. url: '/pages/report/report?form=my',
  62. imgUrl: '../../imaes/baogao2@2x.png'
  63. },
  64. /* {
  65. id: 5,
  66. title: '开通区域',
  67. url: '/pages/openArea/openArea?form=my',
  68. imgUrl: '../../imaes/quyu@2x.png'
  69. }, */
  70. {
  71. id: 7,
  72. title: '设置',
  73. url: '/pages/set/set?form=my',
  74. imgUrl: '../../imaes/shezhi@2x.png'
  75. },
  76. /* {
  77. id: 7,
  78. title: '测试页面',
  79. url: '/pages/aatest/test',
  80. imgUrl: '../../imaes/shezhi@2x.png'
  81. } */
  82. ]
  83. },
  84. /**
  85. * 生命周期函数--监听页面加载
  86. * 调用login接口,判断是否需要绑定手机号和头像昵称
  87. */
  88. onLoad(options) {
  89. app.globalData.selectedInex = 2
  90. this.userLogin()
  91. },
  92. // 获取用户档案信息
  93. getDocumentList () {
  94. var that = this
  95. wx.showLoading({
  96. title: '加载中...',
  97. mask: true
  98. })
  99. documentList({
  100. currentPage: 1
  101. }).then(res => {
  102. wx.hideLoading()
  103. const response = res.data.vos || []
  104. if (response.length == 0) {
  105. wx.showModal({
  106. content: '预约用户前需要先添加检测人员信息',
  107. cancelColor: '#666',
  108. confirmColor: '#333',
  109. success (res) {
  110. if (res.confirm) {
  111. wx.navigateTo({
  112. url: '/pages/createFile/createFile?from=index'
  113. })
  114. }
  115. }
  116. })
  117. } else {
  118. that.authNavTo('/pages/appointment/appointment')
  119. }
  120. }).catch(e => {
  121. wx.hideLoading()
  122. wx.showModal({
  123. content: e,
  124. confirmColor: '#333',
  125. showCancel: false
  126. })
  127. })
  128. },
  129. // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效
  130. getUserinfo () {
  131. var that = this
  132. wx.getUserProfile({
  133. desc: '用于获取用户权益'
  134. }).then(res => {
  135. const _avatarUrl = res.userInfo.avatarUrl
  136. const _nickName = res.userInfo.nickName
  137. that.setData({
  138. avatarUrl: _avatarUrl,
  139. nickname: _nickName
  140. })
  141. that.handleConfirmNickname()
  142. }).catch(e => {
  143. wx.showToast({
  144. title: '授权失败',
  145. icon: 'error'
  146. })
  147. })
  148. },
  149. // 退出登录后重新登录
  150. handleReLogin () {
  151. this.userLogin()
  152. },
  153. // 通过 code 获取 token
  154. userLogin () {
  155. var that = this
  156. wx.login({
  157. success (loginCode) {
  158. if (loginCode.code) {
  159. var data = {
  160. code: loginCode.code
  161. }
  162. wx.showLoading({
  163. title: '登录中...',
  164. mask: true
  165. })
  166. ittLogin(data).then(res => {
  167. wx.hideLoading()
  168. app.globalData.accessToken = res.data.accessToken
  169. app.globalData.userInfo.headImg= res.data.headImg
  170. app.globalData.userInfo.userName = res.data.userName
  171. app.globalData.userInfo.phoneNumber = res.data.phoneNumber
  172. const resIsNeedPhone = res.data.isNeedPhone
  173. const resHeadImg = res.data.headImg
  174. that.getAccountNmberFn()
  175. if (!resIsNeedPhone && resHeadImg !== '') {
  176. app.globalData.userInfo.login = true
  177. that.setData({
  178. isNeedHeadImgBtn: true,
  179. confirmAvatarUrl: app.globalData.userInfo.headImg,
  180. confirmNickname: app.globalData.userInfo.userName,
  181. })
  182. } else {
  183. if (resIsNeedPhone) { // true 代表需要手机号授权
  184. that.setData({
  185. isNeedPhoneBtn: true
  186. })
  187. } else {
  188. if (resHeadImg == '') {
  189. // 需要授权头像
  190. that.setData({
  191. overshow: true,
  192. phone: res.data,
  193. isNeedPhoneBtn: false,
  194. isNeedHeadImgBtn: true
  195. })
  196. } else {
  197. that.setData({
  198. isNeedHeadImgBtn: true,
  199. confirmAvatarUrl: app.globalData.userInfo.headImg,
  200. confirmNickname: app.globalData.userInfo.userName,
  201. })
  202. }
  203. }
  204. }
  205. }).catch(e => {
  206. wx.hideLoading()
  207. })
  208. }
  209. }
  210. })
  211. },
  212. // 获取用户剩余检测机会
  213. getAccountNmberFn () {
  214. var that = this
  215. getAccountNmber({}).then(res => {
  216. that.setData({
  217. count: res.data
  218. })
  219. })
  220. },
  221. // 用户本地选择图片头像
  222. onChooseAvatar(e) {
  223. const { avatarUrl } = e.detail
  224. this.uploadImg(avatarUrl)
  225. },
  226. // 上传用户头像文件到后台
  227. uploadImg (avatarUrl) {
  228. var that = this
  229. var data = {
  230. filePath: avatarUrl
  231. }
  232. wx.showLoading({
  233. title: '上传中...',
  234. mask: true
  235. })
  236. userUploadHeadImg(data).then(res => {
  237. wx.hideLoading()
  238. var response = JSON.parse(res)
  239. if (response.code != '000') {
  240. wx.showToast({
  241. title: '头像上传失败',
  242. icon: 'error'
  243. })
  244. } else {
  245. that.setData({
  246. avatarUrl: response.data
  247. })
  248. }
  249. }).catch(e => {
  250. wx.hideLoading()
  251. wx.showModal({
  252. content: e,
  253. confirmColor: '#333',
  254. showCancel: false
  255. })
  256. })
  257. },
  258. // 拉起手机号授权弹窗
  259. getPhoneNumber (e) {
  260. var that = this
  261. var phonePermission = e.detail.errMsg.split(':')[1]
  262. if (phonePermission == 'ok') {
  263. var phoneCode = e.detail.code
  264. that.bindMobileFn(phoneCode)
  265. } else {
  266. wx.showToast({
  267. title: '授权失败',
  268. icon: 'error'
  269. })
  270. }
  271. },
  272. // 绑定手机号
  273. bindMobileFn (phoneCode) {
  274. var that = this
  275. var data = {
  276. mobileCode: phoneCode
  277. }
  278. wx.showLoading({
  279. title: '加载中...',
  280. mask: true
  281. })
  282. bindMobile(data).then(res => {
  283. wx.hideLoading()
  284. app.globalData.userInfo.phoneNumber = res.data
  285. if (app.globalData.userInfo.headImg == '') {
  286. // 需要授权头像
  287. that.setData({
  288. overshow: true,
  289. phone: res.data,
  290. isNeedPhoneBtn: false,
  291. isNeedHeadImgBtn: true
  292. })
  293. } else {
  294. that.setData({
  295. isNeedHeadImgBtn: true,
  296. confirmAvatarUrl: app.globalData.userInfo.headImg,
  297. confirmNickname: app.globalData.userInfo.userName,
  298. })
  299. app.globalData.userInfo.login = true
  300. }
  301. }).catch(e => {
  302. wx.hideLoading()
  303. wx.showModal({
  304. content: e,
  305. confirmColor: '#333',
  306. showCancel: false
  307. })
  308. })
  309. },
  310. // 提交用户头像和昵称到后台
  311. handleConfirmNickname () {
  312. var that = this
  313. if (that.data.avatarUrl == '') {
  314. wx.showToast({
  315. title: '请选择头像',
  316. icon: 'error'
  317. })
  318. return
  319. }
  320. if (that.data.nickname == '') {
  321. wx.showToast({
  322. title: '请输入昵称',
  323. icon: 'error'
  324. })
  325. return
  326. }
  327. var data = {
  328. avatarUrl: that.data.avatarUrl,
  329. nickName: that.data.nickname
  330. }
  331. wx.showLoading({
  332. title: '加载中...',
  333. mask: true
  334. })
  335. bindBaseInfo(data).then(res => {
  336. wx.hideLoading()
  337. app.globalData.userInfo.login = true
  338. that.setData({
  339. pageLogin: true,
  340. overshow: false,
  341. confirmAvatarUrl: that.data.avatarUrl,
  342. confirmNickname: that.data.nickname
  343. })
  344. app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
  345. app.globalData.userInfo.userName = that.data.confirmNickname
  346. }).catch(e => {
  347. wx.hideLoading()
  348. wx.showModal({
  349. content: e,
  350. confirmColor: '#333',
  351. showCancel: false
  352. })
  353. })
  354. },
  355. /**
  356. * 生命周期函数--监听页面显示
  357. */
  358. onShow() {
  359. this.setData({
  360. pageLogin: app.globalData.userInfo.login
  361. })
  362. },
  363. // 路由跳转
  364. handleNavTo (e) {
  365. var url = e.currentTarget.dataset.url
  366. this.authNavTo(url)
  367. },
  368. // 去充值
  369. handleCharge () {
  370. this.authNavTo('/pages/buy/buy?from=my')
  371. },
  372. // 立即预约
  373. handleAppoint () {
  374. this.getDocumentList()
  375. },
  376. /**
  377. * 路由拦截
  378. * **/
  379. authNavTo (url) {
  380. if (itt.loginAuth()) {
  381. if (url.indexOf('/report') !== -1) {
  382. app.globalData.selectedInex = 1
  383. wx.switchTab({
  384. url: url,
  385. })
  386. } else {
  387. wx.navigateTo({
  388. url: url,
  389. })
  390. }
  391. } else {
  392. itt.errorToast('请先授权登录')
  393. }
  394. }
  395. })