bindingPhone.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // pages/bindingPhone/bindingPhone.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. user_phone:'',//绑定的手机号码
  6. code:'',//验证码
  7. oldCode : '',//短信验证码
  8. userName:'',//联系人
  9. userTelephone:'',//手机号码
  10. hiddenmodal: true, //弹窗
  11. modalCont: '',//提示内容
  12. prevPageUrl:'',//上一个页面的地址
  13. codeCont:'获取验证码',
  14. time_nun:'',//获取验证码后的样式
  15. wait:60,//倒计时时间
  16. },
  17. onLoad: function (options) {
  18. var that = this;
  19. var host = getApp().globalData.servsers;
  20. var un_id = getApp().globalData.un_id;
  21. var pages = getCurrentPages();
  22. var currPage = pages[pages.length - 1]; //当前页面
  23. var prevPage = pages[pages.length - 2]; //上一个页面
  24. var prevPageUrl = '';
  25. if (prevPage != undefined) {
  26. that.setData({
  27. prevPageUrl: prevPage.route
  28. })
  29. }
  30. },
  31. userNameInput: function (e) {
  32. this.setData({
  33. userName: e.detail.value
  34. })
  35. },
  36. userTelephoneInput: function (e) {
  37. this.setData({
  38. userTelephone: e.detail.value
  39. })
  40. },
  41. codeInput:function(e){
  42. this.setData({
  43. code: e.detail.value
  44. })
  45. },
  46. //获取验证码
  47. checkYanzhengma:function(){
  48. var that = this;
  49. var host = getApp().globalData.servsers;
  50. var un_id = getApp().globalData.un_id;
  51. var userName = that.data.userName;
  52. var userTelephone = that.data.userTelephone;
  53. var code = that.data.code;
  54. var codeCont = that.data.codeCont;
  55. if (codeCont != '获取验证码'){
  56. return false;
  57. }
  58. if (userTelephone == '') {
  59. that.setData({
  60. hiddenmodal: false,
  61. modalCont: '请填写手机号'
  62. })
  63. setTimeout(function () {
  64. that.setData({
  65. hiddenmodal: true
  66. })
  67. }, 1000)
  68. return false;
  69. }
  70. if (!checkMobile(that)) {
  71. that.setData({
  72. hiddenmodal: false,
  73. modalCont: '请输入正确的手机号'
  74. })
  75. setTimeout(function () {
  76. that.setData({
  77. hiddenmodal: true
  78. })
  79. }, 1000)
  80. return false;
  81. }
  82. wx.request({
  83. url: host + 'commodityapi/sendCode',
  84. data: {
  85. user_phone: userTelephone
  86. },
  87. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  88. header: {
  89. 'Accept': 'application/json'
  90. },
  91. success: function (data) {
  92. if (data.data == "0") {
  93. that.setData({
  94. hiddenmodal: false,
  95. modalCont: '该手机号已被使用!'
  96. })
  97. setTimeout(function () {
  98. that.setData({
  99. hiddenmodal: true
  100. })
  101. }, 1000)
  102. return false;
  103. } else if (data.data == '1') {
  104. that.setData({
  105. hiddenmodal: false,
  106. modalCont: '验证码发送失败!'
  107. })
  108. setTimeout(function () {
  109. that.setData({
  110. hiddenmodal: true
  111. })
  112. }, 1000)
  113. return false;
  114. } else {
  115. that.setData({
  116. oldCode: data.data
  117. })
  118. that.time(that);
  119. }
  120. }
  121. })
  122. },
  123. //绑定手机
  124. bangdingFun:function(){
  125. var that = this;
  126. var pages = getCurrentPages();
  127. var currPage = pages[pages.length - 1]; //当前页面
  128. var prevPage = pages[pages.length - 2]; //上一个页面
  129. var host = getApp().globalData.servsers;
  130. var un_id = getApp().globalData.un_id;
  131. var userName = that.data.userName;
  132. var userTelephone = that.data.userTelephone;
  133. var code = that.data.code;
  134. var oldCode = that.data.oldCode;
  135. var prevPageUrl = that.data.prevPageUrl;
  136. if (userName == '') {
  137. that.setData({
  138. hiddenmodal: false,
  139. modalCont: '请填写联系人'
  140. })
  141. setTimeout(function () {
  142. that.setData({
  143. hiddenmodal: true
  144. })
  145. }, 1000)
  146. return false;
  147. }
  148. if (userTelephone == '') {
  149. that.setData({
  150. hiddenmodal: false,
  151. modalCont: '请填写手机号'
  152. })
  153. setTimeout(function () {
  154. that.setData({
  155. hiddenmodal: true
  156. })
  157. }, 1000)
  158. return false;
  159. }
  160. if (code == '') {
  161. that.setData({
  162. hiddenmodal: false,
  163. modalCont: '请填写验证码'
  164. })
  165. setTimeout(function () {
  166. that.setData({
  167. hiddenmodal: true
  168. })
  169. }, 1000)
  170. return false;
  171. }
  172. console.log("oldCode:" + oldCode);
  173. console.log("code:" + code);
  174. if (oldCode != code){
  175. that.setData({
  176. hiddenmodal: false,
  177. modalCont: '验证码错误,请重新输入'
  178. })
  179. setTimeout(function () {
  180. that.setData({
  181. hiddenmodal: true,
  182. code : ''
  183. })
  184. }, 1000)
  185. return false;
  186. }
  187. if (un_id != undefined && un_id != '' && un_id != null){
  188. wx.request({
  189. url: host + 'commodityapi/bindingPhone',
  190. data:{
  191. user_phone: userTelephone,
  192. user_id: un_id,
  193. user_name: userName
  194. },
  195. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  196. header: {
  197. 'Accept': 'application/json'
  198. },
  199. success: function (data) {
  200. console.log(data);
  201. if (data == "0") {
  202. that.setData({
  203. hiddenmodal: false,
  204. modalCont: '操作失败,请重试!'
  205. })
  206. setTimeout(function () {
  207. that.setData({
  208. hiddenmodal: true
  209. })
  210. }, 1000)
  211. return false;
  212. } else {
  213. that.setData({
  214. hiddenmodal: false,
  215. modalCont: '绑定成功'
  216. })
  217. setTimeout(function () {
  218. that.setData({
  219. hiddenmodal: true
  220. })
  221. prevPage.setData({
  222. user_phone: userTelephone
  223. })
  224. wx.navigateBack({
  225. delta: 1
  226. })
  227. }, 1500)
  228. return false;
  229. }
  230. }
  231. })
  232. }
  233. },
  234. //倒计时
  235. time:function(){
  236. var that = this;
  237. var wait = that.data.wait;
  238. if (wait == 0) {
  239. that.setData({
  240. codeCont: '获取验证码',
  241. oldCode:'',
  242. wait : 60
  243. })
  244. }else{
  245. that.setData({
  246. codeCont: wait + '秒'
  247. })
  248. wait--;
  249. that.setData({
  250. wait: wait
  251. })
  252. setTimeout(function () { that.time(that) }, 1000)
  253. }
  254. },
  255. //跳过
  256. jumpBinding:function(e){
  257. var that = this;
  258. var pages = getCurrentPages();
  259. var currPage = pages[pages.length - 1]; //当前页面
  260. var prevPage = pages[pages.length - 2]; //上一个页面
  261. prevPage.setData({
  262. user_phone:1
  263. })
  264. wx.navigateBack({
  265. delta: 1
  266. })
  267. //调用支付页面的支付方法
  268. prevPage.save();
  269. }
  270. })
  271. //验证电话
  272. function checkMobile(that) {
  273. var userTelephone = that.data.userTelephone;;
  274. if (trim(userTelephone) != "") {
  275. var regu = /^[1][3-9][0-9]{9}$/;
  276. var re = new RegExp(regu);
  277. if (re.test(userTelephone)) {
  278. return true;
  279. } else {
  280. return false;
  281. }
  282. }
  283. }
  284. function trim(str) { //删除左右两端的空格
  285. return str.replace(/(^\s*)|(\s*$)/g, "");
  286. }