user-address.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <template>
  2. <view>
  3. <view class="page-bottom-fixed padding-main">
  4. <view v-if="data_list.length > 0">
  5. <view v-for="(item, index) in data_list" :key="index" class="item padding-horizontal-main border-radius-main bg-white spacing-mb">
  6. <view @tap="address_conent_event" :data-index="index">
  7. <view class="oh padding-vertical-main">
  8. <text v-if="(item.alias || null) != null" class="address-alias br-main cr-main round margin-right-sm">{{item.alias}}</text>
  9. <text>{{item.name}}</text>
  10. <text class="fr">{{item.tel}}</text>
  11. </view>
  12. <view class="address oh padding-bottom-lg">
  13. <image class="item-icon fl margin-top-xs" :src="common_static_url+'map-icon.png'" mode="widthFix"></image>
  14. <view class="text fr">
  15. <text>{{item.province_name || ''}}{{item.city_name || ''}}{{item.county_name || ''}}{{item.address || ''}}</text>
  16. <text v-if="((item.distance_value || null) != null && (item.distance_unit || null) != null)" class="cr-grey margin-left-lg fr">距离<text class="cr-base">{{item.distance_value}}</text>{{item.distance_unit}}</text>
  17. </view>
  18. </view>
  19. </view>
  20. <view class="br-t oh padding-vertical-main">
  21. <view class="default-checkbox fl margin-top-sm" @tap="address_default_event" :data-value="item.id">
  22. <image class="va-m" :src="common_static_url+'select' + (is_default == item.id ? '-active' : '') + '-icon.png'" mode="widthFix"></image>
  23. <text class="va-m margin-left-sm">设为默认地址</text>
  24. </view>
  25. <view class="item-operation fr oh">
  26. <button v-if="(item.lng || 0) != 0 && (item.lat || 0) != 0" class="round bg-white cr-base br" type="default" size="mini" @tap="address_map_event" :data-index="index" hover-class="none">位置</button>
  27. <button class="round bg-white cr-green br-green" type="default" size="mini" @tap="address_edit_event" :data-index="index" hover-class="none">编辑</button>
  28. <button class="round bg-white cr-red br-red" type="default" size="mini" @tap="address_delete_event" :data-index="index" :data-value="item.id" hover-class="none">删除</button>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. <view v-else>
  34. <!-- 提示信息 -->
  35. <component-no-data :propStatus="data_list_loding_status"></component-no-data>
  36. </view>
  37. <!-- 结尾 -->
  38. <component-bottom-line :propStatus="data_bottom_line_status"></component-bottom-line>
  39. </view>
  40. <!-- 底部操作 -->
  41. <view class="bottom-fixed padding-main">
  42. <view class="bottom-line-exclude oh">
  43. <block v-if="common_user_address_platform_import_list.length > 0 && common_user_address_platform_import_list.indexOf(client_value) != -1">
  44. <view class="submit-list">
  45. <button class="bg-main br-main cr-white round text-size-sm dis-inline-block fl" type="default" hover-class="none" @tap="address_add_event">添加新地址</button>
  46. <button class="bg-main-pair br-main-pair cr-white round text-size-sm dis-inline-block fr" type="default" hover-class="none" @tap="choose_system_address_event">导入系统地址</button>
  47. </view>
  48. </block>
  49. <block v-else>
  50. <button class="bg-main br-main cr-white round text-size-sm wh-auto" type="default" hover-class="none" @tap="address_add_event">添加新地址</button>
  51. </block>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. const app = getApp();
  58. import componentNoData from "../../components/no-data/no-data";
  59. import componentBottomLine from "../../components/bottom-line/bottom-line";
  60. var common_static_url = app.globalData.get_static_url('common');
  61. export default {
  62. data() {
  63. return {
  64. common_static_url: common_static_url,
  65. data_list_loding_status: 1,
  66. data_bottom_line_status: false,
  67. common_user_address_platform_import_list: [],
  68. client_value: app.globalData.application_client_type(),
  69. data_list: [],
  70. params: null,
  71. is_default: 0
  72. };
  73. },
  74. components: {
  75. componentNoData,
  76. componentBottomLine
  77. },
  78. props: {},
  79. onLoad(params) {
  80. this.setData({
  81. params: params
  82. });
  83. },
  84. onShow() {
  85. // 异步初始化配置
  86. this.init_config();
  87. // 数据初始化
  88. this.init();
  89. // 分享菜单处理
  90. app.globalData.page_share_handle();
  91. },
  92. // 下拉刷新
  93. onPullDownRefresh() {
  94. this.get_data_list();
  95. },
  96. methods: {
  97. // 初始化配置
  98. init_config(status) {
  99. if ((status || false) == true) {
  100. this.setData({
  101. common_user_address_platform_import_list: app.globalData.get_config('config.common_user_address_platform_import_list', [])
  102. });
  103. } else {
  104. app.globalData.is_config(this, 'init_config');
  105. }
  106. },
  107. // 初始化
  108. init() {
  109. var user = app.globalData.get_user_info(this, "init");
  110. if (user != false) {
  111. // 用户未绑定用户则转到登录页面
  112. if (app.globalData.user_is_need_login(user)) {
  113. uni.redirectTo({
  114. url: "/pages/login/login?event_callback=init"
  115. });
  116. return false;
  117. } else {
  118. // 获取数据
  119. this.get_data_list();
  120. }
  121. } else {
  122. this.setData({
  123. data_list_loding_status: 0,
  124. data_bottom_line_status: false
  125. });
  126. }
  127. },
  128. // 获取数据列表
  129. get_data_list() {
  130. // 加载loding
  131. uni.showLoading({
  132. title: '加载中...'
  133. });
  134. this.setData({
  135. data_list_loding_status: 1
  136. });
  137. // 获取数据
  138. uni.request({
  139. url: app.globalData.get_request_url("index", "useraddress"),
  140. method: 'POST',
  141. data: this.params,
  142. dataType: 'json',
  143. success: res => {
  144. uni.hideLoading();
  145. uni.stopPullDownRefresh();
  146. if (res.data.code == 0) {
  147. var data = res.data.data;
  148. if (data.data.length > 0) {
  149. // 获取当前默认地址
  150. var is_default = 0;
  151. for (var i in data.data) {
  152. if (data.data[i]['is_default'] == 1) {
  153. is_default = data.data[i]['id'];
  154. }
  155. }
  156. // 设置数据
  157. this.setData({
  158. data_list: data.data,
  159. is_default: is_default,
  160. data_list_loding_status: 3,
  161. data_bottom_line_status: true
  162. });
  163. } else {
  164. this.setData({
  165. data_list_loding_status: 0
  166. });
  167. }
  168. } else {
  169. this.setData({
  170. data_list_loding_status: 0
  171. });
  172. if (app.globalData.is_login_check(res.data, this, 'get_data_list')) {
  173. app.globalData.showToast(res.data.msg);
  174. }
  175. }
  176. },
  177. fail: () => {
  178. uni.hideLoading();
  179. uni.stopPullDownRefresh();
  180. this.setData({
  181. data_list_loding_status: 2
  182. });
  183. app.globalData.showToast('服务器请求出错');
  184. }
  185. });
  186. },
  187. // 删除地址
  188. address_delete_event(e) {
  189. var index = e.currentTarget.dataset.index;
  190. var value = e.currentTarget.dataset.value || null;
  191. if (value == null) {
  192. app.globalData.showToast("地址ID有误");
  193. return false;
  194. }
  195. var self = this;
  196. uni.showModal({
  197. title: '温馨提示',
  198. content: '删除后不可恢复,确定继续吗?',
  199. confirmText: '确认',
  200. cancelText: '不了',
  201. success: result => {
  202. if (result.confirm) {
  203. // 加载loding
  204. uni.showLoading({
  205. title: '处理中...'
  206. });
  207. // 获取数据
  208. uni.request({
  209. url: app.globalData.get_request_url("delete", "useraddress"),
  210. method: 'POST',
  211. data: {
  212. id: value
  213. },
  214. dataType: 'json',
  215. success: res => {
  216. uni.hideLoading();
  217. if (res.data.code == 0) {
  218. var temp_data = self.data_list;
  219. temp_data.splice(index, 1);
  220. self.setData({
  221. data_list: temp_data,
  222. data_list_loding_status: temp_data.length == 0 ? 0 : 3,
  223. data_bottom_line_status: temp_data.length == 0 ? false : true
  224. });
  225. app.globalData.showToast(res.data.msg, 'success');
  226. // 当前删除是否存在缓存中,存在则删除
  227. var cache_address = uni.getStorageSync(app.globalData.data.cache_buy_user_address_select_key);
  228. if ((cache_address.data || null) != null) {
  229. if (cache_address.data.id == value) {
  230. // 删除地址缓存
  231. uni.removeStorageSync(app.globalData.data.cache_buy_user_address_select_key);
  232. }
  233. }
  234. } else {
  235. if (app.globalData.is_login_check(res.data)) {
  236. app.globalData.showToast(res.data.msg);
  237. } else {
  238. app.globalData.showToast('提交失败,请重试!');
  239. }
  240. }
  241. },
  242. fail: () => {
  243. uni.hideLoading();
  244. app.globalData.showToast('服务器请求出错');
  245. }
  246. });
  247. }
  248. }
  249. });
  250. },
  251. // 默认地址设置
  252. address_default_event(e) {
  253. var value = e.currentTarget.dataset.value || null;
  254. if (value == null) {
  255. app.globalData.showToast("地址ID有误");
  256. return false;
  257. }
  258. var self = this;
  259. if (value == self.is_default) {
  260. app.globalData.showToast("设置成功", 'success');
  261. return false;
  262. }
  263. // 加载loding
  264. uni.showLoading({
  265. title: '处理中...'
  266. });
  267. // 获取数据
  268. uni.request({
  269. url: app.globalData.get_request_url("setdefault", "useraddress"),
  270. method: 'POST',
  271. data: {
  272. id: value
  273. },
  274. dataType: 'json',
  275. success: res => {
  276. uni.hideLoading();
  277. if (res.data.code == 0) {
  278. self.setData({
  279. is_default: value
  280. });
  281. app.globalData.showToast(res.data.msg, 'success');
  282. } else {
  283. if (app.globalData.is_login_check(res.data)) {
  284. app.globalData.showToast(res.data.msg);
  285. } else {
  286. app.globalData.showToast('提交失败,请重试!');
  287. }
  288. }
  289. },
  290. fail: () => {
  291. uni.hideLoading();
  292. app.globalData.showToast('服务器请求出错');
  293. }
  294. });
  295. },
  296. // 地址内容事件
  297. address_conent_event(e) {
  298. var index = e.currentTarget.dataset.index || 0;
  299. var is_back = this.params.is_back || 0;
  300. if (is_back == 1) {
  301. uni.setStorage({
  302. key: app.globalData.data.cache_buy_user_address_select_key,
  303. data: this.data_list[index]
  304. });
  305. uni.navigateBack();
  306. }
  307. },
  308. // 获取系统地址事件
  309. choose_system_address_event(e) {
  310. // 百度、头条则需要验证授权
  311. // #ifdef MP-BAIDU || MP-TOUTIAO
  312. // 去验证授权
  313. if(e != 1) {
  314. app.globalData.auth_check(this, 'choose_system_address_event', 'scope.address');
  315. return false;
  316. }
  317. // #endif
  318. // 获取地址授权信息
  319. uni.chooseAddress({
  320. success(res) {
  321. var data = {
  322. "name": res.userName || '',
  323. "tel": res.telNumber || '',
  324. "province": res.provinceName || '',
  325. "city": res.cityName || '',
  326. "county": res.countyName || '',
  327. "address": res.detailInfo || ''
  328. };
  329. // 加载获取数据
  330. uni.showLoading({
  331. title: '处理中...'
  332. });
  333. uni.request({
  334. url: app.globalData.get_request_url("outsystemadd", "useraddress"),
  335. method: 'POST',
  336. data: data,
  337. dataType: 'json',
  338. success: res => {
  339. uni.hideLoading();
  340. if (res.data.code == 0) {
  341. this.get_data_list();
  342. } else {
  343. if (app.globalData.is_login_check(res.data)) {
  344. app.globalData.showToast(res.data.msg);
  345. } else {
  346. app.globalData.showToast('提交失败,请重试!');
  347. }
  348. }
  349. },
  350. fail: () => {
  351. uni.hideLoading();
  352. app.globalData.showToast('服务器请求出错');
  353. }
  354. });
  355. }
  356. });
  357. },
  358. // 添加地址事件
  359. address_add_event(e) {
  360. var temp_params = this.params;
  361. temp_params['id'] = 0;
  362. var query = app.globalData.json_to_url_params(temp_params);
  363. uni.navigateTo({
  364. url: '/pages/user-address-save/user-address-save?' + query
  365. });
  366. },
  367. // 地址编辑
  368. address_edit_event(e) {
  369. var index = e.currentTarget.dataset.index || 0;
  370. var data = this.data_list[index] || null;
  371. if (data == null) {
  372. app.globalData.showToast("地址有误");
  373. return false;
  374. }
  375. // 进入编辑页面
  376. var temp_params = this.params;
  377. temp_params['id'] = data.id;
  378. var query = app.globalData.json_to_url_params(temp_params);
  379. uni.navigateTo({
  380. url: '/pages/user-address-save/user-address-save?' + query
  381. });
  382. },
  383. // 地图查看
  384. address_map_event(e) {
  385. var index = e.currentTarget.dataset.index || 0;
  386. var data = this.data_list[index] || null;
  387. if (data == null) {
  388. app.globalData.showToast("地址有误");
  389. return false;
  390. }
  391. // 打开地图
  392. var name = data.alias || data.name || '';
  393. var address = (data.province_name || '') + (data.city_name || '') + (data.county_name || '') + (data.address || '');
  394. app.globalData.open_location(data.lng, data.lat, name, address);
  395. }
  396. }
  397. };
  398. </script>
  399. <style>
  400. @import './user-address.css';
  401. </style>