index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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(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.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. }
  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. uni.getStorage({
  208. key: 'system',
  209. success: function(res) {
  210. if (res.data.uniPlatform == 'app') {
  211. uni.showToast({
  212. title: '请进入微信小程序进行获取微信地址',
  213. icon: 'none'
  214. })
  215. } else if (res.data.uniPlatform == 'mp-weixin') {
  216. uni.chooseAddress({
  217. success: async function(add) {
  218. let params = {
  219. customer: user._id,
  220. name: add.userName,
  221. phone: add.telNumber,
  222. province: add.provinceName,
  223. city: add.cityName,
  224. area: add.countyName,
  225. address: add.detailInfo
  226. }
  227. const arr = await that.$api(`/address`, 'POST', params);
  228. if (arr.errcode == '0') {
  229. uni.showToast({
  230. title: '添加收货地址成功',
  231. icon: 'none'
  232. })
  233. that.clearPage()
  234. } else {
  235. uni.showToast({
  236. title: arr.errmsg,
  237. icon: 'none'
  238. })
  239. }
  240. },
  241. fail: function() {
  242. that.clearPage()
  243. }
  244. })
  245. }
  246. }
  247. })
  248. },
  249. // 编辑
  250. toCommon(e) {
  251. const that = this;
  252. that.clearPage();
  253. uni.navigateTo({
  254. url: `/pagesMy/address/add?id=${e&&e._id||''}`
  255. })
  256. },
  257. // 删除
  258. toDel(e) {
  259. const that = this;
  260. uni.showModal({
  261. title: '提示',
  262. content: '确定删除该地址吗?',
  263. success: async function(res) {
  264. if (res.confirm) {
  265. const arr = await that.$api(`/address/${e._id}`, 'DELETE');
  266. if (arr.errcode == '0') {
  267. uni.showToast({
  268. title: '删除信息成功',
  269. icon: 'none'
  270. })
  271. if (that.address_id == e._id) uni.$emit("id", '')
  272. that.clearPage()
  273. that.search()
  274. } else {
  275. uni.showToast({
  276. title: arr.errmsg,
  277. icon: 'none'
  278. })
  279. }
  280. }
  281. }
  282. });
  283. },
  284. // 清空列表
  285. clearPage() {
  286. const that = this;
  287. that.$set(that, `list`, [])
  288. that.$set(that, `skip`, 0)
  289. that.$set(that, `limit`, 6)
  290. that.$set(that, `page`, 0)
  291. }
  292. },
  293. }
  294. </script>
  295. <style lang="scss">
  296. .main {
  297. display: flex;
  298. flex-direction: column;
  299. width: 100vw;
  300. height: 100vh;
  301. background-color: var(--f1Color);
  302. .one {
  303. position: relative;
  304. flex-grow: 1;
  305. .list {
  306. display: flex;
  307. background: var(--fffColor);
  308. margin: 2vw 2vw 0 2vw;
  309. padding: 2vw;
  310. border-radius: 5px;
  311. position: relative;
  312. .list_1 {
  313. flex-grow: 1;
  314. .name {
  315. font-size: var(--font16Size);
  316. margin: 0 0 2vw 0;
  317. text {
  318. padding: 0 2vw 0 0;
  319. }
  320. }
  321. .address {
  322. font-size: var(--font14Size);
  323. margin: 0 0 1vw 0;
  324. text {
  325. padding: 0 2vw 0 0;
  326. }
  327. }
  328. .btn {
  329. text-align: center;
  330. button {
  331. margin: 0 2vw 2vw 2vw;
  332. background-color: var(--f35BColor);
  333. color: var(--fffColor);
  334. }
  335. .btn_2 {
  336. background-color: var(--f0fColor);
  337. }
  338. .btn_3 {
  339. background-color: var(--fFB1Color);
  340. }
  341. }
  342. .default {
  343. position: absolute;
  344. top: 0;
  345. right: 0;
  346. text {
  347. background: var(--ff0Color);
  348. color: var(--fffColor);
  349. font-size: var(--font12Size);
  350. padding: 1vw;
  351. border-radius: 5px;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. .two {
  358. display: flex;
  359. justify-content: space-between;
  360. .two_1 {
  361. width: 50vw;
  362. button {
  363. width: 100%;
  364. color: var(--fffColor);
  365. background-color: var(--f08Color);
  366. border-radius: 0;
  367. }
  368. }
  369. .two_1:nth-child(2) {
  370. button {
  371. background-color: var(--fFB1Color);
  372. }
  373. }
  374. }
  375. }
  376. .scroll-view {
  377. position: absolute;
  378. top: 0;
  379. left: 0;
  380. right: 0;
  381. bottom: 0;
  382. .list-scroll-view {
  383. display: flex;
  384. flex-direction: column;
  385. }
  386. }
  387. .is_bottom {
  388. text-align: center;
  389. text {
  390. padding: 2vw 0;
  391. display: inline-block;
  392. color: #858585;
  393. font-size: 14px;
  394. }
  395. }
  396. </style>