index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. userInfo: {},
  7. waterInfo: {},
  8. onQuery: true,
  9. items: [{
  10. id: 'realtimewater',
  11. label: '用水量',
  12. icon: '/static/img/icon-water.png',
  13. url: '?period=7'
  14. }, {
  15. id: 'waterprice',
  16. label: '水价查询',
  17. icon: '/static/img/icon_water_price.png'
  18. }, {
  19. id: 'payment',
  20. label: '缴费',
  21. icon: '/static/img/icon-payment.png'
  22. }, {
  23. id: 'wateranalysis',
  24. label: '用水趋势',
  25. icon: '/static/img/icon_water_analysis.png'
  26. }, {
  27. id: 'waterabnormal',
  28. label: '异常预警',
  29. icon: '/static/img/icon_water_abnormal.png'
  30. }, {
  31. id: 'customermanage',
  32. label: '户号管理',
  33. icon: '/static/img/icon_customer_manage.png'
  34. }],
  35. weixinLogin: false,
  36. },
  37. getPhoneNumber(e) {
  38. let that = this;
  39. if (e.detail.encryptedData) {
  40. const {
  41. iv,
  42. encryptedData
  43. } = e.detail;
  44. const {
  45. param,
  46. item = {}
  47. } = e.currentTarget.dataset;
  48. wx.login({
  49. success: res => {
  50. const {
  51. code
  52. } = res;
  53. app.$http.postForm('/user/bindingMobile', {
  54. code,
  55. encryptedData,
  56. iv
  57. }).then(({
  58. status,
  59. data = {}
  60. }) => {
  61. if (status === 0) {
  62. // 绑定成功
  63. if (this.data.userInfo.deviceId) {
  64. wx.navigateTo({
  65. url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
  66. })
  67. }
  68. wx.showToast({
  69. title: '授权成功',
  70. icon: 'none',
  71. duration: 2000
  72. })
  73. that.setData({
  74. weixinLogin:true
  75. })
  76. return;
  77. }
  78. // 绑定成功未注册
  79. else {
  80. wx.showToast({
  81. title: '您尚未注册,请到平台端开通账号后重试',
  82. icon: 'none',
  83. duration: 2000
  84. })
  85. return;
  86. }
  87. })
  88. }
  89. })
  90. }
  91. },
  92. gotopage(e) {
  93. const {
  94. param,
  95. item = {}
  96. } = e.currentTarget.dataset;
  97. if (!this.data.weixinLogin) {
  98. wx.showToast({
  99. title: '请您先进行授权',
  100. icon: 'none',
  101. duration: 2000
  102. })
  103. return;
  104. }
  105. console.log("app的值",app)
  106. if (!this.data.userInfo.userNumber && param != 'customermanage' && item.id != 'customermanage') {
  107. wx.showToast({
  108. title: '请先绑定你的户号',
  109. icon: 'none',
  110. duration: 2000
  111. })
  112. return;
  113. }
  114. wx.navigateTo({
  115. url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
  116. })
  117. },
  118. getAccountInfo() {
  119. this.setData({
  120. onQuery: true
  121. })
  122. app.$util.selfUserInfo(app.globalData, true).then((data) => {
  123. if (data) {
  124. this.setData({
  125. userInfo: data,
  126. waterInfo: data.waterStages
  127. })
  128. } else {
  129. this.setData({
  130. userInfo: {},
  131. waterInfo: {}
  132. })
  133. }
  134. this.setData({
  135. onQuery: false
  136. })
  137. }).catch(() => {
  138. this.setData({
  139. onQuery: false
  140. })
  141. })
  142. },
  143. // 获取用户注册状态 /user/getUser
  144. isRegister(){
  145. let that=this;
  146. app.$http.postForm('/user/getUser').then(({ status, msg, data }) => {
  147. // 已授权但客户不存在
  148. // 未授权
  149. if(status===-602){
  150. that.setData({
  151. weixinLogin:false
  152. })
  153. }else{
  154. that.setData({
  155. weixinLogin:true
  156. })
  157. }
  158. })
  159. },
  160. onShow() {
  161. this.getAccountInfo();
  162. this.isRegister()
  163. },
  164. toLogin(msg) {
  165. this.popup && this.popup.setData({
  166. type: 'error',
  167. title: '提示',
  168. content: msg,
  169. showCancel: false
  170. });
  171. this.popup.show();
  172. },
  173. cancelEvent: function () {
  174. this.popup.close();
  175. },
  176. confirmEvent: function () {
  177. this.getAccountInfo(true);
  178. },
  179. /**
  180. * 生命周期函数--监听页面初次渲染完成
  181. */
  182. onReady: function () {
  183. //获得popup组件
  184. this.popup = this.selectComponent("#popup");
  185. },
  186. })