createaddress.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. //index.js
  2. //获取应用实例
  3. const city = require('../../../../utils/city.js');
  4. const cityObjs = require('../../../../utils/city.js');
  5. const config = require('../../../../utils/config.js');
  6. const app = getApp()
  7. Page({
  8. data: {
  9. indicatorDots: true,
  10. datatype: '',
  11. id: '',
  12. city: '',
  13. county: '',
  14. consignee: '',
  15. phone: '',
  16. address: '',
  17. canIUse: wx.canIUse('button.open-type.getUserInfo')
  18. },
  19. //事件处理函数
  20. bindViewTap: function() {
  21. wx.navigateTo({
  22. url: '../logs/logs'
  23. })
  24. },
  25. onLoad: function(options) {
  26. if (app.globalData.userphone != null) {
  27. this.setData({
  28. phone: app.globalData.userphone
  29. })
  30. }
  31. var that = this
  32. if (JSON.stringify(options) == "{}") {
  33. wx.getLocation({
  34. type: 'wgs84',
  35. success: function(res) {
  36. let latitude = res.latitude
  37. let longitude = res.longitude
  38. wx.request({
  39. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`,
  40. success: res => {
  41. that.setData({
  42. city: res.data.result.ad_info.city,
  43. currentCityCode: res.data.result.ad_info.adcode,
  44. county: res.data.result.ad_info.district
  45. })
  46. },
  47. fail() {
  48. app.timeOut()
  49. }
  50. })
  51. },
  52. fail() {
  53. app.timeOut()
  54. }
  55. })
  56. } else if (options.type == "bill") {
  57. this.setData({
  58. datatype: options.type
  59. })
  60. wx.getLocation({
  61. type: 'wgs84',
  62. success: function(res) {
  63. let latitude = res.latitude
  64. let longitude = res.longitude
  65. wx.request({
  66. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`,
  67. success: res => {
  68. that.setData({
  69. city: res.data.result.ad_info.city,
  70. currentCityCode: res.data.result.ad_info.adcode,
  71. county: res.data.result.ad_info.district
  72. })
  73. },
  74. fail() {
  75. app.timeOut()
  76. }
  77. })
  78. }
  79. })
  80. } else if (options.type == "chose") {
  81. this.setData({
  82. datatype: options.type
  83. })
  84. wx.getLocation({
  85. type: 'wgs84',
  86. success: function(res) {
  87. let latitude = res.latitude
  88. let longitude = res.longitude
  89. wx.request({
  90. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`,
  91. success: res => {
  92. that.setData({
  93. city: res.data.result.ad_info.city,
  94. currentCityCode: res.data.result.ad_info.adcode,
  95. county: res.data.result.ad_info.district
  96. })
  97. }
  98. })
  99. },
  100. fail() {
  101. app.timeOut()
  102. }
  103. })
  104. } else {
  105. this.setData({
  106. datatype: options.datatype,
  107. address: options.address,
  108. city: options.city,
  109. consignee: options.consignee,
  110. county: options.county,
  111. id: options.id,
  112. phone: options.phone
  113. })
  114. }
  115. let _this = this;
  116. _this.setData({
  117. navH: app.globalData.navHeight
  118. })
  119. },
  120. // hrefCl: function(e) {
  121. // wx.navigateTo({
  122. // url: '/pages/consultant/consultant'
  123. // })
  124. // },
  125. // hrefPm: function(e) {
  126. // wx.navigateTo({
  127. // url: '/pages/promotion/promotion'
  128. // })
  129. // },
  130. // hrefEq: function(e) {
  131. // wx.navigateTo({
  132. // url: '/pages/equipment/equipment'
  133. // })
  134. // },
  135. hrefswitchcity: function() {
  136. wx.navigateTo({
  137. url: '/pages/switchcity/switchcity'
  138. });
  139. },
  140. inputconsignee: function(e) {
  141. this.setData({
  142. consignee: e.detail.value
  143. })
  144. },
  145. inputphone: function(e) {
  146. this.setData({
  147. phone: e.detail.value
  148. })
  149. },
  150. inputaddress: function(e) {
  151. this.setData({
  152. address: e.detail.value
  153. })
  154. },
  155. commit: function() {
  156. if (this.data.consignee == null || this.data.consignee == '') {
  157. wx.showToast({
  158. title: '请正确输入收货人!',
  159. icon: 'none',
  160. duration: 1500
  161. })
  162. return false;
  163. }
  164. var myphone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
  165. if (!myphone.test(this.data.phone)) {
  166. wx.showToast({
  167. title: '请正确输入手机号!',
  168. icon: 'none',
  169. duration: 1500
  170. })
  171. return false;
  172. }
  173. if (this.data.address == null || this.data.address == '') {
  174. wx.showToast({
  175. title: '请输入详细地址!',
  176. icon: 'none',
  177. duration: 1500
  178. })
  179. return false;
  180. }
  181. wx.showLoading({
  182. title: '保存中......',
  183. icon: 'loading',
  184. duration: 5500
  185. })
  186. if (this.data.datatype == "update") {
  187. wx.request({
  188. url: app.globalData.url + '/updateWXUserAddress',
  189. method: 'post',
  190. header: {
  191. "Content-Type": "application/x-www-form-urlencoded"
  192. },
  193. data: {
  194. xa_phone: this.data.phone,
  195. xa_consignee: this.data.consignee,
  196. xa_city: this.data.city,
  197. xa_county: this.data.county,
  198. xa_address: this.data.address,
  199. id: this.data.id
  200. },
  201. success: function(res) {
  202. wx.hideLoading()
  203. wx.showToast({
  204. title: '保存成功!',
  205. icon: 'success',
  206. duration: 1500
  207. })
  208. var pages = getCurrentPages();
  209. var currPage = pages[pages.length - 1];
  210. var prevPage = pages[pages.length - 2];
  211. wx.request({
  212. url: app.globalData.url + '/getWXUserAddress',
  213. method: 'get',
  214. data: {
  215. xu_openid: app.globalData.openid
  216. },
  217. success: function(res2) {
  218. prevPage.setData({
  219. addressList: res2.data
  220. })
  221. wx.navigateBack({
  222. delta: 1
  223. })
  224. }
  225. })
  226. },
  227. fail() {
  228. app.timeOut()
  229. }
  230. })
  231. } else if (this.data.datatype == "bill") {
  232. wx.request({
  233. url: app.globalData.url + '/insertWXUserAddress',
  234. method: 'post',
  235. header: {
  236. "Content-Type": "application/x-www-form-urlencoded"
  237. },
  238. data: {
  239. xu_openid: app.globalData.openid,
  240. xa_phone: this.data.phone,
  241. xa_consignee: this.data.consignee,
  242. xa_city: this.data.city,
  243. xa_county: this.data.county,
  244. xa_address: this.data.address
  245. },
  246. success: function(res) {
  247. wx.hideLoading()
  248. wx.showToast({
  249. title: '保存成功!',
  250. icon: 'success',
  251. duration: 1500
  252. })
  253. var pages = getCurrentPages();
  254. var currPage = pages[pages.length - 1];
  255. var prevPage = pages[pages.length - 2];
  256. wx.request({
  257. url: app.globalData.url + '/getEqUserAddress',
  258. method: 'get',
  259. data: {
  260. xu_openid: app.globalData.openid
  261. },
  262. success: function(resbill) {
  263. prevPage.setData({
  264. xa_consignee: resbill.data.xa_consignee,
  265. xa_city: resbill.data.xa_city,
  266. xa_county: resbill.data.xa_county,
  267. xa_address: resbill.data.xa_address,
  268. xa_phone: resbill.data.xa_phone,
  269. })
  270. wx.navigateBack({
  271. delta: 1
  272. })
  273. }
  274. })
  275. },
  276. fail() {
  277. app.timeOut()
  278. }
  279. })
  280. } else if (this.data.datatype == "chose") {
  281. wx.request({
  282. url: app.globalData.url + '/insertWXUserAddress',
  283. method: 'post',
  284. header: {
  285. "Content-Type": "application/x-www-form-urlencoded"
  286. },
  287. data: {
  288. xu_openid: app.globalData.openid,
  289. xa_phone: this.data.phone,
  290. xa_consignee: this.data.consignee,
  291. xa_city: this.data.city,
  292. xa_county: this.data.county,
  293. xa_address: this.data.address
  294. },
  295. success: function(res) {
  296. wx.hideLoading()
  297. wx.showToast({
  298. title: '保存成功!',
  299. icon: 'success',
  300. duration: 1500
  301. })
  302. var pages = getCurrentPages();
  303. var currPage = pages[pages.length - 1];
  304. var prevPage = pages[pages.length - 2];
  305. wx.request({
  306. url: app.globalData.url + '/getWXUserAddress',
  307. method: 'get',
  308. data: {
  309. xu_openid: app.globalData.openid
  310. },
  311. success: function(resbill) {
  312. prevPage.setData({
  313. addressList: resbill.data,
  314. datatype: 'bill'
  315. })
  316. wx.navigateBack({
  317. delta: 1
  318. })
  319. }
  320. })
  321. },
  322. fail() {
  323. app.timeOut()
  324. }
  325. })
  326. } else {
  327. wx.request({
  328. url: app.globalData.url + '/insertWXUserAddress',
  329. method: 'post',
  330. header: {
  331. "Content-Type": "application/x-www-form-urlencoded"
  332. },
  333. data: {
  334. xu_openid: app.globalData.openid,
  335. xa_phone: this.data.phone,
  336. xa_consignee: this.data.consignee,
  337. xa_city: this.data.city,
  338. xa_county: this.data.county,
  339. xa_address: this.data.address
  340. },
  341. success: function(res) {
  342. wx.hideLoading()
  343. wx.showToast({
  344. title: '保存成功!',
  345. icon: 'success',
  346. duration: 1500
  347. })
  348. var pages = getCurrentPages();
  349. var currPage = pages[pages.length - 1];
  350. var prevPage = pages[pages.length - 2];
  351. wx.request({
  352. url: app.globalData.url + '/getWXUserAddress',
  353. method: 'get',
  354. data: {
  355. xu_openid: app.globalData.openid
  356. },
  357. success: function(res) {
  358. prevPage.setData({
  359. addressList: res.data
  360. })
  361. wx.navigateBack({
  362. delta: 1
  363. })
  364. },
  365. fail() {
  366. app.timeOut()
  367. }
  368. })
  369. },
  370. fail() {
  371. app.timeOut()
  372. }
  373. })
  374. }
  375. }
  376. })