center.js 5.4 KB

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