东风启辰小程序端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

461 lines
13KB

  1. // pages/perfectMsg/perfectMsg.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. imgUrl: app.globalData.urlStatic,//图片路径
  9. selectType:2,//选中的手机号类型
  10. marginT: 0,
  11. verificationCode:"获取验证码",
  12. sendCode:true,
  13. mobile2:"",
  14. phoneInputShow: false,
  15. isUserAddress:true,//用户是否有地址
  16. submitData:{
  17. realName:"",//姓名
  18. mobile:"",//手机号
  19. province:"",//经销商省份
  20. city:"",//经销商城市
  21. agent_code:"",//经销商编码
  22. agentDetail:"",//经销商详情
  23. addressDetail:"",//地址详情
  24. scene:"",//场景值名称
  25. captcha:"",//验证码(若非微信绑定手机号需要传入)
  26. },
  27. provinceDataAll: null,//地区所有数据
  28. provinceDataArr: [[""], [""]],//省市数据
  29. provinceDataValue: [0, 0],//选中的省市下标
  30. nowProvince: "",//选中的省市文字
  31. storeArr: [],//专营店数据
  32. storeValue: 0,//选中的专营店下标
  33. httpState:false,//是否正在进行http请求
  34. windowTipShow:false,//是否显示完善信息后的弹窗
  35. isUserInfo:false,//是否有个人信息
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad: function (options) {
  41. app.globalData.nowPage = 3;
  42. if (app.globalData.openid) {
  43. this.loadFun();
  44. } else {
  45. app.globalData.openidSuccessFuc = this.loadFun;
  46. }
  47. if (app.globalData.userPhoneType == "ios") {
  48. this.setData({
  49. marginT: -20
  50. })
  51. }
  52. if(app.globalData.userInfoData && app.globalData.userInfoData.nickName){
  53. this.setData({
  54. isUserInfo:true
  55. })
  56. }
  57. },
  58. loadFun:function(){
  59. if (app.globalData.userMobile) {
  60. this.data.submitData.mobile = app.globalData.userMobile;
  61. this.setData({
  62. phoneInputShow: true,
  63. submitData: this.data.submitData
  64. })
  65. }
  66. this.getAddress();
  67. },
  68. /**
  69. * 生命周期函数--监听页面初次渲染完成
  70. */
  71. onReady: function () {
  72. },
  73. /**
  74. * 生命周期函数--监听页面显示
  75. */
  76. onShow: function () {
  77. },
  78. /**
  79. * 生命周期函数--监听页面隐藏
  80. */
  81. onHide: function () {
  82. },
  83. /**
  84. * 生命周期函数--监听页面卸载
  85. */
  86. onUnload: function () {
  87. },
  88. /**
  89. * 页面相关事件处理函数--监听用户下拉动作
  90. */
  91. onPullDownRefresh: function () {
  92. },
  93. /**
  94. * 页面上拉触底事件的处理函数
  95. */
  96. onReachBottom: function () {
  97. },
  98. /**
  99. * 用户点击右上角分享
  100. */
  101. onShareAppMessage: function () {
  102. return app.sharePack();
  103. },
  104. enterLucky:function(){//每日抽奖
  105. wx.navigateTo({
  106. url: '/pages/luckyStar/luckyStar'
  107. })
  108. },
  109. ruleControl:function(){//规则显示控制
  110. this.setData({
  111. ruleShow:!this.data.ruleShow
  112. })
  113. },
  114. chooseType:function(e){//选中的手机号类型
  115. if(e){
  116. this.setData({
  117. selectType:e.currentTarget.dataset.type
  118. })
  119. }else{
  120. this.setData({
  121. selectType:1
  122. })
  123. }
  124. },
  125. getAddressDetail: function (e) {//获取详细地址
  126. this.data.submitData.addressDetail = e.detail.value;
  127. this.setData({
  128. submitData:this.data.submitData
  129. })
  130. },
  131. getRealName:function(e){//获取用户输入的姓名
  132. this.data.submitData.realName = e.detail.value;
  133. this.setData({
  134. submitData:this.data.submitData
  135. })
  136. },
  137. getMobile:function(e){//获取用户输入的电话
  138. this.setData({
  139. mobile2:e.detail.value
  140. })
  141. },
  142. getCaptcha: function (e) {//获取用户输入的验证码
  143. this.data.submitData.captcha = e.detail.value;
  144. this.setData({
  145. submitData:this.data.submitData
  146. })
  147. },
  148. getCode: function (e) {//获取验证码
  149. if (!app.mobileVerify(this.data.mobile2)) {
  150. if (this.data.mobile2) {
  151. wx.showToast({
  152. title: '请输入正确的电话',
  153. icon: 'none'
  154. })
  155. } else {
  156. wx.showToast({
  157. title: '请输入电话',
  158. icon: 'none'
  159. })
  160. }
  161. return;
  162. }
  163. if (!this.data.sendCode) {
  164. return;
  165. }
  166. this.data.sendCode = false;
  167. app.wxRequest(app.globalData.urlRoot + "captcha/sendCaptcha", { mobile: this.data.mobile2 }, res => {
  168. if (res.code == 200) {
  169. this.countDown();
  170. wx.showToast({
  171. title: '验证码获取成功',
  172. icon: "none"
  173. })
  174. this.setData({
  175. verificationCode: 60
  176. })
  177. } else {
  178. this.data.sendCode = true;
  179. wx.showToast({
  180. title: res.msg,
  181. icon: "none"
  182. })
  183. }
  184. }, this)
  185. },
  186. countDown: function () {//倒计时
  187. setTimeout(() => {
  188. this.setData({
  189. verificationCode: this.data.verificationCode - 1
  190. })
  191. if (this.data.verificationCode > 0) {
  192. this.countDown();
  193. } else {
  194. this.setData({
  195. verificationCode: "获取验证码"
  196. })
  197. this.data.sendCode = true;
  198. }
  199. }, 1000);
  200. },
  201. getUserPhone: function (e) {//获取用户手机号
  202. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  203. app.getMobile(e.detail.encryptedData, e.detail.iv, res => {
  204. if (res.code == 200) {
  205. this.setData({
  206. phoneInputShow: true
  207. })
  208. this.chooseType();
  209. if (res.data && res.data.decodeData) {
  210. this.data.submitData.mobile = res.data.decodeData.phoneNumber;
  211. this.setData({
  212. submitData: this.data.submitData
  213. })
  214. }
  215. } else {
  216. wx.showToast({
  217. title: res.msg,
  218. icon: "none"
  219. })
  220. }
  221. }, this);
  222. }
  223. },
  224. getAddress: function () {//获取地址
  225. app.wxRequest(app.globalData.urlRoot + "address/getAddressV2", {}, res => {
  226. console.log(res);
  227. if (res.code == 200) {
  228. if (res.data) {
  229. this.data.submitData.realName = res.data.realName;
  230. this.data.submitData.mobile = res.data.mobile;
  231. this.data.submitData.province = res.data.province;
  232. this.data.submitData.city = res.data.city;
  233. this.data.submitData.addressDetail = res.data.addressDetail;
  234. this.setData({
  235. submitData: this.data.submitData,
  236. mobile2:res.data.mobile
  237. })
  238. // if(!res.data.agentDetail){
  239. this.getDistributorList("","");
  240. // this.getUserLocation();//获取用户当前位置
  241. // }
  242. }else{
  243. this.getUserLocation();//获取用户当前位置
  244. this.setData({
  245. isUserAddress:false
  246. })
  247. }
  248. } else {
  249. wx.showToast({
  250. title: res.msg,
  251. icon: "none"
  252. })
  253. }
  254. }, this);
  255. },
  256. getUserLocation: function (e) {//获取用户地理位置
  257. wx.getLocation({
  258. type: 'wgs84', //wgs84 gcj02
  259. success: (res) => {
  260. this.getDistributorList(res.longitude, res.latitude);
  261. },
  262. fail: (res) => {
  263. this.getDistributorList("", "");
  264. }
  265. })
  266. },
  267. getDistributorList: function (longitude, latitude) {//获取经销商列表
  268. app.wxRequest(app.globalData.urlRoot + "agent/getAgentList", { longitude: longitude, latitude: latitude }, res => {
  269. if (res.code == 200) {
  270. //整理数据
  271. var datas = res.data;
  272. var province = [];
  273. var city = [];
  274. for (let i = 0; i < res.data.list.length; i++) {
  275. province.push(res.data.list[i].province);
  276. }
  277. if(this.data.submitData.province){
  278. for(let i=0;i<province.length;i++){
  279. if(province[i]==this.data.submitData.province){
  280. res.data.nearData.provinceIndex = i;
  281. }
  282. }
  283. }
  284. for (let j = 0; j < res.data.list[res.data.nearData.provinceIndex].children.length; j++) {
  285. city.push(res.data.list[res.data.nearData.provinceIndex].children[j].city)
  286. }
  287. if(this.data.submitData.city){
  288. for(let i=0;i<city.length;i++){
  289. if(city[i]==this.data.submitData.city){
  290. res.data.nearData.cityIndex = i;
  291. }
  292. }
  293. }
  294. if(this.data.submitData.agentDetail){
  295. for(let i=0;i<res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children.length;i++){
  296. if(res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children[i]==this.data.submitData.agentDetail){
  297. res.data.nearData.agentIndex = i;
  298. }
  299. }
  300. }else{
  301. res.data.nearData.agentIndex = 0;
  302. }
  303. //将数据赋值给变量
  304. var storeArr = res.data.list[res.data.nearData.provinceIndex].children[res.data.nearData.cityIndex].children;
  305. var storeValue = res.data.nearData.agentIndex;
  306. this.data.submitData.agentDetail = storeArr[storeValue].agent_detail;
  307. this.data.submitData.agent_code = storeArr[storeValue].agent_code;
  308. this.data.submitData.province = province[res.data.nearData.provinceIndex];
  309. this.data.submitData.city = city[res.data.nearData.cityIndex];
  310. this.setData({
  311. provinceDataAll: res.data.list,
  312. provinceDataArr: [province, city],
  313. provinceDataValue: [res.data.nearData.provinceIndex, res.data.nearData.cityIndex],
  314. nowProvince: province[res.data.nearData.provinceIndex] + " " + city[res.data.nearData.cityIndex],
  315. storeArr: storeArr,
  316. storeValue: storeValue
  317. })
  318. } else {
  319. wx.showToast({
  320. title: res.msg,
  321. icon: "none"
  322. })
  323. }
  324. }, this);
  325. },
  326. provinceDataChange: function (e) {
  327. if (e.detail.column == 0) {
  328. var city = [];
  329. for (let i = 0; i < this.data.provinceDataAll[e.detail.value].children.length; i++) {
  330. city.push(this.data.provinceDataAll[e.detail.value].children[i].city);
  331. }
  332. this.data.provinceDataArr[1] = city;
  333. this.setData({
  334. provinceDataArr: this.data.provinceDataArr
  335. })
  336. }
  337. },
  338. provinceDataChane: function (e) {
  339. this.setData({
  340. provinceDataValue: e.detail.value,
  341. nowProvince: this.data.provinceDataArr[0][e.detail.value[0]] + " " + this.data.provinceDataArr[1][e.detail.value[1]],
  342. storeArr: this.data.provinceDataAll[e.detail.value[0]].children[e.detail.value[1]].children,
  343. storeValue: 0
  344. })
  345. },
  346. storeChane: function (e) {
  347. this.setData({
  348. storeValue: e.detail.value
  349. })
  350. },
  351. submitUserMsg:function(){//数据提交
  352. if (!this.data.submitData.realName) {
  353. wx.showToast({
  354. title: '请输入姓名',
  355. icon: "none"
  356. })
  357. return;
  358. }
  359. if (!this.data.mobile2 && this.data.selectType==2) {
  360. wx.showToast({
  361. title: '请输入电话',
  362. icon: "none"
  363. })
  364. return;
  365. }
  366. if (this.data.selectType == 2) {
  367. if (!this.data.submitData.captcha) {
  368. wx.showToast({
  369. title: '请输入验证码',
  370. icon: "none"
  371. })
  372. return;
  373. }
  374. }
  375. if(this.data.httpState){
  376. return;
  377. }
  378. this.data.httpState = true;
  379. this.data.submitData.province = this.data.provinceDataArr[0][this.data.provinceDataValue[0]];
  380. this.data.submitData.city = this.data.provinceDataArr[1][this.data.provinceDataValue[1]];
  381. this.data.submitData.agent_code = this.data.storeArr[this.data.storeValue].agent_code;
  382. this.data.submitData.agentDetail = this.data.storeArr[this.data.storeValue].agent_detail;
  383. this.data.submitData.scene = app.globalData.sceneSource;
  384. if(this.data.isUserAddress){
  385. this.updateAddress();
  386. }else{
  387. this.addAddress();
  388. }
  389. },
  390. addAddress: function () {//添加地址
  391. var mobile = this.data.submitData.mobile;
  392. if(this.data.selectType==2){
  393. this.data.submitData.mobile = this.data.mobile2;
  394. }
  395. app.wxRequest(app.globalData.urlRoot + "address/addAddressV2", this.data.submitData, res => {
  396. this.data.httpState = false;
  397. if (res.code == 200) {
  398. this.windowTipControl();
  399. }else{
  400. this.data.submitData.mobile = mobile;
  401. wx.showToast({
  402. title: res.msg,
  403. })
  404. }
  405. }, this, "POST")
  406. },
  407. updateAddress: function () {//更新地址
  408. var mobile = this.data.submitData.mobile;
  409. if(this.data.selectType==2){
  410. this.data.submitData.mobile = this.data.mobile2;
  411. }
  412. app.wxRequest(app.globalData.urlRoot + "address/updateAddressV2", this.data.submitData, res => {
  413. this.data.httpState = false;
  414. if (res.code == 200) {
  415. this.windowTipControl();
  416. }else{
  417. this.data.submitData.mobile = mobile;
  418. wx.showToast({
  419. title: res.msg,
  420. })
  421. }
  422. }, this, "POST");
  423. },
  424. windowTipControl:function(){
  425. this.setData({
  426. windowTipShow:!this.data.windowTipShow
  427. })
  428. },
  429. enterGuess:function(){//去竞猜
  430. // wx.redirectTo({
  431. // url: '/pages/guessSecondPrize/guessSecondPrize',
  432. // })
  433. wx.redirectTo({
  434. url: '/pages/guessSecondPoster/guessSecondPoster',
  435. })
  436. },
  437. getUserWxMsg:function(e){//通过微信获取用户信息
  438. if (e.detail.errMsg == "getUserInfo:ok") {
  439. this.setData({
  440. isUserInfo:true
  441. })
  442. app.submitUserMsg(e.detail.userInfo.avatarUrl, e.detail.userInfo.nickName);
  443. this.enterGuess();
  444. }
  445. }
  446. })