my.js 10 KB

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