add.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // customized/pages/address/add/add.js
  2. let app = getApp();
  3. let host = app.globalData.servsers; // 请求的url
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. name:'',
  10. phone:'',
  11. region: ['上海市', '上海市', '黄浦区'],
  12. detailAddress:'',
  13. id:null,
  14. switchChecked:false, //是否默认
  15. type:'add',
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. console.log(options,"options")
  22. if (options.type ==='edit'){ // 表示编辑
  23. this.setData({
  24. type:'edit'
  25. })
  26. this.getEditAddress(this)
  27. }
  28. },
  29. /**
  30. * 生命周期函数--监听页面初次渲染完成
  31. */
  32. onReady: function () {
  33. },
  34. /**
  35. * 生命周期函数--监听页面显示
  36. */
  37. onShow: function () {
  38. },
  39. /**
  40. * 生命周期函数--监听页面隐藏
  41. */
  42. onHide: function () {
  43. },
  44. /**
  45. * 生命周期函数--监听页面卸载
  46. */
  47. onUnload: function () {
  48. },
  49. /**
  50. * 页面相关事件处理函数--监听用户下拉动作
  51. */
  52. onPullDownRefresh: function () {
  53. },
  54. /**
  55. * 页面上拉触底事件的处理函数
  56. */
  57. onReachBottom: function () {
  58. },
  59. /**
  60. * 用户点击右上角分享
  61. */
  62. onShareAppMessage: function () {
  63. },
  64. bindRegionChange(e) {
  65. console.log('picker发送选择改变,携带值为', e.detail.value)
  66. this.setData({
  67. region: e.detail.value
  68. })
  69. },
  70. switchChange(e){
  71. this.setData({
  72. switchChecked: e.detail.value
  73. })
  74. },
  75. nameChange(e){
  76. this.setData({
  77. name: e.detail.value
  78. })
  79. },
  80. phoneChange(e) {
  81. this.setData({
  82. phone: e.detail.value
  83. })
  84. },
  85. addressInput(e){ // 详细地址
  86. console.log(e)
  87. this.setData({
  88. detailAddress: e.detail.value
  89. })
  90. },
  91. addAddress(){ // 保存地址
  92. let that =this;
  93. if(!that.data.name){
  94. wx.showToast({
  95. title: '收货人没有输入',
  96. icon:"none"
  97. });
  98. return
  99. }
  100. if(!that.data.phone){
  101. wx.showToast({
  102. title: '手机号码没有输入',
  103. icon: "none"
  104. });
  105. return
  106. }
  107. if (!that.data.detailAddress) {
  108. wx.showToast({
  109. title: '详细地址没有输入',
  110. icon: "none"
  111. });
  112. return
  113. }
  114. let data ={
  115. consignee: that.data.name, //收货人
  116. province: that.data.region[0],
  117. city: that.data.region[1],
  118. area: that.data.region[2],
  119. adress: that.data.detailAddress,
  120. phone: that.data.phone,
  121. adress_flag: that.data.switchChecked ? 0 : 1 // 是否默认 0是 1 否
  122. },
  123. url = host + '/adressapi/insertadd',
  124. title = "地址添加成功";
  125. if(that.data.type == 'edit'){
  126. data['action_account'] = null;
  127. data['id'] = that.data.id;
  128. url = host + '/adressapi/update';
  129. title = "地址编辑成功";
  130. } else {
  131. data['user_id'] =app.globalData.user_id;
  132. url = host + '/adressapi/insertadd';
  133. title = "地址添加成功";
  134. }
  135. wx.request({
  136. url: url,
  137. header: {
  138. 'content-type': 'application/x-www-form-urlencoded'
  139. },
  140. data: data,
  141. success(res){
  142. console.log(res,"地址保存")
  143. wx.showToast({
  144. title: title,
  145. icon:'none',
  146. success(){
  147. setTimeout(()=>{
  148. wx.navigateBack({
  149. delta: 1
  150. })
  151. },1000)
  152. }
  153. })
  154. }
  155. })
  156. },
  157. getEditAddress(that){ // 获取编辑的地址
  158. let address = wx.getStorageSync('address'),
  159. name = address.consignee,
  160. phone =address.phone,
  161. region = [address.province,address.city,address.area],
  162. detailAddress =address.adress,
  163. switchChecked = address.adress_flag == '0'? true : false ,
  164. id = address.id;
  165. console.log(address)
  166. that.setData({
  167. name:name,
  168. phone:phone,
  169. region: region,
  170. detailAddress: detailAddress,
  171. switchChecked: switchChecked,
  172. id: id
  173. })
  174. }
  175. })