my.js 8.1 KB

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