add.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <view>
  3. <view class="address-box">
  4. <view class="address-box-list">
  5. <view class="address-box-label">
  6. 姓名
  7. </view>
  8. <view class="address-box-input">
  9. <input v-model="name" placeholder="请输入" type="text">
  10. </view>
  11. </view>
  12. <view class="address-box-list">
  13. <view class="address-box-label">
  14. 手机号
  15. </view>
  16. <view class="address-box-input">
  17. <input v-model="phone" placeholder="请输入" type="tel">
  18. </view>
  19. </view>
  20. <!-- <view class="address-box-list">
  21. <view class="address-box-label">
  22. 地址
  23. </view>
  24. <view class="address-box-input">
  25. <picker mode="multiSelector" @change="bindChange" @columnchange="bindMultiPickerColumnChange" :value="multiIndex" :range="multiArray">
  26. <view class="uni-input" v-if="selectAddr.length > 0">{{addrStr}}</view>
  27. <view class="uni-input" v-if="selectAddr.length == 0" style="color:grey;">{{addrStr}}</view>
  28. </picker>
  29. </view>
  30. </view> -->
  31. <view class="address-box-list">
  32. <view class="address-box-label">
  33. 详细地址
  34. </view>
  35. <view class="address-box-input">
  36. <input v-model="content" placeholder="请输入" type="text">
  37. </view>
  38. </view>
  39. </view>
  40. <view class="address-btn" @click="saveBtn">
  41. 保 存
  42. </view>
  43. <view v-if="id > 0" class="address-btn-del" @click="delAddr">
  44. 删 除
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. var app = getApp();
  50. export default {
  51. data() {
  52. return {
  53. id: 0,
  54. name: '',
  55. phone: '',
  56. content: '',
  57. // addressId1: 0,
  58. // addressId2: 0,
  59. multiArray: [],
  60. multiArray2: [],
  61. multiIndex: [0, 0],
  62. addrStr: '请选择',
  63. selectAddr: [],
  64. addrs: [],
  65. // addrs: [
  66. // {
  67. // id: 1,
  68. // name: '亚洲',
  69. // pid: 0
  70. // },
  71. // {
  72. // id: 2,
  73. // name: '欧洲',
  74. // pid: 0
  75. // },
  76. // {
  77. // id: 3,
  78. // name: '中国',
  79. // pid: 1
  80. // },
  81. // {
  82. // id: 4,
  83. // name: '日本',
  84. // pid: 1
  85. // },
  86. // {
  87. // id: 5,
  88. // name: '英国',
  89. // pid: 2
  90. // },
  91. // {
  92. // id: 6,
  93. // name: '法国',
  94. // pid: 2
  95. // }
  96. // ]
  97. }
  98. },
  99. onLoad(option) {
  100. this.id = option.id?option.id:0;
  101. if(this.id > 0){
  102. uni.setNavigationBarTitle({
  103. title: '编辑收货地址'
  104. });
  105. this.getAddrDetail();
  106. }
  107. // this.getAddr();
  108. },
  109. onShow() {
  110. var that = this;
  111. app.ajaxReadyCallback = res => { //各个接口统一回调方法
  112. var apiname = res.data.apiname;
  113. console.log(res);
  114. if(apiname == 'addr'){
  115. this.addrs = res.data.data;
  116. that.initAddr();
  117. } else if (apiname == 'addrDetail'){
  118. this.name = res.data.data.name;
  119. this.phone = res.data.data.phone;
  120. this.content = res.data.data.content;
  121. // this.addressId1 = res.data.data.addressId1;
  122. // this.addressId2 = res.data.data.addressId2;
  123. // this.selectAddr = [this.addressId1,this.addressId2];
  124. this.formatSelect();
  125. } else if (apiname == 'saveAddr') {
  126. uni.showToast({
  127. title: '保存成功',
  128. icon: 'none',
  129. duration: 2000
  130. })
  131. setTimeout(function(){
  132. uni.navigateBack({
  133. delta: 1
  134. });
  135. },1000);
  136. } else if (apiname == 'delAddr'){
  137. uni.showToast({
  138. title: '删除成功',
  139. icon: 'none',
  140. duration: 2000
  141. })
  142. setTimeout(function(){
  143. uni.navigateBack({
  144. delta: 1
  145. });
  146. },1000);
  147. }
  148. }
  149. },
  150. methods: {
  151. /* formatSelect(){
  152. const address = [];
  153. this.selectAddr.forEach((item) => {
  154. this.addrs.forEach((item2) => {
  155. if(item == item2.id){
  156. address.push(item2.name);
  157. }
  158. });
  159. });
  160. if(address.length > 0){
  161. this.addrStr = address.join(',');
  162. }else{
  163. this.addrStr = '请选择';
  164. }
  165. this.addressId1 = this.selectAddr[0];
  166. this.addressId2 = this.selectAddr[1];
  167. console.log(address,this.addrStr,this.selectAddr);
  168. },
  169. initAddr(){
  170. const arra1 = [];
  171. const arra2 = [];
  172. const arrb1 = [];
  173. const arrb2 = [];
  174. this.addrs.forEach((item) => {
  175. if(item.pid == 0){
  176. arra1.push(item.name);
  177. arrb1.push(item.id);
  178. }
  179. });
  180. this.addrs.forEach((item) => {
  181. if(item.pid == arrb1[0]){
  182. arra2.push(item.name);
  183. arrb2.push(item.id);
  184. }
  185. });
  186. this.multiArray.push(arra1);
  187. this.multiArray.push(arra2);
  188. this.multiArray2.push(arrb1);
  189. this.multiArray2.push(arrb2);
  190. this.formatSelect();
  191. },
  192. changeAddr(id){
  193. const arra1 = this.multiArray[0];
  194. const arra2 = [];
  195. const arrb1 = this.multiArray2[0];
  196. const arrb2 = [];
  197. this.addrs.forEach((item) => {
  198. if(item.pid == id){
  199. arra2.push(item.name);
  200. arrb2.push(item.id);
  201. }
  202. });
  203. this.multiArray = [];
  204. this.multiArray2 = [];
  205. this.multiArray.push(arra1);
  206. this.multiArray.push(arra2);
  207. this.multiArray2.push(arrb1);
  208. this.multiArray2.push(arrb2);
  209. },
  210. bindMultiPickerColumnChange: function(e) {
  211. console.log(e);
  212. console.log('修改的列为:' + e.detail.column + ',值为:' + e.detail.value);
  213. this.multiIndex[e.detail.column] = e.detail.value;
  214. switch (e.detail.column) {
  215. case 0: //拖动第1列
  216. this.changeAddr(this.multiArray2[0][this.multiIndex[0]]);
  217. this.multiIndex.splice(1, 1, 0) //将multiIndex数据中的第2列初始化
  218. this.selectAddr[0] = this.multiArray2[0][this.multiIndex[0]];
  219. this.selectAddr[1] = this.multiArray2[1][this.multiIndex[1]];
  220. break
  221. case 1: //拖动第2列
  222. this.selectAddr[1] = this.multiArray2[1][this.multiIndex[1]];
  223. break
  224. }
  225. console.log(this.selectAddr,this.multiIndex);
  226. this.formatSelect();
  227. this.$forceUpdate();
  228. },
  229. bindChange: function(e){
  230. this.multiIndex = e.detail.value;
  231. this.selectAddr[0] = this.multiArray2[0][this.multiIndex[0]];
  232. this.selectAddr[1] = this.multiArray2[1][this.multiIndex[1]];
  233. this.formatSelect();
  234. }, */
  235. // getAddr(){
  236. // app.ajax({
  237. // url: app.globalData.serverUrl + 'shopAddress/getAddress',
  238. // type: 'POST',
  239. // apiname: 'addr'
  240. // });
  241. // },
  242. getAddrDetail(){
  243. app.ajax({
  244. url: app.globalData.serverUrl + 'shopAddress/detail',
  245. type: 'POST',
  246. apiname: 'addrDetail',
  247. data: {
  248. id: this.id
  249. }
  250. });
  251. },
  252. delAddr(){
  253. var that = this;
  254. uni.showModal({
  255. title: '提示',
  256. content: '确定要删除此地址吗?',
  257. success: function (res) {
  258. if (res.confirm) {
  259. app.ajax({
  260. url: app.globalData.serverUrl + 'shopAddress/del',
  261. type: 'POST',
  262. apiname: 'delAddr',
  263. data: {
  264. id: that.id
  265. }
  266. });
  267. } else if (res.cancel) {
  268. console.log('用户点击取消');
  269. }
  270. }
  271. });
  272. },
  273. saveBtn(){
  274. if(!this.name){
  275. uni.showToast({
  276. title: '请填写姓名',
  277. icon: 'none',
  278. duration: 2000
  279. })
  280. return;
  281. }
  282. if(!this.phone){
  283. uni.showToast({
  284. title: '请填写手机号',
  285. icon: 'none',
  286. duration: 2000
  287. })
  288. return;
  289. }
  290. // if(!this.addressId1||!this.addressId2){
  291. // uni.showToast({
  292. // title: '请选择地址',
  293. // icon: 'none',
  294. // duration: 2000
  295. // })
  296. // return;
  297. // }
  298. if(!this.content){
  299. uni.showToast({
  300. title: '请填写详细地址',
  301. icon: 'none',
  302. duration: 2000
  303. })
  304. return;
  305. }
  306. let param = {
  307. id: this.id,
  308. name: this.name,
  309. phone: this.phone,
  310. // address_id1: this.addressId1,
  311. // address_id2: this.addressId2,
  312. content: this.content
  313. }
  314. let url = app.globalData.serverUrl + 'shopAddress/add';
  315. if(this.id > 0){
  316. url = app.globalData.serverUrl + 'shopAddress/update';
  317. }
  318. app.ajax({
  319. url: url,
  320. type: 'POST',
  321. apiname: 'saveAddr',
  322. data: param
  323. });
  324. }
  325. }
  326. }
  327. </script>
  328. <style lang="scss" scoped>
  329. .address-box{
  330. background-color: #FFFFFF;
  331. padding: 0 20rpx;
  332. margin-top: 20rpx;
  333. }
  334. .address-box-list{
  335. height: 90rpx;
  336. line-height: 90rpx;
  337. border-bottom: 1rpx solid #f2f2f2;
  338. }
  339. .address-box-list:last-child{
  340. border: 0;
  341. }
  342. .address-box-label{
  343. display: inline-block;
  344. width: 150rpx;
  345. height: 100%;
  346. float: left;
  347. }
  348. .address-box-input{
  349. display: inline-block;
  350. width: 550rpx;
  351. height: 100%;
  352. float: right;
  353. }
  354. .address-box-input input{
  355. height: 70rpx;
  356. margin-top: 10rpx;
  357. }
  358. .address-btn{
  359. width: 700rpx;
  360. height: 70rpx;
  361. line-height: 70rpx;
  362. color: #FFFFFF;
  363. background-color: $theme-color;
  364. border-radius: 10rpx;
  365. margin: 50rpx auto 20rpx auto;
  366. text-align: center;
  367. }
  368. .address-btn-del{
  369. width: 700rpx;
  370. height: 70rpx;
  371. line-height: 70rpx;
  372. color: $theme-color;
  373. background-color: #FFFFFF;
  374. border: 1rpx solid $theme-color;
  375. border-radius: 10rpx;
  376. margin: 10rpx auto;
  377. text-align: center;
  378. }
  379. </style>