index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. var util = require('../../../../../utils/util.js');
  2. var api = require('../../../../../utils/api');
  3. var Parse = getApp().Parse;
  4. import placeArrays from "../../../../../utils/citysearch"
  5. const placeArray=placeArrays.placeArray
  6. Page({
  7. data: {
  8. type:1,
  9. address: {
  10. id: 0,
  11. province_id: 0,
  12. city_id: 0,
  13. district_id: 0,
  14. address: '',
  15. full_region: '',
  16. name: '',
  17. mobile: '',
  18. is_default: 0
  19. },
  20. addressId: 0,
  21. openSelectRegion: false,
  22. selectRegionList: [{
  23. id: 0,
  24. name: '省份',
  25. parent_id: 1,
  26. type: 1
  27. },
  28. {
  29. id: 0,
  30. name: '城市',
  31. parent_id: 1,
  32. type: 2
  33. },
  34. {
  35. id: 0,
  36. name: '区县',
  37. parent_id: 1,
  38. type: 3
  39. }
  40. ],
  41. regionType: 1,
  42. regionList: [],
  43. selectRegionDone: false,
  44. placeArray: placeArray,
  45. province: placeArray[0].name, //- 省
  46. pIndex: 0,
  47. city: placeArray[0].city[0].name, //- 市
  48. cIndex: 0,
  49. area: placeArray[0].city[0].area[0], //- 区
  50. aIndex: 0,
  51. isAddress:false,
  52. fullName:""
  53. },
  54. onClose(){
  55. this.setData({
  56. isAddress:false
  57. })
  58. },
  59. confirm(){
  60. let full_region = this.data.province+this.data.city+this.data.area
  61. this.setData({
  62. ['address.full_region']:full_region,
  63. isAddress:false
  64. })
  65. },
  66. showAddress(){
  67. this.setData({
  68. isAddress:true
  69. })
  70. },
  71. changeProvince: function(e){
  72. const val = e.detail.value //这些val都是数组形式的了
  73. this.setData({
  74. pIndex: val,
  75. cIndex: 0,
  76. aIndex: 0,
  77. province: placeArray[val].name,
  78. city: placeArray[val].city[0].name,
  79. area: placeArray[val].city[0].area[0]
  80. })
  81. },
  82. changeCity: function(e){
  83. const val = e.detail.value
  84. this.setData({
  85. cIndex: val,
  86. aIndex: 0,
  87. city: placeArray[this.data.pIndex].city[val].name,
  88. area: placeArray[this.data.pIndex].city[val].area[0]
  89. })
  90. },
  91. changeArea: function(e){
  92. const val = e.detail.value
  93. this.setData({
  94. aIndex: val,
  95. area: placeArray[this.data.pIndex].city[this.data.cIndex].area[val]
  96. })
  97. },
  98. mobilechange(e) {
  99. let mobile = e.detail.value;
  100. let address = this.data.address;
  101. if (util.testMobile(mobile)) {
  102. address.mobile = mobile;
  103. this.setData({
  104. address: address
  105. });
  106. }
  107. },
  108. bindinputName(event) {
  109. let address = this.data.address;
  110. address.name = event.detail.value;
  111. this.setData({
  112. address: address
  113. });
  114. },
  115. bindinputAddress(event) {
  116. let address = this.data.address;
  117. address.address = event.detail.value;
  118. this.setData({
  119. address: address
  120. });
  121. },
  122. switchChange(e) {
  123. let status = e.detail.value;
  124. let is_default = 0;
  125. if (status == true) {
  126. is_default = 1;
  127. }
  128. let address = 'address.is_default';
  129. this.setData({
  130. [address]: is_default
  131. });
  132. },
  133. async getAddressDetail() {
  134. let that = this;
  135. let checkedAddress = {}
  136. let query = new Parse.Query("ShopAddress");
  137. query.equalTo("objectId", that.data.addressId);
  138. let address = await query.first()
  139. if(address)
  140. {
  141. checkedAddress.name = address.get("name")
  142. checkedAddress.country_id = address.get("country_id")
  143. checkedAddress.province_id = address.get("province_id")
  144. checkedAddress.city_id = address.get("city_id")
  145. checkedAddress.district_id = address.get("district_id")
  146. checkedAddress.address = address.get("address")
  147. checkedAddress.mobile = address.get("mobile")
  148. checkedAddress.is_default = address.get("is_default")
  149. checkedAddress.province_name = address.get("province_name")
  150. checkedAddress.city_name = address.get("city_name")
  151. checkedAddress.district_name = address.get("district_name")
  152. checkedAddress.full_region = address.get("full_region")
  153. that.setData({
  154. address:checkedAddress
  155. })
  156. }
  157. // util.request(api.AddressDetail, {
  158. // id: that.data.addressId
  159. // }).then(function(res) {
  160. // if (res.errno === 0) {
  161. // that.setData({
  162. // address: res.data
  163. // });
  164. // }
  165. // });
  166. },
  167. deleteAddress: function() {
  168. let id = this.data.addressId;
  169. wx.showModal({
  170. title: '提示',
  171. content: '您确定要删除么?',
  172. success: function(res) {
  173. if (res.confirm) {
  174. util.request(api.DeleteAddress, {
  175. id: id
  176. }, 'POST').then(function(res) {
  177. if (res.errno === 0) {
  178. wx.removeStorageSync('addressId');
  179. util.showErrorToast('删除成功');
  180. wx.navigateBack();
  181. } else {
  182. util.showErrorToast(res.errmsg);
  183. }
  184. });
  185. }
  186. }
  187. })
  188. },
  189. setRegionDoneStatus() {
  190. let that = this;
  191. let doneStatus = that.data.selectRegionList.every(item => {
  192. return item.id != 0;
  193. });
  194. that.setData({
  195. selectRegionDone: doneStatus
  196. })
  197. },
  198. chooseRegion() {
  199. let that = this;
  200. this.setData({
  201. openSelectRegion: !this.data.openSelectRegion
  202. });
  203. //设置区域选择数据
  204. let address = this.data.address;
  205. if (address.province_id > 0 && address.city_id > 0 && address.district_id > 0) {
  206. let selectRegionList = this.data.selectRegionList;
  207. selectRegionList[0].id = address.province_id;
  208. selectRegionList[0].name = address.province_name;
  209. selectRegionList[0].parent_id = 1;
  210. selectRegionList[1].id = address.city_id;
  211. selectRegionList[1].name = address.city_name;
  212. selectRegionList[1].parent_id = address.province_id;
  213. selectRegionList[2].id = address.district_id;
  214. selectRegionList[2].name = address.district_name;
  215. selectRegionList[2].parent_id = address.city_id;
  216. this.setData({
  217. selectRegionList: selectRegionList,
  218. regionType: 3
  219. });
  220. this.getRegionList(address.city_id);
  221. } else {
  222. this.setData({
  223. selectRegionList: [{
  224. id: 0,
  225. name: '省份',
  226. parent_id: 1,
  227. type: 1
  228. },
  229. {
  230. id: 0,
  231. name: '城市',
  232. parent_id: 1,
  233. type: 2
  234. },
  235. {
  236. id: 0,
  237. name: '区县',
  238. parent_id: 1,
  239. type: 3
  240. }
  241. ],
  242. regionType: 1
  243. })
  244. this.getRegionList(1);
  245. }
  246. this.setRegionDoneStatus();
  247. },
  248. onLoad: function(options) {
  249. let {activeColor,titleColor}=options
  250. this.setData({activeColor,titleColor})
  251. // 页面初始化 options为页面跳转所带来的参数
  252. if (options.id) {
  253. this.setData({
  254. addressId: options.id,
  255. type:2
  256. });
  257. this.getAddressDetail();
  258. }
  259. // this.getRegionList(1);
  260. },
  261. onReady: function() {
  262. },
  263. async saveEditAddress(){
  264. let that = this
  265. let query = new Parse.Query("ShopAddress");
  266. query.equalTo("objectId", that.data.addressId);
  267. let shopAddress = await query.first()
  268. shopAddress.set("name",that.data.address.name)
  269. shopAddress.set("mobile",that.data.address.mobile)
  270. shopAddress.set("province_id",that.data.address.province_id)
  271. shopAddress.set("city_id",that.data.address.city_id)
  272. shopAddress.set("district_id",that.data.address.district_id)
  273. shopAddress.set("address",that.data.address.address)
  274. shopAddress.set("province_name",that.data.province)
  275. shopAddress.set("city_name",that.data.city)
  276. shopAddress.set("district_name",that.data.area)
  277. shopAddress.set("full_region",that.data.address.full_region)
  278. shopAddress.set("is_default",that.data.address.is_default)
  279. if(that.data.address.is_default == 1){
  280. that.setDefault()
  281. }
  282. await shopAddress.save().then(data=>{
  283. wx.navigateBack()
  284. })
  285. },
  286. selectRegionType(event) {
  287. let that = this;
  288. let regionTypeIndex = event.target.dataset.regionTypeIndex;
  289. let selectRegionList = that.data.selectRegionList;
  290. //判断是否可点击
  291. if (regionTypeIndex + 1 == this.data.regionType || (regionTypeIndex - 1 >= 0 && selectRegionList[regionTypeIndex - 1].id <= 0)) {
  292. return false;
  293. }
  294. this.setData({
  295. regionType: regionTypeIndex + 1
  296. })
  297. let selectRegionItem = selectRegionList[regionTypeIndex];
  298. this.getRegionList(selectRegionItem.parent_id);
  299. this.setRegionDoneStatus();
  300. },
  301. selectRegion(event) {
  302. let that = this;
  303. let regionIndex = event.target.dataset.regionIndex;
  304. let regionItem = this.data.regionList[regionIndex];
  305. let regionType = regionItem.type;
  306. let selectRegionList = this.data.selectRegionList;
  307. selectRegionList[regionType - 1] = regionItem;
  308. if (regionType != 3) {
  309. this.setData({
  310. selectRegionList: selectRegionList,
  311. regionType: regionType + 1
  312. })
  313. this.getRegionList(regionItem.id);
  314. } else {
  315. this.setData({
  316. selectRegionList: selectRegionList
  317. })
  318. }
  319. //重置下级区域为空
  320. selectRegionList.map((item, index) => {
  321. if (index > regionType - 1) {
  322. item.id = 0;
  323. item.name = index == 1 ? '城市' : '区县';
  324. item.parent_id = 0;
  325. }
  326. return item;
  327. });
  328. this.setData({
  329. selectRegionList: selectRegionList
  330. })
  331. that.setData({
  332. regionList: that.data.regionList.map(item => {
  333. //标记已选择的
  334. if (that.data.regionType == item.type && that.data.selectRegionList[that.data.regionType - 1].id == item.id) {
  335. item.selected = true;
  336. } else {
  337. item.selected = false;
  338. }
  339. return item;
  340. })
  341. });
  342. this.setRegionDoneStatus();
  343. },
  344. doneSelectRegion() {
  345. if (this.data.selectRegionDone === false) {
  346. return false;
  347. }
  348. let address = this.data.address;
  349. let selectRegionList = this.data.selectRegionList;
  350. address.province_id = selectRegionList[0].id;
  351. address.city_id = selectRegionList[1].id;
  352. address.district_id = selectRegionList[2].id;
  353. address.province_name = selectRegionList[0].name;
  354. address.city_name = selectRegionList[1].name;
  355. address.district_name = selectRegionList[2].name;
  356. address.full_region = selectRegionList.map(item => {
  357. return item.name;
  358. }).join('');
  359. this.setData({
  360. address: address,
  361. openSelectRegion: false
  362. });
  363. },
  364. cancelSelectRegion() {
  365. this.setData({
  366. openSelectRegion: false,
  367. regionType: this.data.regionDoneStatus ? 3 : 1
  368. });
  369. },
  370. getRegionList(regionId) {
  371. let that = this;
  372. let regionType = that.data.regionType;
  373. util.request(api.RegionList, {
  374. parentId: regionId
  375. }).then(function(res) {
  376. if (res.errno === 0) {
  377. that.setData({
  378. regionList: res.data.map(item => {
  379. //标记已选择的
  380. if (regionType == item.type && that.data.selectRegionList[regionType - 1].id == item.id) {
  381. item.selected = true;
  382. } else {
  383. item.selected = false;
  384. }
  385. return item;
  386. })
  387. });
  388. }
  389. });
  390. },
  391. async saveAddress() {
  392. let address = this.data.address;
  393. let company = getApp().globalData.company
  394. let user = Parse.User.current();
  395. if (address.name == '' || address.name == undefined) {
  396. util.showErrorToast('请输入姓名');
  397. return false;
  398. }
  399. if (address.mobile == '' || address.mobile == undefined) {
  400. util.showErrorToast('请输入手机号码');
  401. return false;
  402. }
  403. if (address.province == "" || address.city == "") {
  404. util.showErrorToast('请输入省市区');
  405. return false;
  406. }
  407. if (address.address == '' || address.address == undefined) {
  408. util.showErrorToast('请输入详细地址');
  409. return false;
  410. }
  411. let that = this;
  412. let ShopAddress = Parse.Object.extend("ShopAddress");
  413. let shopAddress = new ShopAddress();
  414. shopAddress.set("user",{
  415. "__type": "Pointer",
  416. "className": "_User",
  417. "objectId": user.id
  418. })
  419. shopAddress.set("company",{
  420. "__type": "Pointer",
  421. "className": "Company",
  422. "objectId": company
  423. })
  424. shopAddress.set("name",that.data.address.name)
  425. shopAddress.set("mobile",that.data.address.mobile)
  426. shopAddress.set("province_id",that.data.address.province_id)
  427. shopAddress.set("city_id",that.data.address.city_id)
  428. shopAddress.set("district_id",that.data.address.district_id)
  429. shopAddress.set("address",that.data.address.address)
  430. shopAddress.set("province_name",that.data.province)
  431. shopAddress.set("city_name",that.data.city)
  432. shopAddress.set("district_name",that.data.area)
  433. shopAddress.set("full_region",that.data.address.full_region)
  434. shopAddress.set("is_default",that.data.address.is_default)
  435. if(that.data.address.is_default == 1){
  436. that.setDefault()
  437. }
  438. await shopAddress.save().then(data=>{
  439. wx.navigateBack()
  440. })
  441. // util.request(api.SaveAddress, {
  442. // id: address.id,
  443. // name: address.name,
  444. // mobile: address.mobile,
  445. // province_id: address.province_id,
  446. // city_id: address.city_id,
  447. // district_id: address.district_id,
  448. // address: address.address,
  449. // is_default: address.is_default,
  450. // }, 'POST').then(function(res) {
  451. // if (res.errno === 0) {
  452. //
  453. // }
  454. // });
  455. },
  456. setDefault(){
  457. let ShopAddress = new Parse.Query("ShopAddress")
  458. ShopAddress.equalTo("user",Parse.User.current().id)
  459. ShopAddress.equalTo("is_default",1)
  460. ShopAddress.first().then(res=>{
  461. if(res){
  462. res.set("is_default",0)
  463. res.save()
  464. }
  465. })
  466. },
  467. onShow: function() {
  468. let id = this.data.addressId;
  469. if (id > 0) {
  470. wx.setNavigationBarTitle({
  471. title: '编辑地址',
  472. })
  473. } else {
  474. wx.setNavigationBarTitle({
  475. title: '新增地址',
  476. })
  477. }
  478. },
  479. onHide: function() {
  480. // 页面隐藏
  481. },
  482. onUnload: function() {
  483. // 页面关闭
  484. }
  485. })