index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // showTips: false
  37. },
  38. getPhoneNumber(e) {
  39. let that = this;
  40. if (e.detail.encryptedData) {
  41. const {
  42. iv,
  43. encryptedData
  44. } = e.detail;
  45. const {
  46. param,
  47. item = {}
  48. } = e.currentTarget.dataset;
  49. wx.login({
  50. success: res => {
  51. const {
  52. code
  53. } = res;
  54. app.$http.postForm('/user/bindingMobile', {
  55. code,
  56. encryptedData,
  57. iv
  58. }).then(({
  59. status,
  60. data = {}
  61. }) => {
  62. if (status === 0) {
  63. // 绑定成功
  64. if (this.data.userInfo.deviceId) {
  65. wx.navigateTo({
  66. url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
  67. })
  68. }
  69. // that.setData({
  70. // showTips: false,
  71. // weixinLogin:true
  72. // })
  73. wx.showToast({
  74. title: '授权成功',
  75. icon: 'none',
  76. duration: 2000
  77. })
  78. that.setData({
  79. weixinLogin:true
  80. })
  81. return;
  82. }
  83. // 绑定成功未注册
  84. else {
  85. wx.showToast({
  86. title: '您尚未注册,请到平台端开通账号后重试',
  87. icon: 'none',
  88. duration: 2000
  89. })
  90. return;
  91. // that.setData({
  92. // showTips: true,
  93. // weixinLogin:true
  94. // })
  95. }
  96. // wx.setStorage({
  97. // key: 'isLogin',
  98. // data: true
  99. // })
  100. })
  101. }
  102. })
  103. }
  104. },
  105. gotopage(e) {
  106. const {
  107. param,
  108. item = {}
  109. } = e.currentTarget.dataset;
  110. if (!this.data.weixinLogin) {
  111. wx.showToast({
  112. title: '请您先进行授权',
  113. icon: 'none',
  114. duration: 2000
  115. })
  116. return;
  117. }
  118. console.log("app的值",app)
  119. if (!this.data.userInfo.userNumber && param != 'customermanage' && item.id != 'customermanage') {
  120. wx.showToast({
  121. title: '请先绑定你的户号',
  122. icon: 'none',
  123. duration: 2000
  124. })
  125. return;
  126. }
  127. wx.navigateTo({
  128. url: `/pages/${item.id || param}/${item.id || param}${item.url ? item.url : ''}`
  129. })
  130. },
  131. getAccountInfo() {
  132. this.setData({
  133. onQuery: true
  134. })
  135. app.$util.selfUserInfo(app.globalData, true).then((data) => {
  136. if (data) {
  137. this.setData({
  138. userInfo: data,
  139. waterInfo: data.waterStages
  140. })
  141. } else {
  142. this.setData({
  143. userInfo: {},
  144. waterInfo: {}
  145. })
  146. }
  147. this.setData({
  148. onQuery: false
  149. })
  150. }).catch(() => {
  151. this.setData({
  152. onQuery: false
  153. })
  154. })
  155. },
  156. // 获取用户注册状态 /user/getUser
  157. isRegister(){
  158. let that=this;
  159. app.$http.postForm('/user/getUser').then(({ status, msg, data }) => {
  160. // 已授权但客户不存在
  161. // 未授权
  162. if(status===-602){
  163. that.setData({
  164. // showTips: true,
  165. weixinLogin:false
  166. })
  167. }else{
  168. that.setData({
  169. showTips: false,
  170. weixinLogin:true
  171. })
  172. }
  173. })
  174. },
  175. onShow() {
  176. this.getAccountInfo();
  177. this.isRegister()
  178. },
  179. toLogin(msg) {
  180. this.popup && this.popup.setData({
  181. type: 'error',
  182. title: '提示',
  183. content: msg,
  184. showCancel: false
  185. });
  186. this.popup.show();
  187. },
  188. cancelEvent: function () {
  189. this.popup.close();
  190. },
  191. confirmEvent: function () {
  192. this.getAccountInfo(true);
  193. },
  194. /**
  195. * 生命周期函数--监听页面初次渲染完成
  196. */
  197. onReady: function () {
  198. //获得popup组件
  199. this.popup = this.selectComponent("#popup");
  200. },
  201. })