center.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // pages/ucenter/center/center.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. userInfo: {},
  8. isLogin: false,
  9. sexType: [{
  10. value: 1,
  11. text: '男'
  12. }, {
  13. value: 2,
  14. text: '女'
  15. }],
  16. sexIndex: null,
  17. phone: null,
  18. email: null,
  19. checkPass: false,
  20. errorMsg: ''
  21. },
  22. inputSex(e) {
  23. //console.log(this.data.sexType[e.detail.value]);
  24. this.setData({
  25. sexIndex: e.detail.value
  26. });
  27. this.checkAll();
  28. },
  29. inputPhone(e) {
  30. this.setData({
  31. phone: e.detail.value
  32. });
  33. this.checkAll();
  34. },
  35. inputEmail(e) {
  36. this.setData({
  37. email: e.detail.value
  38. });
  39. this.checkAll();
  40. },
  41. checkPhone() {
  42. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  43. return false;
  44. }
  45. return true;
  46. },
  47. checkMail() {
  48. if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(this.data.email))) {
  49. return false;
  50. }
  51. return true;
  52. },
  53. checkAll() {
  54. if (this.data.userInfo.sex == null || (this.data.userInfo.sex != 1 && this.data.userInfo.sex != 2)){
  55. if (!this.data.sexIndex) {
  56. this.setData({
  57. errorMsg: '请选择性别'
  58. });
  59. this.setData({
  60. checkPass: false
  61. });
  62. return;
  63. } else {
  64. this.setData({
  65. errorMsg: ''
  66. });
  67. }
  68. }
  69. if (!this.checkPhone()) {
  70. this.setData({
  71. errorMsg: '手机号输入有误,请重新输入'
  72. });
  73. this.setData({
  74. checkPass: false
  75. });
  76. return;
  77. } else {
  78. this.setData({
  79. errorMsg: ''
  80. });
  81. }
  82. if (!this.checkMail()) {
  83. this.setData({
  84. errorMsg: '邮箱输入有误,请重新输入'
  85. });
  86. this.setData({
  87. checkPass: false
  88. });
  89. return;
  90. } else {
  91. this.setData({
  92. errorMsg: ''
  93. });
  94. }
  95. this.setData({
  96. checkPass: true
  97. });
  98. },
  99. bindUpdateUser() {
  100. let that = this;
  101. let loginName = this.data.userInfo.loginName;
  102. let {
  103. sexIndex,
  104. phone,
  105. email
  106. } = this.data;
  107. var sex = null;
  108. if (sexIndex!=null){
  109. sex = this.data.sexType[sexIndex].value;
  110. }else{
  111. sex = this.data.userInfo.sex;
  112. }
  113. wx.request({
  114. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/updateUser',
  115. data: {
  116. loginName,
  117. sex: sex,
  118. phonenumber: phone,
  119. email,
  120. },
  121. method: 'POST',
  122. success(res1) {
  123. //console.log(res1);
  124. //console.log(res1.data.code == 1);
  125. if (res1.data.code == 1) {
  126. that.setData({
  127. errorMsg: res1.data.msg
  128. })
  129. } else {
  130. //返回该用户
  131. //跳转到上一页
  132. wx.setStorageSync("userInfo", res1.data);
  133. wx.setStorageSync("isLogin", true);
  134. wx.navigateBack();
  135. //let url = `/pages/ucenter/index/index`;
  136. //wx.redirectTo({
  137. //url
  138. //});
  139. }
  140. }
  141. });
  142. },
  143. /**
  144. * 生命周期函数--监听页面加载
  145. */
  146. onLoad: function(options) {
  147. },
  148. /**
  149. * 生命周期函数--监听页面初次渲染完成
  150. */
  151. onReady: function() {
  152. },
  153. /**
  154. * 生命周期函数--监听页面显示
  155. */
  156. onShow: function() {
  157. let userInfo = wx.getStorageSync('userInfo');
  158. let isLogin = wx.getStorageSync('isLogin');
  159. // 页面显示
  160. if (userInfo && isLogin) {
  161. //userInfo.flag = true;
  162. //console.log(userInfo);
  163. this.setData({
  164. userInfo: userInfo,
  165. isLogin: isLogin,
  166. phone: userInfo.phonenumber,
  167. email:userInfo.email
  168. });
  169. } else {
  170. //未登录信息
  171. this.setData({
  172. userInfo: {}
  173. });
  174. }
  175. },
  176. /**
  177. * 生命周期函数--监听页面隐藏
  178. */
  179. onHide: function() {
  180. },
  181. /**
  182. * 生命周期函数--监听页面卸载
  183. */
  184. onUnload: function() {
  185. },
  186. /**
  187. * 页面相关事件处理函数--监听用户下拉动作
  188. */
  189. onPullDownRefresh: function() {
  190. },
  191. /**
  192. * 页面上拉触底事件的处理函数
  193. */
  194. onReachBottom: function() {
  195. },
  196. /**
  197. * 用户点击右上角分享
  198. */
  199. onShareAppMessage: function() {
  200. }
  201. })