my.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. getNickName () {
  194. if (!this.data.pageLogin) {
  195. this.setData({
  196. overshow: true
  197. })
  198. }
  199. },
  200. // 拉起手机号授权弹窗
  201. getPhoneNumber (e) {
  202. var that = this
  203. var phonePermission = e.detail.errMsg.split(':')[1]
  204. if (phonePermission == 'ok') {
  205. var phoneCode = e.detail.code
  206. that.bindMobileFn(phoneCode)
  207. } else {
  208. wx.showToast({
  209. title: '手机号授权失败',
  210. icon: 'error'
  211. })
  212. }
  213. },
  214. // 绑定手机号
  215. bindMobileFn (phoneCode) {
  216. var that = this
  217. var data = {
  218. mobileCode: phoneCode
  219. }
  220. wx.showLoading({
  221. title: '加载中...',
  222. mask: true
  223. })
  224. bindMobile(data).then(res => {
  225. wx.hideLoading()
  226. app.globalData.userInfo.phoneNumber = res.data
  227. if (app.globalData.userInfo.headImg == '') {
  228. // 需要授权头像
  229. that.setData({
  230. overshow: true,
  231. phone: res.data,
  232. isNeedPhoneBtn: false,
  233. isNeedHeadImgBtn: true
  234. })
  235. } else {
  236. that.setData({
  237. isNeedHeadImgBtn: true,
  238. confirmAvatarUrl: app.globalData.userInfo.headImg,
  239. confirmNickname: app.globalData.userInfo.userName,
  240. })
  241. app.globalData.userInfo.login = true
  242. }
  243. }).catch(e => {
  244. wx.hideLoading()
  245. wx.showModal({
  246. content: e,
  247. confirmColor: '#333',
  248. showCancel: false
  249. })
  250. })
  251. },
  252. // 绑定用户头像和昵称
  253. handleConfirmNickname () {
  254. var that = this
  255. if (that.data.avatarUrl == '') {
  256. wx.showToast({
  257. title: '请选择头像',
  258. icon: 'error'
  259. })
  260. return
  261. }
  262. if (that.data.nickname == '') {
  263. wx.showToast({
  264. title: '请输入昵称',
  265. icon: 'error'
  266. })
  267. return
  268. }
  269. var data = {
  270. avatarUrl: that.data.avatarUrl,
  271. nickName: that.data.nickname
  272. }
  273. wx.showLoading({
  274. title: '加载中...',
  275. mask: true
  276. })
  277. bindBaseInfo(data).then(res => {
  278. wx.hideLoading()
  279. app.globalData.userInfo.login = true
  280. that.setData({
  281. pageLogin: true,
  282. overshow: false,
  283. confirmAvatarUrl: that.data.avatarUrl,
  284. confirmNickname: that.data.nickname
  285. })
  286. app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
  287. app.globalData.userInfo.userName = that.data.confirmNickname
  288. }).catch(e => {
  289. wx.hideLoading()
  290. wx.showModal({
  291. content: e,
  292. confirmColor: '#333',
  293. showCancel: false
  294. })
  295. })
  296. },
  297. /**
  298. * 生命周期函数--监听页面显示
  299. */
  300. onShow() {
  301. this.setData({
  302. pageLogin: app.globalData.userInfo.login
  303. })
  304. },
  305. // 路由跳转
  306. handleNavTo (e) {
  307. var url = e.currentTarget.dataset.url
  308. this.authNavTo(url)
  309. },
  310. handleAppoint () {
  311. this.authNavTo('/pages/appointment/appointment')
  312. },
  313. /**
  314. * 路由拦截
  315. * **/
  316. authNavTo (url) {
  317. if (itt.loginAuth()) {
  318. if (url.indexOf('/report') !== -1) {
  319. app.globalData.selectedInex = 1
  320. wx.switchTab({
  321. url: url,
  322. })
  323. } else {
  324. wx.navigateTo({
  325. url: url,
  326. })
  327. }
  328. } else {
  329. itt.errorToast('请先授权登录')
  330. }
  331. }
  332. })