center.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(email))) {
  115. return false;
  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. if(this.data.isBindPhone==0){
  131. let url = `/pages/bindPhone/bindPhone`;
  132. wx.navigateTo({
  133. url
  134. });
  135. return;
  136. }
  137. let loginName = this.data.userInfo.loginName;
  138. let {
  139. sexIndex,
  140. carNum: license_number,
  141. email
  142. } = userdata;
  143. var sex = null;
  144. if (sexIndex!=null){
  145. sex = this.data.sexType[sexIndex].value;
  146. }
  147. wx.request({
  148. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/updateUser',
  149. data: {
  150. loginName,
  151. sex: sex,
  152. license_number,
  153. email,
  154. },
  155. method: 'POST',
  156. success(res1) {
  157. if (res1.data.code == 1) {
  158. that.setData({
  159. errorMsg: res1.data.msg
  160. })
  161. } else {
  162. //返回该用户
  163. //跳转到上一页
  164. wx.setStorageSync("userInfo", res1.data);
  165. wx.setStorageSync("isLogin", true);
  166. let userInfo = wx.getStorageSync('userInfo');
  167. that.setData({
  168. userInfo: userInfo
  169. });
  170. // wx.navigateBack();
  171. //let url = `/pages/ucenter/index/index`;
  172. //wx.redirectTo({
  173. //url
  174. //});
  175. }
  176. }
  177. });
  178. },
  179. /**
  180. * 生命周期函数--监听页面加载
  181. */
  182. onLoad: function(options) {
  183. },
  184. /**
  185. * 生命周期函数--监听页面初次渲染完成
  186. */
  187. onReady: function() {
  188. },
  189. /**
  190. * 生命周期函数--监听页面显示
  191. */
  192. onShow: function() {
  193. let userInfo = wx.getStorageSync('userInfo');
  194. let isLogin = wx.getStorageSync('isLogin');
  195. // 页面显示
  196. if (userInfo && isLogin) {
  197. //userInfo.flag = true;
  198. console.log(userInfo);
  199. this.setData({
  200. userInfo: userInfo,
  201. isLogin: isLogin,
  202. isBindPhone: userInfo.bindingPhone,
  203. carNum: userInfo.license_number,
  204. email:userInfo.email,
  205. sexIndex:userInfo.sex
  206. });
  207. } else {
  208. //未登录信息
  209. this.setData({
  210. userInfo: {}
  211. });
  212. }
  213. },
  214. /**
  215. * 生命周期函数--监听页面隐藏
  216. */
  217. onHide: function() {
  218. },
  219. /**
  220. * 生命周期函数--监听页面卸载
  221. */
  222. onUnload: function() {
  223. },
  224. /**
  225. * 页面相关事件处理函数--监听用户下拉动作
  226. */
  227. onPullDownRefresh: function() {
  228. },
  229. /**
  230. * 页面上拉触底事件的处理函数
  231. */
  232. onReachBottom: function() {
  233. },
  234. /**
  235. * 用户点击右上角分享
  236. */
  237. onShareAppMessage: function() {
  238. }
  239. })