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

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