东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
5.4KB

  1. // pages/address/address.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. imgUrl: app.globalData.urlStatic,//图片路径
  9. submitData:{
  10. realName:"",
  11. mobile: "",//电话
  12. province: "",//省份
  13. city: "",//城市
  14. district: "",//地区
  15. addressDetail:""//详细
  16. },
  17. isAddress: false,//是否有地址
  18. phoneInputShow: false,//是否显示电话输入框
  19. marginT:0,
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. app.globalData.nowPage = 4;
  26. if (app.globalData.userPhoneType=="ios"){
  27. this.setData({
  28. marginT:-20
  29. })
  30. }
  31. if (app.globalData.openid) {
  32. this.loadFun();
  33. } else {
  34. app.globalData.openidSuccessFuc = this.loadFun;
  35. }
  36. // if (app.globalData.userMobile) {
  37. // this.data.submitData.mobile = app.globalData.userMobile;
  38. // this.setData({
  39. // phoneInputShow: true,
  40. // submitData: this.data.submitData
  41. // })
  42. // }
  43. },
  44. loadFun: function () {
  45. this.getAddress();
  46. },
  47. /**
  48. * 生命周期函数--监听页面初次渲染完成
  49. */
  50. onReady: function () {
  51. },
  52. /**
  53. * 生命周期函数--监听页面显示
  54. */
  55. onShow: function () {
  56. },
  57. /**
  58. * 生命周期函数--监听页面隐藏
  59. */
  60. onHide: function () {
  61. },
  62. /**
  63. * 生命周期函数--监听页面卸载
  64. */
  65. onUnload: function () {
  66. },
  67. /**
  68. * 页面相关事件处理函数--监听用户下拉动作
  69. */
  70. onPullDownRefresh: function () {
  71. },
  72. /**
  73. * 页面上拉触底事件的处理函数
  74. */
  75. onReachBottom: function () {
  76. },
  77. /**
  78. * 用户点击右上角分享
  79. */
  80. onShareAppMessage: function () {
  81. return app.sharePack();
  82. },
  83. addressChange:function(e){//所在地区发生改变
  84. this.data.submitData.province = e.detail.value[0];
  85. this.data.submitData.city = e.detail.value[1];
  86. this.data.submitData.district = e.detail.value[2];
  87. this.setData({
  88. submitData:this.data.submitData
  89. })
  90. },
  91. getRealName: function (e) {//获取收货人
  92. this.data.submitData.realName = e.detail.value;
  93. this.setData({
  94. submitData: this.data.submitData
  95. })
  96. },
  97. getMobile: function (e) {//获取手机号码
  98. this.data.submitData.mobile = e.detail.value;
  99. this.setData({
  100. submitData: this.data.submitData
  101. })
  102. },
  103. getAddressDetail: function (e) {//获取详细地址
  104. this.data.submitData.addressDetail = e.detail.value;
  105. this.setData({
  106. submitData: this.data.submitData
  107. })
  108. },
  109. getAddress: function () {//获取地址
  110. app.wxRequest(app.globalData.urlRoot +"address/getAddress",{},res=>{
  111. if(res.code == 200){
  112. if(res.data){
  113. this.data.isAddress = true;
  114. this.data.submitData.realName = res.data.realName;
  115. this.data.submitData.mobile = res.data.mobile;
  116. this.data.submitData.province = res.data.province;
  117. this.data.submitData.city = res.data.city;
  118. this.data.submitData.district = res.data.district;
  119. this.data.submitData.addressDetail = res.data.addressDetail;
  120. this.setData({
  121. submitData: this.data.submitData
  122. })
  123. }
  124. }else{
  125. wx.showToast({
  126. title: res.msg,
  127. icon:"none"
  128. })
  129. }
  130. },this);
  131. },
  132. addAddress:function(){//添加地址
  133. app.wxRequest(app.globalData.urlRoot + "address/addAddress", this.data.submitData,res => {
  134. wx.showToast({
  135. title: res.msg,
  136. })
  137. if (res.code == 200) {
  138. wx.navigateBack({
  139. delta: 1
  140. })
  141. }
  142. },this,"POST")
  143. },
  144. updateAddress: function () {//更新地址
  145. app.wxRequest(app.globalData.urlRoot + "address/updateAddress", this.data.submitData, res => {
  146. wx.showToast({
  147. title: res.msg,
  148. })
  149. if (res.code == 200) {
  150. wx.navigateBack({
  151. delta:1
  152. })
  153. }
  154. }, this,"POST");
  155. },
  156. getUserPhone: function (e) {//获取用户手机号
  157. this.setData({
  158. phoneInputShow: true
  159. })
  160. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  161. app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
  162. if (res.code == 200) {
  163. this.data.submitData.mobile = res.data.decodeData.phoneNumber;
  164. this.setData({
  165. submitData: this.data.submitData
  166. })
  167. } else {
  168. wx.showToast({
  169. title: res.msg,
  170. icon: "none"
  171. })
  172. }
  173. }, this);
  174. }
  175. },
  176. submitAddress:function(){//保存地址
  177. if (!this.data.submitData.realName){
  178. wx.showToast({
  179. title: '请输入收货人',
  180. icon:"none"
  181. })
  182. return;
  183. }
  184. if (!app.mobileVerify(this.data.submitData.mobile)) {
  185. if (this.data.submitData.mobile) {
  186. wx.showToast({
  187. title: '请输入正确的电话',
  188. icon: 'none'
  189. })
  190. } else {
  191. wx.showToast({
  192. title: '请输入电话',
  193. icon: 'none'
  194. })
  195. }
  196. return;
  197. }
  198. if (!this.data.submitData.province) {
  199. wx.showToast({
  200. title: '请选择所在地区',
  201. icon: 'none'
  202. })
  203. return;
  204. }
  205. if (!this.data.submitData.addressDetail) {
  206. wx.showToast({
  207. title: '请输入详细地址',
  208. icon: "none"
  209. })
  210. return;
  211. }
  212. if(this.data.isAddress){
  213. this.updateAddress();
  214. } else {
  215. this.addAddress();
  216. }
  217. }
  218. })