my.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // pages/my/my.js
  2. import itt from '../../utils/util'
  3. import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
  4. const app = getApp()
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. overshow: false,
  11. pageLogin: false,
  12. showPhoneAuthBtn: true,
  13. showAvaNick: false,
  14. isNeedPhone: false,
  15. isRegister: false,
  16. avatarUrl: '',
  17. confirmAvatarUrl: '',
  18. confirmNickname: '点击获取头像昵称',
  19. nickname: '',
  20. phone: '',
  21. count: 0,
  22. gridList: [
  23. {
  24. id: 0,
  25. title: '我的地址',
  26. url: '/pages/address/address?form=my&back=0',
  27. imgUrl: '../../imaes/dizhi@2x.png'
  28. },
  29. {
  30. id: 1,
  31. title: '我的档案',
  32. url: '/pages/myFile/myFile?form=my',
  33. imgUrl: '../../imaes/dangan@2x.png'
  34. },
  35. {
  36. id: 2,
  37. title: '我的预约',
  38. url: '/pages/myAppointment/myAppointment?form=my',
  39. imgUrl: '../../imaes/yuyue@2x.png'
  40. },
  41. {
  42. id: 3,
  43. title: '我的记录',
  44. url: '/pages/myRecord/myRecord?form=my',
  45. imgUrl: '../../imaes/jilu@2x.png'
  46. },
  47. {
  48. id: 4,
  49. title: '我的报告',
  50. url: '/pages/report/report?form=my',
  51. imgUrl: '../../imaes/baogao2@2x.png'
  52. },
  53. {
  54. id: 5,
  55. title: '开通区域',
  56. url: '/pages/openArea/openArea?form=my',
  57. imgUrl: '../../imaes/quyu@2x.png'
  58. },
  59. {
  60. id: 6,
  61. title: '设置',
  62. url: '/pages/set/set?form=my',
  63. imgUrl: '../../imaes/shezhi@2x.png'
  64. }
  65. ]
  66. },
  67. /**
  68. * 生命周期函数--监听页面加载
  69. * 调用login接口,判断是否需要绑定手机号和头像昵称
  70. */
  71. onLoad(options) {
  72. this.userLogin()
  73. },
  74. // 通过 code 获取 token
  75. userLogin () {
  76. var that = this
  77. wx.login({
  78. success (res) {
  79. if (res.code) {
  80. var data = {
  81. code: res.code
  82. }
  83. ittLogin(data).then(loginRes => {
  84. app.globalData.accessToken = loginRes.data.accessToken
  85. that.setData({
  86. isNeedPhone: loginRes.data.isNeedPhone,
  87. isRegister: loginRes.data.isRegister
  88. })
  89. if (loginRes.data.isNeedPhone) {
  90. that.setData({
  91. showPhoneAuthBtn: true,
  92. showAvaNick: false
  93. })
  94. } else {
  95. app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
  96. if (loginRes.data.isRegister) {
  97. that.setData({
  98. showPhoneAuthBtn: false,
  99. showAvaNick: true
  100. })
  101. } else {
  102. // 全部授权
  103. that.setData({
  104. showPhoneAuthBtn: false,
  105. showAvaNick: true,
  106. pageLogin: true,
  107. confirmAvatarUrl: loginRes.data.headImg,
  108. confirmNickname: loginRes.data.userName.length > 4 ? loginRes.data.userName.substring(0,4) : loginRes.data.userName
  109. })
  110. app.globalData.userInfo.login = true
  111. app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
  112. app.globalData.userInfo.headImg = loginRes.data.headImg
  113. app.globalData.userInfo.userName = loginRes.data.userName
  114. }
  115. }
  116. }).catch(e => {
  117. console.log(e, 'ittlogin');
  118. })
  119. }
  120. }
  121. })
  122. },
  123. onChooseAvatar(e) {
  124. const { avatarUrl } = e.detail
  125. this.setData({
  126. avatarUrl,
  127. })
  128. },
  129. // 拉起用户头像和昵称
  130. getNickName () {
  131. if (!this.data.pageLogin) {
  132. this.setData({
  133. overshow: true
  134. })
  135. }
  136. },
  137. // 拉起手机号授权弹窗
  138. getPhoneNumber (e) {
  139. var that = this
  140. var phoneCode = e.detail.code
  141. wx.login({
  142. success (res) {
  143. if (res.code) {
  144. let data = {
  145. code: res.code
  146. }
  147. ittLogin(data).then(ittLoginRes => {
  148. that.bindMobileFn(phoneCode, ittLoginRes)
  149. }).catch(e => {
  150. console.log(e, 'ittlogin');
  151. })
  152. }
  153. }
  154. })
  155. },
  156. // 绑定手机号
  157. // isRegister = true 代表第一次进入显示获取头像
  158. bindMobileFn (phoneCode, ittLoginRes) {
  159. var that = this
  160. var data = {
  161. mobileCode: phoneCode
  162. }
  163. bindMobile(data).then(res => {
  164. app.globalData.userInfo.phoneNumber = res.data
  165. if (that.data.isRegister) {
  166. that.setData({
  167. overshow: true,
  168. phone: res.data,
  169. showPhoneAuthBtn: false,
  170. showAvaNick: true
  171. })
  172. } else {
  173. that.setData({
  174. phone: res.data,
  175. showPhoneAuthBtn: false,
  176. showAvaNick: true,
  177. pageLogin: true,
  178. avatarUrl: ittLoginRes.data.headImg,
  179. nickname: ittLoginRes.data.userName.length > 4 ? ittLoginRes.data.userName.substring(0,4) : ittLoginRes.data.userName
  180. })
  181. app.globalData.userInfo.login = true
  182. app.globalData.userInfo.headImg = ittLoginRes.data.headImg
  183. app.globalData.userInfo.userName = ittLoginRes.data.userName
  184. }
  185. })
  186. },
  187. bindNickNameInput (e) {
  188. this.setData({
  189. nickname: e.detail.value
  190. })
  191. },
  192. // 绑定用户头像和昵称
  193. handleConfirmNickname () {
  194. var that = this
  195. var data = {
  196. avatarUrl: that.data.avatarUrl,
  197. nickName: that.data.nickname
  198. }
  199. bindBaseInfo(data).then(res => {
  200. app.globalData.userInfo.login = true
  201. that.setData({
  202. pageLogin: true,
  203. overshow: false,
  204. confirmAvatarUrl: that.data.avatarUrl,
  205. confirmNickname: that.data.nickname.length > 4 ? that.data.nickname.substring(0,4) : that.data.nickname
  206. })
  207. app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
  208. app.globalData.userInfo.userName = that.data.confirmNickname
  209. })
  210. },
  211. /**
  212. * 生命周期函数--监听页面显示
  213. */
  214. onShow() {
  215. this.setData({
  216. pageLogin: app.globalData.userInfo.login
  217. })
  218. },
  219. // 路由跳转
  220. handleNavTo (e) {
  221. var url = e.currentTarget.dataset.url
  222. this.authNavTo(url)
  223. },
  224. /**
  225. * 路由拦截
  226. * **/
  227. authNavTo (url) {
  228. if (itt.loginAuth()) {
  229. wx.navigateTo({
  230. url: url,
  231. })
  232. } else {
  233. itt.errorToast('请先授权登录')
  234. }
  235. }
  236. })