东风启辰小程序端
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.

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