my.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. wx.showLoading({
  84. title: '登录中...',
  85. mask: true
  86. })
  87. ittLogin(data).then(loginRes => {
  88. wx.hideLoading()
  89. app.globalData.accessToken = loginRes.data.accessToken
  90. that.setData({
  91. isNeedPhone: loginRes.data.isNeedPhone,
  92. isRegister: loginRes.data.isRegister
  93. })
  94. if (loginRes.data.isNeedPhone) {
  95. that.setData({
  96. showPhoneAuthBtn: true,
  97. showAvaNick: false
  98. })
  99. } else {
  100. app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
  101. if (loginRes.data.isRegister) {
  102. that.setData({
  103. showPhoneAuthBtn: false,
  104. showAvaNick: true
  105. })
  106. } else {
  107. // 全部授权
  108. that.setData({
  109. showPhoneAuthBtn: false,
  110. showAvaNick: true,
  111. pageLogin: true,
  112. confirmAvatarUrl: loginRes.data.headImg,
  113. confirmNickname: loginRes.data.userName.length > 4 ? loginRes.data.userName.substring(0,4) : loginRes.data.userName
  114. })
  115. app.globalData.userInfo.login = true
  116. app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
  117. app.globalData.userInfo.headImg = loginRes.data.headImg
  118. app.globalData.userInfo.userName = loginRes.data.userName
  119. }
  120. }
  121. }).catch(e => {
  122. wx.hideLoading()
  123. wx.showModal({
  124. content: e,
  125. confirmColor: '#333',
  126. showCancel: false
  127. })
  128. })
  129. }
  130. }
  131. })
  132. },
  133. onChooseAvatar(e) {
  134. const { avatarUrl } = e.detail
  135. this.setData({
  136. avatarUrl,
  137. })
  138. },
  139. // 拉起用户头像和昵称
  140. getNickName () {
  141. if (!this.data.pageLogin) {
  142. this.setData({
  143. overshow: true
  144. })
  145. }
  146. },
  147. // 拉起手机号授权弹窗
  148. getPhoneNumber (e) {
  149. var that = this
  150. var phoneCode = e.detail.code
  151. wx.login({
  152. success (res) {
  153. if (res.code) {
  154. let data = {
  155. code: res.code
  156. }
  157. wx.showLoading({
  158. title: '登录中...',
  159. mask: true
  160. })
  161. ittLogin(data).then(ittLoginRes => {
  162. wx.hideLoading()
  163. that.bindMobileFn(phoneCode, ittLoginRes)
  164. }).catch(e => {
  165. wx.hideLoading()
  166. wx.showModal({
  167. content: e,
  168. confirmColor: '#333',
  169. showCancel: false
  170. })
  171. })
  172. }
  173. }
  174. })
  175. },
  176. // 绑定手机号
  177. // isRegister = true 代表第一次进入显示获取头像
  178. bindMobileFn (phoneCode, ittLoginRes) {
  179. var that = this
  180. var data = {
  181. mobileCode: phoneCode
  182. }
  183. wx.showLoading({
  184. title: '加载中...',
  185. mask: true
  186. })
  187. bindMobile(data).then(res => {
  188. wx.hideLoading()
  189. app.globalData.userInfo.phoneNumber = res.data
  190. if (that.data.isRegister) {
  191. that.setData({
  192. overshow: true,
  193. phone: res.data,
  194. showPhoneAuthBtn: false,
  195. showAvaNick: true
  196. })
  197. } else {
  198. that.setData({
  199. phone: res.data,
  200. showPhoneAuthBtn: false,
  201. showAvaNick: true,
  202. pageLogin: true,
  203. avatarUrl: ittLoginRes.data.headImg,
  204. nickname: ittLoginRes.data.userName.length > 4 ? ittLoginRes.data.userName.substring(0,4) : ittLoginRes.data.userName
  205. })
  206. app.globalData.userInfo.login = true
  207. app.globalData.userInfo.headImg = ittLoginRes.data.headImg
  208. app.globalData.userInfo.userName = ittLoginRes.data.userName
  209. }
  210. }).catch(e => {
  211. wx.hideLoading()
  212. wx.showModal({
  213. content: e,
  214. confirmColor: '#333',
  215. showCancel: false
  216. })
  217. })
  218. },
  219. bindNickNameInput (e) {
  220. this.setData({
  221. nickname: e.detail.value
  222. })
  223. },
  224. // 绑定用户头像和昵称
  225. handleConfirmNickname () {
  226. var that = this
  227. var data = {
  228. avatarUrl: that.data.avatarUrl,
  229. nickName: that.data.nickname
  230. }
  231. wx.showLoading({
  232. title: '加载中...',
  233. mask: true
  234. })
  235. bindBaseInfo(data).then(res => {
  236. wx.hideLoading()
  237. app.globalData.userInfo.login = true
  238. that.setData({
  239. pageLogin: true,
  240. overshow: false,
  241. confirmAvatarUrl: that.data.avatarUrl,
  242. confirmNickname: that.data.nickname.length > 4 ? that.data.nickname.substring(0,4) : that.data.nickname
  243. })
  244. app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
  245. app.globalData.userInfo.userName = that.data.confirmNickname
  246. }).catch(e => {
  247. wx.hideLoading()
  248. wx.showModal({
  249. content: e,
  250. confirmColor: '#333',
  251. showCancel: false
  252. })
  253. })
  254. },
  255. /**
  256. * 生命周期函数--监听页面显示
  257. */
  258. onShow() {
  259. this.setData({
  260. pageLogin: app.globalData.userInfo.login
  261. })
  262. },
  263. // 路由跳转
  264. handleNavTo (e) {
  265. var url = e.currentTarget.dataset.url
  266. this.authNavTo(url)
  267. },
  268. /**
  269. * 路由拦截
  270. * **/
  271. authNavTo (url) {
  272. if (itt.loginAuth()) {
  273. if (url.indexOf('/report') !== -1) {
  274. app.globalData.selectedInex = '1'
  275. wx.switchTab({
  276. url: url,
  277. })
  278. } else {
  279. wx.navigateTo({
  280. url: url,
  281. })
  282. }
  283. } else {
  284. itt.errorToast('请先授权登录')
  285. }
  286. }
  287. })