my.js 7.5 KB

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