center.js 5.9 KB

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