东风启辰小程序端
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.

227 lines
5.2KB

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