东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 line
11KB

  1. // pages/placingOrder/placingOrder.js
  2. const app = getApp()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. },
  9. /**
  10. * 组件的初始数据
  11. */
  12. data: {
  13. imgUrl: app.globalData.urlStatic,//图片路径
  14. agreement:false,
  15. isAgreement: true,//是否同意协议
  16. siteSelect: false,//是否显示位置选择框
  17. phoneInputShow: false,//是否显示电话输入框
  18. provinceDataAll:null,//地区所有数据
  19. provinceDataArr:[[""],[""]],//省市数据
  20. provinceDataValue:[0,0],//选中的省市下标
  21. nowProvince:"",//选中的省市文字
  22. storeArr:[],//专营店数据
  23. storeValue:0,//选中的专营店下标
  24. verificationCode: '获取验证码',//验证码文案
  25. sendCode: true,
  26. appointment: true,
  27. mobile2: "",
  28. tipShow:false,
  29. subscribeData: {
  30. realname: "",//姓名
  31. mobile: "",//手机号
  32. captcha: "",//验证码
  33. province: "",//省份
  34. city:"",//市区
  35. agent_code: "",//经销商编码
  36. agentDetail: "",//经销商详细信息
  37. parentOpenid: app.globalData.parentOpenid,//好友openid
  38. scene: app.globalData.sceneSource,
  39. source:2,
  40. },
  41. getBuyState: false,
  42. mobileType: 2
  43. },
  44. attached: function () {
  45. if (app.globalData.openid) {
  46. this.loadFun();
  47. } else {
  48. app.globalData.openidSuccessFuc = this.loadFun;
  49. }
  50. if(app.globalData.firstEnterOrder){
  51. this.setData({
  52. tipShow:true
  53. })
  54. app.globalData.firstEnterOrder=false;
  55. }
  56. },
  57. /**
  58. * 组件的方法列表
  59. */
  60. methods: {
  61. closeXieyi: function () {
  62. this.triggerEvent('placing')
  63. },
  64. getUserPhone: function (e) {//获取用户手机号
  65. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  66. app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
  67. if (res.code == 200) {
  68. this.setData({
  69. phoneInputShow: true
  70. })
  71. this.cutType();
  72. if (res.data && res.data.decodeData) {
  73. this.data.subscribeData.mobile = res.data.decodeData.phoneNumber;
  74. this.setData({
  75. subscribeData: this.data.subscribeData
  76. })
  77. }
  78. } else {
  79. wx.showToast({
  80. title: res.msg,
  81. icon: "none"
  82. })
  83. }
  84. }, this);
  85. }
  86. },
  87. loadFun: function () {
  88. this.getUserLocation();//获取用户当前位置
  89. if (app.globalData.userMobile) {
  90. this.data.subscribeData.mobile = app.globalData.userMobile;
  91. this.setData({
  92. phoneInputShow: true,
  93. subscribeData: this.data.subscribeData
  94. })
  95. }
  96. },
  97. getRealname: function (e) {//获取用户输入的姓名
  98. this.data.subscribeData.realname = e.detail.value;
  99. },
  100. getMobile: function (e) {//获取用户输入的电话
  101. this.data.subscribeData.mobile = e.detail.value;
  102. },
  103. getMobile2: function (e) {//获取用户输入的电话---无验证码
  104. this.data.mobile2 = e.detail.value;
  105. },
  106. getCaptcha: function (e) {//获取用户输入的验证码
  107. this.data.subscribeData.captcha = e.detail.value;
  108. },
  109. getDistributorList: function (longitude, latitude) {//获取经销商列表
  110. app.wxRequest(app.globalData.urlRoot + "agent/getAgentList", { longitude: longitude, latitude: latitude }, res => {
  111. if (res.code == 200) {
  112. //整理数据
  113. var datas = res.data;
  114. var province = [];
  115. var city = [];
  116. for(let i=0;i<res.data.list.length;i++){
  117. province.push(res.data.list[i].province);
  118. }
  119. for (let j = 0; j < res.data.list[res.data.nearData.provinceIndex].children.length; j++) {
  120. city.push(res.data.list[res.data.nearData.provinceIndex].children[j].city)
  121. }
  122. //将数据赋值给变量
  123. this.setData({
  124. provinceDataAll: res.data.list,
  125. provinceDataArr: [province, city],
  126. provinceDataValue: [res.data.nearData.provinceIndex, res.data.nearData.cityIndex],
  127. nowProvince: province[res.data.nearData.provinceIndex] + " " + city[res.data.nearData.cityIndex],
  128. storeArr: res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children,
  129. storeValue: res.data.nearData.agentIndex
  130. })
  131. } else {
  132. wx.showToast({
  133. title: res.msg,
  134. icon: "none"
  135. })
  136. }
  137. }, this);
  138. },
  139. getCode: function (e) {//获取验证码
  140. if (!app.mobileVerify(this.data.mobile2)) {
  141. if (this.data.mobile2) {
  142. wx.showToast({
  143. title: '请输入正确的电话',
  144. icon: 'none'
  145. })
  146. } else {
  147. wx.showToast({
  148. title: '请输入电话',
  149. icon: 'none'
  150. })
  151. }
  152. return;
  153. }
  154. if (!this.data.sendCode) {
  155. return;
  156. }
  157. this.data.sendCode = false;
  158. app.wxRequest(app.globalData.urlRoot + "captcha/sendCaptcha", { mobile: this.data.mobile2 }, res => {
  159. if (res.code == 200) {
  160. this.countDown();
  161. wx.showToast({
  162. title: '验证码获取成功',
  163. icon: "none"
  164. })
  165. this.setData({
  166. verificationCode: 60
  167. })
  168. } else {
  169. this.data.sendCode = true;
  170. wx.showToast({
  171. title: res.msg,
  172. icon: "none"
  173. })
  174. }
  175. }, this)
  176. },
  177. countDown: function () {//倒计时
  178. setTimeout(() => {
  179. this.setData({
  180. verificationCode: this.data.verificationCode - 1
  181. })
  182. if (this.data.verificationCode > 0) {
  183. this.countDown();
  184. } else {
  185. this.setData({
  186. verificationCode: "获取验证码"
  187. })
  188. this.data.sendCode = true;
  189. }
  190. }, 1000);
  191. },
  192. getUserLocation: function (e) {
  193. wx.getLocation({
  194. type: 'gcj02', //wgs84
  195. success: (res) => {
  196. this.getDistributorList(res.longitude, res.latitude);
  197. },
  198. fail: (res) => {
  199. this.getDistributorList("", "");
  200. },
  201. complete: (res) => {
  202. this.setData({
  203. siteSelect: true,
  204. })
  205. }
  206. })
  207. },
  208. showSite: function () {//显示地址选择框
  209. if (!this.data.siteSelect) {
  210. }
  211. },
  212. agreementState: function () {//协议
  213. this.setData({
  214. isAgreement: !this.data.isAgreement
  215. })
  216. },
  217. agreementControl: function () {
  218. this.setData({
  219. agreement: !this.data.agreement
  220. })
  221. },
  222. preBuy: function () {//下订支付
  223. if (!this.data.subscribeData.realname) {
  224. wx.showToast({
  225. title: '请输入姓名',
  226. icon: "none"
  227. })
  228. return;
  229. }
  230. if (!this.data.mobile2 && this.data.mobileType==2) {
  231. wx.showToast({
  232. title: '请输入电话',
  233. icon: "none"
  234. })
  235. return;
  236. }
  237. if (this.data.mobileType == 2) {
  238. if (!this.data.subscribeData.captcha) {
  239. wx.showToast({
  240. title: '请输入验证码',
  241. icon: "none"
  242. })
  243. return;
  244. }
  245. }
  246. if (!this.data.isAgreement) {
  247. wx.showToast({
  248. title: '请同意协议',
  249. icon: 'none'
  250. })
  251. return;
  252. }
  253. if (!this.data.appointment) {
  254. return;
  255. }
  256. this.data.appointment = false;
  257. this.data.subscribeData.province = this.data.provinceDataArr[0][this.data.provinceDataValue[0]];
  258. this.data.subscribeData.city = this.data.provinceDataArr[1][this.data.provinceDataValue[1]];
  259. this.data.subscribeData.agent_code = this.data.storeArr[this.data.storeValue].agent_code;
  260. this.data.subscribeData.agentDetail = this.data.storeArr[this.data.storeValue].agent_detail;
  261. var sourceMobile = this.data.subscribeData.mobile;
  262. if (this.data.mobileType == 2) {
  263. this.data.subscribeData.mobile = this.data.mobile2;
  264. }
  265. this.data.subscribeData.parentOpenid = app.globalData.parentOpenid;
  266. wx.showLoading({
  267. title: '加载中',
  268. mask:true
  269. })
  270. app.wxRequest(app.globalData.urlRoot + "wxPay/preBuy", this.data.subscribeData, res => {
  271. wx.hideLoading();
  272. this.data.subscribeData.mobile = sourceMobile;
  273. if (res.code == 200) {
  274. wx.requestPayment({
  275. timeStamp: res.data.jsSubmit.timeStamp,
  276. nonceStr: res.data.jsSubmit.nonceStr,
  277. signType: res.data.jsSubmit.signType,
  278. package: res.data.jsSubmit.package,
  279. paySign: res.data.jsSubmit.paySign,
  280. success:(option)=>{
  281. this.paySuccess(res.data.trade_id);
  282. },
  283. fail: (option) => {
  284. this.data.appointment = true;
  285. wx.showToast({
  286. title: '支付失败',
  287. icon:"none"
  288. })
  289. }
  290. })
  291. } else {
  292. this.data.appointment = true;
  293. wx.showToast({
  294. title: res.msg,
  295. icon: "none"
  296. })
  297. }
  298. }, this, "POST")
  299. },
  300. paySuccess: function (e) {//支付成功
  301. app.wxRequest(app.globalData.urlRoot + "wxPay/paySuccess", { source:2,trade_id:e},res=>{
  302. if(res.code==200){
  303. // wx.showToast({
  304. // title: '下订成功',
  305. // mask:true
  306. // })
  307. this.setData({
  308. getBuyState:true
  309. })
  310. app.globalData.getBuyState = {};
  311. app.globalData.getBuyState.success = true;
  312. app.globalData.getBuyState.cdate = res.data.cdate;
  313. app.globalData.getBuyState.coupon_code = res.data.coupon_code;
  314. // setTimeout(()=>{
  315. // this.closeXieyi();
  316. // },1500)
  317. } else {
  318. this.data.appointment = true;
  319. wx.showToast({
  320. title: res.msg,
  321. icon:'none'
  322. })
  323. }
  324. },this,"POST");
  325. },
  326. closePage:function(){
  327. this.closeXieyi();
  328. // wx.reLaunch({
  329. // url: '/pages/index/index',
  330. // })
  331. },
  332. provinceDataChange:function(e){
  333. if (e.detail.column==0) {
  334. var city = [];
  335. for (let i = 0; i < this.data.provinceDataAll[e.detail.value].children.length;i++){
  336. city.push(this.data.provinceDataAll[e.detail.value].children[i].city);
  337. }
  338. this.data.provinceDataArr[1] = city;
  339. this.setData({
  340. provinceDataArr: this.data.provinceDataArr
  341. })
  342. }
  343. },
  344. provinceDataChane:function(e){
  345. this.setData({
  346. provinceDataValue: e.detail.value,
  347. nowProvince: this.data.provinceDataArr[0][e.detail.value[0]] + " " + this.data.provinceDataArr[1][e.detail.value[1]],
  348. storeArr: this.data.provinceDataAll[e.detail.value[0]].children[e.detail.value[1]].children,
  349. storeValue:0
  350. })
  351. },
  352. storeChane:function(e){
  353. this.setData({
  354. storeValue:e.detail.value
  355. })
  356. },
  357. enterMycenter:function(){
  358. wx.reLaunch({
  359. url: '/pages/myCenter/myCenter',
  360. })
  361. },
  362. cutType: function () {
  363. this.setData({
  364. mobileType: this.data.mobileType == 1 ? 2 : 1
  365. })
  366. },
  367. tipShowControl:function(){
  368. this.setData({
  369. tipShow:!this.data.tipShow
  370. })
  371. },
  372. tipShowControl2:function(){
  373. this.setData({
  374. tipShow:!this.data.tipShow
  375. })
  376. this.preBuy();
  377. }
  378. }
  379. })