index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  6. <radio-group @change="addressChange">
  7. <view class="list" v-for="(item,index) in list" :key="index">
  8. <label class="radio" v-if="type=='shopping'">
  9. <radio :value="item._id" :checked="item._id == address_id" />
  10. </label>
  11. <view class="list_1">
  12. <view class="name">
  13. <text>{{item.name}}</text>
  14. <text>{{item.phone}}</text>
  15. </view>
  16. <view class="address">
  17. <text>{{item.province}}</text>
  18. <text>{{item.city}}</text>
  19. <text>{{item.area}}</text>
  20. <text>{{item.address}}</text>
  21. <text>{{item.number}}</text>
  22. </view>
  23. <view class="btn">
  24. <button class="btn_1" size="mini" @click="toDefa(item)" v-if="item.is_default==false">设为默认</button>
  25. <button class="btn_2" size="mini" @click="toCommon(item)">编辑</button>
  26. <button class="btn_3" size="mini" @click="toDel(item)">删除</button>
  27. </view>
  28. <view class="default" v-if="item.is_default==true">
  29. <text>默认</text>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="is_bottom" v-if="is_bottom">
  34. <text>{{config.bottom_title}}</text>
  35. </view>
  36. </radio-group>
  37. </scroll-view>
  38. </view>
  39. <view class="two">
  40. <view class="two_1">
  41. <button type="default" @click="toWxaddress()">获取微信地址</button>
  42. </view>
  43. <view class="two_1">
  44. <button type="default" @click="toCommon()">新增收货地址</button>
  45. </view>
  46. </view>
  47. </view>
  48. </mobile-frame>
  49. </template>
  50. <script>
  51. export default {
  52. data() {
  53. return {
  54. // 购物选地址
  55. type: '',
  56. address_id: '',
  57. // 系统设置
  58. config: {},
  59. user: {},
  60. list: [],
  61. total: 0,
  62. skip: 0,
  63. limit: 10,
  64. page: 0,
  65. // 数据是否触底
  66. is_bottom: false,
  67. scrollTop: 0,
  68. };
  69. },
  70. onLoad: function(e) {
  71. const that = this;
  72. that.$set(that, `type`, e && e.type || '');
  73. that.$set(that, `address_id`, e && e.id || '');
  74. },
  75. onShow: async function(e) {
  76. const that = this;
  77. that.searchConfig();
  78. that.watchLogin();
  79. },
  80. onHide: function() {
  81. const that = this;
  82. that.clearPage()
  83. },
  84. onPullDownRefresh: function() {
  85. const that = this;
  86. that.clearPage()
  87. that.search();
  88. uni.stopPullDownRefresh();
  89. },
  90. methods: {
  91. // 查询基本设置
  92. searchConfig() {
  93. const that = this;
  94. uni.getStorage({
  95. key: 'config',
  96. success: function(res) {
  97. if (res.data) that.$set(that, `config`, res.data)
  98. },
  99. fail: function(err) {
  100. console.log(err);
  101. }
  102. })
  103. },
  104. // 监听用户是否登录
  105. watchLogin() {
  106. const that = this;
  107. uni.getStorage({
  108. key: 'token',
  109. success: function(res) {
  110. let user = that.$jwt(res.data);
  111. that.$set(that, `user`, user);
  112. that.search()
  113. },
  114. fail: function(err) {
  115. uni.navigateTo({
  116. url: `/pages/login/index`
  117. })
  118. }
  119. })
  120. },
  121. // 查询列表
  122. async search() {
  123. const that = this;
  124. let user = that.user;
  125. let info = {
  126. skip: that.skip,
  127. limit: that.limit,
  128. customer: user._id
  129. }
  130. const res = await that.$api(`/address`, 'GET', {
  131. ...info
  132. })
  133. if (res.errcode == '0') {
  134. let list = [...that.list, ...res.data];
  135. that.$set(that, `list`, list);
  136. that.$set(that, `total`, res.total)
  137. }
  138. },
  139. // 分页
  140. toPage() {
  141. const that = this;
  142. let list = that.list;
  143. let limit = that.limit;
  144. if (that.total > list.length) {
  145. uni.showLoading({
  146. title: '加载中',
  147. mask: true
  148. })
  149. let page = that.page + 1;
  150. that.$set(that, `page`, page)
  151. let skip = page * limit;
  152. that.$set(that, `skip`, skip)
  153. that.search();
  154. uni.hideLoading();
  155. } else that.$set(that, `is_bottom`, true)
  156. },
  157. // 计算是否触底
  158. toScroll(e) {
  159. const that = this;
  160. let up = that.scrollTop;
  161. that.$set(that, `scrollTop`, e.detail.scrollTop);
  162. let num = Math.sign(up - e.detail.scrollTop);
  163. if (num == 1) that.$set(that, `is_bottom`, false);
  164. },
  165. // 选择收货地址
  166. addressChange(e) {
  167. const that = this;
  168. let data = that.list.find((i) => i._id == e.detail.value);
  169. if (data) {
  170. that.address_id == data._id;
  171. uni.$emit("id", e.detail.value);
  172. uni.navigateBack({
  173. delta: 1
  174. });
  175. }
  176. },
  177. // 设置默认
  178. toDefa(e) {
  179. const that = this;
  180. uni.showModal({
  181. title: '提示',
  182. content: '确定设置该地址为默认地址吗?',
  183. success: async function(res) {
  184. if (res.confirm) {
  185. const arr = await that.$api(`/address/toDefault/${e._id}`, `POST`);
  186. if (arr.errcode == '0') {
  187. uni.showToast({
  188. title: '添加默认成功',
  189. icon: 'none'
  190. })
  191. that.clearPage()
  192. that.search()
  193. } else {
  194. uni.showToast({
  195. title: arr.errmsg,
  196. icon: 'none'
  197. })
  198. }
  199. }
  200. }
  201. });
  202. },
  203. // 获取微信地址
  204. toWxaddress() {
  205. const that = this;
  206. let user = that.user;
  207. let config = that.$config;
  208. if (config.system) {
  209. if (config.system.uniPlatform == 'app') {
  210. uni.showToast({
  211. title: '请进入微信小程序进行获取微信地址',
  212. icon: 'none'
  213. })
  214. } else if (config.system.uniPlatform == 'mp-weixin') {
  215. uni.chooseAddress({
  216. success: async function(add) {
  217. let params = {
  218. customer: user._id,
  219. name: add.userName,
  220. phone: add.telNumber,
  221. province: add.provinceName,
  222. city: add.cityName,
  223. area: add.countyName,
  224. address: add.detailInfo
  225. }
  226. const arr = await that.$api(`/address`, 'POST', params);
  227. if (arr.errcode == '0') {
  228. uni.showToast({
  229. title: '添加收货地址成功',
  230. icon: 'none'
  231. })
  232. that.clearPage()
  233. } else {
  234. uni.showToast({
  235. title: arr.errmsg,
  236. icon: 'none'
  237. })
  238. }
  239. },
  240. fail: function() {
  241. that.clearPage()
  242. }
  243. })
  244. }
  245. }
  246. },
  247. // 编辑
  248. toCommon(e) {
  249. const that = this;
  250. that.clearPage();
  251. uni.navigateTo({
  252. url: `/pagesMy/address/add?id=${e&&e._id||''}`
  253. })
  254. },
  255. // 删除
  256. toDel(e) {
  257. const that = this;
  258. uni.showModal({
  259. title: '提示',
  260. content: '确定删除该地址吗?',
  261. success: async function(res) {
  262. if (res.confirm) {
  263. const arr = await that.$api(`/address/${e._id}`, 'DELETE');
  264. if (arr.errcode == '0') {
  265. uni.showToast({
  266. title: '删除信息成功',
  267. icon: 'none'
  268. })
  269. if (that.address_id == e._id) uni.$emit("id", '')
  270. that.clearPage()
  271. that.search()
  272. } else {
  273. uni.showToast({
  274. title: arr.errmsg,
  275. icon: 'none'
  276. })
  277. }
  278. }
  279. }
  280. });
  281. },
  282. // 清空列表
  283. clearPage() {
  284. const that = this;
  285. that.$set(that, `list`, [])
  286. that.$set(that, `skip`, 0)
  287. that.$set(that, `limit`, 6)
  288. that.$set(that, `page`, 0)
  289. }
  290. },
  291. }
  292. </script>
  293. <style lang="scss">
  294. .main {
  295. display: flex;
  296. flex-direction: column;
  297. width: 100vw;
  298. height: 100vh;
  299. background-color: var(--f1Color);
  300. .one {
  301. position: relative;
  302. flex-grow: 1;
  303. .list {
  304. display: flex;
  305. background: var(--fffColor);
  306. margin: 2vw 2vw 0 2vw;
  307. padding: 2vw;
  308. border-radius: 5px;
  309. position: relative;
  310. .list_1 {
  311. flex-grow: 1;
  312. .name {
  313. font-size: var(--font16Size);
  314. margin: 0 0 2vw 0;
  315. text {
  316. padding: 0 2vw 0 0;
  317. }
  318. }
  319. .address {
  320. font-size: var(--font14Size);
  321. margin: 0 0 1vw 0;
  322. text {
  323. padding: 0 2vw 0 0;
  324. }
  325. }
  326. .btn {
  327. text-align: center;
  328. button {
  329. margin: 0 2vw 2vw 2vw;
  330. background-color: var(--f35BColor);
  331. color: var(--fffColor);
  332. }
  333. .btn_2 {
  334. background-color: var(--f0fColor);
  335. }
  336. .btn_3 {
  337. background-color: var(--fFB1Color);
  338. }
  339. }
  340. .default {
  341. position: absolute;
  342. top: 0;
  343. right: 0;
  344. text {
  345. background: var(--fFB1Color);
  346. color: var(--fffColor);
  347. font-size: var(--font12Size);
  348. padding: 1vw;
  349. border-radius: 5px;
  350. }
  351. }
  352. }
  353. }
  354. }
  355. .two {
  356. display: flex;
  357. justify-content: space-between;
  358. .two_1 {
  359. width: 50vw;
  360. button {
  361. width: 100%;
  362. color: var(--fffColor);
  363. background-color: var(--f08Color);
  364. border-radius: 0;
  365. }
  366. }
  367. .two_1:nth-child(2) {
  368. button {
  369. background-color: var(--ff0Color);
  370. }
  371. }
  372. }
  373. }
  374. .scroll-view {
  375. position: absolute;
  376. top: 0;
  377. left: 0;
  378. right: 0;
  379. bottom: 0;
  380. .list-scroll-view {
  381. display: flex;
  382. flex-direction: column;
  383. }
  384. }
  385. .is_bottom {
  386. text-align: center;
  387. text {
  388. padding: 2vw 0;
  389. display: inline-block;
  390. color: #858585;
  391. font-size: 14px;
  392. }
  393. }
  394. </style>