add.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <template>
  2. <view class="main">
  3. <view class="one">
  4. <uni-forms ref="form" v-model="form" :rules="rules" label-width="auto">
  5. <uni-forms-item label="收货人" name="name">
  6. <input class="input" type="text" v-model="form.name" placeholder="请输入收货人" />
  7. </uni-forms-item>
  8. <uni-forms-item label="联系电话" name="phone">
  9. <input class="input" type="number" v-model="form.phone" placeholder="请输入联系电话" />
  10. </uni-forms-item>
  11. <uni-forms-item label="地图选点">
  12. <button type="default" size="mini" class="toLoacl" @click="toLocaltion()">地图选点</button>
  13. </uni-forms-item>
  14. <uni-forms-item label="所在省份" name="province">
  15. <picker class="picker" mode="selector" :range="provinceList" @change="proChange" range-key="name">
  16. <view>{{form.province||'请选择所在省份'}}</view>
  17. </picker>
  18. </uni-forms-item>
  19. <uni-forms-item label="所在市" name="city">
  20. <picker class="picker" mode="selector" :range="cityList" @change="cityChange" range-key="name">
  21. <view>{{form.city||'请选择所在市'}}</view>
  22. </picker>
  23. </uni-forms-item>
  24. <uni-forms-item label="所在区" name="area">
  25. <picker class="picker" mode="selector" :range="areaList" @change="areaChange">
  26. <view>{{form.area||'请选择所在区'}}</view>
  27. </picker>
  28. </uni-forms-item>
  29. <uni-forms-item label="详细地址" name="address">
  30. <textarea class="input" v-model="form.address" placeholder="请输入详细地址" />
  31. </uni-forms-item>
  32. <uni-forms-item label="是否默认" name="is_default">
  33. <switch color="#FB1438" :checked="form.is_default=='1'?true:false" @change="defaChange" />
  34. </uni-forms-item>
  35. </uni-forms>
  36. <view class="btn">
  37. <button type="primary" size="mini" @click="onSubmit('form')">提交保存</button>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. import {
  44. getAddress
  45. } from '@/common/tool.js';
  46. export default {
  47. data() {
  48. return {
  49. user: {},
  50. id: '',
  51. form: {},
  52. rules: {
  53. name: {
  54. rules: [ //
  55. {
  56. required: true,
  57. errorMessage: '请输入收货人',
  58. },
  59. {
  60. minLength: 2,
  61. errorMessage: '收货人名称长度至少在 {minLength} 个字符以上',
  62. }
  63. ]
  64. },
  65. phone: {
  66. rules: [{
  67. required: true,
  68. errorMessage: '请输入联系电话',
  69. tel: true
  70. }]
  71. },
  72. province: {
  73. rules: [{
  74. required: true,
  75. errorMessage: '请选择所在省'
  76. }]
  77. },
  78. city: {
  79. rules: [{
  80. required: true,
  81. errorMessage: '请选择所在市'
  82. }]
  83. },
  84. area: {
  85. rules: [{
  86. required: false,
  87. errorMessage: '请选择所在区'
  88. }]
  89. },
  90. address: {
  91. rules: [{
  92. required: true,
  93. errorMessage: '请输入详细地址'
  94. }]
  95. },
  96. },
  97. provinceList: [ //所在省份
  98. ],
  99. cityList: [ //所在市
  100. ],
  101. areaList: [ //所在区
  102. ],
  103. paltForm: ''
  104. };
  105. },
  106. onLoad: function(e) {
  107. const that = this;
  108. that.$set(that, `id`, e.id || '');
  109. if (e.id) {
  110. uni.setNavigationBarTitle({
  111. title: '修改地址'
  112. });
  113. }
  114. that.searchPor();
  115. that.watchLogin();
  116. },
  117. onShow: function() {
  118. const that = this;
  119. let config = that.$config;
  120. if (config) {
  121. // 账号平台
  122. that.$set(that, `paltForm`, config.system.uniPlatform)
  123. }
  124. },
  125. methods: {
  126. // 监听用户是否登录
  127. watchLogin() {
  128. const that = this;
  129. uni.getStorage({
  130. key: 'token',
  131. success: async function(res) {
  132. let user = that.$jwt(res.data);
  133. if (user) {
  134. that.$set(that, `user`, user);
  135. if (that.id) {
  136. let arr = await that.$api(`address/${that.id}`, 'GET')
  137. if (arr.errcode == '0') {
  138. let index = that.provinceList.findIndex((i) => i.name == arr.data
  139. .province);
  140. if (index) {
  141. that.proChange(parseFloat(index));
  142. }
  143. that.$set(that, `form`, arr.data)
  144. }
  145. } else {
  146. that.$set(that, `form`, {
  147. name: user.name || '',
  148. phone: user.phone || ''
  149. })
  150. }
  151. }
  152. },
  153. fail: function(err) {
  154. uni.navigateTo({
  155. url: '/pagesHome/login/index'
  156. })
  157. }
  158. })
  159. },
  160. // 查找省
  161. searchPor() {
  162. const that = this;
  163. let list = that.$config.china;
  164. that.$set(that, `provinceList`, list)
  165. },
  166. // 定位
  167. toLocaltion() {
  168. const that = this;
  169. uni.chooseLocation({
  170. success: function(res) {
  171. that.searchAddress(res);
  172. }
  173. })
  174. },
  175. // 解析位置详细信息
  176. searchAddress(e) {
  177. const that = this;
  178. that.$set(that.form, `address`, e.address);
  179. if (that.paltForm == 'app') {
  180. var point = new plus.maps.Point(e.longitude, e.latitude);
  181. plus.maps.Map.reverseGeocode(point, {}, function(event) {
  182. var address = event.address;
  183. var reg = /.+?(省|市|自治区|自治州|县|区)/g;
  184. let addressList = address.match(reg).toString().split(",");
  185. let province = addressList[0] || '';
  186. let city = addressList[1] || '';
  187. let area = addressList[2] || '';
  188. that.$set(that.form, `province`, province);
  189. that.$set(that.form, `city`, city);
  190. that.$set(that.form, `area`, area);
  191. })
  192. } else if (that.paltForm == 'mp-weixin') {
  193. getAddress(e.longitude, e.latitude).then((res) => {
  194. if (res.status == 0) {
  195. let province = res.result.address_component.province;
  196. let city = res.result.address_component.city;
  197. let area = res.result.address_component.district;
  198. that.$set(that.form, `province`, province);
  199. that.$set(that.form, `city`, city);
  200. that.$set(that.form, `area`, area);
  201. } else {
  202. uni.showToast({
  203. title: '解析位置失败,请重新选择!'
  204. });
  205. }
  206. })
  207. }
  208. },
  209. // 选择省份
  210. proChange(e) {
  211. const that = this;
  212. let index = 0;
  213. if (e && e.detail && e.detail.value) index = e.detail.value
  214. else index = e;
  215. let data = that.provinceList[index];
  216. if (data) {
  217. if (that.id) {
  218. that.$set(that.form, `city`, '');
  219. that.$set(that.form, `area`, '');
  220. }
  221. that.$set(that.form, `province`, data.name);
  222. that.$set(that, `cityList`, data.city)
  223. }
  224. },
  225. // 选择市
  226. cityChange(e) {
  227. const that = this;
  228. let data = that.cityList[e.detail.value];
  229. if (data) {
  230. if (that.id) {
  231. that.$set(that.form, `area`, '');
  232. }
  233. that.$set(that.form, `city`, data.name);
  234. that.$set(that, `areaList`, data.area);
  235. }
  236. },
  237. // 选择区
  238. areaChange(e) {
  239. const that = this;
  240. let data = that.areaList[e.detail.value];
  241. if (data) {
  242. that.$set(that.form, `area`, data)
  243. }
  244. },
  245. // 是否默认
  246. defaChange(e) {
  247. const that = this;
  248. let value = e.detail.value == true ? '1' : '0';
  249. that.$set(that.form, `is_default`, value)
  250. },
  251. // 提交保存
  252. onSubmit(ref) {
  253. const that = this;
  254. let id = that.id;
  255. let user = that.user;
  256. that.$refs[ref].validate().then(async params => {
  257. params.customer = user.id;
  258. let res;
  259. if (id) res = await that.$api(`address/${id}`, 'POST', params)
  260. else res = await that.$api(`address`, 'POST', params);
  261. if (res.errcode == '0') {
  262. uni.showToast({
  263. title: '维护信息成功',
  264. icon: 'none'
  265. })
  266. uni.navigateBack({
  267. delta: 1
  268. })
  269. } else {
  270. uni.showToast({
  271. title: res.errmsg,
  272. icon: 'none'
  273. })
  274. }
  275. })
  276. },
  277. }
  278. }
  279. </script>
  280. <style lang="scss">
  281. .main {
  282. display: flex;
  283. flex-direction: column;
  284. .one {
  285. padding: 2vw;
  286. .toLoacl {
  287. margin: 1vw 0 0 0;
  288. }
  289. .input {
  290. width: 94%;
  291. padding: 2vw;
  292. border: 1px solid #3333;
  293. border-radius: 5px;
  294. }
  295. .picker {
  296. border: 1px solid #3333;
  297. border-radius: 5px;
  298. padding: 2vw;
  299. }
  300. .btn {
  301. text-align: center;
  302. button {
  303. width: 30%;
  304. font-size: 14px;
  305. background-color: var(--fFFColor);
  306. }
  307. }
  308. }
  309. }
  310. </style>