center.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // pages/ucenter/center/center.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. userInfo: {},
  8. isLogin: false,
  9. isBindPhone:0,
  10. sexType: [{
  11. value: 1,
  12. text: '男'
  13. }, {
  14. value: 2,
  15. text: '女'
  16. }, {
  17. value: 3,
  18. text: '保密'
  19. }],
  20. sexIndex: null,
  21. carNum: null,
  22. email: null,
  23. checkPass: false,
  24. errorMsg: ''
  25. },
  26. inputSex(e) {
  27. this.setData({
  28. sexIndex: parseInt(e.detail.value)+1
  29. });
  30. let userdata= {
  31. sexIndex: e.detail.value,
  32. carNum: null,
  33. email: null,
  34. };
  35. this.bindUpdateUser(userdata);
  36. },
  37. inputCarNum(e){
  38. let that = this;
  39. wx.showModal({
  40. title: '修改车牌号',
  41. showCancel:true,
  42. editable: true,
  43. confirmColor:'#00AADD',
  44. cancelColor:'#00AADD',
  45. success: function (res1) {
  46. if (res1.confirm) {
  47. let carNum = res1.content;
  48. that.confirmCarNum(carNum);
  49. }
  50. }
  51. });
  52. },
  53. confirmCarNum(carNum) {
  54. if (!this.checkCarNum(carNum)) {
  55. wx.showModal({
  56. title: '提示',
  57. showCancel:false,
  58. content: '车牌号输入有误,请重新输入',
  59. confirmColor:'#00AADD',
  60. });
  61. return;
  62. } else {
  63. this.setData({
  64. carNum: carNum,
  65. errorMsg: ''
  66. });
  67. }
  68. let userdata= {
  69. sexIndex: null,
  70. carNum: carNum,
  71. email: null,
  72. };
  73. this.bindUpdateUser(userdata);
  74. },
  75. inputEmail(e) {
  76. let that = this;
  77. wx.showModal({
  78. title: '修改邮箱',
  79. showCancel:true,
  80. editable: true,
  81. confirmColor:'#00AADD',
  82. cancelColor:'#00AADD',
  83. success: function (res1) {
  84. if (res1.confirm) {
  85. let email = res1.content;
  86. that.confirmEmail(email);
  87. }
  88. }
  89. });
  90. },
  91. confirmEmail(email) {
  92. if (!this.checkMail(email)) {
  93. wx.showModal({
  94. title: '提示',
  95. showCancel:false,
  96. content: '邮箱输入有误,请重新输入',
  97. confirmColor:'#00AADD',
  98. });
  99. return;
  100. } else {
  101. this.setData({
  102. email: email,
  103. errorMsg: ''
  104. });
  105. }
  106. let userdata= {
  107. sexIndex: null,
  108. carNum: null,
  109. email: email
  110. };
  111. this.bindUpdateUser(userdata);
  112. },
  113. checkMail(email) {
  114. if(email){
  115. if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(email))) {
  116. return false;
  117. }
  118. }
  119. return true;
  120. },
  121. checkCarNum(carNum){
  122. if (carNum){
  123. // if (!(/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(this.data.carNum))) {
  124. if (!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领].*$/.test(carNum)) {
  125. return false;
  126. }
  127. }
  128. return true;
  129. },
  130. bindUpdateUser(userdata) {
  131. let that = this;
  132. let loginName = this.data.userInfo.loginName;
  133. let {
  134. sexIndex,
  135. carNum: license_number,
  136. email
  137. } = userdata;
  138. var sex = null;
  139. if (sexIndex!=null){
  140. sex = this.data.sexType[sexIndex].value;
  141. }
  142. wx.request({
  143. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/updateUser',
  144. data: {
  145. loginName,
  146. sex: sex,
  147. license_number,
  148. email,
  149. },
  150. method: 'POST',
  151. success(res1) {
  152. if (res1.data.code == 1) {
  153. that.setData({
  154. errorMsg: res1.data.msg
  155. })
  156. }else if (res1.data.code == 2) {
  157. wx.showModal({
  158. title: '提示',
  159. content: '您未绑定手机号,请重新登录后自动刷新绑定信息',
  160. confirmText: '去登录',
  161. showCancel:false,
  162. confirmColor:'#00AADD',
  163. success: function (res1) {
  164. if (res1.confirm) {
  165. wx.removeStorageSync('userInfo');
  166. wx.removeStorageSync('isLogin');
  167. let url = '/pages/login/phone_login/phone_login';
  168. wx.redirectTo({
  169. url
  170. })
  171. }
  172. }
  173. });
  174. } else {
  175. //返回该用户
  176. //跳转到上一页
  177. wx.setStorageSync("userInfo", res1.data);
  178. wx.setStorageSync("isLogin", true);
  179. let userInfo = wx.getStorageSync('userInfo');
  180. that.setData({
  181. userInfo: userInfo
  182. });
  183. // wx.navigateBack();
  184. //let url = `/pages/ucenter/index/index`;
  185. //wx.redirectTo({
  186. //url
  187. //});
  188. }
  189. }
  190. });
  191. },
  192. /**
  193. * 生命周期函数--监听页面加载
  194. */
  195. onLoad: function(options) {
  196. },
  197. /**
  198. * 生命周期函数--监听页面初次渲染完成
  199. */
  200. onReady: function() {
  201. },
  202. /**
  203. * 生命周期函数--监听页面显示
  204. */
  205. onShow: function() {
  206. let userInfo = wx.getStorageSync('userInfo');
  207. let isLogin = wx.getStorageSync('isLogin');
  208. // 页面显示
  209. if (userInfo && isLogin) {
  210. //userInfo.flag = true;
  211. console.log(userInfo);
  212. this.setData({
  213. userInfo: userInfo,
  214. isLogin: isLogin,
  215. isBindPhone: userInfo.bindingPhone,
  216. carNum: userInfo.license_number,
  217. email:userInfo.email,
  218. sexIndex:userInfo.sex
  219. });
  220. } else {
  221. //未登录信息
  222. this.setData({
  223. userInfo: {}
  224. });
  225. }
  226. },
  227. /**
  228. * 生命周期函数--监听页面隐藏
  229. */
  230. onHide: function() {
  231. },
  232. /**
  233. * 生命周期函数--监听页面卸载
  234. */
  235. onUnload: function() {
  236. },
  237. /**
  238. * 页面相关事件处理函数--监听用户下拉动作
  239. */
  240. onPullDownRefresh: function() {
  241. },
  242. /**
  243. * 页面上拉触底事件的处理函数
  244. */
  245. onReachBottom: function() {
  246. },
  247. /**
  248. * 用户点击右上角分享
  249. */
  250. onShareAppMessage: function() {
  251. }
  252. })