index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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: 6,
  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() {
  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.reLaunch({
  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. if (list.length == 0) uni.$emit("id", '')
  138. if (that.id) {
  139. for (let i = 0; i < list.length; i++) {
  140. if (list[i]._id === that.id) {
  141. that.address_id = that.id;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. },
  148. // 分页
  149. toPage() {
  150. const that = this;
  151. let list = that.list;
  152. let limit = that.limit;
  153. if (that.total > list.length) {
  154. uni.showLoading({
  155. title: '加载中',
  156. mask: true
  157. })
  158. let page = that.page + 1;
  159. that.$set(that, `page`, page)
  160. let skip = page * limit;
  161. that.$set(that, `skip`, skip)
  162. that.search();
  163. uni.hideLoading();
  164. } else that.$set(that, `is_bottom`, true)
  165. },
  166. // 计算是否触底
  167. toScroll(e) {
  168. const that = this;
  169. let up = that.scrollTop;
  170. that.$set(that, `scrollTop`, e.detail.scrollTop);
  171. let num = Math.sign(up - e.detail.scrollTop);
  172. if (num == 1) that.$set(that, `is_bottom`, false);
  173. },
  174. // 选择收货地址
  175. addressChange(e) {
  176. const that = this;
  177. for (let i = 0; i < that.list.length; i++) {
  178. if (that.list[i]._id === e.detail.value) {
  179. that.address_id = e.detail.value;
  180. break;
  181. }
  182. }
  183. uni.$emit("id", e.detail.value)
  184. uni.navigateBack({
  185. delta: 1
  186. })
  187. },
  188. // 设置默认
  189. toDefa(e) {
  190. const that = this;
  191. uni.showModal({
  192. title: '提示',
  193. content: '确定设置该地址为默认地址吗?',
  194. success: async function(res) {
  195. if (res.confirm) {
  196. const arr = await that.$api(`/address/toDefault/${e._id}`, `POST`);
  197. if (arr.errcode == '0') {
  198. uni.showToast({
  199. title: '添加默认成功',
  200. icon: 'none'
  201. })
  202. that.clearPage()
  203. that.search()
  204. } else {
  205. uni.showToast({
  206. title: arr.errmsg,
  207. icon: 'none'
  208. })
  209. }
  210. }
  211. }
  212. });
  213. },
  214. // 获取微信地址
  215. toWxaddress() {
  216. const that = this;
  217. let user = that.user;
  218. uni.getStorage({
  219. key: 'system',
  220. success: function(res) {
  221. if (res.data.uniPlatform == 'app') {
  222. uni.showToast({
  223. title: '请进入微信小程序进行获取微信地址',
  224. icon: 'none'
  225. })
  226. } else if (res.data.uniPlatform == 'mp-weixin') {
  227. uni.chooseAddress({
  228. success: async function(add) {
  229. let params = {
  230. customer: user._id,
  231. name: add.userName,
  232. phone: add.telNumber,
  233. province: add.provinceName,
  234. city: add.cityName,
  235. area: add.countyName,
  236. address: add.detailInfo
  237. }
  238. const arr = await that.$api(`/address`, 'POST', params);
  239. if (arr.errcode == '0') {
  240. uni.showToast({
  241. title: '添加收货地址成功',
  242. icon: 'none'
  243. })
  244. that.clearPage()
  245. } else {
  246. uni.showToast({
  247. title: arr.errmsg,
  248. icon: 'none'
  249. })
  250. }
  251. },
  252. fail: function() {
  253. that.clearPage()
  254. }
  255. })
  256. }
  257. }
  258. })
  259. },
  260. // 编辑
  261. toCommon(e) {
  262. const that = this;
  263. that.clearPage();
  264. uni.navigateTo({
  265. url: `/pagesMy/address/add?id=${e&&e._id||''}`
  266. })
  267. },
  268. // 删除
  269. toDel(e) {
  270. const that = this;
  271. uni.showModal({
  272. title: '提示',
  273. content: '确定删除该地址吗?',
  274. success: async function(res) {
  275. if (res.confirm) {
  276. const arr = await that.$api(`/address/${e._id}`, 'DELETE');
  277. if (arr.errcode == '0') {
  278. if (that.address_id === e._id) uni.$emit("id", '')
  279. uni.showToast({
  280. title: '删除信息成功',
  281. icon: 'none'
  282. })
  283. that.clearPage()
  284. that.search()
  285. } else {
  286. uni.showToast({
  287. title: arr.errmsg,
  288. icon: 'none'
  289. })
  290. }
  291. }
  292. }
  293. });
  294. },
  295. // 清空列表
  296. clearPage() {
  297. const that = this;
  298. that.$set(that, `list`, [])
  299. that.$set(that, `skip`, 0)
  300. that.$set(that, `limit`, 6)
  301. that.$set(that, `page`, 0)
  302. }
  303. },
  304. }
  305. </script>
  306. <style lang="scss">
  307. .main {
  308. display: flex;
  309. flex-direction: column;
  310. width: 100vw;
  311. height: 100vh;
  312. background-color: var(--f1Color);
  313. .one {
  314. position: relative;
  315. flex-grow: 1;
  316. .list {
  317. display: flex;
  318. background: var(--fffColor);
  319. margin: 2vw 2vw 0 2vw;
  320. padding: 2vw;
  321. border-radius: 5px;
  322. position: relative;
  323. .list_1 {
  324. flex-grow: 1;
  325. .name {
  326. font-size: var(--font16Size);
  327. margin: 0 0 2vw 0;
  328. text {
  329. padding: 0 2vw 0 0;
  330. }
  331. }
  332. .address {
  333. font-size: var(--font14Size);
  334. margin: 0 0 1vw 0;
  335. text {
  336. padding: 0 2vw 0 0;
  337. }
  338. }
  339. .btn {
  340. text-align: center;
  341. button {
  342. margin: 0 2vw 2vw 2vw;
  343. background-color: var(--f35BColor);
  344. color: var(--fffColor);
  345. }
  346. .btn_2 {
  347. background-color: var(--f0fColor);
  348. }
  349. .btn_3 {
  350. background-color: var(--fFB1Color);
  351. }
  352. }
  353. .default {
  354. position: absolute;
  355. top: 0;
  356. right: 0;
  357. text {
  358. background: var(--ff0Color);
  359. color: var(--fffColor);
  360. font-size: var(--font12Size);
  361. padding: 1vw;
  362. border-radius: 5px;
  363. }
  364. }
  365. }
  366. }
  367. }
  368. .two {
  369. display: flex;
  370. justify-content: space-between;
  371. .two_1 {
  372. width: 50vw;
  373. button {
  374. width: 100%;
  375. color: var(--fffColor);
  376. background-color: var(--f08Color);
  377. border-radius: 0;
  378. }
  379. }
  380. .two_1:nth-child(2) {
  381. button {
  382. background-color: var(--fFB1Color);
  383. }
  384. }
  385. }
  386. }
  387. .scroll-view {
  388. position: absolute;
  389. top: 0;
  390. left: 0;
  391. right: 0;
  392. bottom: 0;
  393. .list-scroll-view {
  394. display: flex;
  395. flex-direction: column;
  396. }
  397. }
  398. .is_bottom {
  399. text-align: center;
  400. text {
  401. padding: 2vw 0;
  402. display: inline-block;
  403. color: #858585;
  404. font-size: 14px;
  405. }
  406. }
  407. </style>