add.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one">
  5. <uni-forms ref="form" v-model="form" :rules="rules" label-width="auto">
  6. <uni-forms-item label="收货人" name="name">
  7. <uni-easyinput type="text" v-model="form.name" placeholder="请输入收货人" />
  8. </uni-forms-item>
  9. <uni-forms-item label="联系电话" name="phone">
  10. <uni-easyinput type="number" v-model="form.phone" placeholder="请输入联系电话" />
  11. </uni-forms-item>
  12. <uni-forms-item label="地图选点">
  13. <button type="default" size="mini" class="toLoacl" @click="toLocaltion()">地图选点</button>
  14. </uni-forms-item>
  15. <uni-forms-item label="所在省份" name="province">
  16. <picker class="picker" mode="selector" :range="provinceList" @change="proChange" range-key="value">
  17. <view>{{form.province||'请选择所在省份'}}</view>
  18. </picker>
  19. </uni-forms-item>
  20. <uni-forms-item label="所在市" name="city">
  21. <picker class="picker" mode="selector" :range="cityList" @change="cityChange" range-key="value">
  22. <view>{{form.city||'请选择所在市'}}</view>
  23. </picker>
  24. </uni-forms-item>
  25. <uni-forms-item label="所在区" name="area">
  26. <picker class="picker" mode="selector" :range="areaList" @change="areaChange" range-key="value">
  27. <view>{{form.area||'请选择所在区'}}</view>
  28. </picker>
  29. </uni-forms-item>
  30. <uni-forms-item label="详细地址" name="address">
  31. <uni-easyinput type="textarea" v-model="form.address" placeholder="请输入详细地址" />
  32. </uni-forms-item>
  33. <uni-forms-item label="是否默认" name="is_default">
  34. <switch color="#FB1438" :checked="form.is_default=='1'?true:false" @change="defaChange" />
  35. </uni-forms-item>
  36. </uni-forms>
  37. <view class="btn">
  38. <button type="primary" size="mini" @click="onSubmit('form')">提交保存</button>
  39. </view>
  40. </view>
  41. </view>
  42. </mobile-frame>
  43. </template>
  44. <script>
  45. export default {
  46. data() {
  47. return {
  48. user: {},
  49. id: '',
  50. form: {},
  51. rules: {
  52. name: {
  53. rules: [{
  54. required: true,
  55. errorMessage: '请输入收货人',
  56. }]
  57. },
  58. phone: {
  59. rules: [{
  60. required: true,
  61. errorMessage: '请输入联系电话',
  62. tel: true
  63. }]
  64. },
  65. province: {
  66. rules: [{
  67. required: true,
  68. errorMessage: '请选择所在省'
  69. }]
  70. },
  71. city: {
  72. rules: [{
  73. required: true,
  74. errorMessage: '请选择所在市'
  75. }]
  76. },
  77. area: {
  78. rules: [{
  79. required: false,
  80. errorMessage: '请选择所在区'
  81. }]
  82. },
  83. address: {
  84. rules: [{
  85. required: true,
  86. errorMessage: '请输入详细地址'
  87. }]
  88. },
  89. },
  90. provinceList: [ //所在省份
  91. {
  92. code: '1',
  93. value: '吉林省',
  94. },
  95. {
  96. code: '2',
  97. value: '辽宁省',
  98. },
  99. {
  100. code: '3',
  101. value: '黑龙江省',
  102. },
  103. ],
  104. cityList: [ //所在市
  105. {
  106. code: '1-1',
  107. value: '长春市',
  108. },
  109. {
  110. code: '2-1',
  111. value: '大连市',
  112. },
  113. {
  114. code: '3-1',
  115. value: '哈尔滨市',
  116. },
  117. ],
  118. areaList: [ //所在区
  119. {
  120. code: '1-1-1',
  121. value: '朝阳区'
  122. },
  123. {
  124. code: '2-1-1',
  125. value: '开发区'
  126. },
  127. {
  128. code: '3-1-1',
  129. value: '发展区'
  130. },
  131. ]
  132. };
  133. },
  134. onLoad: function(e) {
  135. const that = this;
  136. that.$set(that, `id`, e && e.id || '');
  137. that.watchLogin();
  138. },
  139. onShow: function() {
  140. const that = this;
  141. },
  142. onUnload: function() {},
  143. methods: {
  144. // 监听用户是否登录
  145. watchLogin() {
  146. const that = this;
  147. uni.getStorage({
  148. key: 'token',
  149. success: async function(res) {
  150. let user = that.$jwt(res.data);
  151. if (user) {
  152. that.$set(that, `user`, user);
  153. if (that.id) {
  154. let arr = await that.$api(`/address/${that.id}`, 'GET')
  155. if (arr.errcode == '0') {
  156. that.$set(that, `form`, arr.data)
  157. }
  158. } else {
  159. that.$set(that, `form`, {
  160. name: user.name || '',
  161. phone: user.phone || ''
  162. })
  163. }
  164. }
  165. },
  166. fail: function(err) {
  167. uni.reLaunch({
  168. url: '/pages/login/index'
  169. })
  170. }
  171. })
  172. },
  173. // 定位
  174. toLocaltion() {
  175. const that = this;
  176. uni.chooseLocation({
  177. success: function(res) {
  178. if (res) {
  179. that.$set(that.form, `address`, res.address || '')
  180. }
  181. },
  182. fail: function(err) {
  183. console.log(err);
  184. }
  185. })
  186. // const key = that.$config.txlocalKey;
  187. // const referer = that.$config.txlocalReferer;
  188. // uni.navigateTo({
  189. // url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`
  190. // });
  191. },
  192. // 选择省份
  193. proChange(e) {
  194. const that = this;
  195. let data = that.provinceList[e.detail.value];
  196. if (data) {
  197. that.$set(that.form, `province`, data.value)
  198. }
  199. },
  200. // 选择区
  201. areaChange(e) {
  202. const that = this;
  203. let data = that.areaList[e.detail.value];
  204. if (data) {
  205. that.$set(that.form, `area`, data.value)
  206. }
  207. },
  208. // 选择市
  209. cityChange(e) {
  210. const that = this;
  211. let data = that.cityList[e.detail.value];
  212. if (data) {
  213. that.$set(that.form, `city`, data.value)
  214. }
  215. },
  216. // 是否默认
  217. defaChange(e) {
  218. const that = this;
  219. let value = e.detail.value == true ? '1' : '0';
  220. that.$set(that.form, `is_default`, value)
  221. },
  222. // 提交保存
  223. onSubmit(ref) {
  224. const that = this;
  225. let id = that.id;
  226. let user = that.user;
  227. that.$refs[ref].validate().then(async params => {
  228. params.customer = user.id;
  229. let res;
  230. if (id) res = await that.$api(`/address/${id}`, 'POST', params)
  231. else res = await that.$api(`/address`, 'POST', params);
  232. if (res.errcode == '0') {
  233. uni.showToast({
  234. title: '维护信息成功',
  235. icon: 'none'
  236. })
  237. uni.navigateBack({
  238. delta: 1
  239. })
  240. } else {
  241. uni.showToast({
  242. title: res.errmsg,
  243. icon: 'none'
  244. })
  245. }
  246. })
  247. },
  248. }
  249. }
  250. </script>
  251. <style lang="scss">
  252. .main {
  253. display: flex;
  254. flex-direction: column;
  255. width: 100vw;
  256. height: 100vh;
  257. .one {
  258. padding: 2vw;
  259. .toLoacl {
  260. margin: 1vw 0 0 0;
  261. }
  262. .picker {
  263. border: 1px solid #3333;
  264. border-radius: 5px;
  265. padding: 2vw;
  266. }
  267. .btn {
  268. text-align: center;
  269. button {
  270. width: 30%;
  271. font-size: 14px;
  272. background-color: var(--f35BColor);
  273. }
  274. }
  275. }
  276. }
  277. </style>