index.vue 9.3 KB

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