accountSettings.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // pages/accountSettings/accountSettings.js
  2. var http = require("../../utils/http.js");
  3. var util = require('../../utils/util.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. // username: '',
  10. password: '',
  11. phoneNumber: '',
  12. countryCode: '', //区号
  13. confirmPassword: '', //确认密码
  14. // 验证码相关
  15. show: true,
  16. count: '',
  17. timer: null,
  18. hadGotCode: false, //是否已经点击了获取验证码
  19. validCode: '',
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. },
  26. /**
  27. * 获取用户手机号码
  28. */
  29. // getPhoneNumber: function (e) {
  30. // wx.showLoading();
  31. // // 参数e是绑定的授权方法自动传入过来的, 主要是为了拿到vi和encryptedData值从后台换取用户联系方式
  32. // var iv = e.detail.iv;
  33. // var encryptedData = e.detail.encryptedData;
  34. // //调用后台接口获取用户手机号码
  35. // var params = {
  36. // url: "/p/user/getPhoneNumber",
  37. // method: "GET",
  38. // data: {
  39. // encryptedData: encryptedData,
  40. // ivStr: iv,
  41. // },
  42. // callBack: (res) => {
  43. // wx.hideLoading();
  44. // this.setData({
  45. // phoneNumber: res.phoneNumber,
  46. // countryCode: res.countryCode
  47. // })
  48. // console.log(this.data.phoneNumber)
  49. // }
  50. // };
  51. // http.request(params);
  52. // },
  53. /**
  54. * 获取验证码
  55. */
  56. getValidCode: function () {
  57. if (!util.checkPhoneNumber(this.data.phoneNumber) || this.data.phoneNumber.length != 11) {
  58. wx.showToast({
  59. title: '请输入正确的手机号',
  60. icon: 'none'
  61. });
  62. return
  63. }
  64. if (this.data.hadGotCode) {
  65. return
  66. }
  67. this.setData({
  68. hadGotCode: true,
  69. })
  70. console.log('获取验证码')
  71. var params = {
  72. url: "/p/shop/sendCode",
  73. method: "POST",
  74. data: {
  75. mobile: this.data.phoneNumber,
  76. },
  77. callBack: res => {
  78. this.setData({
  79. hadGotCode: true
  80. })
  81. const timeCount = 60;
  82. if (!this.data.timer) {
  83. let count = timeCount
  84. let timer = this.data.timer
  85. setTimeout(() => {
  86. this.setData({
  87. count: count,
  88. show: false
  89. })
  90. }, 1000)
  91. timer = setInterval(() => {
  92. if (count > 0 && count <= timeCount) {
  93. this.setData({
  94. count: count--
  95. })
  96. } else {
  97. clearInterval(timer);
  98. this.setData({
  99. timer: null,
  100. show: true,
  101. hadGotCode: false
  102. })
  103. }
  104. }, 1000)
  105. }
  106. }
  107. };
  108. http.request(params);
  109. },
  110. /**
  111. * 获取输入的手机号
  112. */
  113. bindMobileInt(e){
  114. this.setData({
  115. phoneNumber: e.detail.value
  116. })
  117. },
  118. /**
  119. * 获取输入的验证码的值
  120. */
  121. bindValidCodeInt: function (e) {
  122. this.setData({
  123. validCode: e.detail.value
  124. });
  125. },
  126. /**
  127. * 获取登录密码数据
  128. */
  129. bindPasswordInt: function(e) {
  130. this.setData({
  131. password: e.detail.value
  132. })
  133. },
  134. /**
  135. * 获取确认密码数据
  136. */
  137. bindConfirmPasswordInt: function (e) {
  138. this.setData({
  139. confirmPassword: e.detail.value
  140. })
  141. },
  142. /**
  143. * 请求接口
  144. */
  145. accountSettingsSubmit:function() {
  146. if (!util.checkPhoneNumber(this.data.phoneNumber) || this.data.phoneNumber.length == 0){
  147. wx.showToast({
  148. title: '请输入正确的手机号',
  149. icon: 'none'
  150. })
  151. } else if (this.data.validCode.length == 0) {
  152. wx.showToast({
  153. title: '请输入验证码',
  154. icon: 'none'
  155. });
  156. } else if (this.data.password.trim().length < 6 || this.data.password.trim().length > 12) {
  157. wx.showToast({
  158. title: '登录密码格式不对',
  159. icon: 'none'
  160. })
  161. } else if (this.data.confirmPassword.length == 0) {
  162. wx.showToast({
  163. title: '请再次输入密码',
  164. icon: 'none'
  165. })
  166. } else if (this.data.confirmPassword != this.data.password) {
  167. wx.showToast({
  168. title: '两次密码输入不一致',
  169. icon: 'none'
  170. })
  171. } else {
  172. wx.showLoading();
  173. var params = {
  174. url: "/p/shop/saveUsernameAndPassword",
  175. method: "POST",
  176. data: {
  177. username: this.data.phoneNumber.trim(),
  178. password: this.data.password.trim(),
  179. code: this.data.validCode.trim()
  180. },
  181. callBack: (res) => {
  182. wx.hideLoading();
  183. wx.showToast({
  184. icon: 'success',
  185. title: '设置成功!',
  186. duration: 2000,
  187. success: function () {
  188. wx.switchTab({
  189. url: '/pages/user/user'
  190. })
  191. }
  192. })
  193. }
  194. };
  195. http.request(params);
  196. }
  197. },
  198. /**
  199. * 生命周期函数--监听页面初次渲染完成
  200. */
  201. onReady: function () {
  202. },
  203. /**
  204. * 生命周期函数--监听页面显示
  205. */
  206. onShow: function () {
  207. },
  208. /**
  209. * 生命周期函数--监听页面隐藏
  210. */
  211. onHide: function () {
  212. },
  213. /**
  214. * 生命周期函数--监听页面卸载
  215. */
  216. onUnload: function () {
  217. },
  218. /**
  219. * 页面相关事件处理函数--监听用户下拉动作
  220. */
  221. onPullDownRefresh: function () {
  222. },
  223. /**
  224. * 页面上拉触底事件的处理函数
  225. */
  226. onReachBottom: function () {
  227. },
  228. /**
  229. * 用户点击右上角分享
  230. */
  231. onShareAppMessage: function () {
  232. }
  233. })