index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { editAddress, getAddressDetail} from '../../api/user.js';
  2. import { getCity } from '../../api/api.js';
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. parameter: {
  10. 'navbar': '1',
  11. 'return': '1',
  12. 'title': '添加地址'
  13. },
  14. region: ['省', '市', '区'],
  15. valueRegion: [0, 0, 0],
  16. cartId:'',//购物车id
  17. pinkId:0,//拼团id
  18. couponId:0,//优惠券id
  19. id:0,//地址id
  20. userAddress: { is_default:false},//地址详情
  21. cityId:0,
  22. district:[],
  23. multiArray:[],
  24. multiIndex: [0, 0, 0],
  25. },
  26. /**
  27. * 授权回调
  28. *
  29. */
  30. onLoadFun:function(){
  31. this.getUserAddress();
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. this.setData({
  38. cartId: options.cartId || '',
  39. pinkId: options.pinkId || 0,
  40. couponId: options.couponId || 0,
  41. id: options.id || 0,
  42. 'parameter.title': options.id ? '修改地址' : '添加地址'
  43. });
  44. this.getCityList();
  45. },
  46. getCityList:function(){
  47. let that = this;
  48. getCity().then(res=>{
  49. that.setData({ district:res.data});
  50. that.initialize();
  51. })
  52. },
  53. initialize:function(){
  54. let that = this, province = [], city = [], area = [];
  55. if (that.data.district.length) {
  56. let cityChildren = that.data.district[0].c || [];
  57. let areaChildren = cityChildren.length ? (cityChildren[0].c || []) : [];
  58. that.data.district.forEach(function (item) {
  59. province.push(item.n);
  60. });
  61. cityChildren.forEach(function (item) {
  62. city.push(item.n);
  63. });
  64. areaChildren.forEach(function (item) {
  65. area.push(item.n);
  66. });
  67. that.setData({
  68. multiArray: [province, city, area],
  69. });
  70. }
  71. },
  72. bindRegionChange: function (e) {
  73. let multiIndex = this.data.multiIndex, province = this.data.district[multiIndex[0]] || { c: [] }, city = province.c[multiIndex[1]] || { v: 0 }, multiArray = this.data.multiArray, value = e.detail.value;
  74. console.log(value);
  75. console.log(province);
  76. this.setData({
  77. region: [multiArray[0][value[0]], multiArray[1][value[1]], multiArray[2][value[2]]],
  78. cityId: city.v,
  79. valueRegion: [0,0,0]
  80. });
  81. this.initialize();
  82. },
  83. bindMultiPickerColumnChange:function(e){
  84. let that = this, column = e.detail.column, value = e.detail.value, currentCity = this.data.district[value] || { c: [] }, multiArray = that.data.multiArray, multiIndex = that.data.multiIndex;
  85. multiIndex[column] = value;
  86. switch (column){
  87. case 0:
  88. let areaList = currentCity.c[0] || { c: [] };
  89. multiArray[1] = currentCity.c.map((item)=>{
  90. return item.n;
  91. });
  92. multiArray[2] = areaList.c.map((item)=>{
  93. return item.n;
  94. });
  95. break;
  96. case 1:
  97. let cityList = that.data.district[multiIndex[0]].c[multiIndex[1]].c || [];
  98. multiArray[2] = cityList.map((item)=>{
  99. return item.n;
  100. });
  101. break;
  102. case 2:
  103. break;
  104. }
  105. this.setData({ multiArray: multiArray, multiIndex: multiIndex});
  106. },
  107. getUserAddress:function(){
  108. if(!this.data.id) return false;
  109. let that = this;
  110. getAddressDetail(this.data.id).then(res=>{
  111. var region = [res.data.province, res.data.city, res.data.district];
  112. that.setData({
  113. userAddress: res.data,
  114. region: region,
  115. });
  116. });
  117. },
  118. getWxAddress:function(){
  119. var that = this;
  120. wx.authorize({
  121. scope: 'scope.address',
  122. success: function (res) {
  123. wx.chooseAddress({
  124. success: function (res) {
  125. var addressP = {};
  126. addressP.province = res.provinceName;
  127. addressP.city = res.cityName;
  128. addressP.district = res.countyName;
  129. editAddress({
  130. address: addressP,
  131. is_default: 1,
  132. real_name: res.userName,
  133. post_code: res.postalCode,
  134. phone: res.telNumber,
  135. detail: res.detailInfo,
  136. id: 0,
  137. type: 1,
  138. }).then(res => {
  139. setTimeout(function () {
  140. if (that.data.cartId) {
  141. var cartId = that.data.cartId;
  142. var pinkId = that.data.pinkId;
  143. var couponId = that.data.couponId;
  144. that.setData({ cartId: '', pinkId: '', couponId: '' })
  145. wx.navigateTo({
  146. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  147. });
  148. } else {
  149. wx.navigateBack({ delta: 1 });
  150. }
  151. }, 1000);
  152. return app.Tips({ title: "添加成功", icon: 'success' });
  153. }).catch(err => {
  154. return app.Tips({ title: err });
  155. });
  156. },
  157. fail: function (res) {
  158. if (res.errMsg == 'chooseAddress:cancel') return app.Tips({ title: '取消选择' });
  159. },
  160. })
  161. },
  162. fail: function (res) {
  163. wx.showModal({
  164. title: '您已拒绝导入微信地址权限',
  165. content: '是否进入权限管理,调整授权?',
  166. success(res) {
  167. if (res.confirm) {
  168. wx.openSetting({
  169. success: function (res) {
  170. console.log(res.authSetting)
  171. }
  172. });
  173. } else if (res.cancel) {
  174. return app.Tips({ title: '已取消!' });
  175. }
  176. }
  177. })
  178. },
  179. })
  180. },
  181. /**
  182. * 提交用户添加地址
  183. *
  184. */
  185. formSubmit:function(e){
  186. var that = this, value = e.detail.value;
  187. if (!value.real_name) return app.Tips({title:'请填写收货人姓名'});
  188. if (!value.phone) return app.Tips({title:'请填写联系电话'});
  189. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(value.phone)) return app.Tips({title:'请输入正确的手机号码'});
  190. if (that.data.region[0] =='省') return app.Tips({title:'请选择所在地区'});
  191. if (!value.detail) return app.Tips({title:'请填写详细地址'});
  192. value.id=that.data.id;
  193. value.address={
  194. province:that.data.region[0],
  195. city: that.data.region[1],
  196. district: that.data.region[2],
  197. city_id: that.data.cityId,
  198. };
  199. value.is_default = that.data.userAddress.is_default ? 1 : 0;
  200. editAddress(value).then(res=>{
  201. if (that.data.id)
  202. app.Tips({ title: '修改成功', icon: 'success' });
  203. else
  204. app.Tips({ title: '添加成功', icon: 'success' });
  205. setTimeout(function () {
  206. if (that.data.cartId) {
  207. var cartId = that.data.cartId;
  208. var pinkId = that.data.pinkId;
  209. var couponId = that.data.couponId;
  210. that.setData({ cartId: '', pinkId: '', couponId: '' })
  211. wx.navigateTo({
  212. url: '/pages/order_confirm/index?cartId=' + cartId + '&addressId=' + (that.data.id ? that.data.id : res.data.id) + '&pinkId=' + pinkId + '&couponId=' + couponId
  213. });
  214. } else {
  215. wx.navigateBack({ delta: 1 });
  216. }
  217. }, 1000);
  218. }).catch(err=>{
  219. return app.Tips({title:err});
  220. })
  221. },
  222. ChangeIsDefault:function(e){
  223. this.setData({ 'userAddress.is_default': !this.data.userAddress.is_default});
  224. },
  225. })